Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get-version command to default configuration #994

Merged
merged 9 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/add_get_version_default_workflow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
knope: minor
---

# Add `get-version` default workflow

For **single-package repositories** with no custom workflows defined,
there is now a [default workflow](https://knope.tech/reference/default-config/#workflows) called `get-version` that
prints out the current package version.

If you want similar functionality for multi-package repositories, please add your ideas to [issue #988](https://github.com/knope-dev/knope/issues/988).

Thanks to @BatmanAoD for the suggestion and @alex-way for the implementation!

PR #994 closed #885.
23 changes: 21 additions & 2 deletions crates/knope/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ fn generate_workflows(has_forge: bool, packages: &[Package]) -> Vec<Workflow> {
]
};
release_steps.insert(0, Step::PrepareRelease(PrepareRelease::default()));
vec![

let mut workflows = vec![
Workflow {
name: String::from("release"),
help_text: None,
Expand All @@ -344,7 +345,25 @@ fn generate_workflows(has_forge: bool, packages: &[Package]) -> Vec<Workflow> {
help_text: None,
steps: vec![Step::CreateChangeFile],
},
]
];

if packages.len() == 1 {
let mut get_version_variables = IndexMap::new();
get_version_variables.insert(String::from("$version"), Variable::Version);

let get_version_steps = vec![Step::Command {
command: String::from("echo \"$version\""),
variables: Some(get_version_variables),
shell: None,
}];

workflows.push(Workflow {
name: String::from("get-version"),
help_text: Some(String::from("Get the current version of the project")),
steps: get_version_steps,
});
}
workflows
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions crates/knope/tests/default_workflows/get_version/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod multi_package;
mod single_package;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "single-package-get-version"
version = "1.23.45"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[packages.rust]
versioned_files = ["Cargo.toml"]

[packages.js]
versioned_files = ["package.json"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "13.24.54"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::helpers::TestCase;

#[test]
fn get_version() {
TestCase::new(file!()).run("get-version");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unrecognized subcommand 'get-version'

Usage: knope[EXE] [OPTIONS] [COMMAND]

For more information, try '--help'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "single-package-get-version"
version = "1.23.45"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::helpers::TestCase;

#[test]
fn get_version() {
TestCase::new(file!()).run("get-version");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.23.45
5 changes: 1 addition & 4 deletions crates/knope/tests/default_workflows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
mod cargo_workspace;
mod document_change;
mod gitea;
mod github;
mod no_forge;
mod get_version;
mod release;
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::helpers::TestCase;

#[test]
fn help() {
TestCase::new(file!()).run("release --help");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Usage: knope release [OPTIONS]
Usage: knope[EXE] release [OPTIONS]

Options:
--dry-run
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::helpers::TestCase;

#[test]
fn help() {
TestCase::new(file!()).run("release --help");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Usage: knope release [OPTIONS]
Usage: knope[EXE] release [OPTIONS]

Options:
--dry-run
Expand Down
7 changes: 6 additions & 1 deletion crates/knope/tests/default_workflows/release/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@

mod cargo_workspace;
mod gitea;
mod github;
mod help_multi_package;
mod help_single_package;
mod no_forge;
11 changes: 11 additions & 0 deletions crates/knope/tests/generate/packages/out/knope.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ name = "document-change"

[[workflows.steps]]
type = "CreateChangeFile"

[[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"
11 changes: 11 additions & 0 deletions crates/knope/tests/generate/packages_with_changelog/out/knope.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ name = "document-change"

[[workflows.steps]]
type = "CreateChangeFile"

[[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"
21 changes: 17 additions & 4 deletions docs/src/content/docs/reference/default-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ scopes = ["something-else"]
When there are no workflows defined in a `knope.toml` file, Knope will use the default workflows.
Some pieces will differ depending on the configured packages and forges:

```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}
```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}
[[workflows]]
name = "release"

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

[[workflows.steps]]
type = "Command"
command = "git push"

[workflows.steps.variables]
"$version" = "Version"

[[workflows.steps]]
type = "Command"
command = "git push"

[[workflows.steps]]
type = "Release"

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

[[workflows.steps]]
type = "CreateChangeFile"


[[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"
```

## Forges
Expand Down
Empty file removed src/config/toml/package.rs
Empty file.
Empty file removed src/step/releases/go.rs
Empty file.