clyde 0.7.0

A cross-platform package manager for prebuilt applications
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// SPDX-FileCopyrightText: 2022 Aurélien Gâteau <mail@agateau.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::slice::Iter;

use anyhow::Error as AnyhowError;
use anyhow::{anyhow, Result};
use regex::Regex;
use semver::Version;

use clyde::app::App;
use clyde::arch_os::{Arch, ArchOs, Os};
use clyde::checksum::compute_checksum;
use clyde::file_cache::FileCache;
use clyde::package::{Asset, FetcherConfig, Package, Release};
use clyde::ui::Ui;

lazy_static! {
    // Order matters: x86_64 must be looked for before x86
    static ref ARCH_VEC: Vec<(&'static str, Arch)> = vec![
        ("x86_64", Arch::X86_64),
        ("amd64", Arch::X86_64),
        ("x64", Arch::X86_64),
        ("x86", Arch::X86),
        ("i?[36]86", Arch::X86),
        ("aarch[-_]?64", Arch::Aarch64),
        ("arm64", Arch::Aarch64),
        ("32[-_]?bit", Arch::X86),
        ("64[-_]?bit", Arch::X86_64),
        ("universal", Arch::Any),
    ];
    static ref OS_VEC: Vec<(&'static str, Os)> = vec![
        ("linux", Os::Linux),
        ("darwin", Os::MacOs),
        ("apple", Os::MacOs),
        ("macos(|10|11)", Os::MacOs),
        ("mac", Os::MacOs),
        ("osx", Os::MacOs),
        ("win(|dows|32|64)", Os::Windows),
    ];
    static ref UNSUPPORTED_EXTS : HashSet<&'static str> = HashSet::from(["deb", "rpm", "msi", "apk", "asc", "sha256", "sbom", "txt", "dmg", "sh"]);

    // Packer extensions, ordered from worse to best
    static ref PACKER_EXTENSIONS: Vec<&'static str> = vec![
        "zip", "gz", "bz2", "xz"
    ];

    // Libc names, ordered from worse to best. List msvc at the end because on Windows it tends to
    // produce smaller binaries
    static ref LIBC_NAMES: Vec<&'static str> = vec![
        "gnu", "musl", "msvc"
    ];
}

const ARCH_OS_SEPARATOR_PATTERN: &str = "(\\b|[-_.])";

fn compute_url_checksum(
    ui: &Ui,
    cache: &FileCache,
    package: &Package,
    version: &Version,
    url: &str,
) -> Result<String> {
    let path = cache.download(ui, &package.name, version, url)?;
    ui.info("Computing checksum");
    compute_checksum(&path)
}

// Must take an iterator as argument because each *_VEC is a unique type
fn find_in_iter<T: Copy>(iter: Iter<'_, (&'static str, T)>, name: &str) -> Option<T> {
    for (pattern, key) in iter {
        let pattern = format!("{ARCH_OS_SEPARATOR_PATTERN}{pattern}{ARCH_OS_SEPARATOR_PATTERN}");
        let rx = Regex::new(&pattern).unwrap();
        if rx.is_match(name) {
            return Some(*key);
        }
    }
    None
}

fn extract_arch_os(
    name: &str,
    default_arch: Option<Arch>,
    default_os: Option<Os>,
) -> Option<ArchOs> {
    let arch = find_in_iter(ARCH_VEC.iter(), name).or(default_arch)?;
    let os = find_in_iter(OS_VEC.iter(), name).or(default_os)?;
    Some(ArchOs::new(arch, os))
}

fn get_extension(name: &str) -> Option<&str> {
    name.rsplit_once('.').map(|x| x.1)
}

fn get_filename(url: &str) -> Result<String> {
    let (_, name) = url
        .rsplit_once('/')
        .ok_or_else(|| anyhow!("Can't find archive name in URL {}", url))?;
    Ok(name.to_string())
}

fn get_lname(url: &str) -> Result<String> {
    Ok(get_filename(url)?.to_ascii_lowercase())
}

fn is_supported_name(name: &str) -> bool {
    let ext = match get_extension(name) {
        Some(x) => x,
        None => return true,
    };
    if UNSUPPORTED_EXTS.contains(ext) {
        return false;
    }
    true
}

pub fn add_asset(
    ui: &Ui,
    cache: &FileCache,
    package: &Package,
    version: &Version,
    release: &mut Release,
    arch_os: &ArchOs,
    url: &str,
) -> Result<()> {
    let checksum = compute_url_checksum(ui, cache, package, version, url)?;

    let asset = Asset {
        url: url.to_string(),
        sha256: checksum,
    };

    release.insert(arch_os.clone(), asset);

    Ok(())
}

/// Given an asset URL, return a score based on the packer it uses. The better the packer, the
/// higher the score.
fn get_pack_score(name: &str) -> usize {
    let ext = match get_extension(name) {
        Some(x) => x,
        None => return 0,
    };

    match PACKER_EXTENSIONS.iter().position(|&x| x == ext) {
        Some(idx) => idx + 1,
        None => 0,
    }
}

