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