1use gh_workflow::*;
2
3use crate::tasks::workflows::{runners::Platform, vars, vars::StepOutput};
4
5pub const 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 setup_linux() -> Step<Run> {
142 named::bash("./script/linux")
143}
144
145fn install_mold() -> Step<Run> {
146 named::bash("./script/install-mold")
147}
148
149fn download_wasi_sdk() -> Step<Run> {
150 named::bash("./script/download-wasi-sdk")
151}
152
153pub(crate) fn install_linux_dependencies(job: Job) -> Job {
154 job.add_step(setup_linux())
155 .add_step(install_mold())
156 .add_step(download_wasi_sdk())
157}
158
159pub fn script(name: &str) -> Step<Run> {
160 if name.ends_with(".ps1") {
161 Step::new(name).run(name).shell(PWSH_SHELL)
162 } else {
163 Step::new(name).run(name).shell(BASH_SHELL)
164 }
165}
166
167pub struct NamedJob<J: JobType = RunJob> {
168 pub name: String,
169 pub job: Job<J>,
170}
171
172// impl NamedJob {
173// pub fn map(self, f: impl FnOnce(Job) -> Job) -> Self {
174// NamedJob {
175// name: self.name,
176// job: f(self.job),
177// }
178// }
179// }
180
181pub(crate) const DEFAULT_REPOSITORY_OWNER_GUARD: &str =
182 "(github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')";
183
184pub fn repository_owner_guard_expression(trigger_always: bool) -> Expression {
185 Expression::new(format!(
186 "{}{}",
187 DEFAULT_REPOSITORY_OWNER_GUARD,
188 trigger_always.then_some(" && always()").unwrap_or_default()
189 ))
190}
191
192pub trait CommonJobConditions: Sized {
193 fn with_repository_owner_guard(self) -> Self;
194}
195
196impl CommonJobConditions for Job {
197 fn with_repository_owner_guard(self) -> Self {
198 self.cond(repository_owner_guard_expression(false))
199 }
200}
201
202pub(crate) fn release_job(deps: &[&NamedJob]) -> Job {
203 dependant_job(deps)
204 .with_repository_owner_guard()
205 .timeout_minutes(60u32)
206}
207
208pub(crate) fn dependant_job(deps: &[&NamedJob]) -> Job {
209 let job = Job::default();
210 if deps.len() > 0 {
211 job.needs(deps.iter().map(|j| j.name.clone()).collect::<Vec<_>>())
212 } else {
213 job
214 }
215}
216
217impl FluentBuilder for Job {}
218impl FluentBuilder for Workflow {}
219impl FluentBuilder for Input {}
220
221/// A helper trait for building complex objects with imperative conditionals in a fluent style.
222/// Copied from GPUI to avoid adding GPUI as dependency
223/// todo(ci) just put this in gh-workflow
224#[allow(unused)]
225pub trait FluentBuilder {
226 /// Imperatively modify self with the given closure.
227 fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
228 where
229 Self: Sized,
230 {
231 f(self)
232 }
233
234 /// Conditionally modify self with the given closure.
235 fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
236 where
237 Self: Sized,
238 {
239 self.map(|this| if condition { then(this) } else { this })
240 }
241
242 /// Conditionally modify self with the given closure.
243 fn when_else(
244 self,
245 condition: bool,
246 then: impl FnOnce(Self) -> Self,
247 else_fn: impl FnOnce(Self) -> Self,
248 ) -> Self
249 where
250 Self: Sized,
251 {
252 self.map(|this| if condition { then(this) } else { else_fn(this) })
253 }
254
255 /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
256 fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
257 where
258 Self: Sized,
259 {
260 self.map(|this| {
261 if let Some(value) = option {
262 then(this, value)
263 } else {
264 this
265 }
266 })
267 }
268 /// Conditionally unwrap and modify self with the given closure, if the given option is None.
269 fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self
270 where
271 Self: Sized,
272 {
273 self.map(|this| if option.is_some() { this } else { then(this) })
274 }
275}
276
277// (janky) helper to generate steps with a name that corresponds
278// to the name of the calling function.
279pub mod named {
280 use super::*;
281
282 /// Returns a uses step with the same name as the enclosing function.
283 /// (You shouldn't inline this function into the workflow definition, you must
284 /// wrap it in a new function.)
285 pub fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> {
286 Step::new(function_name(1)).uses(owner, repo, ref_)
287 }
288
289 /// Returns a bash-script step with the same name as the enclosing function.
290 /// (You shouldn't inline this function into the workflow definition, you must
291 /// wrap it in a new function.)
292 pub fn bash(script: impl AsRef<str>) -> Step<Run> {
293 Step::new(function_name(1))
294 .run(script.as_ref())
295 .shell(BASH_SHELL)
296 }
297
298 /// Returns a pwsh-script step with the same name as the enclosing function.
299 /// (You shouldn't inline this function into the workflow definition, you must
300 /// wrap it in a new function.)
301 pub fn pwsh(script: &str) -> Step<Run> {
302 Step::new(function_name(1)).run(script).shell(PWSH_SHELL)
303 }
304
305 /// Runs the command in either powershell or bash, depending on platform.
306 /// (You shouldn't inline this function into the workflow definition, you must
307 /// wrap it in a new function.)
308 pub fn run(platform: Platform, script: &str) -> Step<Run> {
309 match platform {
310 Platform::Windows => Step::new(function_name(1)).run(script).shell(PWSH_SHELL),
311 Platform::Linux | Platform::Mac => {
312 Step::new(function_name(1)).run(script).shell(BASH_SHELL)
313 }
314 }
315 }
316
317 /// Returns a Workflow with the same name as the enclosing module.
318 pub fn workflow() -> Workflow {
319 Workflow::default().name(
320 named::function_name(1)
321 .split("::")
322 .collect::<Vec<_>>()
323 .into_iter()
324 .rev()
325 .skip(1)
326 .rev()
327 .collect::<Vec<_>>()
328 .join("::"),
329 )
330 }
331
332 /// Returns a Job with the same name as the enclosing function.
333 /// (note job names may not contain `::`)
334 pub fn job<J: JobType>(job: Job<J>) -> NamedJob<J> {
335 NamedJob {
336 name: function_name(1).split("::").last().unwrap().to_owned(),
337 job,
338 }
339 }
340
341 /// Returns the function name N callers above in the stack
342 /// (typically 1).
343 /// This only works because xtask always runs debug builds.
344 pub fn function_name(i: usize) -> String {
345 let mut name = "<unknown>".to_string();
346 let mut count = 0;
347 backtrace::trace(|frame| {
348 if count < i + 3 {
349 count += 1;
350 return true;
351 }
352 backtrace::resolve_frame(frame, |cb| {
353 if let Some(s) = cb.name() {
354 name = s.to_string()
355 }
356 });
357 false
358 });
359
360 name.split("::")
361 .skip_while(|s| s != &"workflows")
362 .skip(1)
363 .collect::<Vec<_>>()
364 .join("::")
365 }
366}
367
368pub fn git_checkout(ref_name: &dyn std::fmt::Display) -> Step<Run> {
369 named::bash(&format!(
370 "git fetch origin {ref_name} && git checkout {ref_name}"
371 ))
372}
373
374pub fn authenticate_as_zippy() -> (Step<Use>, StepOutput) {
375 let step = named::uses(
376 "actions",
377 "create-github-app-token",
378 "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1",
379 )
380 .add_with(("app-id", vars::ZED_ZIPPY_APP_ID))
381 .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY))
382 .id("get-app-token");
383 let output = StepOutput::new(&step, "token");
384 (step, output)
385}