steps.rs

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