diff --git a/README.md b/README.md index b1b38244..4a9fa082 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,16 @@ If no guidance is needed, proceed with: pop new parachain my-app ``` +`pop-cli` supports diverse project templates, to use a specific one use the flag `--template`: +```sh +# Create an assets parachain +pop new parachain my-app pop -t assets +# Create a contracts parachain +pop new parachain my-app pop -t contracts +# Create a evm parachain +pop new parachain my-app pop -t evm +``` + We also integrate other provider templates in the tool, check them running: ```sh diff --git a/crates/pop-cli/src/commands/new/parachain.rs b/crates/pop-cli/src/commands/new/parachain.rs index d3500627..fa613ce5 100644 --- a/crates/pop-cli/src/commands/new/parachain.rs +++ b/crates/pop-cli/src/commands/new/parachain.rs @@ -135,7 +135,7 @@ async fn guide_user_to_generate_parachain() -> Result<()> { decimals: 12, initial_endowment: "1u64 << 60".to_string(), }; - if matches!(template, Template::Base) { + if template.matches(&Provider::Pop) { customizable_options = prompt_customizable_options()?; } @@ -215,7 +215,7 @@ fn get_customization_value( decimals: Option, initial_endowment: Option, ) -> Result { - if !matches!(template, Template::Base) + if !matches!(template, Template::Standard) && (symbol.is_some() || decimals.is_some() || initial_endowment.is_some()) { log::warning("Customization options are not available for this template")?; @@ -329,7 +329,7 @@ mod tests { let command = NewParachainCommand { name: Some(dir.path().join("test_parachain").to_str().unwrap().to_string()), provider: Some(Provider::Pop), - template: Some(Template::Base), + template: Some(Template::Standard), symbol: Some("UNIT".to_string()), decimals: Some(12), initial_endowment: Some("1u64 << 60".to_string()), @@ -347,11 +347,11 @@ mod tests { #[test] fn test_is_template_supported() -> Result<()> { - is_template_supported(&Provider::Pop, &Template::Base)?; + is_template_supported(&Provider::Pop, &Template::Standard)?; assert!(is_template_supported(&Provider::Pop, &Template::ParityContracts).is_err()); assert!(is_template_supported(&Provider::Pop, &Template::ParityFPT).is_err()); - assert!(is_template_supported(&Provider::Parity, &Template::Base).is_err()); + assert!(is_template_supported(&Provider::Parity, &Template::Standard).is_err()); is_template_supported(&Provider::Parity, &Template::ParityContracts)?; is_template_supported(&Provider::Parity, &Template::ParityFPT) } @@ -359,7 +359,7 @@ mod tests { #[test] fn test_get_customization_values() -> Result<()> { let config = get_customization_value( - &Template::Base, + &Template::Standard, Some("DOT".to_string()), Some(6), Some("10000".to_string()), diff --git a/crates/pop-parachains/README.md b/crates/pop-parachains/README.md index bf10722d..b5667f00 100644 --- a/crates/pop-parachains/README.md +++ b/crates/pop-parachains/README.md @@ -9,7 +9,7 @@ Generate a new Parachain: ```rust use pop_parachains::{instantiate_template_dir, Config, Git, Template}; -let template = Template::Base; +let template = Template::Standard; let destination_path = ...; let config = Config { symbol: ..., diff --git a/crates/pop-parachains/src/new_parachain.rs b/crates/pop-parachains/src/new_parachain.rs index d4cb6cfd..1784d8da 100644 --- a/crates/pop-parachains/src/new_parachain.rs +++ b/crates/pop-parachains/src/new_parachain.rs @@ -6,7 +6,7 @@ use crate::{ git::Git, helpers::{sanitize, write_to_file}, }, - Config, Template, + Config, Provider, Template, }; use anyhow::Result; use std::{fs, path::Path}; @@ -21,21 +21,21 @@ pub fn instantiate_template_dir( ) -> Result> { sanitize(target)?; - if matches!(template, &Template::Base) { - return instantiate_base_template(target, config, tag_version); + if template.matches(&Provider::Pop) { + return instantiate_standard_template(template, target, config, tag_version); } let tag = Git::clone_and_degit(template.repository_url()?, target, tag_version)?; Ok(tag) } -pub fn instantiate_base_template( +pub fn instantiate_standard_template( + template: &Template, target: &Path, config: Config, tag_version: Option, ) -> Result> { let temp_dir = ::tempfile::TempDir::new_in(std::env::temp_dir())?; let source = temp_dir.path(); - let template = crate::templates::Template::Base; let tag = Git::clone_and_degit(template.repository_url()?, source, tag_version)?; @@ -80,12 +80,12 @@ mod tests { decimals: 18, initial_endowment: "1000000".to_string(), }; - instantiate_base_template(temp_dir.path(), config, None)?; + instantiate_standard_template(&Template::Standard, temp_dir.path(), config, None)?; Ok(temp_dir) } #[test] - fn test_parachain_instantiate_base_template() -> Result<()> { + fn test_parachain_instantiate_standard_template() -> Result<()> { let temp_dir = setup_template_and_instantiate().expect("Failed to setup template and instantiate"); diff --git a/crates/pop-parachains/src/templates.rs b/crates/pop-parachains/src/templates.rs index 3bd13199..abde3d55 100644 --- a/crates/pop-parachains/src/templates.rs +++ b/crates/pop-parachains/src/templates.rs @@ -37,7 +37,7 @@ impl Provider { pub fn default_template(&self) -> Template { match &self { - Provider::Pop => Template::Base, + Provider::Pop => Template::Standard, Provider::Parity => Template::ParityContracts, } } @@ -78,12 +78,12 @@ pub enum Template { // Pop #[default] #[strum( - serialize = "base", + serialize = "standard", message = "Standard", detailed_message = "A standard parachain", props(Provider = "Pop", Repository = "https://github.com/r0gue-io/base-parachain") )] - Base, + Standard, #[strum( serialize = "assets", message = "Assets", @@ -91,6 +91,20 @@ pub enum Template { props(Provider = "Pop", Repository = "https://github.com/r0gue-io/assets-parachain") )] Assets, + #[strum( + serialize = "contracts", + message = "Contracts", + detailed_message = "Parachain configured to supports Wasm-based contracts.", + props(Provider = "Pop", Repository = "https://github.com/r0gue-io/contracts-parachain") + )] + Contracts, + #[strum( + serialize = "evm", + message = "EVM", + detailed_message = "Parachain configured with frontier, enabling compatibility with the Ethereum Virtual Machine (EVM).", + props(Provider = "Pop", Repository = "https://github.com/r0gue-io/evm-parachain") + )] + EVM, // Parity #[strum( serialize = "cpt", @@ -141,64 +155,75 @@ pub enum Error { #[cfg(test)] mod tests { use super::*; - use std::str::FromStr; + use std::{collections::HashMap, str::FromStr}; + + fn templates_names() -> HashMap { + HashMap::from([ + ("standard".to_string(), Template::Standard), + ("assets".to_string(), Template::Assets), + ("contracts".to_string(), Template::Contracts), + ("evm".to_string(), Template::EVM), + ("cpt".to_string(), Template::ParityContracts), + ("fpt".to_string(), Template::ParityFPT), + ]) + } + fn templates_urls() -> HashMap { + HashMap::from([ + ("standard".to_string(), "https://github.com/r0gue-io/base-parachain"), + ("assets".to_string(), "https://github.com/r0gue-io/assets-parachain"), + ("contracts".to_string(), "https://github.com/r0gue-io/contracts-parachain"), + ("evm".to_string(), "https://github.com/r0gue-io/evm-parachain"), + ("cpt".to_string(), "https://github.com/paritytech/substrate-contracts-node"), + ("fpt".to_string(), "https://github.com/paritytech/frontier-parachain-template"), + ]) + } #[test] fn test_is_template_correct() { - let mut template = Template::Base; - assert_eq!(template.matches(&Provider::Pop), true); - assert_eq!(template.matches(&Provider::Parity), false); - - template = Template::ParityContracts; - assert_eq!(template.matches(&Provider::Pop), false); - assert_eq!(template.matches(&Provider::Parity), true); - - template = Template::ParityFPT; - assert_eq!(template.matches(&Provider::Pop), false); - assert_eq!(template.matches(&Provider::Parity), true); - - template = Template::Assets; - assert_eq!(template.matches(&Provider::Pop), true); - assert_eq!(template.matches(&Provider::Parity), false); + for template in Template::VARIANTS { + if matches!( + template, + Template::Standard | Template::Assets | Template::Contracts | Template::EVM + ) { + assert_eq!(template.matches(&Provider::Pop), true); + assert_eq!(template.matches(&Provider::Parity), false); + } + if matches!(template, Template::ParityContracts | Template::ParityFPT) { + assert_eq!(template.matches(&Provider::Pop), false); + assert_eq!(template.matches(&Provider::Parity), true); + } + } } #[test] fn test_convert_string_to_template() { - assert_eq!(Template::from_str("base").unwrap(), Template::Base); - assert_eq!(Template::from_str("").unwrap_or_default(), Template::Base); - assert_eq!(Template::from_str("assets").unwrap(), Template::Assets); - assert_eq!(Template::from_str("cpt").unwrap(), Template::ParityContracts); - assert_eq!(Template::from_str("fpt").unwrap(), Template::ParityFPT); + let template_names = templates_names(); + // Test the default + assert_eq!(Template::from_str("").unwrap_or_default(), Template::Standard); + // Test the rest + for template in Template::VARIANTS { + assert_eq!( + &Template::from_str(&template.to_string()).unwrap(), + template_names.get(&template.to_string()).unwrap() + ); + } } #[test] fn test_repository_url() { - let mut template = Template::Base; - assert_eq!( - template.repository_url().unwrap(), - "https://github.com/r0gue-io/base-parachain" - ); - template = Template::ParityContracts; - assert_eq!( - template.repository_url().unwrap(), - "https://github.com/paritytech/substrate-contracts-node" - ); - template = Template::ParityFPT; - assert_eq!( - template.repository_url().unwrap(), - "https://github.com/paritytech/frontier-parachain-template" - ); - template = Template::Assets; - assert_eq!( - template.repository_url().unwrap(), - "https://github.com/r0gue-io/assets-parachain" - ); + let template_urls = templates_urls(); + for template in Template::VARIANTS { + assert_eq!( + &template.repository_url().unwrap(), + template_urls.get(&template.to_string()).unwrap() + ); + } } #[test] fn test_default_template_of_provider() { let mut provider = Provider::Pop; - assert_eq!(provider.default_template(), Template::Base); + assert_eq!(provider.default_template(), Template::Standard); provider = Provider::Parity; assert_eq!(provider.default_template(), Template::ParityContracts); } @@ -206,7 +231,10 @@ mod tests { #[test] fn test_templates_of_provider() { let mut provider = Provider::Pop; - assert_eq!(provider.templates(), [&Template::Base, &Template::Assets]); + assert_eq!( + provider.templates(), + [&Template::Standard, &Template::Assets, &Template::Contracts, &Template::EVM] + ); provider = Provider::Parity; assert_eq!(provider.templates(), [&Template::ParityContracts, &Template::ParityFPT]); }