steps.rs

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