pop_chains/try_runtime/
binary.rs1use pop_common::{
4 Error,
5 git::GitHub,
6 polkadot_sdk::sort_by_latest_semantic_version,
7 sourcing::{
8 ArchiveFileSpec, Binary,
9 GitHub::*,
10 Source,
11 filters::prefix,
12 traits::{
13 Source as SourceT,
14 enums::{Source as _, *},
15 },
16 },
17 target,
18};
19use std::path::PathBuf;
20use strum_macros::EnumProperty;
21
22#[derive(Debug, EnumProperty, PartialEq)]
23pub(super) enum TryRuntimeCli {
24 #[strum(props(
25 Repository = "https://github.com/r0gue-io/try-runtime-cli",
26 Binary = "try-runtime-cli",
27 Fallback = "v0.8.0"
28 ))]
29 TryRuntime,
30}
31
32impl SourceT for TryRuntimeCli {
33 type Error = Error;
34 fn source(&self) -> Result<Source, Error> {
36 let repo = GitHub::parse(self.repository())?;
38 let binary = self.binary();
39 Ok(Source::GitHub(ReleaseArchive {
40 owner: repo.org,
41 repository: repo.name,
42 tag: None,
43 tag_pattern: self.tag_pattern().map(|t| t.into()),
44 prerelease: false,
45 version_comparator: sort_by_latest_semantic_version,
46 fallback: self.fallback().into(),
47 archive: format!("{binary}-{}.tar.gz", target()?),
48 contents: vec![ArchiveFileSpec::new(binary.into(), Some(binary.into()), true)],
49 latest: None,
50 }))
51 }
52}
53
54pub async fn try_runtime_generator(cache: PathBuf, version: Option<&str>) -> Result<Binary, Error> {
60 let cli = TryRuntimeCli::TryRuntime;
61 let name = cli.binary().to_string();
62 let source = cli
63 .source()?
64 .resolve(&name, version, cache.as_path(), |f| prefix(f, &name))
65 .await
66 .into();
67 let binary = Binary::Source { name, source, cache: cache.to_path_buf() };
68 Ok(binary)
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74 use tempfile::tempdir;
75
76 #[tokio::test]
77 async fn try_runtime_generator_works() -> Result<(), Error> {
78 let temp_dir = tempdir()?;
79 let path = temp_dir.path().to_path_buf();
80 let version = "v0.8.0";
81 let binary = try_runtime_generator(path.clone(), None).await?;
82 assert!(matches!(binary, Binary::Source { name: _, source, cache }
83 if source == Source::GitHub(ReleaseArchive {
84 owner: "r0gue-io".to_string(),
85 repository: "try-runtime-cli".to_string(),
86 tag: Some(version.to_string()),
87 tag_pattern: None,
88 prerelease: false,
89 version_comparator: sort_by_latest_semantic_version,
90 fallback: version.into(),
91 archive: format!("try-runtime-cli-{}.tar.gz", target()?),
92 contents: ["try-runtime-cli"].map(|b| ArchiveFileSpec::new(b.into(), Some(b.into()), true)).to_vec(),
93 latest: binary.latest().map(|l| l.to_string()),
94 }).into() &&
95 cache == path
96 ));
97 Ok(())
98 }
99}