From 7d353c9f17ad7a6b7abedc5bc3b9a4d431dd1dc9 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 16 Nov 2024 12:56:03 -0500 Subject: [PATCH] feat: enable versioned templates (#3223) * feat: enable versioned templates * no subtemplate for 0.6 --- packages/cli/src/cli/create.rs | 32 +++++++++++++++++++++----------- packages/cli/src/cli/init.rs | 10 ++++++---- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/cli/src/cli/create.rs b/packages/cli/src/cli/create.rs index 5fccffbb0..29981bd3c 100644 --- a/packages/cli/src/cli/create.rs +++ b/packages/cli/src/cli/create.rs @@ -16,8 +16,8 @@ pub struct Create { name: Option, /// Template path - #[clap(default_value = DEFAULT_TEMPLATE, short, long)] - template: String, + #[clap(short, long)] + template: Option, /// Branch to select when using `template` from a git repository. /// Mutually exclusive with: `--revision`, `--tag`. @@ -55,6 +55,9 @@ impl Create { self.name = Some(create::name_from_path(&self.path)?); } + // If no template is specified, use the default one and set the branch to the latest release. + resolve_template_and_branch(&mut self.template, &mut self.branch); + let args = GenerateArgs { define: self.option, destination: Some(self.path), @@ -67,7 +70,7 @@ impl Create { name: self.name, silent: self.yes, template_path: TemplatePath { - auto_path: Some(self.template), + auto_path: self.template, branch: self.branch, revision: self.revision, subfolder: self.subtemplate, @@ -83,6 +86,20 @@ impl Create { } } +/// If no template is specified, use the default one and set the branch to the latest release. +/// +/// Allows us to version templates under the v0.5/v0.6 scheme on the templates repo. +pub(crate) fn resolve_template_and_branch( + template: &mut Option, + branch: &mut Option, +) { + if template.is_none() { + use crate::dx_build_info::{PKG_VERSION_MAJOR, PKG_VERSION_MINOR}; + *template = Some(DEFAULT_TEMPLATE.to_string()); + *branch = Some(format!("v{PKG_VERSION_MAJOR}.{PKG_VERSION_MINOR}")); + }; +} + /// Prevent hidden cursor if Ctrl+C is pressed when interacting /// with cargo-generate's prompts. /// @@ -230,14 +247,7 @@ pub(crate) mod tests { pub(crate) fn subcommand(name: &str) -> Command { let mut command = BINARY.command(); - command - .arg(name) - .arg("--yes") // Skip any questions by choosing default answers. - .arg("--subtemplate") - // Probably should use some template that doesn't require specifying - // either `--subtemplate` or `--option`. - // Maybe a simple template in tests/ dir? - .arg("Fullstack"); + command.arg(name).arg("--yes"); // Skip any questions by choosing default answers. command } diff --git a/packages/cli/src/cli/init.rs b/packages/cli/src/cli/init.rs index 0a1693843..0de4d0147 100644 --- a/packages/cli/src/cli/init.rs +++ b/packages/cli/src/cli/init.rs @@ -1,5 +1,4 @@ use super::*; -use crate::cli::create::DEFAULT_TEMPLATE; use cargo_generate::{GenerateArgs, TemplatePath}; #[derive(Clone, Debug, Default, Deserialize, Parser)] @@ -14,8 +13,8 @@ pub struct Init { name: Option, /// Template path - #[clap(default_value = DEFAULT_TEMPLATE, short, long)] - template: String, + #[clap(short, long)] + template: Option, /// Branch to select when using `template` from a git repository. /// Mutually exclusive with: `--revision`, `--tag`. @@ -53,6 +52,9 @@ impl Init { self.name = Some(create::name_from_path(&self.path)?); } + // If no template is specified, use the default one and set the branch to the latest release. + create::resolve_template_and_branch(&mut self.template, &mut self.branch); + let args = GenerateArgs { define: self.option, destination: Some(self.path), @@ -60,7 +62,7 @@ impl Init { name: self.name, silent: self.yes, template_path: TemplatePath { - auto_path: Some(self.template), + auto_path: self.template, branch: self.branch, revision: self.revision, subfolder: self.subtemplate,