From 1acb8fbfdf3026e6bde6b0ba42fb3f79df851a12 Mon Sep 17 00:00:00 2001 From: Eric Swanson <64809312+ericswanson-dfinity@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:18:04 -0800 Subject: [PATCH] refactor: Category -> ProjectTemplateCategory (#4015) --- src/dfx-core/src/config/model/mod.rs | 1 + .../src/config/model/project_template.rs | 8 ++++ src/dfx-core/src/config/project_templates.rs | 16 ++------ src/dfx/src/commands/new.rs | 3 +- src/dfx/src/lib/project/templates.rs | 37 +++++++++---------- 5 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 src/dfx-core/src/config/model/project_template.rs diff --git a/src/dfx-core/src/config/model/mod.rs b/src/dfx-core/src/config/model/mod.rs index 2014e4ae34..b29410f073 100644 --- a/src/dfx-core/src/config/model/mod.rs +++ b/src/dfx-core/src/config/model/mod.rs @@ -5,5 +5,6 @@ pub mod dfinity; pub mod extension_canister_type; pub mod local_server_descriptor; pub mod network_descriptor; +pub mod project_template; pub mod replica_config; pub mod settings_digest; diff --git a/src/dfx-core/src/config/model/project_template.rs b/src/dfx-core/src/config/model/project_template.rs new file mode 100644 index 0000000000..f35ca7e399 --- /dev/null +++ b/src/dfx-core/src/config/model/project_template.rs @@ -0,0 +1,8 @@ +#[derive(Debug, Clone, Eq, PartialEq)] +pub enum ProjectTemplateCategory { + Backend, + Frontend, + FrontendTest, + Extra, + Support, +} diff --git a/src/dfx-core/src/config/project_templates.rs b/src/dfx-core/src/config/project_templates.rs index 28c5565131..9febc449f2 100644 --- a/src/dfx-core/src/config/project_templates.rs +++ b/src/dfx-core/src/config/project_templates.rs @@ -1,3 +1,4 @@ +use crate::config::model::project_template::ProjectTemplateCategory; use itertools::Itertools; use std::collections::BTreeMap; use std::fmt::Display; @@ -12,15 +13,6 @@ pub enum ResourceLocation { Bundled { get_archive_fn: GetArchiveFn }, } -#[derive(Debug, Clone, Eq, PartialEq)] -pub enum Category { - Backend, - Frontend, - FrontendTest, - Extra, - Support, -} - #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct ProjectTemplateName(pub String); @@ -44,7 +36,7 @@ pub struct ProjectTemplate { /// Used to determine which CLI group (`--type`, `--backend`, `--frontend`) /// as well as for interactive selection - pub category: Category, + pub category: ProjectTemplateCategory, /// Other project templates to patch in alongside this one pub requirements: Vec, @@ -99,7 +91,7 @@ pub fn find_project_template(name: &ProjectTemplateName) -> Option Vec { +pub fn get_sorted_templates(category: ProjectTemplateCategory) -> Vec { PROJECT_TEMPLATES .get() .unwrap() @@ -114,7 +106,7 @@ pub fn get_sorted_templates(category: Category) -> Vec { .collect() } -pub fn project_template_cli_names(category: Category) -> Vec { +pub fn project_template_cli_names(category: ProjectTemplateCategory) -> Vec { PROJECT_TEMPLATES .get() .unwrap() diff --git a/src/dfx/src/commands/new.rs b/src/dfx/src/commands/new.rs index 08e67b7256..093fe23805 100644 --- a/src/dfx/src/commands/new.rs +++ b/src/dfx/src/commands/new.rs @@ -11,9 +11,10 @@ use anyhow::{anyhow, bail, ensure, Context, Error}; use clap::builder::PossibleValuesParser; use clap::Parser; use console::{style, Style}; +use dfx_core::config::model::project_template::ProjectTemplateCategory as Category; use dfx_core::config::project_templates::{ find_project_template, get_project_template, get_sorted_templates, project_template_cli_names, - Category, ProjectTemplate, ProjectTemplateName, ResourceLocation, + ProjectTemplate, ProjectTemplateName, ResourceLocation, }; use dfx_core::json::{load_json_file, save_json_file}; use dialoguer::theme::ColorfulTheme; diff --git a/src/dfx/src/lib/project/templates.rs b/src/dfx/src/lib/project/templates.rs index 364556a504..8f52cc8ec9 100644 --- a/src/dfx/src/lib/project/templates.rs +++ b/src/dfx/src/lib/project/templates.rs @@ -1,7 +1,6 @@ use crate::util::assets; -use dfx_core::config::project_templates::{ - Category, ProjectTemplate, ProjectTemplateName, ResourceLocation, -}; +use dfx_core::config::model::project_template::ProjectTemplateCategory; +use dfx_core::config::project_templates::{ProjectTemplate, ProjectTemplateName, ResourceLocation}; const NPM_INSTALL: &str = "npm install --quiet --no-progress --workspaces --if-present"; const NPM_INSTALL_SPINNER_MESSAGE: &str = "Installing node dependencies..."; @@ -16,7 +15,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_motoko_files, }, - category: Category::Backend, + category: ProjectTemplateCategory::Backend, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -30,7 +29,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_rust_files, }, - category: Category::Backend, + category: ProjectTemplateCategory::Backend, post_create: vec!["cargo update".to_string()], post_create_failure_warning: Some(CARGO_UPDATE_FAILURE_MESSAGE.to_string()), post_create_spinner_message: None, @@ -44,7 +43,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_azle_files, }, - category: Category::Backend, + category: ProjectTemplateCategory::Backend, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -58,7 +57,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_kybra_files, }, - category: Category::Backend, + category: ProjectTemplateCategory::Backend, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -72,7 +71,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_svelte_files, }, - category: Category::Frontend, + category: ProjectTemplateCategory::Frontend, post_create: vec![NPM_INSTALL.to_string()], post_create_failure_warning: Some(NPM_INSTALL_FAILURE_WARNING.to_string()), post_create_spinner_message: Some(NPM_INSTALL_SPINNER_MESSAGE.to_string()), @@ -86,7 +85,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_react_files, }, - category: Category::Frontend, + category: ProjectTemplateCategory::Frontend, post_create: vec![NPM_INSTALL.to_string()], post_create_failure_warning: Some(NPM_INSTALL_FAILURE_WARNING.to_string()), post_create_spinner_message: Some(NPM_INSTALL_SPINNER_MESSAGE.to_string()), @@ -100,7 +99,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_vue_files, }, - category: Category::Frontend, + category: ProjectTemplateCategory::Frontend, post_create: vec![NPM_INSTALL.to_string()], post_create_failure_warning: Some(NPM_INSTALL_FAILURE_WARNING.to_string()), post_create_spinner_message: Some(NPM_INSTALL_SPINNER_MESSAGE.to_string()), @@ -114,7 +113,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_vanillajs_files, }, - category: Category::Frontend, + category: ProjectTemplateCategory::Frontend, post_create: vec![NPM_INSTALL.to_string()], post_create_failure_warning: Some(NPM_INSTALL_FAILURE_WARNING.to_string()), post_create_spinner_message: Some(NPM_INSTALL_SPINNER_MESSAGE.to_string()), @@ -128,7 +127,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_assets_files, }, - category: Category::Frontend, + category: ProjectTemplateCategory::Frontend, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -142,7 +141,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_svelte_test_files, }, - category: Category::FrontendTest, + category: ProjectTemplateCategory::FrontendTest, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -156,7 +155,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_react_test_files, }, - category: Category::FrontendTest, + category: ProjectTemplateCategory::FrontendTest, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -170,7 +169,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_vue_test_files, }, - category: Category::FrontendTest, + category: ProjectTemplateCategory::FrontendTest, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -184,7 +183,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_vanillajs_test_files, }, - category: Category::FrontendTest, + category: ProjectTemplateCategory::FrontendTest, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -198,7 +197,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_internet_identity_files, }, - category: Category::Extra, + category: ProjectTemplateCategory::Extra, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -212,7 +211,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_bitcoin_files, }, - category: Category::Extra, + category: ProjectTemplateCategory::Extra, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None, @@ -226,7 +225,7 @@ pub fn builtin_templates() -> Vec { resource_location: ResourceLocation::Bundled { get_archive_fn: assets::new_project_js_files, }, - category: Category::Support, + category: ProjectTemplateCategory::Support, post_create: vec![], post_create_failure_warning: None, post_create_spinner_message: None,