/// Given an asset URL, return a score based on the libc it uses. The better the libc, the higher
/// the score.
fn get_libc_score(name: &str) -> usize {
    match LIBC_NAMES.iter().position(|&x| name.contains(x)) {
        Some(idx) => idx + 1,
        None => 0,
    }
}

/// Given two asset URLs, return the one we prefer to use
fn select_best_url<'a>(ui: &Ui, u1: &'a str, u2: &'a str) -> &'a str {
    // We know get_lname() is going to succeed here, because it has already been called before
    let n1 = get_lname(u1).unwrap();
    let n2 = get_lname(u2).unwrap();

    let u1_libc_score = get_libc_score(&n1);
    let u2_libc_score = get_libc_score(&n2);
    match u1_libc_score.cmp(&u2_libc_score) {
        Ordering::Greater => return u1,
        Ordering::Less => return u2,
        Ordering::Equal => (),
    };

    let u1_pack_score = get_pack_score(&n1);
    let u2_pack_score = get_pack_score(&n2);
    match u1_pack_score.cmp(&u2_pack_score) {
        Ordering::Greater => u1,
        Ordering::Less => u2,
        Ordering::Equal => {
            ui.warn(&format!(
                "Don't know which of '{n1}' and '{n2}' is the best, picking the first one"
            ));
            u1
        }
    }
}

#[derive(Default)]
pub struct BestUrlOptions {
    default_arch: Option<Arch>,
    default_os: Option<Os>,
    include: Option<Regex>,
}

fn parse_include(include: &Option<String>) -> Result<Option<Regex>> {
    match include {
        None => Ok(None),
        Some(expr) => match Regex::new(expr) {
            Ok(rx) => Ok(Some(rx)),
            Err(e) => Err(anyhow!("Failed to parse regular expression {expr}: {e}")),
        },
    }
}

impl BestUrlOptions {
    pub fn new(default_arch: Option<Arch>, default_os: Option<Os>, include: Option<Regex>) -> Self {
        BestUrlOptions {
            default_arch,
            default_os,
            include,
        }
    }
}

impl TryFrom<&FetcherConfig> for BestUrlOptions {
    type Error = AnyhowError;

    fn try_from(config: &FetcherConfig) -> Result<BestUrlOptions> {
        Ok(match config {
            FetcherConfig::Forgejo {
                arch, os, include, ..
            } => BestUrlOptions::new(*arch, *os, parse_include(include)?),
            FetcherConfig::GitHub {
                arch, os, include, ..
            } => BestUrlOptions::new(*arch, *os, parse_include(include)?),
            FetcherConfig::GitLab {
                arch, os, include, ..
            } => BestUrlOptions::new(*arch, *os, parse_include(include)?),
            _ => {
                panic!(
                    "BestUrlOptions::try_from should not be called for {:?}",
                    config
                )
            }
        })
    }
}

/// Given a bunch of asset URLs returns the best URL per arch-os
pub fn select_best_urls(
    ui: &Ui,
    urls: &[String],
    options: BestUrlOptions,
) -> Result<HashMap<ArchOs, String>> {
    let mut best_urls = HashMap::<ArchOs, String>::new();
    for url in urls {
        let lname = get_lname(url)?;
        if !is_supported_name(&lname) {
            continue;
        }

        if let Some(ref include) = options.include {
            let filename = get_filename(url)?;
            if !include.is_match(&filename) {
                continue;
            }
        }

        let arch_os = match extract_arch_os(&lname, options.default_arch, options.default_os) {
            Some(x) => x,
            None => {
                ui.warn(&format!("Can't extract arch-os from {lname}, skipping"));
                continue;
            }
        };

        let url = match best_urls.get(&arch_os) {
            Some(current_url) => select_best_url(ui, current_url, url),
            None => url,
        };
        best_urls.insert(arch_os, url.to_string());
    }
    Ok(best_urls)
}

pub fn add_assets(
    app: &App,
    ui: &Ui,
    path: &Path,
    version: &Version,
    arch_os: &Option<String>,
    urls: &[String],
) -> Result<()> {
    let package = Package::from_file(path)?;

    let mut release = match package.releases.get(version) {
        Some(x) => x.clone(),
        None => Release::new(),
    };

    if let Some(arch_os) = arch_os {
        if urls.len() > 1 {
            return Err(anyhow!("When using --arch-os, only one URL can be passed"));
        }
        let url = urls.first().unwrap();
        let arch_os = ArchOs::parse(arch_os)?;
        add_asset(
            ui,
            &app.download_cache,
            &package,
            version,
            &mut release,
            &arch_os,
            url,
        )?;
    } else {
        let urls_for_arch_os = select_best_urls(ui, urls, BestUrlOptions::default())?;
        for (arch_os, url) in urls_for_arch_os {
            ui.info(&format!("{arch_os}: {url}"));
            let result = add_asset(
                &ui.nest(),
                &app.download_cache,
                &package,
                version,
                &mut release,
                &arch_os,
                &url,
            );
            if let Err(err) = result {
                ui.error(&format!("Can't add {arch_os:?} build from {url}: {err}"));
                return Err(err);
            };
        }
    }

    let new_package = package.replace_release(version, release);
    new_package.to_file(path)?;

    Ok(())
}

