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 // Check if RUSTC_WRAPPER is set first (it won't be for fork PRs without secrets).
237 Platform::Windows => {
238 named::pwsh("if ($env:RUSTC_WRAPPER) { & $env:RUSTC_WRAPPER --show-stats }; exit 0")
239 }
240 Platform::Linux | Platform::Mac => named::bash("sccache --show-stats || true"),
241 }
242}
243
244pub fn cache_nix_dependencies_namespace() -> Step<Use> {
245 named::uses("namespacelabs", "nscloud-cache-action", "v1").add_with(("cache", "nix"))
246}
247
248pub fn cache_nix_store_macos() -> Step<Use> {
249 // On macOS, `/nix` is on a read-only root filesystem so nscloud's `cache: nix`
250 // cannot mount or symlink there. Instead we cache a user-writable directory and
251 // use nix-store --import/--export in separate steps to transfer store paths.
252 named::uses("namespacelabs", "nscloud-cache-action", "v1").add_with(("path", "~/nix-cache"))
253}
254
255pub fn setup_linux() -> Step<Run> {
256 named::bash("./script/linux")
257}
258
259fn install_mold() -> Step<Run> {
260 named::bash("./script/install-mold")
261}
262
263fn download_wasi_sdk() -> Step<Run> {
264 named::bash("./script/download-wasi-sdk")
265}
266
267pub(crate) fn install_linux_dependencies(job: Job) -> Job {
268 job.add_step(setup_linux())
269 .add_step(install_mold())
270 .add_step(download_wasi_sdk())
271}
272
273pub fn script(name: &str) -> Step<Run> {
274 if name.ends_with(".ps1") {
275 Step::new(name).run(name).shell(PWSH_SHELL)
276 } else {
277 Step::new(name).run(name)
278 }
279}
280
281pub struct NamedJob<J: JobType = RunJob> {
282 pub name: String,
283 pub job: Job<J>,
284}
285
286// impl NamedJob {
287// pub fn map(self, f: impl FnOnce(Job) -> Job) -> Self {
288// NamedJob {
289// name: self.name,
290// job: f(self.job),
291// }
292// }
293// }
294
295pub(crate) const DEFAULT_REPOSITORY_OWNER_GUARD: &str =
296 "(github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')";
297
298pub fn repository_owner_guard_expression(trigger_always: bool) -> Expression {
299 Expression::new(format!(
300 "{}{}",
301 DEFAULT_REPOSITORY_OWNER_GUARD,
302 trigger_always.then_some(" && always()").unwrap_or_default()
303 ))
304}
305
306pub trait CommonJobConditions: Sized {
307 fn with_repository_owner_guard(self) -> Self;
308}
309
310impl CommonJobConditions for Job {
311 fn with_repository_owner_guard(self) -> Self {
312 self.cond(repository_owner_guard_expression(false))
313 }
314}
315
316pub(crate) fn release_job(deps: &[&NamedJob]) -> Job {
317 dependant_job(deps)
318 .with_repository_owner_guard()
319 .timeout_minutes(60u32)
320}
321
322pub(crate) fn dependant_job(deps: &[&NamedJob]) -> Job {
323 let job = Job::default();
324 if deps.len() > 0 {
325 job.needs(deps.iter().map(|j| j.name.clone()).collect::<Vec<_>>())
326 } else {
327 job
328 }
329}
330
331impl FluentBuilder for Job {}
332impl FluentBuilder for Workflow {}
333impl FluentBuilder for Input {}
334
335/// A helper trait for building complex objects with imperative conditionals in a fluent style.
336/// Copied from GPUI to avoid adding GPUI as dependency
337/// todo(ci) just put this in gh-workflow
338#[allow(unused)]
339pub trait FluentBuilder {
340 /// Imperatively modify self with the given closure.
341 fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
342 where
343 Self: Sized,
344 {
345 f(self)
346 }
347
348 /// Conditionally modify self with the given closure.
349 fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
350 where
351 Self: Sized,
352 {
353 self.map(|this| if condition { then(this) } else { this })
354 }
355
356 /// Conditionally modify self with the given closure.
357 fn when_else(
358 self,
359 condition: bool,
360 then: impl FnOnce(Self) -> Self,
361 else_fn: impl FnOnce(Self) -> Self,
362 ) -> Self
363 where
364 Self: Sized,
365 {
366 self.map(|this| if condition { then(this) } else { else_fn(this) })
367 }
368
369 /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
370 fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
371 where
372 Self: Sized,
373 {
374 self.map(|this| {
375 if let Some(value) = option {
376 then(this, value)
377 } else {
378 this
379 }
380 })
381 }
382 /// Conditionally unwrap and modify self with the given closure, if the given option is None.
383 fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self
384 where
385 Self: Sized,
386 {
387 self.map(|this| if option.is_some() { this } else { then(this) })
388 }
389}
390
391// (janky) helper to generate steps with a name that corresponds
392// to the name of the calling function.
393pub mod named {
394 use super::*;
395
396 /// Returns a uses step with the same name as the enclosing function.
397 /// (You shouldn't inline this function into the workflow definition, you must
398 /// wrap it in a new function.)
399 pub fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> {
400 Step::new(function_name(1)).uses(owner, repo, ref_)
401 }
402
403 /// Returns a bash-script step with the same name as the enclosing function.
404 /// (You shouldn't inline this function into the workflow definition, you must
405 /// wrap it in a new function.)
406 pub fn bash(script: impl AsRef<str>) -> Step<Run> {
407 Step::new(function_name(1)).run(script.as_ref())
408 }
409
410 /// Returns a pwsh-script step with the same name as the enclosing function.
411 /// (You shouldn't inline this function into the workflow definition, you must
412 /// wrap it in a new function.)
413 pub fn pwsh(script: &str) -> Step<Run> {
414 Step::new(function_name(1)).run(script).shell(PWSH_SHELL)
415 }
416
417 /// Runs the command in either powershell or bash, depending on platform.
418 /// (You shouldn't inline this function into the workflow definition, you must
419 /// wrap it in a new function.)
420 pub fn run(platform: Platform, script: &str) -> Step<Run> {
421 match platform {
422 Platform::Windows => Step::new(function_name(1)).run(script).shell(PWSH_SHELL),
423 Platform::Linux | Platform::Mac => Step::new(function_name(1)).run(script),
424 }
425 }
426
427 /// Returns a Workflow with the same name as the enclosing module with default
428 /// set for the running shell.
429 pub fn workflow() -> Workflow {
430 Workflow::default()
431 .name(
432 named::function_name(1)
433 .split("::")
434 .collect::<Vec<_>>()
435 .into_iter()
436 .rev()
437 .skip(1)
438 .rev()
439 .collect::<Vec<_>>()
440 .join("::"),
441 )
442 .defaults(Defaults::default().run(RunDefaults::default().shell(BASH_SHELL)))
443 }
444
445 /// Returns a Job with the same name as the enclosing function.
446 /// (note job names may not contain `::`)
447 pub fn job<J: JobType>(job: Job<J>) -> NamedJob<J> {
448 NamedJob {
449 name: function_name(1).split("::").last().unwrap().to_owned(),
450 job,
451 }
452 }
453
454 /// Returns the function name N callers above in the stack
455 /// (typically 1).
456 /// This only works because xtask always runs debug builds.
457 pub fn function_name(i: usize) -> String {
458 let mut name = "<unknown>".to_string();
459 let mut count = 0;
460 backtrace::trace(|frame| {
461 if count < i + 3 {
462 count += 1;
463 return true;
464 }
465 backtrace::resolve_frame(frame, |cb| {
466 if let Some(s) = cb.name() {
467 name = s.to_string()
468 }
469 });
470 false
471 });
472
473 name.split("::")
474 .skip_while(|s| s != &"workflows")
475 .skip(1)
476 .collect::<Vec<_>>()
477 .join("::")
478 }
479}
480
481pub fn git_checkout(ref_name: &dyn std::fmt::Display) -> Step<Run> {
482 named::bash(&format!(
483 "git fetch origin {ref_name} && git checkout {ref_name}"
484 ))
485}
486
487pub fn authenticate_as_zippy() -> (Step<Use>, StepOutput) {
488 let step = named::uses(
489 "actions",
490 "create-github-app-token",
491 "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1",
492 )
493 .add_with(("app-id", vars::ZED_ZIPPY_APP_ID))
494 .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY))
495 .id("get-app-token");
496 let output = StepOutput::new(&step, "token");
497 (step, output)
498}