Skip to content

Commit

Permalink
separate dependency module
Browse files Browse the repository at this point in the history
  • Loading branch information
weezy20 committed Mar 19, 2024
1 parent c6d1991 commit 1d92314
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 48 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
91 changes: 91 additions & 0 deletions src/engines/pallet_engine/dependency.rs
Original file line number Diff line number Diff line change
@@ -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<reqwest::Url> for Location {
fn from(url: reqwest::Url) -> Self {
Self::Git(url)
}
}
impl From<std::path::PathBuf> 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<semver::Version> for Location {
fn from(version: semver::Version) -> Self {
Self::CratesIO(version)
}
}
impl Into<String> 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<toml_edit::Value> for Location {
fn into(self) -> toml_edit::Value {
Into::<String>::into(self).into()
}
}

#[derive(Debug)]
pub(super) struct Dependency {
pub(super) features: Vec<Features>,
/// 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,
// }
// }
}
47 changes: 1 addition & 46 deletions src/engines/pallet_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Features>,
/// 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,
// }
// }
}
}
2 changes: 1 addition & 1 deletion src/engines/pallet_engine/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(super) fn step_builder(pallet: AddPallet) -> Result<Vec<Steps>> {
// 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"),
};
Expand Down
1 change: 1 addition & 0 deletions tests/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1d92314

Please sign in to comment.