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        Platform::Windows => named::pwsh("& $env:RUSTC_WRAPPER --show-stats; exit 0"),
176        Platform::Linux | Platform::Mac => named::bash("sccache --show-stats || true"),
177    }
178}
179
180pub fn cache_nix_dependencies_namespace() -> Step<Use> {
181    named::uses("namespacelabs", "nscloud-cache-action", "v1").add_with(("cache", "nix"))
182}
183
184pub fn cache_nix_store_macos() -> Step<Use> {
185    // On macOS, `/nix` is on a read-only root filesystem so nscloud's `cache: nix`
186    // cannot mount or symlink there. Instead we cache a user-writable directory and
187    // use nix-store --import/--export in separate steps to transfer store paths.
188    named::uses("namespacelabs", "nscloud-cache-action", "v1").add_with(("path", "~/nix-cache"))
189}
190
191pub fn setup_linux() -> Step<Run> {
192    named::bash("./script/linux")
193}
194
195fn install_mold() -> Step<Run> {
196    named::bash("./script/install-mold")
197}
198
199fn download_wasi_sdk() -> Step<Run> {
200    named::bash("./script/download-wasi-sdk")
201}
202
203pub(crate) fn install_linux_dependencies(job: Job) -> Job {
204    job.add_step(setup_linux())
205        .add_step(install_mold())
206        .add_step(download_wasi_sdk())
207}
208
209pub fn script(name: &str) -> Step<Run> {
210    if name.ends_with(".ps1") {
211        Step::new(name).run(name).shell(PWSH_SHELL)
212    } else {
213        Step::new(name).run(name)
214    }
215}
216
217pub struct NamedJob<J: JobType = RunJob> {
218    pub name: String,
219    pub job: Job<J>,
220}
221
222// impl NamedJob {
223//     pub fn map(self, f: impl FnOnce(Job) -> Job) -> Self {
224//         NamedJob {
225//             name: self.name,
226//             job: f(self.job),
227//         }
228//     }
229// }
230
231pub(crate) const DEFAULT_REPOSITORY_OWNER_GUARD: &str =
232    "(github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')";
233
234pub fn repository_owner_guard_expression(trigger_always: bool) -> Expression {
235    Expression::new(format!(
236        "{}{}",
237        DEFAULT_REPOSITORY_OWNER_GUARD,
238        trigger_always.then_some(" && always()").unwrap_or_default()
239    ))
240}
241
242pub trait CommonJobConditions: Sized {
243    fn with_repository_owner_guard(self) -> Self;
244}
245
246impl CommonJobConditions for Job {
247    fn with_repository_owner_guard(self) -> Self {
248        self.cond(repository_owner_guard_expression(false))
249    }
250}
251
252pub(crate) fn release_job(deps: &[&NamedJob]) -> Job {
253    dependant_job(deps)
254        .with_repository_owner_guard()
255        .timeout_minutes(60u32)
256}
257
258pub(crate) fn dependant_job(deps: &[&NamedJob]) -> Job {
259    let job = Job::default();
260    if deps.len() > 0 {
261        job.needs(deps.iter().map(|j| j.name.clone()).collect::<Vec<_>>())
262    } else {
263        job
264    }
265}
266
267impl FluentBuilder for Job {}
268impl FluentBuilder for Workflow {}
269impl FluentBuilder for Input {}
270
271/// A helper trait for building complex objects with imperative conditionals in a fluent style.
272/// Copied from GPUI to avoid adding GPUI as dependency
273/// todo(ci) just put this in gh-workflow
274#[allow(unused)]
275pub trait FluentBuilder {
276    /// Imperatively modify self with the given closure.
277    fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
278    where
279        Self: Sized,
280    {
281        f(self)
282    }
283
284    /// Conditionally modify self with the given closure.
285    fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
286    where
287        Self: Sized,
288    {
289        self.map(|this| if condition { then(this) } else { this })
290    }
291
292    /// Conditionally modify self with the given closure.
293    fn when_else(
294        self,
295        condition: bool,
296        then: impl FnOnce(Self) -> Self,
297        else_fn: impl FnOnce(Self) -> Self,
298    ) -> Self
299    where
300        Self: Sized,
301    {
302        self.map(|this| if condition { then(this) } else { else_fn(this) })
303    }
304
305    /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
306    fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
307    where
308        Self: Sized,
309    {
310        self.map(|this| {
311            if let Some(value) = option {
312                then(this, value)
313            } else {
314                this
315            }
316        })
317    }
318    /// Conditionally unwrap and modify self with the given closure, if the given option is None.
319    fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self
320    where
321        Self: Sized,
322    {
323        self.map(|this| if option.is_some() { this } else { then(this) })
324    }
325}
326
327// (janky) helper to generate steps with a name that corresponds
328// to the name of the calling function.
329pub mod named {
330    use super::*;
331
332    /// Returns a uses step with the same name as the enclosing function.
333    /// (You shouldn't inline this function into the workflow definition, you must
334    /// wrap it in a new function.)
335    pub fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> {
336        Step::new(function_name(1)).uses(owner, repo, ref_)
337    }
338
339    /// Returns a bash-script step with the same name as the enclosing function.
340    /// (You shouldn't inline this function into the workflow definition, you must
341    /// wrap it in a new function.)
342    pub fn bash(script: impl AsRef<str>) -> Step<Run> {
343        Step::new(function_name(1)).run(script.as_ref())
344    }
345
346    /// Returns a pwsh-script step with the same name as the enclosing function.
347    /// (You shouldn't inline this function into the workflow definition, you must
348    /// wrap it in a new function.)
349    pub fn pwsh(script: &str) -> Step<Run> {
350        Step::new(function_name(1)).run(script).shell(PWSH_SHELL)
351    }
352
353    /// Runs the command in either powershell or bash, depending on platform.
354    /// (You shouldn't inline this function into the workflow definition, you must
355    /// wrap it in a new function.)
356    pub fn run(platform: Platform, script: &str) -> Step<Run> {
357        match platform {
358            Platform::Windows => Step::new(function_name(1)).run(script).shell(PWSH_SHELL),
359            Platform::Linux | Platform::Mac => Step::new(function_name(1)).run(script),
360        }
361    }
362
363    /// Returns a Workflow with the same name as the enclosing module with default
364    /// set for the running shell.
365    pub fn workflow() -> Workflow {
366        Workflow::default()
367            .name(
368                named::function_name(1)
369                    .split("::")
370                    .collect::<Vec<_>>()
371                    .into_iter()
372                    .rev()
373                    .skip(1)
374                    .rev()
375                    .collect::<Vec<_>>()
376                    .join("::"),
377            )
378            .defaults(Defaults::default().run(RunDefaults::default().shell(BASH_SHELL)))
379    }
380
381    /// Returns a Job with the same name as the enclosing function.
382    /// (note job names may not contain `::`)
383    pub fn job<J: JobType>(job: Job<J>) -> NamedJob<J> {
384        NamedJob {
385            name: function_name(1).split("::").last().unwrap().to_owned(),
386            job,
387        }
388    }
389
390    /// Returns the function name N callers above in the stack
391    /// (typically 1).
392    /// This only works because xtask always runs debug builds.
393    pub fn function_name(i: usize) -> String {
394        let mut name = "<unknown>".to_string();
395        let mut count = 0;
396        backtrace::trace(|frame| {
397            if count < i + 3 {
398                count += 1;
399                return true;
400            }
401            backtrace::resolve_frame(frame, |cb| {
402                if let Some(s) = cb.name() {
403                    name = s.to_string()
404                }
405            });
406            false
407        });
408
409        name.split("::")
410            .skip_while(|s| s != &"workflows")
411            .skip(1)
412            .collect::<Vec<_>>()
413            .join("::")
414    }
415}
416
417pub fn git_checkout(ref_name: &dyn std::fmt::Display) -> Step<Run> {
418    named::bash(&format!(
419        "git fetch origin {ref_name} && git checkout {ref_name}"
420    ))
421}
422
423pub fn authenticate_as_zippy() -> (Step<Use>, StepOutput) {
424    let step = named::uses(
425        "actions",
426        "create-github-app-token",
427        "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1",
428    )
429    .add_with(("app-id", vars::ZED_ZIPPY_APP_ID))
430    .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY))
431    .id("get-app-token");
432    let output = StepOutput::new(&step, "token");
433    (step, output)
434}