use gh_workflow::{ctx::Context, *};use serde_json::Value; use crate::tasks::workflows::{ runners::Platform, steps::named::function_name, vars::{self, StepOutput},}; pub(crate) fn use_clang(job: Job) -> Job { job.add_env(Env::new("CC", "clang")) .add_env(Env::new("CXX", "clang++"))} const SCCACHE_R2_BUCKET: &str = "sccache-zed"; pub(crate) const BASH_SHELL: &str = "bash -euxo pipefail {0}";// https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstepsshellpub const PWSH_SHELL: &str = "pwsh"; pub(crate) struct Nextest(Step<Run>); pub(crate) fn cargo_nextest(platform: Platform) -> Nextest { Nextest(named::run( platform, "cargo nextest run --workspace --no-fail-fast --no-tests=warn", ))} impl Nextest { #[allow(dead_code)] pub(crate) fn with_filter_expr(mut self, filter_expr: &str) -> Self { if let Some(nextest_command) = self.0.value.run.as_mut() { nextest_command.push_str(&format!(r#" -E "{filter_expr}""#)); } self } pub(crate) fn with_changed_packages_filter(mut self, orchestrate_job: &str) -> Self { if let Some(nextest_command) = self.0.value.run.as_mut() { nextest_command.push_str(&format!( r#"${{{{ needs.{orchestrate_job}.outputs.changed_packages && format(' -E "{{0}}"', needs.{orchestrate_job}.outputs.changed_packages) || '' }}}}"# )); } self }} impl From<Nextest> for Step<Run> { fn from(value: Nextest) -> Self { value.0 }} #[derive(Default)]enum FetchDepth { #[default] Shallow, Full, Custom(serde_json::Value),} #[derive(Default)]pub(crate) struct CheckoutStep { fetch_depth: FetchDepth, fetch_tags: bool, name: Option<String>, token: Option<String>, path: Option<String>, repository: Option<String>, ref_: Option<String>,} impl CheckoutStep { pub fn with_full_history(mut self) -> Self { self.fetch_depth = FetchDepth::Full; self } pub fn with_custom_name(mut self, name: &str) -> Self { self.name = Some(name.to_string()); self } pub fn with_custom_fetch_depth(mut self, fetch_depth: impl Into<Value>) -> Self { self.fetch_depth = FetchDepth::Custom(fetch_depth.into()); self } /// Sets `fetch-depth` to `2` on the main branch and `350` on all other branches. pub fn with_deep_history_on_non_main(self) -> Self { self.with_custom_fetch_depth("${{ github.ref == 'refs/heads/main' && 2 || 350 }}") } pub fn with_token(mut self, token: &StepOutput) -> Self { self.token = Some(token.to_string()); self } pub fn with_path(mut self, path: &str) -> Self { self.path = Some(path.to_string()); self } pub fn with_repository(mut self, repository: &str) -> Self { self.repository = Some(repository.to_string()); self } pub fn with_ref(mut self, ref_: impl ToString) -> Self { self.ref_ = Some(ref_.to_string()); self } pub fn with_fetch_tags(mut self) -> Self { self.fetch_tags = true; self }} impl From<CheckoutStep> for Step<Use> { fn from(value: CheckoutStep) -> Self { Step::new(value.name.unwrap_or("steps::checkout_repo".to_string())) .uses( "actions", "checkout", "93cb6efe18208431cddfb8368fd83d5badbf9bfd", // v5.0.1 ) // prevent checkout action from running `git clean -ffdx` which // would delete the target directory .add_with(("clean", false)) .map(|step| match value.fetch_depth { FetchDepth::Shallow => step, FetchDepth::Full => step.add_with(("fetch-depth", 0)), FetchDepth::Custom(depth) => step.add_with(("fetch-depth", depth)), }) .when_some(value.path, |step, path| step.add_with(("path", path))) .when_some(value.repository, |step, repository| { step.add_with(("repository", repository)) }) .when(value.fetch_tags, |step| step.add_with(("fetch-tags", true))) .when_some(value.ref_, |step, ref_| step.add_with(("ref", ref_))) .when_some(value.token, |step, token| step.add_with(("token", token))) }} impl FluentBuilder for CheckoutStep {} pub fn checkout_repo() -> CheckoutStep { CheckoutStep::default()} // Audit mode: logs egress, blocks nothing. Namespace requires v2.19.0+.pub fn harden_runner() -> Step<Use> { named::uses( "step-security", "harden-runner", "9af89fc71515a100421586dfdb3dc9c984fbf411", // v2.19.4 ) .add_with(("egress-policy", "audit"))} pub fn setup_pnpm() -> Step<Use> { named::uses( "pnpm", "action-setup", "fe02b34f77f8bc703788d5817da081398fad5dd2", // v4.0.0 ) .add_with(("version", "9"))} pub fn setup_node() -> Step<Use> { named::uses( "actions", "setup-node", "49933ea5288caeca8642d1e84afbd3f7d6820020", // v4 ) .add_with(("node-version", "20"))} pub fn setup_sentry() -> Step<Use> { named::uses( "matbour", "setup-sentry-cli", "3e938c54b3018bdd019973689ef984e033b0454b", ) .add_with(("token", vars::SENTRY_AUTH_TOKEN))} pub fn prettier() -> Step<Run> { named::bash("./script/prettier")} pub fn cargo_fmt() -> Step<Run> { named::bash("cargo fmt --all -- --check")} pub fn install_cargo_edit() -> Step<Use> { taiki_install_action("cargo-edit")} pub fn taiki_install_action(tool: &str) -> Step<Use> { Step::new(named::function_name(1)) .uses( "taiki-e", "install-action", "02cc5f8ca9f2301050c0c099055816a41ee05507", // v2 ) .add_with(("tool", tool))} pub fn cargo_install_nextest() -> Step<Use> { named::uses( "taiki-e", "install-action", "921e2c9f7148d7ba14cd819f417db338f63e733c", // nextest )} pub fn setup_cargo_config(platform: Platform) -> Step<Run> { match platform { Platform::Windows => named::pwsh(indoc::indoc! {r#" New-Item -ItemType Directory -Path "./../.cargo" -Force Copy-Item -Path "./.cargo/ci-config.toml" -Destination "./../.cargo/config.toml" "#}), Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#" mkdir -p ./../.cargo cp ./.cargo/ci-config.toml ./../.cargo/config.toml "#}), }} pub fn cleanup_cargo_config(platform: Platform) -> Step<Run> { let step = match platform { Platform::Windows => named::pwsh(indoc::indoc! {r#" Remove-Item -Recurse -Path "./../.cargo" -Force -ErrorAction SilentlyContinue "#}), Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#" rm -rf ./../.cargo "#}), }; step.if_condition(Expression::new("always()"))} pub fn clear_target_dir_if_large(platform: Platform) -> Step<Run> { match platform { Platform::Windows => named::pwsh("./script/clear-target-dir-if-larger-than.ps1 350 200"), Platform::Linux => named::bash("./script/clear-target-dir-if-larger-than 350 200"), Platform::Mac => named::bash("./script/clear-target-dir-if-larger-than 350 200"), }} pub fn clippy(platform: Platform, target: Option<&str>) -> Step<Run> { match platform { Platform::Windows => named::pwsh("./script/clippy.ps1"), _ => match target { Some(target) => named::bash(format!("./script/clippy --target {target}")), None => named::bash("./script/clippy"), }, }} pub fn install_rustup_target(target: &str) -> Step<Run> { named::bash(format!("rustup target add {target}"))} pub fn cache_rust_dependencies_namespace() -> Step<Use> { named::uses( "namespacelabs", "nscloud-cache-action", "a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9", // v1 ) .add_with(("cache", "rust")) .add_with(("path", "~/.rustup"))} pub fn setup_sccache(platform: Platform) -> Step<Run> { let step = match platform { Platform::Windows => named::pwsh("./script/setup-sccache.ps1"), Platform::Linux | Platform::Mac => named::bash("./script/setup-sccache"), }; step.add_env(("R2_ACCOUNT_ID", vars::R2_ACCOUNT_ID)) .add_env(("R2_ACCESS_KEY_ID", vars::R2_ACCESS_KEY_ID)) .add_env(("R2_SECRET_ACCESS_KEY", vars::R2_SECRET_ACCESS_KEY)) .add_env(("SCCACHE_BUCKET", SCCACHE_R2_BUCKET))} pub fn show_sccache_stats(platform: Platform) -> Step<Run> { match platform { // Use $env:RUSTC_WRAPPER (absolute path) because GITHUB_PATH changes // don't take effect until the next step in PowerShell. // Check if RUSTC_WRAPPER is set first (it won't be for fork PRs without secrets). Platform::Windows => { named::pwsh("if ($env:RUSTC_WRAPPER) { & $env:RUSTC_WRAPPER --show-stats }; exit 0") } Platform::Linux | Platform::Mac => named::bash("sccache --show-stats || true"), }} pub fn cache_nix_dependencies_namespace() -> Step<Use> { named::uses( "namespacelabs", "nscloud-cache-action", "a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9", // v1 ) .add_with(("cache", "nix"))} pub fn cache_nix_store_macos() -> Step<Use> { // On macOS, `/nix` is on a read-only root filesystem so nscloud's `cache: nix` // cannot mount or symlink there. Instead we cache a user-writable directory and // use nix-store --import/--export in separate steps to transfer store paths. named::uses( "namespacelabs", "nscloud-cache-action", "a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9", // v1 ) .add_with(("path", "~/nix-cache"))} pub fn setup_linux() -> Step<Run> { named::bash("./script/linux")} fn download_wasi_sdk() -> Step<Run> { named::bash("./script/download-wasi-sdk")} pub(crate) fn install_linux_dependencies(job: Job) -> Job { job.add_step(setup_linux()).add_step(download_wasi_sdk())} pub fn script(name: &str) -> Step<Run> { if name.ends_with(".ps1") { Step::new(name).run(name).shell(PWSH_SHELL) } else { Step::new(name).run(name) }} pub struct NamedJob<J: JobType = RunJob> { pub name: String, pub job: Job<J>,} // impl NamedJob {// pub fn map(self, f: impl FnOnce(Job) -> Job) -> Self {// NamedJob {// name: self.name,// job: f(self.job),// }// }// } pub(crate) const DEFAULT_REPOSITORY_OWNER_GUARD: &str = "(github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')"; pub fn repository_owner_guard_expression(trigger_always: bool) -> Expression { Expression::new(format!( "{}{}", DEFAULT_REPOSITORY_OWNER_GUARD, trigger_always.then_some(" && always()").unwrap_or_default() ))} pub trait CommonJobConditions: Sized { fn with_repository_owner_guard(self) -> Self;} impl CommonJobConditions for Job { fn with_repository_owner_guard(self) -> Self { self.cond(repository_owner_guard_expression(false)) }} pub(crate) fn release_job(deps: &[&NamedJob]) -> Job { dependant_job(deps) .with_repository_owner_guard() .timeout_minutes(60u32)} pub(crate) fn dependant_job(deps: &[&NamedJob]) -> Job { let job = Job::default(); if deps.len() > 0 { job.needs(deps.iter().map(|j| j.name.clone()).collect::<Vec<_>>()) } else { job }} impl FluentBuilder for Job {}impl FluentBuilder for Workflow {}impl FluentBuilder for Input {}impl<T> FluentBuilder for Step<T> {} /// A helper trait for building complex objects with imperative conditionals in a fluent style./// Copied from GPUI to avoid adding GPUI as dependency/// todo(ci) just put this in gh-workflow#[allow(unused)]pub trait FluentBuilder { /// Imperatively modify self with the given closure. fn map<U>(self, f: impl FnOnce(Self) -> U) -> U where Self: Sized, { f(self) } /// Conditionally modify self with the given closure. fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self where Self: Sized, { self.map(|this| if condition { then(this) } else { this }) } /// Conditionally modify self with the given closure. fn when_else( self, condition: bool, then: impl FnOnce(Self) -> Self, else_fn: impl FnOnce(Self) -> Self, ) -> Self where Self: Sized, { self.map(|this| if condition { then(this) } else { else_fn(this) }) } /// Conditionally unwrap and modify self with the given closure, if the given option is Some. fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self where Self: Sized, { self.map(|this| { if let Some(value) = option { then(this, value) } else { this } }) } /// Conditionally unwrap and modify self with the given closure, if the given option is None. fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self where Self: Sized, { self.map(|this| if option.is_some() { this } else { then(this) }) }} // (janky) helper to generate steps with a name that corresponds// to the name of the calling function.pub mod named { use super::*; /// Returns a uses step with the same name as the enclosing function. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> { Step::new(function_name(1)).uses(owner, repo, ref_) } /// Returns a bash-script step with the same name as the enclosing function. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn bash(script: impl AsRef<str>) -> Step<Run> { Step::new(function_name(1)).run(script.as_ref()) } /// Returns a pwsh-script step with the same name as the enclosing function. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn pwsh(script: &str) -> Step<Run> { Step::new(function_name(1)).run(script).shell(PWSH_SHELL) } /// Runs the command in either powershell or bash, depending on platform. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn run(platform: Platform, script: &str) -> Step<Run> { match platform { Platform::Windows => Step::new(function_name(1)).run(script).shell(PWSH_SHELL), Platform::Linux | Platform::Mac => Step::new(function_name(1)).run(script), } } /// Returns a Workflow with the same name as the enclosing module with default /// set for the running shell. pub fn workflow() -> Workflow { Workflow::default() .name( named::function_name(1) .split("::") .collect::<Vec<_>>() .into_iter() .rev() .skip(1)Lines 1–500 of 1143. Load more
use gh_workflow::{ctx::Context, *}; use serde_json::Value; use crate::tasks::workflows::{ runners::Platform, steps::named::function_name, vars::{self, StepOutput}, }; pub(crate) fn use_clang(job: Job) -> Job { job.add_env(Env::new("CC", "clang")) .add_env(Env::new("CXX", "clang++")) } const SCCACHE_R2_BUCKET: &str = "sccache-zed"; pub(crate) const BASH_SHELL: &str = "bash -euxo pipefail {0}"; // https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstepsshell pub const PWSH_SHELL: &str = "pwsh"; pub(crate) struct Nextest(Step<Run>); pub(crate) fn cargo_nextest(platform: Platform) -> Nextest { Nextest(named::run( platform, "cargo nextest run --workspace --no-fail-fast --no-tests=warn", )) } impl Nextest { #[allow(dead_code)] pub(crate) fn with_filter_expr(mut self, filter_expr: &str) -> Self { if let Some(nextest_command) = self.0.value.run.as_mut() { nextest_command.push_str(&format!(r#" -E "{filter_expr}""#)); } self } pub(crate) fn with_changed_packages_filter(mut self, orchestrate_job: &str) -> Self { if let Some(nextest_command) = self.0.value.run.as_mut() { nextest_command.push_str(&format!( r#"${{{{ needs.{orchestrate_job}.outputs.changed_packages && format(' -E "{{0}}"', needs.{orchestrate_job}.outputs.changed_packages) || '' }}}}"# )); } self } } impl From<Nextest> for Step<Run> { fn from(value: Nextest) -> Self { value.0 } } #[derive(Default)] enum FetchDepth { #[default] Shallow, Full, Custom(serde_json::Value), } #[derive(Default)] pub(crate) struct CheckoutStep { fetch_depth: FetchDepth, fetch_tags: bool, name: Option<String>, token: Option<String>, path: Option<String>, repository: Option<String>, ref_: Option<String>, } impl CheckoutStep { pub fn with_full_history(mut self) -> Self { self.fetch_depth = FetchDepth::Full; self } pub fn with_custom_name(mut self, name: &str) -> Self { self.name = Some(name.to_string()); self } pub fn with_custom_fetch_depth(mut self, fetch_depth: impl Into<Value>) -> Self { self.fetch_depth = FetchDepth::Custom(fetch_depth.into()); self } /// Sets `fetch-depth` to `2` on the main branch and `350` on all other branches. pub fn with_deep_history_on_non_main(self) -> Self { self.with_custom_fetch_depth("${{ github.ref == 'refs/heads/main' && 2 || 350 }}") } pub fn with_token(mut self, token: &StepOutput) -> Self { self.token = Some(token.to_string()); self } pub fn with_path(mut self, path: &str) -> Self { self.path = Some(path.to_string()); self } pub fn with_repository(mut self, repository: &str) -> Self { self.repository = Some(repository.to_string()); self } pub fn with_ref(mut self, ref_: impl ToString) -> Self { self.ref_ = Some(ref_.to_string()); self } pub fn with_fetch_tags(mut self) -> Self { self.fetch_tags = true; self } } impl From<CheckoutStep> for Step<Use> { fn from(value: CheckoutStep) -> Self { Step::new(value.name.unwrap_or("steps::checkout_repo".to_string())) .uses( "actions", "checkout", "93cb6efe18208431cddfb8368fd83d5badbf9bfd", // v5.0.1 ) // prevent checkout action from running `git clean -ffdx` which // would delete the target directory .add_with(("clean", false)) .map(|step| match value.fetch_depth { FetchDepth::Shallow => step, FetchDepth::Full => step.add_with(("fetch-depth", 0)), FetchDepth::Custom(depth) => step.add_with(("fetch-depth", depth)), }) .when_some(value.path, |step, path| step.add_with(("path", path))) .when_some(value.repository, |step, repository| { step.add_with(("repository", repository)) }) .when(value.fetch_tags, |step| step.add_with(("fetch-tags", true))) .when_some(value.ref_, |step, ref_| step.add_with(("ref", ref_))) .when_some(value.token, |step, token| step.add_with(("token", token))) } } impl FluentBuilder for CheckoutStep {} pub fn checkout_repo() -> CheckoutStep { CheckoutStep::default() } // Audit mode: logs egress, blocks nothing. Namespace requires v2.19.0+. pub fn harden_runner() -> Step<Use> { named::uses( "step-security", "harden-runner", "9af89fc71515a100421586dfdb3dc9c984fbf411", // v2.19.4 ) .add_with(("egress-policy", "audit")) } pub fn setup_pnpm() -> Step<Use> { named::uses( "pnpm", "action-setup", "fe02b34f77f8bc703788d5817da081398fad5dd2", // v4.0.0 ) .add_with(("version", "9")) } pub fn setup_node() -> Step<Use> { named::uses( "actions", "setup-node", "49933ea5288caeca8642d1e84afbd3f7d6820020", // v4 ) .add_with(("node-version", "20")) } pub fn setup_sentry() -> Step<Use> { named::uses( "matbour", "setup-sentry-cli", "3e938c54b3018bdd019973689ef984e033b0454b", ) .add_with(("token", vars::SENTRY_AUTH_TOKEN)) } pub fn prettier() -> Step<Run> { named::bash("./script/prettier") } pub fn cargo_fmt() -> Step<Run> { named::bash("cargo fmt --all -- --check") } pub fn install_cargo_edit() -> Step<Use> { taiki_install_action("cargo-edit") } pub fn taiki_install_action(tool: &str) -> Step<Use> { Step::new(named::function_name(1)) .uses( "taiki-e", "install-action", "02cc5f8ca9f2301050c0c099055816a41ee05507", // v2 ) .add_with(("tool", tool)) } pub fn cargo_install_nextest() -> Step<Use> { named::uses( "taiki-e", "install-action", "921e2c9f7148d7ba14cd819f417db338f63e733c", // nextest ) } pub fn setup_cargo_config(platform: Platform) -> Step<Run> { match platform { Platform::Windows => named::pwsh(indoc::indoc! {r#" New-Item -ItemType Directory -Path "./../.cargo" -Force Copy-Item -Path "./.cargo/ci-config.toml" -Destination "./../.cargo/config.toml" "#}), Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#" mkdir -p ./../.cargo cp ./.cargo/ci-config.toml ./../.cargo/config.toml "#}), } } pub fn cleanup_cargo_config(platform: Platform) -> Step<Run> { let step = match platform { Platform::Windows => named::pwsh(indoc::indoc! {r#" Remove-Item -Recurse -Path "./../.cargo" -Force -ErrorAction SilentlyContinue "#}), Platform::Linux | Platform::Mac => named::bash(indoc::indoc! {r#" rm -rf ./../.cargo "#}), }; step.if_condition(Expression::new("always()")) } pub fn clear_target_dir_if_large(platform: Platform) -> Step<Run> { match platform { Platform::Windows => named::pwsh("./script/clear-target-dir-if-larger-than.ps1 350 200"), Platform::Linux => named::bash("./script/clear-target-dir-if-larger-than 350 200"), Platform::Mac => named::bash("./script/clear-target-dir-if-larger-than 350 200"), } } pub fn clippy(platform: Platform, target: Option<&str>) -> Step<Run> { match platform { Platform::Windows => named::pwsh("./script/clippy.ps1"), _ => match target { Some(target) => named::bash(format!("./script/clippy --target {target}")), None => named::bash("./script/clippy"), }, } } pub fn install_rustup_target(target: &str) -> Step<Run> { named::bash(format!("rustup target add {target}")) } pub fn cache_rust_dependencies_namespace() -> Step<Use> { named::uses( "namespacelabs", "nscloud-cache-action", "a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9", // v1 ) .add_with(("cache", "rust")) .add_with(("path", "~/.rustup")) } pub fn setup_sccache(platform: Platform) -> Step<Run> { let step = match platform { Platform::Windows => named::pwsh("./script/setup-sccache.ps1"), Platform::Linux | Platform::Mac => named::bash("./script/setup-sccache"), }; step.add_env(("R2_ACCOUNT_ID", vars::R2_ACCOUNT_ID)) .add_env(("R2_ACCESS_KEY_ID", vars::R2_ACCESS_KEY_ID)) .add_env(("R2_SECRET_ACCESS_KEY", vars::R2_SECRET_ACCESS_KEY)) .add_env(("SCCACHE_BUCKET", SCCACHE_R2_BUCKET)) } pub fn show_sccache_stats(platform: Platform) -> Step<Run> { match platform { // Use $env:RUSTC_WRAPPER (absolute path) because GITHUB_PATH changes // don't take effect until the next step in PowerShell. // Check if RUSTC_WRAPPER is set first (it won't be for fork PRs without secrets). Platform::Windows => { named::pwsh("if ($env:RUSTC_WRAPPER) { & $env:RUSTC_WRAPPER --show-stats }; exit 0") } Platform::Linux | Platform::Mac => named::bash("sccache --show-stats || true"), } } pub fn cache_nix_dependencies_namespace() -> Step<Use> { named::uses( "namespacelabs", "nscloud-cache-action", "a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9", // v1 ) .add_with(("cache", "nix")) } pub fn cache_nix_store_macos() -> Step<Use> { // On macOS, `/nix` is on a read-only root filesystem so nscloud's `cache: nix` // cannot mount or symlink there. Instead we cache a user-writable directory and // use nix-store --import/--export in separate steps to transfer store paths. named::uses( "namespacelabs", "nscloud-cache-action", "a90bb5d4b27522ce881c6e98eebd7d7e6d1653f9", // v1 ) .add_with(("path", "~/nix-cache")) } pub fn setup_linux() -> Step<Run> { named::bash("./script/linux") } fn download_wasi_sdk() -> Step<Run> { named::bash("./script/download-wasi-sdk") } pub(crate) fn install_linux_dependencies(job: Job) -> Job { job.add_step(setup_linux()).add_step(download_wasi_sdk()) } pub fn script(name: &str) -> Step<Run> { if name.ends_with(".ps1") { Step::new(name).run(name).shell(PWSH_SHELL) } else { Step::new(name).run(name) } } pub struct NamedJob<J: JobType = RunJob> { pub name: String, pub job: Job<J>, } // impl NamedJob { // pub fn map(self, f: impl FnOnce(Job) -> Job) -> Self { // NamedJob { // name: self.name, // job: f(self.job), // } // } // } pub(crate) const DEFAULT_REPOSITORY_OWNER_GUARD: &str = "(github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')"; pub fn repository_owner_guard_expression(trigger_always: bool) -> Expression { Expression::new(format!( "{}{}", DEFAULT_REPOSITORY_OWNER_GUARD, trigger_always.then_some(" && always()").unwrap_or_default() )) } pub trait CommonJobConditions: Sized { fn with_repository_owner_guard(self) -> Self; } impl CommonJobConditions for Job { fn with_repository_owner_guard(self) -> Self { self.cond(repository_owner_guard_expression(false)) } } pub(crate) fn release_job(deps: &[&NamedJob]) -> Job { dependant_job(deps) .with_repository_owner_guard() .timeout_minutes(60u32) } pub(crate) fn dependant_job(deps: &[&NamedJob]) -> Job { let job = Job::default(); if deps.len() > 0 { job.needs(deps.iter().map(|j| j.name.clone()).collect::<Vec<_>>()) } else { job } } impl FluentBuilder for Job {} impl FluentBuilder for Workflow {} impl FluentBuilder for Input {} impl<T> FluentBuilder for Step<T> {} /// A helper trait for building complex objects with imperative conditionals in a fluent style. /// Copied from GPUI to avoid adding GPUI as dependency /// todo(ci) just put this in gh-workflow #[allow(unused)] pub trait FluentBuilder { /// Imperatively modify self with the given closure. fn map<U>(self, f: impl FnOnce(Self) -> U) -> U where Self: Sized, { f(self) } /// Conditionally modify self with the given closure. fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self where Self: Sized, { self.map(|this| if condition { then(this) } else { this }) } /// Conditionally modify self with the given closure. fn when_else( self, condition: bool, then: impl FnOnce(Self) -> Self, else_fn: impl FnOnce(Self) -> Self, ) -> Self where Self: Sized, { self.map(|this| if condition { then(this) } else { else_fn(this) }) } /// Conditionally unwrap and modify self with the given closure, if the given option is Some. fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self where Self: Sized, { self.map(|this| { if let Some(value) = option { then(this, value) } else { this } }) } /// Conditionally unwrap and modify self with the given closure, if the given option is None. fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self where Self: Sized, { self.map(|this| if option.is_some() { this } else { then(this) }) } } // (janky) helper to generate steps with a name that corresponds // to the name of the calling function. pub mod named { use super::*; /// Returns a uses step with the same name as the enclosing function. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn uses(owner: &str, repo: &str, ref_: &str) -> Step<Use> { Step::new(function_name(1)).uses(owner, repo, ref_) } /// Returns a bash-script step with the same name as the enclosing function. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn bash(script: impl AsRef<str>) -> Step<Run> { Step::new(function_name(1)).run(script.as_ref()) } /// Returns a pwsh-script step with the same name as the enclosing function. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn pwsh(script: &str) -> Step<Run> { Step::new(function_name(1)).run(script).shell(PWSH_SHELL) } /// Runs the command in either powershell or bash, depending on platform. /// (You shouldn't inline this function into the workflow definition, you must /// wrap it in a new function.) pub fn run(platform: Platform, script: &str) -> Step<Run> { match platform { Platform::Windows => Step::new(function_name(1)).run(script).shell(PWSH_SHELL), Platform::Linux | Platform::Mac => Step::new(function_name(1)).run(script), } } /// Returns a Workflow with the same name as the enclosing module with default /// set for the running shell. pub fn workflow() -> Workflow { Workflow::default() .name( named::function_name(1) .split("::") .collect::<Vec<_>>() .into_iter() .rev() .skip(1)