Skip to content

Commit 3791a6b

Browse files
alex-waydbanty
andauthored
Add get-version command to default configuration (#994)
Adds the `get-version` command to the default configuration: ```toml [[workflows]] name = "get-version" help_text = "Get the current version of the project" [[workflows.steps]] type = "Command" command = "echo \"$version\"" [workflows.steps.variables] "$version" = "Version" ``` Closes #885 --------- Co-authored-by: Dylan Anthony <[email protected]>
1 parent 00c62a5 commit 3791a6b

File tree

40 files changed

+124
-15
lines changed

40 files changed

+124
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
knope: minor
3+
---
4+
5+
# Add `get-version` default workflow
6+
7+
For **single-package repositories** with no custom workflows defined,
8+
there is now a [default workflow](https://knope.tech/reference/default-config/#workflows) called `get-version` that
9+
prints out the current package version.
10+
11+
If you want similar functionality for multi-package repositories, please add your ideas to [issue #988](https://github.com/knope-dev/knope/issues/988).
12+
13+
Thanks to @BatmanAoD for the suggestion and @alex-way for the implementation!
14+
15+
PR #994 closed #885.

crates/knope/src/config/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ fn generate_workflows(has_forge: bool, packages: &[Package]) -> Vec<Workflow> {
333333
]
334334
};
335335
release_steps.insert(0, Step::PrepareRelease(PrepareRelease::default()));
336-
vec![
336+
337+
let mut workflows = vec![
337338
Workflow {
338339
name: String::from("release"),
339340
help_text: None,
@@ -344,7 +345,25 @@ fn generate_workflows(has_forge: bool, packages: &[Package]) -> Vec<Workflow> {
344345
help_text: None,
345346
steps: vec![Step::CreateChangeFile],
346347
},
347-
]
348+
];
349+
350+
if packages.len() == 1 {
351+
let mut get_version_variables = IndexMap::new();
352+
get_version_variables.insert(String::from("$version"), Variable::Version);
353+
354+
let get_version_steps = vec![Step::Command {
355+
command: String::from("echo \"$version\""),
356+
variables: Some(get_version_variables),
357+
shell: None,
358+
}];
359+
360+
workflows.push(Workflow {
361+
name: String::from("get-version"),
362+
help_text: Some(String::from("Get the current version of the project")),
363+
steps: get_version_steps,
364+
});
365+
}
366+
workflows
348367
}
349368

350369
#[cfg(test)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod multi_package;
2+
mod single_package;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "single-package-get-version"
3+
version = "1.23.45"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[packages.rust]
2+
versioned_files = ["Cargo.toml"]
3+
4+
[packages.js]
5+
versioned_files = ["package.json"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "13.24.54"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::helpers::TestCase;
2+
3+
#[test]
4+
fn get_version() {
5+
TestCase::new(file!()).run("get-version");
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unrecognized subcommand 'get-version'
2+
3+
Usage: knope[EXE] [OPTIONS] [COMMAND]
4+
5+
For more information, try '--help'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "single-package-get-version"
3+
version = "1.23.45"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::helpers::TestCase;
2+
3+
#[test]
4+
fn get_version() {
5+
TestCase::new(file!()).run("get-version");
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.23.45
+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
mod cargo_workspace;
21
mod document_change;
3-
mod gitea;
4-
mod github;
5-
mod no_forge;
2+
mod get_version;
63
mod release;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use crate::helpers::TestCase;
2+
13
#[test]
24
fn help() {
35
TestCase::new(file!()).run("release --help");
4-
}
6+
}

crates/knope/tests/default_workflows/release/help_multi_package/stdout.log

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Usage: knope release [OPTIONS]
1+
Usage: knope[EXE] release [OPTIONS]
22

33
Options:
44
--dry-run
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use crate::helpers::TestCase;
2+
13
#[test]
24
fn help() {
35
TestCase::new(file!()).run("release --help");
4-
}
6+
}

crates/knope/tests/default_workflows/release/help_single_package/stdout.log

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Usage: knope release [OPTIONS]
1+
Usage: knope[EXE] release [OPTIONS]
22

33
Options:
44
--dry-run
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
1+
mod cargo_workspace;
2+
mod gitea;
3+
mod github;
4+
mod help_multi_package;
5+
mod help_single_package;
6+
mod no_forge;

crates/knope/tests/generate/packages/out/knope.toml

+11
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@ name = "document-change"
3030

3131
[[workflows.steps]]
3232
type = "CreateChangeFile"
33+
34+
[[workflows]]
35+
name = "get-version"
36+
help_text = "Get the current version of the project"
37+
38+
[[workflows.steps]]
39+
type = "Command"
40+
command = "echo \"$version\""
41+
42+
[workflows.steps.variables]
43+
"$version" = "Version"

crates/knope/tests/generate/packages_with_changelog/out/knope.toml

+11
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,14 @@ name = "document-change"
3131

3232
[[workflows.steps]]
3333
type = "CreateChangeFile"
34+
35+
[[workflows]]
36+
name = "get-version"
37+
help_text = "Get the current version of the project"
38+
39+
[[workflows.steps]]
40+
type = "Command"
41+
command = "echo \"$version\""
42+
43+
[workflows.steps.variables]
44+
"$version" = "Version"

docs/src/content/docs/reference/default-config.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ scopes = ["something-else"]
7474
When there are no workflows defined in a `knope.toml` file, Knope will use the default workflows.
7575
Some pieces will differ depending on the configured packages and forges:
7676

77-
```toml title="knope.toml" {"Does not use a $version variable when there are multiple packages": 9-13} {"Moves git push down here and pushes tags if no forges are configured": 18}
77+
```toml title="knope.toml" {"Does not use a $version variable when there are multiple packages": 11-13} {"Moves git push down here and pushes tags if no forges are configured": 22} {"Omits "get-version" when there are multiple packages": 30-40}
7878
[[workflows]]
7979
name = "release"
8080

@@ -85,13 +85,14 @@ type = "PrepareRelease"
8585
type = "Command"
8686
command = "git commit -m \"chore: prepare release $version\""
8787

88-
[[workflows.steps]]
89-
type = "Command"
90-
command = "git push"
9188

9289
[workflows.steps.variables]
9390
"$version" = "Version"
9491

92+
[[workflows.steps]]
93+
type = "Command"
94+
command = "git push"
95+
9596
[[workflows.steps]]
9697
type = "Release"
9798

@@ -102,6 +103,18 @@ name = "document-change"
102103

103104
[[workflows.steps]]
104105
type = "CreateChangeFile"
106+
107+
108+
[[workflows]]
109+
name = "get-version"
110+
help_text = "Get the current version of the project"
111+
112+
[[workflows.steps]]
113+
type = "Command"
114+
command = "echo \"$version\""
115+
116+
[[workflows.steps.variables]]
117+
"$version" = "Version"
105118
```
106119

107120
## Forges

src/config/toml/package.rs

Whitespace-only changes.

src/step/releases/go.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)