/// Wraps add_assets to make it easier to use as a standalone command
pub fn add_assets_cmd(
    app: &App,
    ui: &Ui,
    path: &Path,
    version: &str,
    arch_os: &Option<String>,
    urls: &[String],
) -> Result<()> {
    let version = Version::parse(version)?;
    add_assets(app, ui, path, &version, arch_os, urls)
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::fs;

    use clyde::test_file_utils::get_fixture_path;

    fn check_extract_arch_os(filename: &str, expected: Option<ArchOs>) {
        let result = extract_arch_os(filename, None, None);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_extract_arch_os_from_store() {
        let yaml_path = get_fixture_path("store_arch_os.csv");
        let content = fs::read(yaml_path).unwrap();
        let content = String::from_utf8(content).unwrap();
        let mut ok = true;
        for line in content.lines() {
            let (name, arch_os_str) = line.trim().split_once(",").unwrap();
            let expected = ArchOs::parse(arch_os_str).unwrap();

            let result = extract_arch_os(name, None, None);
            if result != Some(expected) {
                eprintln!("Failure: name={} arch_os_str={}", name, arch_os_str);
                ok = false;
            }
        }
        assert!(ok);
    }

    #[test]
    fn test_extract_arch_os() {
        check_extract_arch_os(
            "foo-1.2-linux-arm64.tar.gz",
            Some(ArchOs::new(Arch::Aarch64, Os::Linux)),
        );
        check_extract_arch_os(
            "node-v16.16.0-win-x86.zip",
            Some(ArchOs::new(Arch::X86, Os::Windows)),
        );
        check_extract_arch_os(
            "node-v16.16.0-darwin-x64.tar.gz",
            Some(ArchOs::new(Arch::X86_64, Os::MacOs)),
        );
        check_extract_arch_os(
            "bat-v0.21.0-i686-pc-windows-msvc.zip",
            Some(ArchOs::new(Arch::X86, Os::Windows)),
        );
        check_extract_arch_os(
            "cmake-3.24.0-rc5-macos10.10-universal.tar.gz",
            Some(ArchOs::new(Arch::Any, Os::MacOs)),
        );
        check_extract_arch_os(
            "rclone-v1.61.1-osx-arm64.zip",
            Some(ArchOs::new(Arch::Aarch64, Os::MacOs)),
        );
        check_extract_arch_os("bar-3.14.tar.gz", None);
    }

    #[test]
    fn test_extract_arch_os_default_values() {
        let result = extract_arch_os("ninja-windows.zip", Some(Arch::X86_64), None);
        assert_eq!(result, Some(ArchOs::new(Arch::X86_64, Os::Windows)));

        let result = extract_arch_os("ninja-mac.zip", Some(Arch::X86_64), None);
        assert_eq!(result, Some(ArchOs::new(Arch::X86_64, Os::MacOs)));
    }

    #[test]
    fn test_is_supported_name() {
        assert!(is_supported_name("foo.tar.gz"));
        assert!(is_supported_name("foo.zip"));
        assert!(is_supported_name("foo.exe"));
        assert!(is_supported_name("foo-x86_64-linux"));
        assert!(is_supported_name("foo.gz"));
        assert!(is_supported_name("foo.exe.xz"));
        assert!(is_supported_name("foo.bz2"));

        assert!(!is_supported_name("foo.deb"));
        assert!(!is_supported_name("foo.rpm"));
        assert!(!is_supported_name("foo.msi"));
    }

    #[test]
    fn test_select_best_url() {
        let ui = Ui::default();

        assert_eq!(
            select_best_url(&ui, "https://example.com/foo.gz", "https://example.com/foo"),
            "https://example.com/foo.gz"
        );
        assert_eq!(
            select_best_url(
                &ui,
                "https://example.com/foo.gz",
                "https://example.com/foo.xz"
            ),
            "https://example.com/foo.xz"
        );

        // libc is more important than compression
        assert_eq!(
            select_best_url(
                &ui,
                "https://example.com/foo-musl",
                "https://example.com/foo-glibc.gz"
            ),
            "https://example.com/foo-musl"
        );
    }

    #[test]
    fn test_select_best_urls_applies_include() {
        let ui = Ui::default();

        let options = BestUrlOptions::new(None, None, Some(Regex::new("^foo-cli").unwrap()));

        let result = select_best_urls(
            &ui,
            &[
                "https://example.com/foo/foo-cli-x86_64-linux.gz".to_string(),
                "https://example.com/foo/foo-extra-x86_64-linux.gz".to_string(),
            ],
            options,
        )
        .unwrap();

        let expected = HashMap::from([(
            ArchOs::new(Arch::X86_64, Os::Linux),
            "https://example.com/foo/foo-cli-x86_64-linux.gz".to_string(),
        )]);
        assert_eq!(result, expected);
    }
}