-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pre-commit and github action generate commands (#2144)
* feat: pre-commit and github action generate commands Fixes #2115 Fixes #1692 * Commit from GitHub Actions (test) --------- Co-authored-by: mise[bot] <123107610+mise-en-dev@users.noreply.github.com>
- Loading branch information
1 parent
09daa70
commit 16278a1
Showing
17 changed files
with
452 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use xx::file::display_path; | ||
|
||
use crate::config::Settings; | ||
use crate::file; | ||
use crate::git::Git; | ||
|
||
/// [experimental] Generate a git pre-commit hook | ||
/// | ||
/// This command generates a git pre-commit hook that runs a mise task like `mise run pre-commit` | ||
/// when you commit changes to your repository. | ||
#[derive(Debug, clap::Args)] | ||
#[clap(verbatim_doc_comment, alias = "pre-commit", after_long_help = AFTER_LONG_HELP)] | ||
pub struct GitPreCommit { | ||
/// Which hook to generate (saves to .git/hooks/$hook) | ||
#[clap(long, default_value = "pre-commit")] | ||
hook: String, | ||
/// The task to run when the pre-commit hook is triggered | ||
#[clap(long, short, default_value = "pre-commit")] | ||
task: String, | ||
/// write to .git/hooks/pre-commit and make it executable | ||
#[clap(long, short)] | ||
write: bool, | ||
} | ||
|
||
impl GitPreCommit { | ||
pub fn run(self) -> eyre::Result<()> { | ||
let settings = Settings::get(); | ||
settings.ensure_experimental("generate git-pre-commit")?; | ||
let output = self.generate(); | ||
if self.write { | ||
let path = Git::get_root()?.join(".git/hooks").join(&self.hook); | ||
file::write(&path, &output)?; | ||
file::make_executable(&path)?; | ||
miseprintln!("Wrote to {}", display_path(&path)); | ||
} else { | ||
miseprintln!("{output}"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn generate(&self) -> String { | ||
let task = &self.task; | ||
format!( | ||
r#"#!/bin/sh | ||
mise run {task} | ||
"# | ||
) | ||
} | ||
} | ||
|
||
static AFTER_LONG_HELP: &str = color_print::cstr!( | ||
r#"<bold><underline>Examples:</underline></bold> | ||
$ <bold>mise generate git-pre-commit --write --task=pre-commit</bold> | ||
$ <bold>git commit -m "feat: add new feature"</bold> <dim># runs `mise run pre-commit`</dim> | ||
"# | ||
); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use test_log::test; | ||
|
||
use crate::file; | ||
use crate::git::Git; | ||
use crate::test::{cleanup, reset, setup_git_repo}; | ||
|
||
#[test] | ||
fn test_git_pre_commit() { | ||
reset(); | ||
setup_git_repo(); | ||
assert_cli_snapshot!("generate", "pre-commit", "--task=testing123"); | ||
cleanup(); | ||
} | ||
#[test] | ||
fn test_git_pre_commit_write() { | ||
reset(); | ||
setup_git_repo(); | ||
assert_cli_snapshot!("generate", "pre-commit", "-w", "--hook", "testing123"); | ||
let path = Git::get_root().unwrap().join(".git/hooks/testing123"); | ||
assert_snapshot!(file::read_to_string(&path).unwrap()); | ||
assert!(file::is_executable(&path)); | ||
cleanup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use xx::file; | ||
|
||
use crate::config::Settings; | ||
use crate::file::display_path; | ||
use crate::git::Git; | ||
|
||
/// [experimental] Generate a Github Action workflow file | ||
/// | ||
/// This command generates a Github Action workflow file that runs a mise task like `mise run ci` | ||
/// when you push changes to your repository. | ||
#[derive(Debug, clap::Args)] | ||
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)] | ||
pub struct GithubAction { | ||
/// the name of the workflow to generate | ||
#[clap(long, short, default_value = "ci")] | ||
name: String, | ||
/// The task to run when the workflow is triggered | ||
#[clap(long, short, default_value = "ci")] | ||
task: String, | ||
/// write to .github/workflows/$name.yml | ||
#[clap(long, short)] | ||
write: bool, | ||
} | ||
|
||
impl GithubAction { | ||
pub fn run(self) -> eyre::Result<()> { | ||
let settings = Settings::get(); | ||
settings.ensure_experimental("generate github-action")?; | ||
let output = self.generate()?; | ||
if self.write { | ||
let path = Git::get_root()? | ||
.join(".github/workflows") | ||
.join(format!("{}.yml", &self.name)); | ||
file::write(&path, &output)?; | ||
miseprintln!("Wrote to {}", display_path(&path)); | ||
} else { | ||
miseprintln!("{output}"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn generate(&self) -> eyre::Result<String> { | ||
let branch = Git::new(Git::get_root()?).current_branch()?; | ||
let name = &self.name; | ||
let task = &self.task; | ||
Ok(format!( | ||
r#"name: {name} | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
push: | ||
tags: ["*"] | ||
branches: ["{branch}"] | ||
concurrency: | ||
group: ${{{{ github.workflow }}}}-${{{{ github.ref }}}} | ||
cancel-in-progress: true | ||
env: | ||
MISE_EXPERIMENTAL: true | ||
jobs: | ||
{name}: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: jdx/mise-action@v2 | ||
- run: mise run {task} | ||
"# | ||
)) | ||
} | ||
} | ||
|
||
static AFTER_LONG_HELP: &str = color_print::cstr!( | ||
r#"<bold><underline>Examples:</underline></bold> | ||
$ <bold>mise generate github-action --write --task=ci</bold> | ||
$ <bold>git commit -m "feat: add new feature"</bold> | ||
$ <bold>git push</bold> <dim># runs `mise run ci` on Github</dim> | ||
"# | ||
); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use test_log::test; | ||
|
||
use crate::file; | ||
use crate::git::Git; | ||
use crate::test::{cleanup, reset, setup_git_repo}; | ||
|
||
#[test] | ||
fn test_github_action() { | ||
reset(); | ||
setup_git_repo(); | ||
assert_cli_snapshot!("generate", "github-action"); | ||
cleanup(); | ||
} | ||
#[test] | ||
fn test_github_action_write() { | ||
reset(); | ||
setup_git_repo(); | ||
assert_cli_snapshot!( | ||
"generate", | ||
"github-action", | ||
"-w", | ||
"-ttesting123", | ||
"-n=testing123" | ||
); | ||
let path = Git::get_root() | ||
.unwrap() | ||
.join(".github/workflows/testing123.yml"); | ||
let contents = file::read_to_string(path).unwrap(); | ||
assert_snapshot!(contents); | ||
cleanup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use clap::Subcommand; | ||
|
||
mod git_pre_commit; | ||
mod github_action; | ||
|
||
/// [experimental] Generate files for various tools/services | ||
#[derive(Debug, clap::Args)] | ||
pub struct Generate { | ||
#[clap(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum Commands { | ||
GitPreCommit(git_pre_commit::GitPreCommit), | ||
GithubAction(github_action::GithubAction), | ||
} | ||
|
||
impl Commands { | ||
pub fn run(self) -> eyre::Result<()> { | ||
match self { | ||
Self::GitPreCommit(cmd) => cmd.run(), | ||
Self::GithubAction(cmd) => cmd.run(), | ||
} | ||
} | ||
} | ||
|
||
impl Generate { | ||
pub fn run(self) -> eyre::Result<()> { | ||
self.command.run() | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/cli/generate/snapshots/mise__cli__generate__git_pre_commit__tests__git_pre_commit.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: src/cli/generate/git_pre_commit.rs | ||
expression: output | ||
--- | ||
#!/bin/sh | ||
mise run testing123 |
6 changes: 6 additions & 0 deletions
6
...enerate/snapshots/mise__cli__generate__git_pre_commit__tests__git_pre_commit_write-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: src/cli/generate/git_pre_commit.rs | ||
expression: "file::read_to_string(&path).unwrap()" | ||
--- | ||
#!/bin/sh | ||
mise run pre-commit |
5 changes: 5 additions & 0 deletions
5
.../generate/snapshots/mise__cli__generate__git_pre_commit__tests__git_pre_commit_write.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
source: src/cli/generate/git_pre_commit.rs | ||
expression: output | ||
--- | ||
Wrote to ~/cwd/.git/hooks/testing123 |
Oops, something went wrong.