From 1d92314fbdcd91fb12a8907956e5236dbd42272d Mon Sep 17 00:00:00 2001 From: Abhishek Shah Date: Tue, 19 Mar 2024 15:03:29 +0530 Subject: [PATCH] separate dependency module --- Cargo.lock | 1 + Cargo.toml | 2 +- src/engines/pallet_engine/dependency.rs | 91 +++++++++++++++++++++++++ src/engines/pallet_engine/mod.rs | 47 +------------ src/engines/pallet_engine/steps.rs | 2 +- tests/add.rs | 1 + 6 files changed, 96 insertions(+), 48 deletions(-) create mode 100644 src/engines/pallet_engine/dependency.rs diff --git a/Cargo.lock b/Cargo.lock index e8ff5af9..54030fa3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5460,6 +5460,7 @@ dependencies = [ "proc-macro2", "quote", "reqwest", + "semver 1.0.22", "serde", "serde_json", "sp-core 30.0.0", diff --git a/Cargo.toml b/Cargo.toml index a684eb14..db21a534 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ console = "0.15" duct = "0.13" git2 = "0.18" log = "0.4" -# semver = "1.0.20" +semver = "1.0.20" strum = "0.26" strum_macros = "0.26" tempfile = "3.8" diff --git a/src/engines/pallet_engine/dependency.rs b/src/engines/pallet_engine/dependency.rs new file mode 100644 index 00000000..8547345a --- /dev/null +++ b/src/engines/pallet_engine/dependency.rs @@ -0,0 +1,91 @@ +//! Dependency representations for Pallets +use strum_macros::{Display, EnumString}; + +#[derive(EnumString, Display, Debug)] +pub(super) enum Features { + #[strum(serialize = "std")] + Std, + #[strum(serialize = "runtime-benchmarks")] + RuntimeBenchmarks, + #[strum(serialize = "try-runtime")] + TryRuntime, + /// Custom feature + Custom(String), +} +#[derive(Display, Debug)] +pub(super) enum Location { + /// Local path is form `path = "X"` + Local(std::path::PathBuf), + /// `git = {url}` + Git(reqwest::Url), + /// String should be of format `version = "X"` + CratesIO(semver::Version), +} +impl From for Location { + fn from(url: reqwest::Url) -> Self { + Self::Git(url) + } +} +impl From for Location { + fn from(path: std::path::PathBuf) -> Self { + Self::Local(path) + } +} +impl<'a> From<&'a std::path::Path> for Location { + fn from(path: &'a std::path::Path) -> Self { + Self::Local(path.to_path_buf()) + } +} +impl From for Location { + fn from(version: semver::Version) -> Self { + Self::CratesIO(version) + } +} +impl Into for Location { + fn into(self) -> String { + match self { + Location::Local(path) => format!("path = \"{}\"", path.display()), + Location::Git(url) => format!("git = \"{}\"", url), + Location::CratesIO(version) => format!("version = \"{}\"", version), + } + } +} +impl Into for Location { + fn into(self) -> toml_edit::Value { + Into::::into(self).into() + } +} + +#[derive(Debug)] +pub(super) struct Dependency { + pub(super) features: Vec, + /// Maybe local path, git url, or from crates.io in which case we will use this for version + pub(super) path: Location, + pub(super) default_features: bool, +} + +impl Dependency { + /// Create dependencies required for adding a local pallet-parachain-template to runtime + /// ..$(runtime)/pallets/pallet-parachain-template + pub(super) fn local_template_runtime() -> Self { + Self { + features: vec![Features::RuntimeBenchmarks, Features::TryRuntime, Features::Std], + // TODO hardcode for now + path: std::path::Path::new("../pallets/pallet-parachain-template") + .to_path_buf() + .into(), + default_features: false, + } + } + // TODO: Remove code - Node doesn't require template pallet deps by default + // but this maybe desirable for custom pallets. + // /// Create dependencies required for adding a pallet-parachain-template to node + // pub(super) fn template_node() -> Self { + // Self { + // features: vec![Features::RuntimeBenchmarks, Features::TryRuntime], + // // TODO hardcode for now + // path: format!("../pallets/pallet-parachain-template"), + // default_features: true, + // } + // } +} diff --git a/src/engines/pallet_engine/mod.rs b/src/engines/pallet_engine/mod.rs index ad9363e2..c402f467 100644 --- a/src/engines/pallet_engine/mod.rs +++ b/src/engines/pallet_engine/mod.rs @@ -18,6 +18,7 @@ mod pallet_entry; mod parser; mod steps; mod template; +mod dependency; use crate::commands::add::AddPallet; use crate::helpers::{is_git_repo_with_commits, write_to_file}; @@ -586,49 +587,3 @@ impl PalletEngine { Ok(()) } } -// TODO -mod dependency { - use strum_macros::{Display, EnumString}; - - #[derive(EnumString, Display, Debug)] - pub(in crate::engines::pallet_engine) enum Features { - #[strum(serialize = "std")] - Std, - #[strum(serialize = "runtime-benchmarks")] - RuntimeBenchmarks, - #[strum(serialize = "try-runtime")] - TryRuntime, - Custom(String), - } - #[derive(Debug)] - pub(in crate::engines::pallet_engine) struct Dependency { - pub(in crate::engines::pallet_engine) features: Vec, - /// Maybe local path, git url, or from crates.io in which case we will use this for version - pub(in crate::engines::pallet_engine) path: String, - pub(in crate::engines::pallet_engine) default_features: bool, - } - - impl Dependency { - /// Create dependencies required for adding a pallet-parachain-template to runtime - pub(in crate::engines::pallet_engine) fn template_runtime() -> Self { - log::warn!("Using default path for pallet-parachain-template `pallets/pallet-parachain-template`"); - Self { - features: vec![Features::RuntimeBenchmarks, Features::TryRuntime, Features::Std], - // TODO hardcode for now - path: format!("../pallets/pallet-parachain-template"), - default_features: false, - } - } - // TODO: Remove code - Node doesn't require template pallet deps by default - // but this maybe desirable for custom pallets. - // /// Create dependencies required for adding a pallet-parachain-template to node - // pub(in crate::engines::pallet_engine) fn template_node() -> Self { - // Self { - // features: vec![Features::RuntimeBenchmarks, Features::TryRuntime], - // // TODO hardcode for now - // path: format!("../pallets/pallet-parachain-template"), - // default_features: true, - // } - // } - } -} diff --git a/src/engines/pallet_engine/steps.rs b/src/engines/pallet_engine/steps.rs index 2be48bdb..6dbe090f 100644 --- a/src/engines/pallet_engine/steps.rs +++ b/src/engines/pallet_engine/steps.rs @@ -66,7 +66,7 @@ pub(super) fn step_builder(pallet: AddPallet) -> Result> { // TODO (high priority): implement name conflict resolution strategy "Template", ))); - steps.push(RuntimePalletDependency(Dependency::template_runtime())); + steps.push(RuntimePalletDependency(Dependency::local_template_runtime())); }, AddPallet::Frame(_) => unimplemented!("Frame pallets not yet implemented"), }; diff --git a/tests/add.rs b/tests/add.rs index d89ff29f..60d8a5bf 100644 --- a/tests/add.rs +++ b/tests/add.rs @@ -18,6 +18,7 @@ fn add_parachain_pallet_template() { cmd!("git", "add", ".").dir(&temp_dir.path().join("testchain")).run().unwrap(); cmd!("git", "commit", "--no-gpg-sign", "-m", "Initialized testchain") .dir(&temp_dir.path().join("testchain")) + .stdout_null() .run() .unwrap(); // Add pallet-parachain-template