1use gh_workflow::*;
  2
  3use crate::tasks::workflows::vars;
  4
  5const BASH_SHELL: &str = "bash -euxo pipefail {0}";
  6// https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstepsshell
  7const PWSH_SHELL: &str = "pwsh";
  8
  9pub fn checkout_repo() -> Step<Use> {
 10    named::uses(
 11        "actions",
 12        "checkout",
 13        "11bd71901bbe5b1630ceea73d27597364c9af683", // v4
 14    )
 15    // prevent checkout action from running `git clean -ffdx` which
 16    // would delete the target directory
 17    .add_with(("clean", false))
 18}
 19
 20pub fn setup_pnpm() -> Step<Use> {
 21    named::uses(
 22        "pnpm",
 23        "action-setup",
 24        "fe02b34f77f8bc703788d5817da081398fad5dd2", // v4.0.0
 25    )
 26    .add_with(("version", "9"))
 27}
 28
 29pub fn setup_node() -> Step<Use> {
 30    named::uses(
 31        "actions",
 32        "setup-node",
 33        "49933ea5288caeca8642d1e84afbd3f7d6820020", // v4
 34    )
 35    .add_with(("node-version", "20"))
 36}
 37
 38pub fn setup_sentry() -> Step<Use> {
 39    named::uses(
 40        "matbour",
 41        "setup-sentry-cli",
 42        "3e938c54b3018bdd019973689ef984e033b0454b",
 43    )
 44    .add_with(("token", vars::SENTRY_AUTH_TOKEN))
 45}
 46
 47pub fn upload_artifact(name: &str, path: &str) -> Step<Use> {
 48    Step::new(format!("@actions/upload-artifact {}", name))
 49        .uses(
 50            "actions",
 51            "upload-artifact",
 52            "330a01c490aca151604b8cf639adc76d48f6c5d4", // v5
 53        )
 54        .add_with(("name", name))
 55        .add_with(("path", path))
 56}
 57
 58pub fn clear_target_dir_if_large() -> Step<Run> {
 59    named::bash("script/clear-target-dir-if-larger-than ${{ env.MAX_SIZE }}")
 60        .add_env(("MAX_SIZE", "${{ runner.os == 'macOS' && 300 || 100 }}"))
 61}
 62
 63pub fn script(name: &str) -> Step<Run> {
 64    if name.ends_with(".ps1") {
 65        Step::new(name).run(name).shell(PWSH_SHELL)
 66    } else {
 67        Step::new(name).run(name).shell(BASH_SHELL)
 68    }
 69}
 70
 71// (janky) helper to generate steps with a name that corresponds
 72// to the name of the calling function.
 73pub(crate) mod named {
 74    use super::*;
 75
 76    /// Returns a uses step with the same name as the enclosing function.
 77    /// (You shouldn't inline this function into the workflow definition, you must
 78    /// wrap it in a new function.)
 79    pub(crate) fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> {
 80        Step::new(function_name(1)).uses(owner, repo, ref_)
 81    }
 82
 83    /// Returns a bash-script step with the same name as the enclosing function.
 84    /// (You shouldn't inline this function into the workflow definition, you must
 85    /// wrap it in a new function.)
 86    pub(crate) fn bash(script: &str) -> Step<Run> {
 87        Step::new(function_name(1)).run(script).shell(BASH_SHELL)
 88    }
 89
 90    /// Returns a pwsh-script step with the same name as the enclosing function.
 91    /// (You shouldn't inline this function into the workflow definition, you must
 92    /// wrap it in a new function.)
 93    pub(crate) fn pwsh(script: &str) -> Step<Run> {
 94        Step::new(function_name(1)).run(script).shell(PWSH_SHELL)
 95    }
 96
 97    /// Returns a Workflow with the same name as the enclosing module.
 98    pub(crate) fn workflow() -> Workflow {
 99        Workflow::default().name(
100            named::function_name(1)
101                .split("::")
102                .next()
103                .unwrap()
104                .to_owned(),
105        )
106    }
107
108    /// Returns the function name N callers above in the stack
109    /// (typically 1).
110    /// This only works because xtask always runs debug builds.
111    pub(crate) fn function_name(i: usize) -> String {
112        let mut name = "<unknown>".to_string();
113        let mut count = 0;
114        backtrace::trace(|frame| {
115            if count < i + 3 {
116                count += 1;
117                return true;
118            }
119            backtrace::resolve_frame(frame, |cb| {
120                if let Some(s) = cb.name() {
121                    name = s.to_string()
122                }
123            });
124            false
125        });
126        name.split("::")
127            .skip_while(|s| s != &"workflows")
128            .skip(1)
129            .collect::<Vec<_>>()
130            .join("::")
131    }
132}