paru 0.99.4

Aur helper and pacman wrapper
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
413
use crate::config::{Colors, Config};
use crate::fmt::print_indent;
use crate::util::split_repo_aur_mode;
use crate::{esprintln, sprint, sprintln};

use std::collections::{HashMap, HashSet};
use std::fs::read_dir;
use std::io::Write;
use std::iter::FromIterator;
use std::process::Command;
use std::result::Result as StdResult;

use alpm::Version;
use ansi_term::Style;
use anyhow::{bail, Context, Result};
use aur_depends::Base;
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use raur_ext::{Package, RaurExt};
use srcinfo::Srcinfo;

use indicatif::{ProgressBar, ProgressStyle};

const FRAGMENT: &AsciiSet = &CONTROLS.add(b'@').add(b'+');

#[derive(Debug, Clone)]
pub struct Bases {
    pub bases: Vec<Base>,
}

impl FromIterator<Package> for Bases {
    fn from_iter<T: IntoIterator<Item = Package>>(iter: T) -> Self {
        let mut bases = Bases::new();
        bases.extend(iter);
        bases
    }
}

impl Bases {
    pub fn new() -> Self {
        Self { bases: Vec::new() }
    }

    pub fn push(&mut self, pkg: Package) {
        for base in &mut self.bases {
            if base.package_base() == pkg.package_base {
                base.pkgs.push(aur_depends::AurPackage {
                    pkg,
                    make: false,
                    target: false,
                });
                return;
            }
        }

        self.bases.push(Base {
            pkgs: vec![aur_depends::AurPackage {
                pkg,
                make: false,
                target: false,
            }],
        })
    }

    pub fn extend<I: IntoIterator<Item = Package>>(&mut self, iter: I) {
        iter.into_iter().for_each(|p| self.push(p))
    }
}

#[derive(Debug, Default)]
pub struct Warnings<'a> {
    pub pkgs: Vec<raur_ext::Package>,
    pub missing: Vec<&'a str>,
    pub ood: Vec<&'a str>,
    pub orphans: Vec<&'a str>,
}

impl<'a> Warnings<'a> {
    pub fn missing(&self, color: Colors, cols: Option<usize>) -> &Self {
        if !self.missing.is_empty() {
            let b = color.bold;
            let e = color.error;
            let len = ":: could not find packages: ".len();
            sprint!("{} {}", e.paint("::"), b.paint("Could not find packages: "));
            print_indent(Style::new(), len, 4, cols, "  ", &self.missing);
        }
        self
    }

    pub fn ood(&self, color: Colors, cols: Option<usize>) -> &Self {
        if !self.ood.is_empty() {
            let b = color.bold;
            let e = color.error;
            let len = ":: out of date: ".len();
            sprint!("{} {}", e.paint("::"), b.paint("Out of date: "));
            print_indent(Style::new(), len, 4, cols, "  ", &self.ood);
        }
        self
    }

    pub fn orphans(&self, color: Colors, cols: Option<usize>) -> &Self {
        if !self.orphans.is_empty() {
            let b = color.bold;
            let e = color.error;
            let len = ":: orphans: ".len();
            sprint!("{} {}", e.paint("::"), b.paint("Orphans: "));
            print_indent(Style::new(), len, 4, cols, "  ", &self.orphans);
        }
        self
    }

    pub fn all(&self, color: Colors, cols: Option<usize>) {
        self.missing(color, cols);
        self.ood(color, cols);
        self.orphans(color, cols);
    }
}

