steps.rs

  1use gh_workflow::*;
  2
  3use crate::tasks::workflows::{runners::Platform, vars, vars::StepOutput};
  4
  5const SCCACHE_R2_BUCKET: &str = "sccache-zed";
  6
  7const BASH_SHELL: &str = "bash -euxo pipefail {0}";
  8// https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstepsshell
  9pub const PWSH_SHELL: &str = "pwsh";
 10
 11pub(crate) struct Nextest(Step<Run>);
 12
 13pub(crate) fn cargo_nextest(platform: Platform) -> Nextest {
 14    Nextest(named::run(
 15        platform,
 16        "cargo nextest run --workspace --no-fail-fast",
 17    ))
 18}
 19
 20impl Nextest {
 21    pub(crate) fn with_target(mut self, target: &str) -> Step<Run> {
 22        if let Some(nextest_command) = self.0.value.run.as_mut() {
 23            nextest_command.push_str(&format!(r#" --target "{target}""#));
 24        }
 25        self.into()
 26    }
 27
 28    #[allow(dead_code)]
 29    pub(crate) fn with_filter_expr(mut self, filter_expr: &str) -> Self {
 30        if let Some(nextest_command) = self.0.value.run.as_mut() {
 31            nextest_command.push_str(&format!(r#" -E "{filter_expr}""#));
 32        }
 33        self
 34    }
 35
 36    pub(crate) fn with_changed_packages_filter(mut self, orchestrate_job: &str) -> Self {
 37        if let Some(nextest_command) = self.0.value.run.as_mut() {
 38            nextest_command.push_str(&format!(
 39                r#"${{{{ needs.{orchestrate_job}.outputs.changed_packages && format(' -E "{{0}}"', needs.{orchestrate_job}.outputs.changed_packages) || '' }}}}"#
 40            ));
 41        }
 42        self
 43    }
 44}
 45
 46impl From<Nextest> for Step<Run> {
 47    fn from(value: Nextest) -> Self {
 48        value.0
 49    }
 50}
 51
 52pub fn checkout_repo() -> Step<Use> {
 53    named::uses(
 54        "actions",
 55        "checkout",
 56        "11bd71901bbe5b1630ceea73d27597364c9af683", // v4
 57    )
 58    // prevent checkout action from running `git clean -ffdx` which
 59    // would delete the target directory
 60    .add_with(("clean", false))
 61}
 62
 63pub fn checkout_repo_with_token(token: &StepOutput) -> Step<Use> {
 64    named::uses(
 65        "actions",
 66        "checkout",
 67        "11bd71901bbe5b1630ceea73d27597364c9af683", // v4
 68    )
 69    .add_with(("clean", false))
 70    .add_with(("token", token.to_string()))
 71}
 72
 73pub fn setup_pnpm() -> Step<Use> {
 74    named::uses(
 75        "pnpm",
 76        "action-setup",
 77        "fe02b34f77f8bc703788d5817da081398fad5dd2", // v4.0.0
 78    )
 79    .add_with(("version", "9"))
 80}
 81
 82pub fn setup_node() -> Step<Use> {
 83    named::uses(
 84        "actions",
 85        "setup-node",
 86        "49933ea5288caeca8642d1e84afbd3f7d6820020", // v4
 87    )
 88    .add_with(("node-version", "20"))
 89}
 90
 91pub fn setup_sentry() -> Step<Use> {
 92    named::uses(
 93        "matbour",
 94        "setup-sentry-cli",
 95        "3e938c54b3018bdd019973689ef984e033b0454b",
 96    )
 97    .add_with(("token", vars::SENTRY_AUTH_TOKEN))
 98}
 99
100pub fn prettier() -> Step<Run> {
101    named::bash("./script/prettier")
102}
103
104pub fn cargo_fmt() -> Step<Run> {
105    named::bash("cargo fmt --all -- --check")
106}
107
108pub fn cargo_install_nextest() -> Step<Use> {
109    named::uses("taiki-e", "install-action", "nextest")
110}
111
112pub fn setup_cargo_config(platform: Platform) -> Step<Run> {
113    match platform {
114        Platform::Windows => named::pwsh(indoc::indoc! {r#"
115            New-Item -ItemType Directory -Path "./../.cargo" -Force
116            Copy-Item -Path "./.cargo/ci-config.toml" -Destination "./../.cargo/config.toml"
117        "#}),
118
119        Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#"
120            mkdir -p ./../.cargo
121            cp ./.cargo/ci-config.toml ./../.cargo/config.toml
122        "#}),
123    }
124}
125
126pub fn cleanup_cargo_config(platform: Platform) -> Step<Run> {
127    let step = match platform {
128        Platform::Windows => named::pwsh(indoc::indoc! {r#"
129            Remove-Item -Recurse -Path "./../.cargo" -Force -ErrorAction SilentlyContinue
130        "#}),
131        Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#"
132            rm -rf ./../.cargo
133        "#}),
134    };
135
136    step.if_condition(Expression::new("always()"))
137}
138
139pub fn clear_target_dir_if_large(platform: Platform) -> Step<Run> {
140    match platform {
141        Platform::Windows => named::pwsh("./script/clear-target-dir-if-larger-than.ps1 250"),
142        Platform::Linux => named::bash("./script/clear-target-dir-if-larger-than 250"),
143        Platform::Mac => named::bash("./script/clear-target-dir-if-larger-than 300"),
144    }
145}
146
147pub fn clippy(platform: Platform) -> Step<Run> {
148    match platform {
149        Platform::Windows => named::pwsh("./script/clippy.ps1"),
150        _ => named::bash("./script/clippy"),
151    }
152}
153
154pub fn cache_rust_dependencies_namespace() -> Step<Use> {
155    named::uses("namespacelabs", "nscloud-cache-action", "v1")
156        .add_with(("cache", "rust"))
157        .add_with(("path", "~/.rustup"))
158}
159
160pub fn setup_sccache(platform: Platform) -> Step<Run> {
161    let step = match platform {
162        Platform::Windows => named::pwsh("./script/setup-sccache.ps1"),
163        Platform::Linux | Platform::Mac => named::bash("./script/setup-sccache"),
164    };
165    step.add_env(("R2_ACCOUNT_ID", vars::R2_ACCOUNT_ID))
166        .add_env(("R2_ACCESS_KEY_ID", vars::R2_ACCESS_KEY_ID))
167        .add_env(("R2_SECRET_ACCESS_KEY", vars::R2_SECRET_ACCESS_KEY))
168        .add_env(("SCCACHE_BUCKET", SCCACHE_R2_BUCKET))
169}
170
171pub fn show_sccache_stats(platform: Platform) -> Step<Run> {
172    match platform {
173        // Use $env:RUSTC_WRAPPER (absolute path) because GITHUB_PATH changes
174        // don't take effect until the next step in PowerShell.
175        // Check if RUSTC_WRAPPER is set first (it won't be for fork PRs without secrets).
176        Platform::Windows => {
177            named::pwsh("if ($env:RUSTC_WRAPPER) { & $env:RUSTC_WRAPPER --show-stats }; exit 0")
178        }
179        Platform::Linux | Platform::Mac => named::bash("sccache --show-stats || true"),
180    }
181}
182
183pub fn cache_nix_dependencies_namespace() -> Step<Use> {
184    named::uses("namespacelabs", "nscloud-cache-action", "v1").add_with(("cache", "nix"))
185}
186
187pub fn cache_nix_store_macos() -> Step<Use> {
188    // On macOS, `/nix` is on a read-only root filesystem so nscloud's `cache: nix`
189    // cannot mount or symlink there. Instead we cache a user-writable directory and
190    // use nix-store --import/--export in separate steps to transfer store paths.
191    named::uses("namespacelabs", "nscloud-cache-action", "v1").add_with(("path", "~/nix-cache"))
192}
193
194pub fn setup_linux() -> Step<Run> {
195    named::bash("./script/linux")
196}
197
198fn install_mold() -> Step<Run> {
199    named::bash("./script/install-mold")
200}
201
202fn download_wasi_sdk() -> Step<Run> {
203    named::bash("./script/download-wasi-sdk")
204}
205
206pub(crate) fn install_linux_dependencies(job: Job) -> Job {
207    job.add_step(setup_linux())
208        .add_step(install_mold())
209        .add_step(download_wasi_sdk())
210}
211
212pub fn script(name: &str) -> Step<Run> {
213    if name.ends_with(".ps1") {
214        Step::new(name).run(name).shell(PWSH_SHELL)
215    } else {
216        Step::new(name).run(name)
217    }
218}
219
220pub struct NamedJob<J: JobType = RunJob> {
221    pub name: String,
222    pub job: Job<J>,
223}
224
225// impl NamedJob {
226//     pub fn map(self, f: impl FnOnce(Job) -> Job) -> Self {
227//         NamedJob {
228//             name: self.name,
229//             job: f(self.job),
230//         }
231//     }
232// }
233
234pub(crate) const DEFAULT_REPOSITORY_OWNER_GUARD: &str =
235    "(github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')";
236
237pub fn repository_owner_guard_expression(trigger_always: bool) -> Expression {
238    Expression::new(format!(
239        "{}{}",
240        DEFAULT_REPOSITORY_OWNER_GUARD,
241        trigger_always.then_some(" && always()").unwrap_or_default()
242    ))
243}
244
245pub trait CommonJobConditions: Sized {
246    fn with_repository_owner_guard(self) -> Self;
247}
248
249impl CommonJobConditions for Job {
250    fn with_repository_owner_guard(self) -> Self {
251        self.cond(repository_owner_guard_expression(false))
252    }
253}
254
255pub(crate) fn release_job(deps: &[&NamedJob]) -> Job {
256    dependant_job(deps)
257        .with_repository_owner_guard()
258        .timeout_minutes(60u32)
259}
260
261pub(crate) fn dependant_job(deps: &[&NamedJob]) -> Job {
262    let job = Job::default();
263    if deps.len() > 0 {
264        job.needs(deps.iter().map(|j| j.name.clone()).collect::<Vec<_>>())
265    } else {
266        job
267    }
268}
269
270impl FluentBuilder for Job {}
271impl FluentBuilder for Workflow {}
272impl FluentBuilder for Input {}
273
274/// A helper trait for building complex objects with imperative conditionals in a fluent style.
275/// Copied from GPUI to avoid adding GPUI as dependency
276/// todo(ci) just put this in gh-workflow
277#[allow(unused)]
278pub trait FluentBuilder {
279    /// Imperatively modify self with the given closure.
280    fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
281    where
282        Self: Sized,
283    {
284        f(self)
285    }
286
287    /// Conditionally modify self with the given closure.
288    fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
289    where
290        Self: Sized,
291    {
292        self.map(|this| if condition { then(this) } else { this })
293    }
294
295    /// Conditionally modify self with the given closure.
296    fn when_else(
297        self,
298        condition: bool,
299        then: impl FnOnce(Self) -> Self,
300        else_fn: impl FnOnce(Self) -> Self,
301    ) -> Self
302    where
303        Self: Sized,
304    {
305        self.map(|this| if condition { then(this) } else { else_fn(this) })
306    }
307
308    /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
309    fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
310    where
311        Self: Sized,
312    {
313        self.map(|this| {
314            if let Some(value) = option {
315                then(this, value)
316            } else {
317                this
318            }
319        })
320    }
321    /// Conditionally unwrap and modify self with the given closure, if the given option is None.
322    fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self
323    where
324        Self: Sized,
325    {
326        self.map(|this| if option.is_some() { this } else { then(this) })
327    }
328}
329
330// (janky) helper to generate steps with a name that corresponds
331// to the name of the calling function.
332pub mod named {
333    use super::*;
334
335    /// Returns a uses step with the same name as the enclosing function.
336    /// (You shouldn't inline this function into the workflow definition, you must
337    /// wrap it in a new function.)
338    pub fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> {
339        Step::new(function_name(1)).uses(owner, repo, ref_)
340    }
341
342    /// Returns a bash-script step with the same name as the enclosing function.
343    /// (You shouldn't inline this function into the workflow definition, you must
344    /// wrap it in a new function.)
345    pub fn bash(script: impl AsRef<str>) -> Step<Run> {
346        Step::new(function_name(1)).run(script.as_ref())
347    }
348
349    /// Returns a pwsh-script step with the same name as the enclosing function.
350    /// (You shouldn't inline this function into the workflow definition, you must
351    /// wrap it in a new function.)
352    pub fn pwsh(script: &str) -> Step<Run> {
353        Step::new(function_name(1)).run(script).shell(PWSH_SHELL)
354    }
355
356    /// Runs the command in either powershell or bash, depending on platform.
357    /// (You shouldn't inline this function into the workflow definition, you must
358    /// wrap it in a new function.)
359    pub fn run(platform: Platform, script: &str) -> Step<Run> {
360        match platform {
361            Platform::Windows => Step::new(function_name(1)).run(script).shell(PWSH_SHELL),
362            Platform::Linux | Platform::Mac => Step::new(function_name(1)).run(script),
363        }
364    }
365
366    /// Returns a Workflow with the same name as the enclosing module with default
367    /// set for the running shell.
368    pub fn workflow() -> Workflow {
369        Workflow::default()
370            .name(
371                named::function_name(1)
372                    .split("::")
373                    .collect::<Vec<_>>()
374                    .into_iter()
375                    .rev()
376                    .skip(1)
377                    .rev()
378                    .collect::<Vec<_>>()
379                    .join("::"),
380            )
381            .defaults(Defaults::default().run(RunDefaults::default().shell(BASH_SHELL)))
382    }
383
384    /// Returns a Job with the same name as the enclosing function.
385    /// (note job names may not contain `::`)
386    pub fn job<J: JobType>(job: Job<J>) -> NamedJob<J> {
387        NamedJob {
388            name: function_name(1).split("::").last().unwrap().to_owned(),
389            job,
390        }
391    }
392
393    /// Returns the function name N callers above in the stack
394    /// (typically 1).
395    /// This only works because xtask always runs debug builds.
396    pub fn function_name(i: usize) -> String {
397        let mut name = "<unknown>".to_string();
398        let mut count = 0;
399        backtrace::trace(|frame| {
400            if count < i + 3 {
401                count += 1;
402                return true;
403            }
404            backtrace::resolve_frame(frame, |cb| {
405                if let Some(s) = cb.name() {
406                    name = s.to_string()
407                }
408            });
409            false
410        });
411
412        name.split("::")
413            .skip_while(|s| s != &"workflows")
414            .skip(1)
415            .collect::<Vec<_>>()
416            .join("::")
417    }
418}
419
420pub fn git_checkout(ref_name: &dyn std::fmt::Display) -> Step<Run> {
421    named::bash(&format!(
422        "git fetch origin {ref_name} && git checkout {ref_name}"
423    ))
424}
425
426pub fn authenticate_as_zippy() -> (Step<Use>, StepOutput) {
427    let step = named::uses(
428        "actions",
429        "create-github-app-token",
430        "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1",
431    )
432    .add_with(("app-id", vars::ZED_ZIPPY_APP_ID))
433    .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY))
434    .id("get-app-token");
435    let output = StepOutput::new(&step, "token");
436    (step, output)
437}