pop-common 0.14.0

Library that provides a collection of essential utilities and shared functionality for pop.
Documentation
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
499
500
501
502
503
504
505
506
507
508
// SPDX-License-Identifier: GPL-3.0

use crate::{
	SortedSlice, Status,
	sourcing::{
		Error,
		GitHub::{ReleaseArchive, SourceCodeArchive},
		Source::{self, Archive, Git, GitHub},
		from_local_package,
	},
};
use std::path::{Path, PathBuf};

/// A binary used to launch a node.
#[derive(Debug, PartialEq)]
pub enum Binary {
	/// A local binary.
	Local {
		/// The name of the binary.
		name: String,
		/// The path of the binary.
		path: PathBuf,
		/// If applicable, the path to a manifest used to build the binary if missing.
		manifest: Option<PathBuf>,
	},
	/// A binary which needs to be sourced.
	Source {
		/// The name of the binary.
		name: String,
		/// The source of the binary.
		#[allow(private_interfaces)]
		source: Box<Source>,
		/// The cache to be used to store the binary.
		cache: PathBuf,
	},
}

impl Binary {
	/// Whether the binary exists.
	pub fn exists(&self) -> bool {
		self.path().exists()
	}

	/// If applicable, the latest version available.
	pub fn latest(&self) -> Option<&str> {
		match self {
			Self::Local { .. } => None,
			Self::Source { source, .. } => {
				if let GitHub(ReleaseArchive { latest, tag_pattern, .. }) = source.as_ref() {
					{
						// Extract the version from `latest`, provided it is a tag and that a tag
						// pattern exists
						latest.as_deref().and_then(|tag| {
							tag_pattern.as_ref().map_or(Some(tag), |pattern| pattern.version(tag))
						})
					}
				} else {
					None
				}
			},
		}
	}

	/// Whether the binary is defined locally.
	pub fn local(&self) -> bool {
		matches!(self, Self::Local { .. })
	}

	/// The name of the binary.
	pub fn name(&self) -> &str {
		match self {
			Self::Local { name, .. } => name,
			Self::Source { name, .. } => name,
		}
	}

	/// The path of the binary.
	pub fn path(&self) -> PathBuf {
		match self {
			Self::Local { path, .. } => path.to_path_buf(),
			Self::Source { name, cache, .. } => {
				// Determine whether a specific version is specified
				self.version()
					.map_or_else(|| cache.join(name), |v| cache.join(format!("{name}-{v}")))
			},
		}
	}

