1use gh_workflow::*;
  2
  3use crate::tasks::workflows::{runners::Platform, 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 cargo_fmt() -> Step<Run> {
 48    named::bash("cargo fmt --all -- --check")
 49}
 50
 51pub fn cargo_install_nextest(platform: Platform) -> Step<Run> {
 52    named::run(platform, "cargo install cargo-nextest --locked")
 53}
 54
 55pub fn cargo_nextest(platform: Platform) -> Step<Run> {
 56    named::run(
 57        platform,
 58        "cargo nextest run --workspace --no-fail-fast --failure-output immediate-final",
 59    )
 60}
 61
 62pub fn setup_cargo_config(platform: Platform) -> Step<Run> {
 63    match platform {
 64        Platform::Windows => named::pwsh(indoc::indoc! {r#"
 65            New-Item -ItemType Directory -Path "./../.cargo" -Force
 66            Copy-Item -Path "./.cargo/ci-config.toml" -Destination "./../.cargo/config.toml"
 67        "#}),
 68
 69        Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#"
 70            mkdir -p ./../.cargo
 71            cp ./.cargo/ci-config.toml ./../.cargo/config.toml
 72        "#}),
 73    }
 74}
 75
 76pub fn cleanup_cargo_config(platform: Platform) -> Step<Run> {
 77    let step = match platform {
 78        Platform::Windows => named::pwsh(indoc::indoc! {r#"
 79            Remove-Item -Recurse -Path "./../.cargo" -Force -ErrorAction SilentlyContinue
 80        "#}),
 81        Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#"
 82            rm -rf ./../.cargo
 83        "#}),
 84    };
 85
 86    step.if_condition(Expression::new("always()"))
 87}
 88
 89pub fn upload_artifact(name: &str, path: &str) -> Step<Use> {
 90    Step::new(format!("@actions/upload-artifact {}", name))
 91        .uses(
 92            "actions",
 93            "upload-artifact",
 94            "330a01c490aca151604b8cf639adc76d48f6c5d4", // v5
 95        )
 96        .add_with(("name", name))
 97        .add_with(("path", path))
 98}
 99
100pub fn clear_target_dir_if_large(platform: Platform) -> Step<Run> {
101    match platform {
102        Platform::Windows => named::pwsh("./script/clear-target-dir-if-larger-than.ps1 250"),
103        Platform::Linux => named::bash("./script/clear-target-dir-if-larger-than 100"),
104        Platform::Mac => named::bash("./script/clear-target-dir-if-larger-than 300"),
105    }
106}
107
108pub fn script(name: &str) -> Step<Run> {
109    if name.ends_with(".ps1") {
110        Step::new(name).run(name).shell(PWSH_SHELL)
111    } else {
112        Step::new(name).run(name).shell(BASH_SHELL)
113    }
114}
115
116pub(crate) struct NamedJob {
117    pub name: String,
118    pub job: Job,
119}
120
121// (janky) helper to generate steps with a name that corresponds
122// to the name of the calling function.
123pub(crate) mod named {
124    use super::*;
125
126    /// Returns a uses step with the same name as the enclosing function.
127    /// (You shouldn't inline this function into the workflow definition, you must
128    /// wrap it in a new function.)
129    pub(crate) fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> {
130        Step::new(function_name(1)).uses(owner, repo, ref_)
131    }
132
133    /// Returns a bash-script step with the same name as the enclosing function.
134    /// (You shouldn't inline this function into the workflow definition, you must
135    /// wrap it in a new function.)
136    pub(crate) fn bash(script: &str) -> Step<Run> {
137        Step::new(function_name(1)).run(script).shell(BASH_SHELL)
138    }
139
140    /// Returns a pwsh-script step with the same name as the enclosing function.
141    /// (You shouldn't inline this function into the workflow definition, you must
142    /// wrap it in a new function.)
143    pub(crate) fn pwsh(script: &str) -> Step<Run> {
144        Step::new(function_name(1)).run(script).shell(PWSH_SHELL)
145    }
146
147    /// Runs the command in either powershell or bash, depending on platform.
148    /// (You shouldn't inline this function into the workflow definition, you must
149    /// wrap it in a new function.)
150    pub(crate) fn run(platform: Platform, script: &str) -> Step<Run> {
151        match platform {
152            Platform::Windows => Step::new(function_name(1)).run(script).shell(PWSH_SHELL),
153            Platform::Linux | Platform::Mac => {
154                Step::new(function_name(1)).run(script).shell(BASH_SHELL)
155            }
156        }
157    }
158
159    /// Returns a Workflow with the same name as the enclosing module.
160    pub(crate) fn workflow() -> Workflow {
161        Workflow::default().name(
162            named::function_name(1)
163                .split("::")
164                .next()
165                .unwrap()
166                .to_owned(),
167        )
168    }
169
170    /// Returns a Job with the same name as the enclosing function.
171    /// (note job names may not contain `::`)
172    pub(crate) fn job(job: Job) -> NamedJob {
173        NamedJob {
174            name: function_name(1).split("::").last().unwrap().to_owned(),
175            job,
176        }
177    }
178
179    /// Returns the function name N callers above in the stack
180    /// (typically 1).
181    /// This only works because xtask always runs debug builds.
182    pub(crate) fn function_name(i: usize) -> String {
183        let mut name = "<unknown>".to_string();
184        let mut count = 0;
185        backtrace::trace(|frame| {
186            if count < i + 3 {
187                count += 1;
188                return true;
189            }
190            backtrace::resolve_frame(frame, |cb| {
191                if let Some(s) = cb.name() {
192                    name = s.to_string()
193                }
194            });
195            false
196        });
197        name.split("::")
198            .skip_while(|s| s != &"workflows")
199            .skip(1)
200            .collect::<Vec<_>>()
201            .join("::")
202    }
203}