steps.rs

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