浏览代码

feat: enable versioned templates (#3223)

* feat: enable versioned templates

* no subtemplate for 0.6
Jonathan Kelley 7 月之前
父节点
当前提交
7d353c9f17
共有 2 个文件被更改,包括 27 次插入15 次删除
  1. 21 11
      packages/cli/src/cli/create.rs
  2. 6 4
      packages/cli/src/cli/init.rs

+ 21 - 11
packages/cli/src/cli/create.rs

@@ -16,8 +16,8 @@ pub struct Create {
     name: Option<String>,
 
     /// Template path
-    #[clap(default_value = DEFAULT_TEMPLATE, short, long)]
-    template: String,
+    #[clap(short, long)]
+    template: Option<String>,
 
     /// 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<String>,
+    branch: &mut Option<String>,
+) {
+    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
     }
 

+ 6 - 4
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<String>,
 
     /// Template path
-    #[clap(default_value = DEFAULT_TEMPLATE, short, long)]
-    template: String,
+    #[clap(short, long)]
+    template: Option<String>,
 
     /// 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,