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