From 2284850805a664c8f02b992980d447924c4fe69d Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Fri, 13 Sep 2024 02:04:27 +0200 Subject: [PATCH 1/3] fix: workspace manifest creation --- crates/pop-cli/src/commands/build/spec.rs | 22 +- crates/pop-cli/src/commands/install/mod.rs | 5 +- crates/pop-cli/src/commands/new/parachain.rs | 4 +- crates/pop-cli/src/commands/up/contract.rs | 4 +- crates/pop-cli/src/commands/up/parachain.rs | 5 +- crates/pop-common/src/errors.rs | 2 + crates/pop-common/src/git.rs | 3 +- crates/pop-common/src/lib.rs | 10 +- crates/pop-common/src/manifest.rs | 16 + crates/pop-common/src/sourcing/binary.rs | 20 +- crates/pop-common/src/sourcing/mod.rs | 10 +- crates/pop-common/src/templates/extractor.rs | 16 +- crates/pop-parachains/src/build.rs | 4 +- .../pop-parachains/src/generator/container.rs | 5 - crates/pop-parachains/src/new_parachain.rs | 86 ++-- crates/pop-parachains/src/templates.rs | 16 +- crates/pop-parachains/src/up/mod.rs | 5 +- crates/pop-parachains/src/utils/helpers.rs | 4 +- .../templates/container/Cargo.templ | 407 ------------------ 19 files changed, 128 insertions(+), 516 deletions(-) diff --git a/crates/pop-cli/src/commands/build/spec.rs b/crates/pop-cli/src/commands/build/spec.rs index 768b467a..a40871dc 100644 --- a/crates/pop-cli/src/commands/build/spec.rs +++ b/crates/pop-cli/src/commands/build/spec.rs @@ -370,13 +370,13 @@ async fn guide_user_to_generate_spec(args: BuildSpecCommand) -> anyhow::Result { + ChainType::Live => for relay in RelayChain::VARIANTS { if !matches!( relay, - RelayChain::Westend - | RelayChain::Paseo | RelayChain::Kusama - | RelayChain::Polkadot + RelayChain::Westend | + RelayChain::Paseo | RelayChain::Kusama | + RelayChain::Polkadot ) { continue; } else { @@ -386,15 +386,14 @@ async fn guide_user_to_generate_spec(args: BuildSpecCommand) -> anyhow::Result { + }, + _ => for relay in RelayChain::VARIANTS { if matches!( relay, - RelayChain::Westend - | RelayChain::Paseo | RelayChain::Kusama - | RelayChain::Polkadot + RelayChain::Westend | + RelayChain::Paseo | RelayChain::Kusama | + RelayChain::Polkadot ) { continue; } else { @@ -404,8 +403,7 @@ async fn guide_user_to_generate_spec(args: BuildSpecCommand) -> anyhow::Result anyhow::Result<()> { async fn install_homebrew() -> anyhow::Result<()> { match cmd("which", vec!["brew"]).read() { Ok(output) => log::info(format!("ℹī¸ Homebrew installed already at {}.", output))?, - Err(_) => { + Err(_) => run_external_script( "https://raw.githubusercontent.com/Homebrew/install/master/install.sh", ) - .await? - }, + .await?, } Ok(()) } diff --git a/crates/pop-cli/src/commands/new/parachain.rs b/crates/pop-cli/src/commands/new/parachain.rs index ba69ea44..a9adb1bd 100644 --- a/crates/pop-cli/src/commands/new/parachain.rs +++ b/crates/pop-cli/src/commands/new/parachain.rs @@ -241,8 +241,8 @@ fn get_customization_value( decimals: Option, initial_endowment: Option, ) -> Result { - if !matches!(template, Parachain::Standard) - && (symbol.is_some() || decimals.is_some() || initial_endowment.is_some()) + if !matches!(template, Parachain::Standard) && + (symbol.is_some() || decimals.is_some() || initial_endowment.is_some()) { log::warning("Customization options are not available for this template")?; sleep(Duration::from_secs(3)) diff --git a/crates/pop-cli/src/commands/up/contract.rs b/crates/pop-cli/src/commands/up/contract.rs index 5541ec3a..e5be4966 100644 --- a/crates/pop-cli/src/commands/up/contract.rs +++ b/crates/pop-cli/src/commands/up/contract.rs @@ -326,8 +326,8 @@ pub fn has_contract_been_built(path: Option<&Path>) -> bool { Err(_) => return false, }; let contract_name = manifest.package().name(); - project_path.join("target/ink").exists() - && project_path.join(format!("target/ink/{}.contract", contract_name)).exists() + project_path.join("target/ink").exists() && + project_path.join(format!("target/ink/{}.contract", contract_name)).exists() } #[cfg(test)] diff --git a/crates/pop-cli/src/commands/up/parachain.rs b/crates/pop-cli/src/commands/up/parachain.rs index fe1bbc1a..e68d20a1 100644 --- a/crates/pop-cli/src/commands/up/parachain.rs +++ b/crates/pop-cli/src/commands/up/parachain.rs @@ -71,7 +71,7 @@ impl ZombienetCommand { .await { Ok(n) => n, - Err(e) => { + Err(e) => return match e { Error::Config(message) => { outro_cancel(format!("đŸšĢ A configuration error occurred: `{message}`"))?; @@ -82,8 +82,7 @@ impl ZombienetCommand { Ok(()) }, _ => Err(e.into()), - } - }, + }, }; // Source any missing/stale binaries diff --git a/crates/pop-common/src/errors.rs b/crates/pop-common/src/errors.rs index 02fb0c9b..fed84ad2 100644 --- a/crates/pop-common/src/errors.rs +++ b/crates/pop-common/src/errors.rs @@ -27,4 +27,6 @@ pub enum Error { UnsupportedCommand(String), #[error("Unsupported platform: {arch} {os}")] UnsupportedPlatform { arch: &'static str, os: &'static str }, + #[error("Toml error: {0}")] + TomlError(#[from] toml_edit::TomlError), } diff --git a/crates/pop-common/src/git.rs b/crates/pop-common/src/git.rs index 2e6b205a..f93fbc14 100644 --- a/crates/pop-common/src/git.rs +++ b/crates/pop-common/src/git.rs @@ -124,7 +124,8 @@ impl Git { Self::parse_latest_tag(tags.iter().flatten().collect::>()) } - /// Parses a list of tags to identify the latest one, prioritizing tags in the stable format first. + /// Parses a list of tags to identify the latest one, prioritizing tags in the stable format + /// first. fn parse_latest_tag(tags: Vec<&str>) -> Option { match Self::parse_stable_format(tags.clone()) { Some(last_stable_tag) => Some(last_stable_tag), diff --git a/crates/pop-common/src/lib.rs b/crates/pop-common/src/lib.rs index cf375654..d448971f 100644 --- a/crates/pop-common/src/lib.rs +++ b/crates/pop-common/src/lib.rs @@ -34,18 +34,16 @@ pub fn target() -> Result<&'static str, Error> { } match ARCH { - "aarch64" => { + "aarch64" => return match OS { "macos" => Ok("aarch64-apple-darwin"), _ => Ok("aarch64-unknown-linux-gnu"), - } - }, - "x86_64" | "x86" => { + }, + "x86_64" | "x86" => return match OS { "macos" => Ok("x86_64-apple-darwin"), _ => Ok("x86_64-unknown-linux-gnu"), - } - }, + }, &_ => {}, } Err(Error::UnsupportedPlatform { arch: ARCH, os: OS }) diff --git a/crates/pop-common/src/manifest.rs b/crates/pop-common/src/manifest.rs index 5ceb31e4..eba2d8f6 100644 --- a/crates/pop-common/src/manifest.rs +++ b/crates/pop-common/src/manifest.rs @@ -4,6 +4,7 @@ use crate::Error; use anyhow; pub use cargo_toml::{Dependency, Manifest}; use std::{ + collections::HashSet, fs::{read_to_string, write}, path::{Path, PathBuf}, }; @@ -91,6 +92,21 @@ pub fn add_crate_to_workspace(workspace_toml: &Path, crate_path: &Path) -> anyho Ok(()) } +/// Collects the dependencies of the given `Cargo.toml` manifests in a HashMap. +/// +/// # Arguments +/// * `manifests`: Paths of the manifests to collect dependencies from. +pub fn collect_manifest_dependencies(manifests: Vec<&Path>) -> anyhow::Result> { + let mut dependencies = HashSet::new(); + for m in manifests { + let cargo = &std::fs::read_to_string(m)?.parse::()?; + for d in cargo["dependencies"].as_table().unwrap().into_iter() { + dependencies.insert(d.0.into()); + } + } + Ok(dependencies) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/pop-common/src/sourcing/binary.rs b/crates/pop-common/src/sourcing/binary.rs index a8063616..ae9bd5f9 100644 --- a/crates/pop-common/src/sourcing/binary.rs +++ b/crates/pop-common/src/sourcing/binary.rs @@ -45,13 +45,12 @@ impl Binary { pub fn latest(&self) -> Option<&str> { match self { Self::Local { .. } => None, - Self::Source { source, .. } => { + Self::Source { source, .. } => if let GitHub(ReleaseArchive { latest, .. }) = source { latest.as_deref() } else { None - } - }, + }, } } @@ -135,18 +134,15 @@ impl Binary { ) -> Result<(), Error> { match self { Self::Local { name, path, manifest, .. } => match manifest { - None => { + None => return 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 + ))), + Some(manifest) => + from_local_package(manifest, name, release, status, verbose).await, }, + Self::Source { source, cache, .. } => + source.source(cache, release, status, verbose).await, } } diff --git a/crates/pop-common/src/sourcing/mod.rs b/crates/pop-common/src/sourcing/mod.rs index 0ba28a02..81d0bc3f 100644 --- a/crates/pop-common/src/sourcing/mod.rs +++ b/crates/pop-common/src/sourcing/mod.rs @@ -210,9 +210,8 @@ impl GitHub { let artifacts: Vec<_> = artifacts .iter() .map(|name| match reference { - Some(reference) => { - (name.as_str(), cache.join(&format!("{name}-{reference}"))) - }, + Some(reference) => + (name.as_str(), cache.join(&format!("{name}-{reference}"))), None => (name.as_str(), cache.join(&name)), }) .collect(); @@ -379,11 +378,10 @@ async fn from_github_archive( // Prepare archive contents for build let entries: Vec<_> = read_dir(&working_dir)?.take(2).filter_map(|x| x.ok()).collect(); match entries.len() { - 0 => { + 0 => return Err(Error::ArchiveError( "The downloaded archive does not contain any entries.".into(), - )) - }, + )), 1 => working_dir = entries[0].path(), // Automatically switch to top level directory _ => {}, /* Assume that downloaded archive does not have a * top level directory */ diff --git a/crates/pop-common/src/templates/extractor.rs b/crates/pop-common/src/templates/extractor.rs index b4d32305..62228c4c 100644 --- a/crates/pop-common/src/templates/extractor.rs +++ b/crates/pop-common/src/templates/extractor.rs @@ -1,4 +1,4 @@ - // SPDX-License-Identifier: GPL-3.0 +// SPDX-License-Identifier: GPL-3.0 use std::{fs, io, path::Path}; @@ -10,8 +10,8 @@ use anyhow::Result; /// * `template_file` - The name of the template to extract. /// * `repo_directory` - The path to the repository directory containing the template. /// * `target_directory` - The destination path where the template files should be copied. -/// * `ignore_directories` - A vector of directory names to ignore during the extraction. If empty, no -/// directories are ignored. +/// * `ignore_directories` - A vector of directory names to ignore during the extraction. If empty, +/// no directories are ignored. pub fn extract_template_files( template_file: String, repo_directory: &Path, @@ -21,7 +21,11 @@ pub fn extract_template_files( let template_directory = repo_directory.join(&template_file); if template_directory.is_dir() { // Recursively copy all directories and files within. Ignores the specified ones. - copy_dir_all(&template_directory, target_directory, &ignore_directories.unwrap_or_else(|| vec![]))?; + copy_dir_all( + &template_directory, + target_directory, + &ignore_directories.unwrap_or_else(|| vec![]), + )?; return Ok(()); } else { // If not a dir, just copy the file. @@ -49,8 +53,8 @@ fn copy_dir_all( for entry in fs::read_dir(src)? { let entry = entry?; let ty = entry.file_type()?; - if ty.is_dir() - && ignore_directories.contains(&entry.file_name().to_string_lossy().to_string()) + if ty.is_dir() && + ignore_directories.contains(&entry.file_name().to_string_lossy().to_string()) { continue; } else if ty.is_dir() { diff --git a/crates/pop-parachains/src/build.rs b/crates/pop-parachains/src/build.rs index 669ed1e8..38d760c5 100644 --- a/crates/pop-parachains/src/build.rs +++ b/crates/pop-parachains/src/build.rs @@ -49,8 +49,8 @@ pub fn is_supported(path: Option<&Path>) -> Result { const DEPENDENCIES: [&str; 4] = ["cumulus-client-collator", "cumulus-primitives-core", "parachains-common", "polkadot-sdk"]; Ok(DEPENDENCIES.into_iter().any(|d| { - manifest.dependencies.contains_key(d) - || manifest.workspace.as_ref().map_or(false, |w| w.dependencies.contains_key(d)) + manifest.dependencies.contains_key(d) || + manifest.workspace.as_ref().map_or(false, |w| w.dependencies.contains_key(d)) })) } diff --git a/crates/pop-parachains/src/generator/container.rs b/crates/pop-parachains/src/generator/container.rs index 4d40ffed..40752da0 100644 --- a/crates/pop-parachains/src/generator/container.rs +++ b/crates/pop-parachains/src/generator/container.rs @@ -12,9 +12,4 @@ pub(crate) struct Network { #[template(path = "container/Cargo.templ", escape = "none")] pub(crate) struct Cargo { pub(crate) template: String, - pub(crate) template_tag: String, - pub(crate) dancekit_branch: String, - pub(crate) moonkit_branch: String, - pub(crate) sdk_branch: String, - pub(crate) frontier_branch: String, } diff --git a/crates/pop-parachains/src/new_parachain.rs b/crates/pop-parachains/src/new_parachain.rs index 25a71353..b40ebacf 100644 --- a/crates/pop-parachains/src/new_parachain.rs +++ b/crates/pop-parachains/src/new_parachain.rs @@ -5,9 +5,9 @@ use std::{fs, path::Path}; use anyhow::Result; use pop_common::{ git::Git, + manifest::{collect_manifest_dependencies, from_path}, templates::{extractor::extract_template_files, Template, Type}, }; -use toml_edit::ImDocument; use walkdir::WalkDir; use crate::{ @@ -117,7 +117,7 @@ fn instantiate_tanssi_template( // │├â”Ŧ nodes // ││└