	/// Attempts to resolve a version of a binary based on whether one is specified, an existing
	/// version can be found cached locally, or uses the latest version.
	///
	/// # Arguments
	/// * `name` - The name of the binary.
	/// * `specified` - If available, a version explicitly specified.
	/// * `available` - The available versions, which are used to check for existing matches already
	///   cached locally or the latest otherwise.
	/// * `cache` - The location used for caching binaries.
	pub(super) fn resolve_version<'a>(
		name: &str,
		specified: Option<&'a str>,
		available: &'a SortedSlice<impl AsRef<str>>,
		cache: &Path,
	) -> Option<&'a str> {
		match specified {
			Some(version) => Some(version),
			None => available
				.iter()
				// Default to latest version available locally
				.filter_map(|version| {
					let version = version.as_ref();
					let path = cache.join(format!("{name}-{version}"));
					path.exists().then_some(Some(version))
				})
				.nth(0)
				// Default to latest version
				.unwrap_or_else(|| available.first().map(|version| version.as_ref())),
		}
	}

	/// Sources the binary.
	///
	/// # Arguments
	/// * `release` - Whether any binaries needing to be built should be done so using the release
	///   profile.
	/// * `status` - Used to observe status updates.
	/// * `verbose` - Whether verbose output is required.
	pub async fn source(
		&self,
		release: bool,
		status: &impl Status,
		verbose: bool,
	) -> Result<(), Error> {
		match self {
			Self::Local { name, path, manifest, .. } => match manifest {
				None => Err(Error::MissingBinary(format!(
					"The {path:?} binary cannot be sourced automatically."
				))),
				Some(manifest) =>
					from_local_package(manifest, name, release, status, verbose).await,
			},
			Self::Source { source, cache, .. } =>
				source.source(cache, release, status, verbose).await,
		}
	}

	/// Whether any locally cached version can be replaced with a newer version.
	pub fn stale(&self) -> bool {
		// Only binaries sourced from GitHub release archives can currently be determined as stale
		let Self::Source { source, .. } = self else {
			return false;
		};
		let GitHub(ReleaseArchive { tag, latest, .. }) = source.as_ref() else {
			return false;
		};
		latest.as_ref().is_some_and(|l| tag.as_ref() != Some(l))
	}

	/// Specifies that the latest available versions are to be used (where possible).
	pub fn use_latest(&mut self) {
		let Self::Source { source, .. } = self else {
			return;
		};
		if let GitHub(ReleaseArchive { tag, latest: Some(latest), .. }) = source.as_mut() {
			*tag = Some(latest.clone())
		};
	}

	/// If applicable, the version of the binary.
	pub fn version(&self) -> Option<&str> {
		match self {
			Self::Local { .. } => None,
			Self::Source { source, .. } => match source.as_ref() {
				Git { reference, .. } => reference.as_ref().map(|r| r.as_str()),
				GitHub(source) => match source {
					ReleaseArchive { tag, tag_pattern, .. } => tag.as_ref().map(|tag| {
						// Use any tag pattern defined to extract a version, otherwise use the tag.
						tag_pattern.as_ref().and_then(|pattern| pattern.version(tag)).unwrap_or(tag)
					}),
					SourceCodeArchive { reference, .. } => reference.as_ref().map(|r| r.as_str()),
				},
				Archive { .. } | Source::Url { .. } => None,
			},
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{
		polkadot_sdk::{sort_by_latest_semantic_version, sort_by_latest_version},
		sourcing::{ArchiveFileSpec, tests::Output},
		target,
	};
	use anyhow::Result;
	use duct::cmd;
	use std::fs::{File, create_dir_all};
	use tempfile::tempdir;
	use url::Url;

	#[test]
	fn local_binary_works() -> Result<()> {
		let name = "polkadot";
		let temp_dir = tempdir()?;
		let path = temp_dir.path().join(name);
		File::create(&path)?;

		let binary = Binary::Local { name: name.to_string(), path: path.clone(), manifest: None };

		assert!(binary.exists());
		assert_eq!(binary.latest(), None);
		assert!(binary.local());
		assert_eq!(binary.name(), name);
		assert_eq!(binary.path(), path);
		assert!(!binary.stale());
		assert_eq!(binary.version(), None);
		Ok(())
	}

	#[test]
	fn local_package_works() -> Result<()> {
		let name = "polkadot";
		let temp_dir = tempdir()?;
		let path = temp_dir.path().join("target/release").join(name);
		create_dir_all(path.parent().unwrap())?;
		File::create(&path)?;
		let manifest = Some(temp_dir.path().join("Cargo.toml"));

		let binary = Binary::Local { name: name.to_string(), path: path.clone(), manifest };

		assert!(binary.exists());
		assert_eq!(binary.latest(), None);
		assert!(binary.local());
		assert_eq!(binary.name(), name);
		assert_eq!(binary.path(), path);
		assert!(!binary.stale());
		assert_eq!(binary.version(), None);
		Ok(())
	}

	#[test]
	fn resolve_version_works() -> Result<()> {
		let name = "polkadot";
		let temp_dir = tempdir()?;

		let mut available = vec!["v1.13.0", "v1.12.0", "v1.11.0", "stable2409"];
		let available = sort_by_latest_version(available.as_mut_slice());

		// Specified
		let specified = Some("v1.12.0");
		assert_eq!(
			Binary::resolve_version(name, specified, &available, temp_dir.path()),
			specified
		);
		// Latest
		assert_eq!(
			Binary::resolve_version(name, None, &available, temp_dir.path()).unwrap(),
			"stable2409"
		);
		// Cached
		File::create(temp_dir.path().join(format!("{name}-{}", available[1])))?;
		assert_eq!(
			Binary::resolve_version(name, None, &available, temp_dir.path()).unwrap(),
			available[1]
		);
		Ok(())
	}

	#[tokio::test]
	async fn sourced_from_archive_works() -> Result<()> {
		let name = "polkadot";
		let url = "https://github.com/r0gue-io/polkadot/releases/latest/download/polkadot-aarch64-apple-darwin.tar.gz".to_string();
		let contents = vec![
			name.to_string(),
			"polkadot-execute-worker".into(),
			"polkadot-prepare-worker".into(),
		];
		let temp_dir = tempdir()?;
		let path = temp_dir.path().join(name);
		File::create(&path)?;

		let binary = Binary::Source {
			name: name.to_string(),
			source: Archive { url: url.to_string(), contents }.into(),
			cache: temp_dir.path().to_path_buf(),
		};

		assert!(binary.exists());
		assert_eq!(binary.latest(), None);
		assert!(!binary.local());
		assert_eq!(binary.name(), name);
		assert_eq!(binary.path(), path);
		assert!(!binary.stale());
		assert_eq!(binary.version(), None);
		Ok(())
	}

	#[tokio::test]
	async fn sourced_from_git_works() -> Result<()> {
		let package = "hello_world";
		let url = Url::parse("https://github.com/hpaluch/rust-hello-world")?;
		let temp_dir = tempdir()?;
		for reference in [None, Some("436b7dbffdfaaf7ad90bf44ae8fdcb17eeee65a3".to_string())] {
			let path = temp_dir.path().join(
				reference
					.as_ref()
					.map_or(package.into(), |reference| format!("{package}-{reference}")),
			);
			File::create(&path)?;

			let mut binary = Binary::Source {
				name: package.to_string(),
				source: Git {
					url: url.clone(),
					reference: reference.clone(),
					manifest: None,
					package: package.to_string(),
					artifacts: vec![package.to_string()],
				}
				.into(),
				cache: temp_dir.path().to_path_buf(),
			};

			assert!(binary.exists());
			assert_eq!(binary.latest(), None);
			assert!(!binary.local());
			assert_eq!(binary.name(), package);
			assert_eq!(binary.path(), path);
			assert!(!binary.stale());
			assert_eq!(binary.version(), reference.as_deref());
			binary.use_latest();
			assert_eq!(binary.version(), reference.as_deref());
		}

		Ok(())
	}

	#[tokio::test]
	async fn sourced_from_github_release_archive_works() -> Result<()> {
		let owner = "r0gue-io";
		let repository = "polkadot";
		let tag_pattern = "polkadot-{version}";
		let name = "polkadot";
		let archive = format!("{name}-{}.tar.gz", target()?);
		let fallback = "stable2512".to_string();
		let contents = ["polkadot", "polkadot-execute-worker", "polkadot-prepare-worker"];
		let temp_dir = tempdir()?;
		for tag in [None, Some("stable2512".to_string())] {
			let path = temp_dir
				.path()
				.join(tag.as_ref().map_or(name.to_string(), |t| format!("{name}-{t}")));
			File::create(&path)?;
			for latest in [None, Some("polkadot-stable2512".to_string())] {
				let mut binary = Binary::Source {
					name: name.to_string(),
					source: GitHub(ReleaseArchive {
						owner: owner.into(),
						repository: repository.into(),
						tag: tag.clone(),
						tag_pattern: Some(tag_pattern.into()),
						prerelease: false,
						version_comparator: sort_by_latest_semantic_version,
						fallback: fallback.clone(),
						archive: archive.clone(),
						contents: contents
							.into_iter()
							.map(|b| ArchiveFileSpec::new(b.into(), None, true))
							.collect(),
						latest: latest.clone(),
					})
					.into(),
					cache: temp_dir.path().to_path_buf(),
				};

				let latest = latest.as_ref().map(|l| l.replace("polkadot-", ""));

				assert!(binary.exists());
				assert_eq!(binary.latest(), latest.as_deref());
				assert!(!binary.local());
				assert_eq!(binary.name(), name);
				assert_eq!(binary.path(), path);
				assert_eq!(binary.stale(), latest.is_some());
				assert_eq!(binary.version(), tag.as_deref());
				binary.use_latest();
				if latest.is_some() {
					assert_eq!(binary.version(), latest.as_deref());
				}
			}
		}
		Ok(())
	}

	#[tokio::test]
	async fn sourced_from_github_source_code_archive_works() -> Result<()> {
		let owner = "paritytech";
		let repository = "polkadot-sdk";
		let package = "polkadot";
		let manifest = "substrate/Cargo.toml";
		let temp_dir = tempdir()?;
		for reference in [None, Some("72dba98250a6267c61772cd55f8caf193141050f".to_string())] {
			let path = temp_dir
				.path()
				.join(reference.as_ref().map_or(package.to_string(), |t| format!("{package}-{t}")));
			File::create(&path)?;
			let mut binary = Binary::Source {
				name: package.to_string(),
				source: GitHub(SourceCodeArchive {
					owner: owner.to_string(),
					repository: repository.to_string(),
					reference: reference.clone(),
					manifest: Some(PathBuf::from(manifest)),
					package: package.to_string(),
					artifacts: vec![package.to_string()],
				})
				.into(),
				cache: temp_dir.path().to_path_buf(),
			};

			assert!(binary.exists());
			assert_eq!(binary.latest(), None);
			assert!(!binary.local());
			assert_eq!(binary.name(), package);
			assert_eq!(binary.path(), path);
			assert!(!binary.stale());
			assert_eq!(binary.version(), reference.as_deref());
			binary.use_latest();
			assert_eq!(binary.version(), reference.as_deref());
		}
		Ok(())
	}

	#[tokio::test]
	async fn sourced_from_url_works() -> Result<()> {
		let name = "polkadot";
		let url =
			"https://github.com/paritytech/polkadot-sdk/releases/latest/download/polkadot.asc";
		let temp_dir = tempdir()?;
		let path = temp_dir.path().join(name);
		File::create(&path)?;

		let mut binary = Binary::Source {
			name: name.to_string(),
			source: Source::Url { url: url.to_string(), name: name.to_string() }.into(),
			cache: temp_dir.path().to_path_buf(),
		};

		assert!(binary.exists());
		assert_eq!(binary.latest(), None);
		assert!(!binary.local());
		assert_eq!(binary.name(), name);
		assert_eq!(binary.path(), path);
		assert!(!binary.stale());
		assert_eq!(binary.version(), None);
		binary.use_latest();
		assert_eq!(binary.version(), None);
		Ok(())
	}

	#[tokio::test]
	async fn sourcing_from_local_binary_not_supported() -> Result<()> {
		let name = "polkadot".to_string();
		let temp_dir = tempdir()?;
		let path = temp_dir.path().join(&name);
		assert!(matches!(
			Binary::Local { name, path: path.clone(), manifest: None }.source(true, &Output, true).await,
			Err(Error::MissingBinary(error)) if error == format!("The {path:?} binary cannot be sourced automatically.")
		));
		Ok(())
	}

	#[tokio::test]
	async fn sourcing_from_local_package_works() -> Result<()> {
		crate::command_mock::CommandMock::default()
			.execute(async || {
				let temp_dir = tempdir()?;
				let name = "hello_world";
				cmd("cargo", ["new", name, "--bin"]).dir(temp_dir.path()).run()?;
				let path = temp_dir.path().join(name);
				let manifest = Some(path.join("Cargo.toml"));
				let path = path.join("target/release").join(name);
				Binary::Local { name: name.to_string(), path: path.clone(), manifest }
					.source(true, &Output, true)
					.await?;
				assert!(path.exists());
				Ok(())
			})
			.await
	}

	#[tokio::test]
	async fn sourcing_from_url_works() -> Result<()> {
		let name = "polkadot";
		let url =
			"https://github.com/paritytech/polkadot-sdk/releases/latest/download/polkadot.asc";
		let temp_dir = tempdir()?;
		let path = temp_dir.path().join(name);

		Binary::Source {
			name: name.to_string(),
			source: Source::Url { url: url.to_string(), name: name.to_string() }.into(),
			cache: temp_dir.path().to_path_buf(),
		}
		.source(true, &Output, true)
		.await?;
		assert!(path.exists());
		Ok(())
	}
}