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