use xx::file;
use crate::file::display_path;
use crate::git::Git;
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct GithubAction {
#[clap(long, short, default_value = "ci")]
task: String,
#[clap(long, short)]
write: bool,
#[clap(long, default_value = "ci")]
name: String,
}
impl GithubAction {
pub async fn run(self) -> eyre::Result<()> {
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@v6
- uses: jdx/mise-action@v3
- 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>
"#
);