forked from Morganamilo/paru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkgbuild.rs
412 lines (356 loc) · 11.6 KB
/
pkgbuild.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::{
cell::OnceCell,
env::current_dir,
fs::{read_dir, File},
io::Write,
path::{Path, PathBuf},
sync::Arc,
};
use crate::{download::print_download, exec, install::review};
use anyhow::{anyhow, bail, Context, Result};
use aur_fetch::Fetch;
use indicatif::{ProgressBar, ProgressStyle};
use srcinfo::Srcinfo;
use tr::tr;
use url::Url;
use crate::{config::Config, print_error};
#[derive(Debug, Default, Clone)]
pub enum RepoSource {
Url(Url, Option<PathBuf>),
Path(PathBuf),
#[default]
None,
}
impl RepoSource {
pub fn url(&self) -> Option<&Url> {
match self {
RepoSource::Url(url, _) => Some(url),
_ => None,
}
}
pub fn set_path<P: Into<PathBuf>>(&mut self, p: P) {
match self {
RepoSource::Url(_, path) => *path = Some(p.into()),
_ => *self = RepoSource::Path(p.into()),
}
}
pub fn set_url(&mut self, u: Url) {
match self {
RepoSource::Url(url, _) => *url = u,
RepoSource::Path(path) => *self = RepoSource::Url(u, Some(path.clone())),
_ => *self = RepoSource::Url(u, None),
}
}
}
#[derive(Debug)]
pub struct PkgbuildPkg {
pub repo: String,
pub srcinfo: Srcinfo,
pub path: PathBuf,
}
#[derive(Default, Debug, Clone)]
pub struct PkgbuildRepo {
pub name: String,
pub source: RepoSource,
pub depth: u32,
pub skip_review: bool,
pub force_srcinfo: bool,
pub path: PathBuf,
pkgs: OnceCell<Arc<Vec<PkgbuildPkg>>>,
}
impl PkgbuildRepo {
pub fn new(name: String, path: PathBuf) -> Self {
PkgbuildRepo {
depth: 2,
path,
name,
source: RepoSource::None,
skip_review: false,
force_srcinfo: false,
pkgs: OnceCell::new(),
}
}
pub fn path(&self) -> Result<PathBuf> {
match &self.source {
RepoSource::Url(_, Some(path)) => {
Ok(self.path.join(path.strip_prefix("/").unwrap_or(path)))
}
RepoSource::Url(_, None) => Ok(self.path.clone()),
RepoSource::Path(path) => Ok(path.clone()),
RepoSource::None => bail!(tr!("repo {} does not have a URL or Path")),
}
}
pub fn pkgs(&self, config: &Config) -> &[PkgbuildPkg] {
self.pkgs
.get_or_init(move || Arc::new(self.read_pkgs(config)))
}
pub fn base(&self, config: &Config, base: &str) -> Option<&PkgbuildPkg> {
self.pkgs(config)
.iter()
.find(|p| p.srcinfo.base.pkgbase == base)
}
pub fn pkg(&self, config: &Config, pkg: &str) -> Option<(&PkgbuildPkg, &srcinfo::Package)> {
self.pkgs(config)
.iter()
.find_map(|srcinfo| srcinfo.srcinfo.pkg(pkg).map(|p| (srcinfo, p)))
}
pub fn from_cwd(config: &Config) -> Result<PkgbuildRepo> {
let dir = current_dir()?;
let repo = PkgbuildRepo {
name: ".".to_string(),
source: RepoSource::Path(dir.clone()),
depth: 3,
skip_review: true,
force_srcinfo: false,
path: dir,
pkgs: Default::default(),
};
repo.pkgs(config);
Ok(repo)
}
pub fn from_pkgbuilds(config: &Config, dirs: &[PathBuf]) -> Result<PkgbuildRepo> {
let mut pkgs = Vec::new();
let mut repo = Self::from_cwd(config)?;
for dir in dirs {
let dir = dir.canonicalize()?;
repo.print_generate_srcinfo(config, &dir.file_name().unwrap().to_string_lossy());
let srcinfo = read_srcinfo_from_pkgbuild(config, &dir)?;
pkgs.push(PkgbuildPkg {
repo: repo.name.clone(),
srcinfo,
path: dir.clone(),
});
}
repo.pkgs = OnceCell::from(Arc::new(pkgs));
Ok(repo)
}
fn read_pkgs(&self, config: &Config) -> Vec<PkgbuildPkg> {
if matches!(self.source, RepoSource::Url(_, _)) && !self.path.join(".git").exists() {
eprintln!(
"{} {}",
config.color.warning.paint("::"),
tr!(
"repo {} not downloaded (use -Sy --pkgbuilds to download)",
self.name
)
);
}
self.for_each_pkgbuild(Vec::new(), |path, data| match self.read_pkg(config, path) {
Ok(srcinfo) => data.push(srcinfo),
Err(e) => print_error(config.color.error, e),
})
}
fn generate_srcinfos(&self, config: &Config) {
self.for_each_pkgbuild((), |path, _| {
if let Err(e) = self.generate_srcinfo(config, path) {
print_error(config.color.error, e);
}
})
}
fn generate_srcinfo(&self, config: &Config, path: &Path) -> Result<()> {
if !self.force_srcinfo && path.join(".SRCINFO").exists() {
return Ok(());
}
self.print_generate_srcinfo(config, &path.file_name().unwrap().to_string_lossy());
let output = exec::makepkg_output(config, path, &["--printsrcinfo"])
.context(path.display().to_string());
match output {
Ok(output) => {
let mut file = File::create(path.join(".SRCINFO"))?;
file.write_all(&output.stdout)?;
}
Err(e) => {
print_error(config.color.error, e);
}
}
Ok(())
}
fn print_generate_srcinfo(&self, config: &Config, pkg: &str) {
let c = config.color;
println!(
"{} {}",
c.action.paint("::"),
c.bold.paint(tr!(
"Generating .SRCINFO for {repo}/{dir}...",
repo = self.name,
dir = pkg,
))
);
}
fn for_each_pkgbuild<T, F: Fn(&Path, &mut T)>(&self, data: T, f: F) -> T {
self.try_for_each_pkgbuild(data, |path, data| {
f(path, data);
Ok(())
})
.unwrap()
}
fn try_for_each_pkgbuild<T, F: Fn(&Path, &mut T) -> Result<()>>(
&self,
mut data: T,
f: F,
) -> Result<T> {
let path = self.path()?;
if path.exists() {
Self::try_for_each_pkgbuild_internal(&mut data, &f, &path, self.depth)?;
}
Ok(data)
}
fn try_for_each_pkgbuild_internal<T, F: Fn(&Path, &mut T) -> Result<()>>(
data: &mut T,
f: &F,
path: &Path,
depth: u32,
) -> Result<()> {
if depth == 0 {
return Ok(());
}
//log::debug!("for each pkgbuild: {}", path.display());
if path.join("PKGBUILD").exists() {
f(path, data)?;
}
if depth == 1 {
return Ok(());
}
let dir = read_dir(path).context(path.display().to_string())?;
for entry in dir {
let entry = match entry {
Ok(entry) => entry,
Err(_) => return Ok(()),
};
if entry.file_type()?.is_dir() {
Self::try_for_each_pkgbuild_internal(data, f, &entry.path(), depth - 1)?;
}
}
Ok(())
}
fn read_pkg(&self, config: &Config, path: &Path) -> Result<PkgbuildPkg> {
let srcinfo_path = path.join(".SRCINFO");
if !srcinfo_path.exists() {
self.generate_srcinfo(config, path)?;
}
let srcinfo = Srcinfo::parse_file(&srcinfo_path);
match srcinfo {
Ok(srcinfo) => Ok(PkgbuildPkg {
repo: self.name.to_string(),
srcinfo,
path: path.to_path_buf(),
}),
Err(err) => Err(anyhow!(err).context(tr!(
"failed to parse srcinfo \"{}\"",
srcinfo_path.display().to_string()
))),
}
}
}
#[derive(Debug, Clone)]
pub struct PkgbuildRepos {
pub fetch: Fetch,
pub repos: Vec<PkgbuildRepo>,
}
impl PkgbuildRepos {
pub fn new(fetch: Fetch) -> Self {
Self {
fetch,
repos: Vec::new(),
}
}
pub fn add_repo(&mut self, name: String) -> &mut PkgbuildRepo {
self.repos
.push(PkgbuildRepo::new(name.clone(), name.into()));
self.repos.last_mut().unwrap()
}
pub fn repo(&self, name: &str) -> Option<&PkgbuildRepo> {
self.repos.iter().find(|r| r.name == name)
}
pub fn pkg(&self, config: &Config, name: &str) -> Option<(&PkgbuildPkg, &srcinfo::Package)> {
self.repos
.iter()
.flat_map(|r| r.pkgs(config))
.find_map(|s| s.srcinfo.pkg(name).map(|p| (s, p)))
}
pub fn repo_mut(&mut self, name: &str) -> Option<&mut PkgbuildRepo> {
self.repos.iter_mut().find(|r| r.name == name)
}
pub fn aur_depends_repo(&self, config: &Config) -> Vec<aur_depends::PkgbuildRepo<'_>> {
self.repos
.iter()
.map(|r| aur_depends::PkgbuildRepo {
name: &r.name,
pkgs: r.pkgs(config).iter().map(|p| &p.srcinfo).collect(),
})
.collect()
}
pub fn refresh(&self, config: &Config) -> Result<()> {
let cols = config.cols.unwrap_or(0);
let action = config.color.action;
let bold = config.color.bold;
let repos = self
.repos
.iter()
.filter_map(|r| {
r.source
.url()
.map(|u| (r.name.as_str(), u))
.map(|(n, u)| aur_fetch::Repo {
url: u.clone(),
name: n.to_string(),
})
})
.collect::<Vec<_>>();
if repos.is_empty() {
return Ok(());
}
println!(
"\n{} {}",
action.paint("::"),
bold.paint(tr!("Downloading PKGBUILD Repos..."))
);
if cols < 80 {
self.fetch.download_repos_cb(&repos, |cb| {
print_download(config, cb.n, repos.len(), cb.pkg);
})?;
} else {
let total = repos.len().to_string();
let template = format!(
" ({{pos:>{}}}/{{len}}) {{prefix:45!}} [{{wide_bar}}]",
total.len()
);
let pb = ProgressBar::new(repos.len() as u64);
pb.set_style(
ProgressStyle::default_bar()
.template(&template)?
.progress_chars("-> "),
);
self.fetch.download_repos_cb(&repos, |cb| {
pb.inc(1);
pb.set_prefix(cb.pkg.to_string());
})?;
pb.finish();
println!();
}
let review_repos = repos
.iter()
.filter(|r| {
!config
.pkgbuild_repos
.repo(&r.name)
.map(|r| r.skip_review)
.unwrap_or(false)
})
.map(|r| r.name.as_str())
.collect::<Vec<_>>();
review(config, &self.fetch, &review_repos)?;
let all_repos = repos.iter().map(|r| r.name.as_str()).collect::<Vec<_>>();
self.fetch.merge(&all_repos)?;
self.repos.iter().for_each(|r| r.generate_srcinfos(config));
Ok(())
}
}
pub fn read_srcinfo_from_pkgbuild(config: &Config, dir: &Path) -> Result<Srcinfo> {
let output = exec::makepkg_output(config, dir, &["--printsrcinfo"])
.with_context(|| dir.display().to_string())?;
let srcinfo = Srcinfo::parse_buf(output.stdout.as_slice())
.context(tr!("failed to parse srcinfo generated by makepkg"))
.with_context(|| dir.display().to_string())?;
Ok(srcinfo)
}