Skip to main content

pop_chains/try_runtime/
binary.rs

1// SPDX-License-Identifier: GPL-3.0
2
3use 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	/// Defines the source of the binary required for testing runtime upgrades.
35	fn source(&self) -> Result<Source, Error> {
36		// Source from GitHub release asset
37		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
54/// Generate the source of the `try-runtime` binary on the remote repository.
55///
56/// # Arguments
57/// * `cache` - The path to the directory where the binary should be cached.
58/// * `version` - An optional version string. If `None`, the latest available version is used.
59pub 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}