Skip to content

Commit

Permalink
feat(cli): support '--no-client' and '--no-gtest' flags in 'cargo-sai…
Browse files Browse the repository at this point in the history
…ls' template (#487)
  • Loading branch information
DennisInSky authored Aug 23, 2024
1 parent dc99b52 commit 36cf95d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
10 changes: 5 additions & 5 deletions templates/cargo-generate.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[template]
sub_templates = ["program", "program-with-client", "program-with-gtest"]
sub_templates = ["program"]

# [placeholders]
# program-name = { prompt = "Program name in PascalCase", type = "string" }
# program-name-snake = { prompt = "Program name in snake-case", type = "string" }
[placeholders]
with-client = { type = "bool", prompt = "Add client project?" }
with-gtest = { type = "bool", prompt = "Add gtest project?" }

[hooks]
pre = ["../set-vars.rhai"]
pre = ["../pre-hook.rhai"]
50 changes: 50 additions & 0 deletions templates/check-version.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
let sails_cli_min_version_str = "0.4.0";
let sails_cli_min_version = parse_sem_ver(sails_cli_min_version_str);

let sails_cli_version_str = if variable::is_set("sails-cli-version") {
variable::get("sails-cli-version")
} else {
"0"
};
let sails_cli_version = parse_sem_ver(sails_cli_version_str);

if sails_cli_version[0] < sails_cli_min_version[0]
|| sails_cli_version[1] < sails_cli_min_version[1]
|| sails_cli_version[2] < sails_cli_min_version[2] {
abort("Sails CLI version '" + sails_cli_version_str + "' is not supported. Please upgrade to '" + sails_cli_min_version_str + "' or higher");
}

fn parse_sem_ver(version) {
let dot_idx = 0;
let dot_idx_next = index_of(version, ".", dot_idx);
let major = if dot_idx_next == -1 {
version.sub_string(dot_idx)
} else {
version.sub_string(dot_idx..dot_idx_next)
};
major = parse_int(major);

dot_idx = dot_idx_next + 1;
dot_idx_next = index_of(version, ".", dot_idx);
let minor = if dot_idx == 0 {
"0"
} else if dot_idx_next == -1 {
version.sub_string(dot_idx)
} else {
version.sub_string(dot_idx..dot_idx_next)
};
minor = parse_int(minor);

dot_idx = dot_idx_next + 1;
dot_idx_next = index_of(version, ".", dot_idx);
let patch = if dot_idx == 0 {
"0"
} else if dot_idx_next == -1 {
version.sub_string(dot_idx)
} else {
abort("Version '" + version + "' does not conform to semantic versioning");
};
patch = parse_int(patch);

[major, minor, patch]
}
11 changes: 11 additions & 0 deletions templates/pre-hook.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "../set-vars";
import "../check-version";

let with_client = variable::get("with-client");
let with_gtest = variable::get("with-gtest");
if !with_client {
file::delete("client")
}
if !with_client || !with_gtest {
file::delete("tests")
}
5 changes: 3 additions & 2 deletions templates/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ sails-rs = "{{ sails-rs-version }}"

[build-dependencies]
sails-rs = { version = "{{ sails-rs-version }}", features = ["wasm-builder"] }

{% if with-client and with-gtest %}
[dev-dependencies]
{{ project-name }} = { path = ".", features = ["wasm-binary"] }
{{ client-project-name }} = { path = "client" }
sails-rs = { version = "{{ sails-rs-version }}", features = ["gtest"] }
tokio = { version = "{{ tokio-version }}", features = ["rt", "macros"] }

{% endif %}
[features]
wasm-binary = []
2 changes: 1 addition & 1 deletion templates/set-vars.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ variable::set("mocks-feature-name", "mocks");

// Set versions of used crates
variable::set("mockall-version", "0.12");
variable::set("sails-rs-version", "0.3.0");
variable::set("sails-rs-version", "0.4.0");
variable::set("tokio-version", "1.39");

0 comments on commit 36cf95d

Please sign in to comment.