pub fn cache_info_with_warnings<'a, S: AsRef<str>>(
    raur: &raur::Handle,
    cache: &'a mut raur_ext::Cache,
    pkgs: &'a [S],
    ignore: &[String],
) -> StdResult<Warnings<'a>, raur::Error> {
    let mut missing = Vec::new();
    let mut ood = Vec::new();
    let mut orphaned = Vec::new();
    let aur_pkgs = raur.cache_info(cache, pkgs)?;

    for pkg in pkgs {
        if !ignore.iter().any(|p| p == pkg.as_ref()) && !cache.contains(pkg.as_ref()) {
            missing.push(pkg.as_ref())
        }
    }

    for pkg in &aur_pkgs {
        if !ignore.iter().any(|p| p.as_str() == pkg.name) {
            if pkg.out_of_date.is_some() {
                ood.push(cache.get(pkg.name.as_str()).unwrap().name.as_str());
            }

            if pkg.maintainer.is_none() {
                orphaned.push(cache.get(pkg.name.as_str()).unwrap().name.as_str());
            }
        }
    }

    let ret = Warnings {
        pkgs: aur_pkgs,
        missing,
        ood,
        orphans: orphaned,
    };

    Ok(ret)
}

pub fn getpkgbuilds(config: &mut Config) -> Result<i32> {
    let pkgs = config
        .targets
        .iter()
        .map(|t| t.as_str())
        .collect::<Vec<_>>();

    let (repo, aur) = split_repo_aur_mode(config, &pkgs);
    let mut ret = 0;

    if !repo.is_empty() {
        ret = repo_pkgbuilds(config, &repo)?;
    }

    if !aur.is_empty() {
        let action = config.color.action;
        let bold = config.color.bold;
        sprintln!("{} {}", action.paint("::"), bold.paint("Querying AUR..."));
        let warnings =
            cache_info_with_warnings(&config.raur, &mut config.cache, &aur, &config.ignore)?;
        if !warnings.missing.is_empty() {
            ret |= ret
        }
        warnings.missing(config.color, config.cols);
        let aur = warnings.pkgs;

        let mut bases = Bases::new();
        bases.extend(aur);

        config.fetch.clone_dir = std::env::current_dir()?;

        aur_pkgbuilds(config, &bases)?;
    }
    Ok(ret)
}

pub fn repo_pkgbuilds(config: &Config, pkgs: &[&str]) -> Result<i32> {
    let db = config.alpm.localdb();

    let cd = std::env::current_dir().context("could not get current directory")?;
    let asp = &config.asp_bin;

    if Command::new(asp).output().is_err() {
        esprintln!("{} is not installed: can not get repo packages", asp);
        return Ok(1);
    }

    let cd = read_dir(cd)?
        .map(|d| d.map(|d| d.file_name().into_string().unwrap()))
        .collect::<Result<HashSet<_>, _>>()?;

    let mut ok = Vec::new();
    let mut missing = Vec::new();

    for &pkg in pkgs {
        if db.pkg(pkg).is_err() {
            missing.push(pkg);
        } else {
            ok.push(pkg);
        }
    }

    if !missing.is_empty() {
        let len = ":: Missing ABS packages".len();
        sprint!("{} Missing ABS packages", config.color.error.paint("::"));
        print_indent(config.color.base, len, 3, config.cols, "  ", &missing);
    }

    for (n, pkg) in ok.into_iter().enumerate() {
        print_download(config, n + 1, pkgs.len(), pkg);
        let action = if cd.contains(pkg) { "update" } else { "export" };

        Command::new(asp)
            .arg(action)
            .arg(pkg)
            .output()
            .with_context(|| format!("failed to run: {} {} {}", asp, action, pkg))?;
    }

    Ok(!missing.is_empty() as i32)
}

pub fn print_download(_config: &Config, n: usize, total: usize, pkg: &str) {
    let total = total.to_string();
    sprintln!(
        " ({:>padding$}/{}) {}: {}",
        n,
        total,
        //config.color.action.paint("::"),
        "downloading",
        pkg,
        padding = total.len(),
    );
}

