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