Skip to content

Commit

Permalink
feat: allow version requirement in cargo-generate.toml
Browse files Browse the repository at this point in the history
closes #76
  • Loading branch information
taurr committed Aug 16, 2021
1 parent 1eaf993 commit 2424470
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -35,6 +35,7 @@ toml = "0.5.8"
thiserror = "1.0.26"
home = "0.5.3"
sanitize-filename = "0.3"
semver = "1.0.4"

[dependencies.openssl]
version = "0.10.35"
Expand Down
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -355,6 +355,18 @@ exclude = ["*.c"]

The `cargo-generate.toml` file should be placed in the root of the template. If using the `subfolder` feature, the root is the `subfolder` inside the repository, though `cargo-generate` will look for the file in all parent folders until it reaches the repository root.

## Require `cargo-generate` version from template

Using the supported `cargo-generate.toml` file, the template author may setup version requirements towards `cargo-generate`.

```toml
[template]
cargo_generate_version = ">0.8.0"
```

The format for the version requirement is [documented here](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html).


## Cargo gen - alias

`cargo gen` requires an [cargo alias](https://doc.rust-lang.org/cargo/reference/config.html)
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
@@ -1,5 +1,6 @@
use crate::emoji;
use anyhow::Result;
use semver::VersionReq;
use serde::Deserialize;
use std::path::Path;
use std::{collections::HashMap, fs};
Expand All @@ -20,6 +21,7 @@ pub(crate) struct ConfigValues {

#[derive(Deserialize, Debug, PartialEq)]
pub(crate) struct TemplateConfig {
pub(crate) cargo_generate_version: Option<VersionReq>,
pub(crate) include: Option<Vec<String>>,
pub(crate) exclude: Option<Vec<String>>,
}
Expand Down Expand Up @@ -75,6 +77,7 @@ mod tests {
use super::*;
use std::fs::File;
use std::io::Write;
use std::str::FromStr;
use tempfile::tempdir;
use toml::Value;

Expand All @@ -87,6 +90,7 @@ mod tests {
file.write_all(
r#"
[template]
cargo_generate_version = ">=0.8.0"
include = ["Cargo.toml"]
[placeholders]
test = {a = "a"}
Expand All @@ -100,6 +104,7 @@ mod tests {
assert_eq!(
config.template,
Some(TemplateConfig {
cargo_generate_version: Some(VersionReq::from_str(">=0.8.0").unwrap()),
include: Some(vec!["Cargo.toml".into()]),
exclude: None
})
Expand Down
29 changes: 29 additions & 0 deletions src/lib.rs
Expand Up @@ -134,6 +134,7 @@ pub fn generate(mut args: Args) -> Result<()> {
&locate_template_file(CONFIG_FILE_NAME, &template_base_dir, &args.subfolder).ok(),
)?;

check_cargo_generate_version(&template_config)?;
let template_values = resolve_template_values(&args)?;

println!(
Expand Down Expand Up @@ -189,6 +190,34 @@ fn prepare_local_template(args: &Args) -> Result<(TempDir, PathBuf, String), any
Ok((template_base_dir, template_folder, branch))
}

fn check_cargo_generate_version(template_config: &Option<Config>) -> Result<(), anyhow::Error> {
if let Some(Config {
template:
Some(config::TemplateConfig {
cargo_generate_version: Some(requirement),
include: _,
exclude: _,
}),
placeholders: _,
}) = template_config
{
let version = semver::Version::parse(env!("CARGO_PKG_VERSION"))?;
if !requirement.matches(&version) {
bail!(
"{} {} {} {} {}",
emoji::ERROR,
style("Required cargo-generate version not met. Required:")
.bold()
.red(),
style(requirement).bold().yellow(),
style(" was:").bold().red(),
style(version).bold().yellow(),
);
}
}
Ok(())
}

fn resolve_project_name(args: &Args) -> Result<ProjectName> {
match args.name {
Some(ref n) => Ok(ProjectName::new(n)),
Expand Down

0 comments on commit 2424470

Please sign in to comment.