From 31b62a5fc35c0120a21c0254b1e7ad9b871151ba Mon Sep 17 00:00:00 2001 From: "Dotan J. Nahum" Date: Wed, 13 Nov 2024 18:37:20 +0200 Subject: [PATCH] loco version support local and normal --- loco-new/src/bin/main.rs | 3 +-- loco-new/src/settings.rs | 30 ++++++++++++++++++++++++++++-- loco-new/tests/wizard/new.rs | 13 +++++++++---- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/loco-new/src/bin/main.rs b/loco-new/src/bin/main.rs index b0b3dc44..be2c534b 100644 --- a/loco-new/src/bin/main.rs +++ b/loco-new/src/bin/main.rs @@ -107,11 +107,10 @@ fn main() -> Result<()> { let executor = executer::FileSystem::new(generator_tmp_folder.as_path(), to.as_path()); - let mut settings = Settings::from_wizard(&app_name, &user_selection); + let settings = Settings::from_wizard(&app_name, &user_selection); if let Ok(path) = env::var("LOCO_DEV_MODE_PATH") { println!("⚠️ NOTICE: working in dev mode, pointing to local Loco on '{path}'"); - settings.loco_version_text = format!(r#"version="*", path="{path}""#); } let res = match Generator::new(Arc::new(executor), settings).run() { diff --git a/loco-new/src/settings.rs b/loco-new/src/settings.rs index 3767181f..a4caa4e9 100644 --- a/loco-new/src/settings.rs +++ b/loco-new/src/settings.rs @@ -1,5 +1,7 @@ //! Defines configurable application settings. +use std::env; + use heck::ToSnakeCase; use rhai::{CustomType, TypeBuilder}; use serde::{Deserialize, Serialize}; @@ -7,7 +9,7 @@ use serde::{Deserialize, Serialize}; use crate::{wizard, wizard_opts, LOCO_VERSION}; /// Represents general application settings. -#[derive(Serialize, Deserialize, Clone, Debug, Default, CustomType)] +#[derive(Serialize, Deserialize, Clone, Debug, CustomType)] pub struct Settings { pub package_name: String, pub module_name: String, @@ -79,11 +81,35 @@ impl Settings { None }, features, - loco_version_text: format!(r#"version = "{LOCO_VERSION}""#), + loco_version_text: get_loco_version_text(), + } + } +} +impl Default for Settings { + fn default() -> Self { + Self { + package_name: Default::default(), + module_name: Default::default(), + db: Default::default(), + background: Default::default(), + asset: Default::default(), + auth: Default::default(), + mailer: Default::default(), + initializers: Default::default(), + features: Default::default(), + loco_version_text: get_loco_version_text(), } } } +fn get_loco_version_text() -> String { + if let Ok(path) = env::var("LOCO_DEV_MODE_PATH") { + format!(r#"version="*", path="{path}""#) + } else { + format!(r#"version = "{LOCO_VERSION}""#) + } +} + /// Database configuration settings. #[derive(Serialize, Deserialize, Clone, Debug, Default, CustomType)] pub struct Db { diff --git a/loco-new/tests/wizard/new.rs b/loco-new/tests/wizard/new.rs index d4d1d5dc..9f037353 100644 --- a/loco-new/tests/wizard/new.rs +++ b/loco-new/tests/wizard/new.rs @@ -1,13 +1,13 @@ -use duct::cmd; -use rstest::rstest; use std::{fs, path::PathBuf, sync::Arc}; -use uuid::Uuid; +use duct::cmd; use loco::{ generator::{executer::FileSystem, Generator}, settings, wizard, wizard_opts::{AssetsOption, BackgroundOption, DBOption}, }; +use rstest::rstest; +use uuid::Uuid; struct TestDir { pub path: PathBuf, @@ -43,6 +43,8 @@ fn new_from_wizard( #[values(AssetsOption::Serverside, AssetsOption::Clientside, AssetsOption::None)] asset: AssetsOption, ) { + use std::collections::HashMap; + let test_dir = TestDir::new(); let executor = FileSystem::new(&PathBuf::from("base_template"), &test_dir.path); @@ -57,8 +59,10 @@ fn new_from_wizard( let res = Generator::new(Arc::new(executor), settings).run(); assert!(res.is_ok()); + let mut env_map: HashMap<_, _> = std::env::vars().collect(); + env_map.insert("RUSTFLAGS".into(), "-D warnings".into()); assert!(cmd!("cargo", "check") - .env("RUSTFLAGS", "-D warnings") + .full_env(&env_map) // .stdout_null() // .stderr_null() .dir(test_dir.path.as_path()) @@ -68,6 +72,7 @@ fn new_from_wizard( cmd!("cargo", "test") // .stdout_null() // .stderr_null() + .full_env(&env_map) .dir(test_dir.path.as_path()) .run() .expect("run test");