clyde 0.9.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
486
487
488
489
490
491
492
493
494
495
496
497
498
// SPDX-FileCopyrightText: 2022 Aurélien Gâteau <mail@agateau.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later

mod fetcher_config;
mod internal_package;

use std::collections::{BTreeMap, HashMap};
use std::fs::File;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};

pub use fetcher_config::FetcherConfig;

use crate::arch_os::{Arch, ArchOs, Os};

use internal_package::InternalPackage;

pub const EXTRA_FILES_DIR_NAME: &str = "extra_files";

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Asset {
    pub url: String,
    pub sha256: String,
}

pub type ReleaseAssets = HashMap<ArchOs, Asset>;

#[derive(Debug, Clone, Default)]
pub struct Release {
    pub published_at: Option<DateTime<Utc>>,
    pub assets: ReleaseAssets,
}

impl Release {
    pub fn with_assets(mut self, assets: ReleaseAssets) -> Self {
        self.assets = assets;
        self
    }

    pub fn with_published_at(mut self, published_at: Option<DateTime<Utc>>) -> Self {
        self.published_at = published_at;
        self
    }
}

fn is_zero(x: &u32) -> bool {
    *x == 0
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Install {
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub strip: u32,
    pub files: BTreeMap<String, String>,
    #[serde(default)]
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub extra_files: BTreeMap<String, String>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tests: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct Package {
    pub name: String,
    pub description: String,
    pub homepage: String,
    pub repository: String,
    pub comment: String,
    pub releases: BTreeMap<Version, Release>,

    pub installs: BTreeMap<Version, HashMap<ArchOs, Install>>,
    pub package_dir: PathBuf,

    pub fetcher: FetcherConfig,
}

impl Package {
    pub fn from_file(path: &Path) -> Result<Package> {
        let file = File::open(path)?;
        let internal_package: InternalPackage = serde_yaml::from_reader(file)?;
        let package_dir = path
            .parent()
            .ok_or_else(|| anyhow!("No parent dir for package {}", path.display()))?;
        internal_package.to_package(package_dir)
    }

    pub fn from_yaml_str(yaml_str: &str) -> Result<Package> {
        let internal_package: InternalPackage = serde_yaml::from_str(yaml_str)?;
        internal_package.to_package(&PathBuf::new())
    }

    pub fn to_file(&self, path: &Path) -> Result<()> {
        let internal_package = InternalPackage::from(self);
        let file = File::create(path)?;
        serde_yaml::to_writer(file, &internal_package)?;
        Ok(())
    }

    /// Returns a clone of the package with the builds for version `version` replaced by
    /// those from `release`
    pub fn replace_release(&self, version: &Version, release: Release) -> Package {
        let mut releases = self.releases.clone();
        releases.insert(version.clone(), release);
        Package {
            name: self.name.clone(),
            description: self.description.clone(),
            homepage: self.homepage.clone(),
            repository: self.repository.clone(),
            comment: self.comment.clone(),
            releases,
            installs: self.installs.clone(),
            package_dir: self.package_dir.clone(),
            fetcher: self.fetcher.clone(),
        }
    }

    pub fn get_version_matching(&self, requested_version: &VersionReq) -> Option<&Version> {
        self.releases
            .keys()
            .rev()
            .find(|&version| requested_version.matches(version))
    }

    pub fn get_latest_version(&self) -> Option<&Version> {
        let entry = self.releases.iter().last()?;
        Some(entry.0)
    }

    pub fn get_asset(&self, version: &Version, arch_os: &ArchOs) -> Option<&Asset> {
        let release = self.releases.get(version)?;
        let asset = release.assets.get(arch_os);
        if asset.is_some() {
            return asset;
        }
        if arch_os.arch != Arch::Any {
            let asset = release.assets.get(&arch_os.with_any_arch());
            if asset.is_some() {
                return asset;
            }
        }
        if arch_os.os != Os::Any {
            let asset = release.assets.get(&arch_os.with_any_os());
            if asset.is_some() {
                return asset;
            }
        }
        release.assets.get(&ArchOs::any())
    }

    /// Return files definition for wanted_version
    /// Uses the highest version which is less or equal to wanted_version
    pub fn get_install(&self, wanted_version: &Version, arch_os: &ArchOs) -> Option<&Install> {
        let install = self.get_install_internal(wanted_version, arch_os);
        if install.is_some() {
            return install;
        }
        if arch_os.arch != Arch::Any {
            let install = self.get_install_internal(wanted_version, &arch_os.with_any_arch());
            if install.is_some() {
                return install;
            }
        }
        if arch_os.os != Os::Any {
            // Probably less useful than the previous check, but you never know
            let install = self.get_install_internal(wanted_version, &arch_os.with_any_os());
            if install.is_some() {
                return install;
            }
        }
        self.get_install_internal(wanted_version, &ArchOs::any())
    }

    fn get_install_internal(&self, wanted_version: &Version, arch_os: &ArchOs) -> Option<&Install> {
        let entry = self
            .installs
            .iter()
            .rev()
            .find(|(version, _)| *version <= wanted_version)?;
        let installs_for_arch_os = entry.1;
        installs_for_arch_os.get(arch_os)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{fs, str::FromStr};

    use crate::store::INDEX_NAME;

    const TEST_PACKAGE_YAML_CONTENT: &str = "
    name: test
    description: desc
    homepage:
    releases:
      1.2.0:
        any:
          url: https://example.com/foo-1.2.0
          sha256: '1234'
      1.3.0:
        published_at: '2024-01-02T12:34:56Z'
        assets:
          any:
            url: https://example.com/foo-1.3.0
            sha256: '5678'
    installs:
      1.2.0:
        any:
          strip: 1
          files:
            bin/foo-1.2: bin/foo
            share:
          tests:
            - foo --help
            - foo --version
    fetcher: !GitHub
      arch: x86_64
      os: linux
    ";

    /// Helper function to read a YAML file and return the Mapping representing it
    fn read_yaml_from_path(path: &Path) -> serde_yaml::Mapping {
        let file = File::open(path).unwrap();
        let value: serde_yaml::Value = serde_yaml::from_reader(file).unwrap();
        value.as_mapping().unwrap().clone()
    }

    #[test]
    fn from_file_should_load_packages_defined_as_dirs() {
        // GIVEN a package defined as a dir
        let dir = assert_fs::TempDir::new().unwrap();
        let package_dir = dir.join("test");
        fs::create_dir(&package_dir).unwrap();

        let package_file = package_dir.join(INDEX_NAME);
        fs::write(&package_file, TEST_PACKAGE_YAML_CONTENT).unwrap();

        // WHEN loading the package
        // THEN it is loaded as expected
        let package = Package::from_file(&package_file).unwrap();

        // AND its package_dir is correct
        assert_eq!(package.package_dir, package_dir);

        // AND its tests are valid
        let version = Version::new(1, 2, 0);
        let install = package.get_install(&version, &ArchOs::current()).unwrap();
        assert_eq!(install.tests, &["foo --help", "foo --version"]);
    }

    #[test]
    fn from_file_should_load_packages_defined_as_files() {
        // GIVEN a package defined as a file
        let dir = assert_fs::TempDir::new().unwrap();

        let package_file = dir.join("test.yaml");
        fs::write(&package_file, TEST_PACKAGE_YAML_CONTENT).unwrap();

        // WHEN loading the package
        // THEN it is loaded as expected
        let package = Package::from_file(&package_file).unwrap();

        // AND its package_dir is correct
        assert_eq!(package.package_dir, dir.path());

        // AND its tests are valid
        let version = Version::new(1, 2, 0);
        let install = package.get_install(&version, &ArchOs::current()).unwrap();
        assert_eq!(install.tests, &["foo --help", "foo --version"]);
    }

    #[test]
    fn test_loading_package() {
        // GIVEN a package defined by TEST_PACKAGE_YAML_CONTENT
        // WHEN its loaded
        let package = Package::from_yaml_str(TEST_PACKAGE_YAML_CONTENT).unwrap();

        // THEN the 1.2.0 release, which uses the V1 variant, is correctly loaded
        let release_120 = package
            .releases
            .get(&Version::from_str("1.2.0").unwrap())
            .unwrap();
        assert!(release_120.published_at.is_none());
        let asset_120 = release_120.assets.get(&ArchOs::any()).unwrap();
        assert_eq!(asset_120.sha256, "1234");

        // AND the 1.3.0 release, which uses the V2 variant, is correctly loaded
        let release_130 = package
            .releases
            .get(&Version::from_str("1.3.0").unwrap())
            .unwrap();
        assert_eq!(
            release_130.published_at,
            Some(
                DateTime::parse_from_rfc3339("2024-01-02T12:34:56Z")
                    .unwrap()
                    .to_utc()
            )
        );
        assert!(release_130.assets.contains_key(&ArchOs::any()));
        let asset_130 = release_130.assets.get(&ArchOs::any()).unwrap();
        assert_eq!(asset_130.sha256, "5678");

        // AND the install section for the 1.2.0 release is correctly loaded
        let install = package
            .get_install(&Version::new(1, 2, 0), &ArchOs::current())
            .unwrap();
        assert_eq!(
            install.files.get("bin/foo-1.2"),
            Some(&"bin/foo".to_string())
        );
        assert_eq!(install.files.get("share"), Some(&"".to_string()));
    }

    #[test]
    fn saving_package_must_write_correct_release_format() {
        // GIVEN a package
        let package = Package::from_yaml_str(
            "
            name: test
            description: desc
            homepage:
            releases:
              2.0.0:
                x86_64-linux:
                  url: https://example.com
                  sha256: '1234'
            installs: {}
            ",
        )
        .unwrap();

        // WHEN it's saved to disk
        let dir = assert_fs::TempDir::new().unwrap();
        let path = dir.join("test.yaml");
        package.to_file(&path).unwrap();

        // THEN it uses the correct format for releases
        let root = read_yaml_from_path(&path);

        // Get the 2.0.0 release
        let release = root
            .get("releases")
            .unwrap()
            .as_mapping()
            .unwrap()
            .get("2.0.0")
            .unwrap()
            .as_mapping()
            .unwrap();

        // For now we want the V1 format, so there should be only one key: "x86_64-linux"
        let keys: Vec<String> = release
            .keys()
            .map(|x| x.as_str().unwrap().to_string())
            .collect();
        assert_eq!(keys, &["x86_64-linux"]);
    }

    #[test]
    fn saving_package_keeps_comment() {
        // GIVEN a package with a comment
        let package = Package::from_yaml_str(
            "
            name: test
            description: desc
            homepage:
            comment: Careful with test
            releases:
              2.0.0:
                x86_64-linux:
                  url: https://example.com
                  sha256: '1234'
            installs: {}
            ",
        )
        .unwrap();

        // WHEN it's saved to disk
        let dir = assert_fs::TempDir::new().unwrap();
        let path = dir.join("test.yaml");
        package.to_file(&path).unwrap();

        // THEN the comment is kept
        let root = read_yaml_from_path(&path);
        let comment = root.get("comment").unwrap();
        assert_eq!(comment.as_str().unwrap(), "Careful with test");
    }

    #[test]
    fn test_get_version_matching() {
        let package = Package::from_yaml_str(
            "
            name: test
            description: desc
            homepage:
            releases:
              2.0.0:
                any:
                  url: https://example.com
                  sha256: '1234'
              1.2.1:
                any:
                  url: https://example.com
                  sha256: '1234'
              1.2.0:
                any:
                  url: https://example.com
                  sha256: '1234'
            installs: {}
            ",
        )
        .unwrap();

        let req300 = VersionReq::parse("3.0.0").unwrap();
        let req121 = VersionReq::parse("1.2.1").unwrap();
        let req12 = VersionReq::parse("1.2.*").unwrap();
        let req2 = VersionReq::parse(">=2").unwrap();

        let v121 = Version::new(1, 2, 1);
        let v200 = Version::new(2, 0, 0);

        assert_eq!(package.get_version_matching(&req300), None);
        assert_eq!(package.get_version_matching(&req121), Some(&v121));
        assert_eq!(package.get_version_matching(&req12), Some(&v121));
        assert_eq!(package.get_version_matching(&req2), Some(&v200));
    }

    #[test]
    fn get_install_should_use_the_any_arch_specific_os_install() {
        // GIVEN a package with any and any-macos installs
        let package = Package::from_yaml_str(
            "
            name: test
            description: desc
            homepage:
            releases: {}
            installs:
              1.0.0:
                any:
                  strip: 1
                  files:
                    foo:
                any-macos:
                  strip: 3
                  files:
                    foo:
            ",
        )
        .unwrap();

        // WHEN installing on macos
        let install = package
            .get_install(
                &Version::new(1, 0, 0),
                &ArchOs::new(Arch::X86_64, Os::MacOs),
            )
            .unwrap();

        // THEN the any-macos install is used
        assert_eq!(install.strip, 3);
    }

    #[test]
    fn strip_should_default_to_0_if_not_set() {
        // GIVEN a package with no value for `strip`
        // WHEN parsing it
        let package = Package::from_yaml_str(
            "
            name: test
            description: desc
            homepage:
            releases: {}
            installs:
              1.0.0:
                any:
                  files:
                    foo:
            ",
        );

        // THEN it succeeds
        let package = package.unwrap();

        // AND strip is 0
        let install = package
            .get_install(&Version::new(1, 0, 0), &ArchOs::any())
            .unwrap();
        assert_eq!(install.strip, 0);
    }
}