pub fn aur_pkgbuilds(config: &Config, bases: &Bases) -> Result<()> {
    let download = bases
        .bases
        .iter()
        .map(|p| p.package_base())
        .collect::<Vec<_>>();

    let cols = config.cols.unwrap_or(0);

    let action = config.color.action;
    let bold = config.color.bold;

    sprintln!(
        "\n{} {}",
        action.paint("::"),
        bold.paint("Downloading pkgbuilds...")
    );

    if bases.bases.is_empty() {
        sprintln!(" pkgbuilds up to date");
        return Ok(());
    }

    let fetched = if cols < 80 {
        config.fetch.download_cb(&download, |cb| {
            let base = bases
                .bases
                .iter()
                .find(|b| b.package_base() == cb.pkg)
                .unwrap();

            print_download(config, cb.n, download.len(), &base.to_string());
        })?
    } else {
        let total = download.len().to_string();
        let truncate = cols - (80 - (total.len() * 2)).min(cols);
        let template = format!(
            " ({{pos:>{}}}/{{len}}) {{prefix:!}} [{{wide_bar}}]",
            total.len()
        );
        let pb = ProgressBar::new(download.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template(&template)
                .progress_chars("-> "),
        );

        let mut prefix = format!("{:<100}", "");
        prefix.truncate(truncate);
        pb.set_prefix(&prefix);

        let fetched = config.fetch.download_cb(&download, |cb| {
            let base = bases
                .bases
                .iter()
                .find(|b| b.package_base() == cb.pkg)
                .unwrap();

            pb.inc(1);
            let mut prefix = format!("{}{:<100}", base, "");
            prefix.truncate(truncate);
            pb.set_prefix(&prefix);
        })?;

        pb.finish();
        fetched
    };

    config.fetch.merge(&fetched)?;

    Ok(())
}

pub fn new_aur_pkgbuilds(
    config: &Config,
    bases: &Bases,
    srcinfos: &HashMap<String, Srcinfo>,
) -> Result<()> {
    let mut pkgs = Vec::new();
    if config.redownload == "all" {
        return aur_pkgbuilds(config, bases);
    }

    for base in &bases.bases {
        if let Some(pkg) = srcinfos.get(base.package_base()) {
            let upstream_ver = pkg.version();
            if Version::new(base.version()) > Version::new(&upstream_ver) {
                pkgs.push(base.clone());
            }
        } else {
            pkgs.push(base.clone());
        }
    }

    let bases = Bases { bases: pkgs };
    aur_pkgbuilds(config, &bases)
}

pub fn show_pkgbuilds(config: &mut Config) -> Result<i32> {
    let stdout = std::io::stdout();
    let mut stdout = stdout.lock();

    let (repo, aur) = split_repo_aur_mode(config, &config.targets);

    if !repo.is_empty() {
        let asp = &config.asp_bin;

        if Command::new(asp).output().is_err() {
            esprintln!("{} is not installed: can not get repo packages", asp);
            return Ok(1);
        }

        for pkg in repo {
            Command::new(asp)
                .arg("update")
                .arg(&pkg)
                .output()
                .with_context(|| format!("failed to run: {} update {:?}", asp, pkg))?;

            Command::new(asp)
                .arg("show")
                .arg(&pkg)
                .spawn()?
                .wait()
                .with_context(|| format!("failed to run: {} show {:?}", asp, pkg))?;

            let _ = stdout.write_all(b"\n");
        }
    }

    if !aur.is_empty() {
        let client = reqwest::blocking::Client::new();

        let warnings = cache_info_with_warnings(&config.raur, &mut config.cache, &aur, &[])?;
        warnings.missing(config.color, config.cols);
        let ret = !warnings.missing.is_empty() as i32;
        let bases = Bases::from_iter(warnings.pkgs);

        for base in &bases.bases {
            let base = base.package_base().to_string();
            let base = utf8_percent_encode(&base, FRAGMENT);
            let url = config
                .aur_url
                .join(&format!("cgit/aur.git/plain/PKGBUILD?h={}", base))?;

            let response = client
                .get(url.clone())
                .send()
                .with_context(|| format!("{}: {}", base, url))?;
            if !response.status().is_success() {
                bail!("{}: {}: {}", base, url, response.status());
            }

            let _ = stdout.write_all(&response.bytes()?);
            let _ = stdout.write_all(b"\n");
        }

        return Ok(ret);
    }

    return Ok(0);
}