From ef846b84a2b10c20f8e5c91054cb70d962b6851d Mon Sep 17 00:00:00 2001 From: Yohan Boogaert Date: Thu, 10 Oct 2024 05:04:50 +0200 Subject: [PATCH] Drop `cargo-generate` support (#104) --- .github/workflows/release.yml | 36 ---------- .github/workflows/rust.yml | 9 ++- .github/workflows/sanity.yml | 8 +-- Cargo.toml | 3 - README.md | 32 --------- src/cli.rs | 119 ---------------------------------- src/project.rs | 71 +++++--------------- 7 files changed, 25 insertions(+), 253 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index afaf731..d9a4672 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,24 +40,6 @@ jobs: name: build-${{ matrix.os }} path: target/release/cargo-temp.exe - - name: Build release all-features (${{ matrix.os }}) - run: cargo build --release --all-features - - - if: matrix.os == 'ubuntu-latest' - run: strip target/release/cargo-temp - - - uses: actions/upload-artifact@v3 - if: matrix.os != 'windows-latest' - with: - name: build-${{ matrix.os }}-all-features - path: target/release/cargo-temp - - - uses: actions/upload-artifact@v3 - if: matrix.os == 'windows-latest' - with: - name: build-${{ matrix.os }}-all-features - path: target/release/cargo-temp.exe - release: needs: [ build ] runs-on: ubuntu-latest @@ -72,36 +54,18 @@ jobs: path: build-ubuntu-latest - run: mv build-ubuntu-latest/cargo-temp build-ubuntu-latest/cargo-temp-${{ steps.get_version.outputs.VERSION }}-linux-x86_64 - - uses: actions/download-artifact@v3 - with: - name: build-ubuntu-latest-all-features - path: build-ubuntu-latest-all-features - - run: mv build-ubuntu-latest-all-features/cargo-temp build-ubuntu-latest/cargo-temp-all-features-${{ steps.get_version.outputs.VERSION }}-linux-x86_64 - - uses: actions/download-artifact@v3 with: name: build-windows-latest path: build-windows-latest - run: mv build-windows-latest/cargo-temp.exe build-windows-latest/cargo-temp-${{ steps.get_version.outputs.VERSION }}-windows-x86_64.exe - - uses: actions/download-artifact@v3 - with: - name: build-windows-latest-all-features - path: build-windows-latest-all-features - - run: mv build-windows-latest-all-features/cargo-temp.exe build-windows-latest/cargo-temp-all-features-${{ steps.get_version.outputs.VERSION }}-windows-x86_64.exe - - uses: actions/download-artifact@v3 with: name: build-macos-latest path: build-macos-latest - run: mv build-macos-latest/cargo-temp build-macos-latest/cargo-temp-${{ steps.get_version.outputs.VERSION }}-macos-x86_64 - - uses: actions/download-artifact@v3 - with: - name: build-macos-latest-all-features - path: build-macos-latest-all-features - - run: mv build-macos-latest-all-features/cargo-temp build-macos-latest/cargo-temp-all-features-${{ steps.get_version.outputs.VERSION }}-macos-x86_64 - - name: Release uses: softprops/action-gh-release@v1 env: diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 82bc156..5d462a7 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -32,11 +32,14 @@ jobs: - name: rustup update run: rustup update + - name: cargo check + run: cargo check + - name: cargo build - run: cargo build --all-features + run: cargo build - name: cargo test - run: cargo test --all-features + run: cargo test - name: rustfmt if: github.event_name == 'pull_request' && matrix.lint @@ -44,4 +47,4 @@ jobs: - name: clippy if: github.event_name == 'pull_request' && matrix.lint - run: cargo clippy --workspace --all-features --tests -- -D warnings + run: cargo clippy --tests -- -D warnings diff --git a/.github/workflows/sanity.yml b/.github/workflows/sanity.yml index df8e624..50ca8e6 100644 --- a/.github/workflows/sanity.yml +++ b/.github/workflows/sanity.yml @@ -26,19 +26,19 @@ jobs: run: rustup update - name: cargo check - run: cargo check --all-features + run: cargo check - name: cargo build - run: cargo build --all-features + run: cargo build - name: cargo test - run: cargo test --all-features + run: cargo test - name: cargo fmt run: cargo fmt --all --check - name: cargo clippy - run: cargo clippy --tests --all-features -- -D warnings + run: cargo clippy --tests -- -D warnings - name: install cargo-outdated run: cargo install cargo-outdated diff --git a/Cargo.toml b/Cargo.toml index f3c5077..f9d89c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,6 @@ keywords = ["cargo", "temporary", "project", "cli", "dependencies"] categories = ["development-tools::cargo-plugins"] include = ["src/**/*.rs", "README.md", "LICENSE.Apache-2.0", "LICENSE.MIT"] -[features] -generate = ["cargo-generate"] - [dependencies] anyhow = "1" cargo-generate = { version = "0.22", optional = true } diff --git a/README.md b/README.md index 800459a..20ebcce 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,6 @@ Requires Rust 1.51. cargo install cargo-temp ``` -If you want to use the `generate` feature, Rust 1.61 is required. - -``` -cargo install --all-features cargo-temp -``` - -Or - -``` -cargo install --features generate cargo-temp -``` - ## Usage Create a new temporary project: @@ -262,24 +250,6 @@ cargo-temp --name project This name will be used as the suffix of the temporary project directory, like `tmp-wXyZ-project`. If you decide to preserve the project, the directory will be renamed to match the project's name. -### Generate - -Generate a temporary project from a template using [`cargo-generate`][cargo-generate] -(current used version is v0.16) - -For example, to create a temporary project using [this template][cargo-generate-example-template]: - -``` -cargo temp generate --git https://github.com/ashleygwilliams/wasm-pack-template -``` - -Almost all the features of the [`cargo-generate`][cargo-generate] CLI are provided as it, -expections are: - -* `--init` - This flag is used by cargo-temp to init the template as the temporary directory. -* `--destination` - Since we already use `--init`, this feature cannot be used with cargo-temp. -* `--allow-commands` - Needs further investigation to avoid conflicts with cargo-temp's subprocesses. - ## Settings The config file is located at `{CONFIG_DIR}/cargo-temp/config.toml`. @@ -465,8 +435,6 @@ foreground = true [demo]: t-rec.gif [comparison]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#comparison-requirements [criterion]: https://docs.rs/criterion/latest/criterion -[cargo-generate]: https://github.com/cargo-generate/cargo-generate -[cargo-generate-example-template]: https://github.com/ashleygwilliams/wasm-pack-template [xdg]: https://docs.rs/xdg/latest/xdg/ [knownfolder]: https://docs.rs/dirs-2/latest/dirs_2/ [ssh-issue]: https://github.com/rust-lang/cargo/issues/1851 diff --git a/src/cli.rs b/src/cli.rs index 5b4e31b..af62f9a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -50,123 +50,4 @@ pub struct Cli { /// If the argument doesn't match any of the options, the default is the latest edition #[arg(long, short = 'e')] pub edition: Option, - - #[cfg(feature = "generate")] - #[command(subcommand)] - pub subcommand: Option, -} - -#[cfg(feature = "generate")] -pub mod generate { - use anyhow::Result; - use std::path::{Path, PathBuf}; - - #[derive(Clone, Debug, clap::Subcommand)] - pub enum Subcommand { - /// Generate a temporary project from a template using `cargo-generate`. - Generate(Args), - } - - #[derive(Clone, Debug, clap::Args)] - pub struct Args { - #[command(flatten)] - pub template_path: cargo_generate::TemplatePath, - /// List defined favorite templates from the config - #[arg( - long, - conflicts_with_all(&[ - "git", "path", "subfolder", "branch", - "name", - "force", - "silent", - "vcs", - "lib", - "bin", - "define", - "template_values_file", - "ssh_identity", - "test", - ]) - )] - pub list_favorites: bool, - /// Directory to create / project name; if the name isn't in kebab-case, it will be converted - /// to kebab-case unless `--force` is given. - #[arg(long, short)] - pub name: Option, - /// Don't convert the project name to kebab-case before creating the directory. - /// Note that cargo generate won't overwrite an existing directory, even if `--force` is given. - #[arg(long, short)] - pub force: bool, - /// Enables more verbose output. - #[arg(long, short)] - pub verbose: bool, - /// Pass template values through a file - /// Values should be in the format `key=value`, one per line - #[arg(long)] - pub template_values_file: Option, - /// If silent mode is set all variables will be - /// extracted from the template_values_file. - /// If a value is missing the project generation will fail - #[arg(long, short, requires("name"))] - pub silent: bool, - /// Use specific configuration file. Defaults to $CARGO_HOME/cargo-generate or $HOME/.cargo/cargo-generate - #[arg(short, long)] - pub config: Option, - /// Specify the VCS used to initialize the generated template. - #[arg(long)] - pub vcs: Option, - /// Populates template variable `crate_type` with value `"lib"` - #[arg(long, conflicts_with = "bin")] - pub lib: bool, - /// Populates a template variable `crate_type` with value `"bin"` - #[arg(long, conflicts_with = "lib")] - pub bin: bool, - /// Use a different ssh identity - #[arg(short = 'i', long = "identity")] - pub ssh_identity: Option, - /// Define a value for use during template expansion - #[arg(long, short, number_of_values = 1)] - pub define: Vec, - /// Use a different gitconfig file, if omitted the usual $HOME/.gitconfig will be used. - #[arg(long = "gitconfig")] - pub gitconfig: Option, - /// Will enforce a fresh git init on the generated project - #[arg(long)] - pub force_git_init: bool, - /// Allow the template to overwrite existing files in the destination. - #[arg(short, long)] - pub overwrite: bool, - /// Skip downloading git submodules (if there are any) - #[arg(short, long)] - pub skip_submodules: bool, - } - - impl Args { - pub fn generate(self, destination: &Path) -> Result { - cargo_generate::generate(cargo_generate::GenerateArgs { - template_path: self.template_path, - list_favorites: self.list_favorites, - name: self.name, - force: self.force, - verbose: self.verbose, - template_values_file: self.template_values_file, - silent: self.silent, - config: self.config, - vcs: self.vcs, - lib: self.lib, - bin: self.bin, - ssh_identity: self.ssh_identity, - gitconfig: self.gitconfig, - define: self.define, - force_git_init: self.force_git_init, - overwrite: self.overwrite, - skip_submodules: self.skip_submodules, - // Not available for the users. - destination: Some(destination.to_path_buf()), - init: false, - allow_commands: false, - other_args: None, - }) - } - } } diff --git a/src/project.rs b/src/project.rs index 094880c..247dcff 100644 --- a/src/project.rs +++ b/src/project.rs @@ -13,32 +13,10 @@ use std::{ process::Command, }; -#[cfg(feature = "generate")] -use crate::cli::generate::Subcommand::Generate; -#[cfg(feature = "generate")] -use std::fs::remove_dir_all; - -pub enum Project { - Temporary(tempfile::TempDir), - #[cfg(feature = "generate")] - Template(PathBuf), -} +pub struct Project(tempfile::TempDir); impl Project { pub fn execute(cli: Cli, config: Config) -> Result<()> { - #[cfg(feature = "generate")] - let project = if let Some(Generate(args)) = cli.subcommand { - Self::Template(args.generate(&config.temporary_project_dir)?) - } else { - Self::temporary( - cli.clone(), - &config.temporary_project_dir, - config.git_repo_depth.as_ref(), - config.vcs.as_deref(), - )? - }; - - #[cfg(not(feature = "generate"))] let project = Self::temporary( cli.clone(), &config.temporary_project_dir, @@ -46,7 +24,7 @@ impl Project { config.vcs.as_deref(), )?; - let project_path = project.path(); + let project_path = project.0.path(); let delete_file = project_path.join("TO_DELETE"); write( @@ -264,7 +242,7 @@ impl Project { )?; } - Ok(Self::Temporary(tmp_dir)) + Ok(Project(tmp_dir)) } fn clean_up( @@ -310,24 +288,17 @@ impl Project { let tmp_dir = self.preserve_dir(project_name, preserved_project_dir)?; log::info!("Project directory_preserved_at: {}", tmp_dir.display()); - } else { - match self { - #[cfg(feature = "generate")] - Self::Template(path) => remove_dir_all(path)?, - Self::Temporary(tempdir) => { - if worktree_branch.is_some() { - let mut command = std::process::Command::new("git"); - command - .args(["worktree", "remove"]) - .arg(tempdir.path()) - .arg("--force"); - ensure!( - command.status().context("Could not start git")?.success(), - "cannot remove working tree" - ); - } - } - } + } else if worktree_branch.is_some() { + let mut command = std::process::Command::new("git"); + command + .args(["worktree", "remove"]) + .arg(self.0.path()) + .arg("--force"); + + ensure!( + command.status().context("Could not start git")?.success(), + "cannot remove working tree" + ); } kill_subprocesses(subprocesses) @@ -338,11 +309,7 @@ impl Project { project_name: Option<&str>, preserved_project_dir: Option<&Path>, ) -> Result { - let tmp_dir = match self { - #[cfg(feature = "generate")] - Self::Template(path) => path, - Self::Temporary(tempdir) => tempdir.into_path(), - }; + let tmp_dir = self.0.into_path(); let mut final_dir = if let Some(preserved_project_dir) = preserved_project_dir { if !preserved_project_dir.exists() { @@ -369,12 +336,4 @@ impl Project { Ok(final_dir) } - - fn path(&self) -> &Path { - match self { - #[cfg(feature = "generate")] - Self::Template(path) => path, - Self::Temporary(tempdir) => tempdir.path(), - } - } }