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