Something went wrong. Try again.
a few hacks on top of zed
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518use gh_workflow::*;use serde_json::Value;
use crate::tasks::workflows::{runners::Platform, vars, vars::StepOutput};
const SCCACHE_R2_BUCKET: &str = "sccache-zed";
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 { pub(crate) fn with_target(mut self, target: &str) -> Step<Run> { if let Some(nextest_command) = self.0.value.run.as_mut() { nextest_command.push_str(&format!(r#" --target "{target}""#)); } self.into() }
#[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, 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 }}
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", "11bd71901bbe5b1630ceea73d27597364c9af683", // v4 ) // 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)), }) .map(|step| match value.token { Some(token) => step.add_with(("token", token)), None => step, }) .map(|step| match value.path { Some(path) => step.add_with(("path", path)), None => step, }) .map(|step| match value.repository { Some(repository) => step.add_with(("repository", repository)), None => step, }) .map(|step| match value.ref_ { Some(ref_) => step.add_with(("ref", ref_)), None => step, }) }}
pub fn checkout_repo() -> CheckoutStep { CheckoutStep::default()}
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 cargo_install_nextest() -> Step<Use> { named::uses("taiki-e", "install-action", "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 250"), Platform::Linux => named::bash("./script/clear-target-dir-if-larger-than 250"), Platform::Mac => named::bash("./script/clear-target-dir-if-larger-than 300"), }}
pub fn clippy(platform: Platform) -> Step<Run> { match platform { Platform::Windows => named::pwsh("./script/clippy.ps1"), _ => named::bash("./script/clippy"), }}
pub fn cache_rust_dependencies_namespace() -> Step<Use> { named::uses("namespacelabs", "nscloud-cache-action", "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", "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", "v1").add_with(("path", "~/nix-cache"))}
pub fn setup_linux() -> Step<Run> { named::bash("./script/linux")}
fn install_mold() -> Step<Run> { named::bash("./script/install-mold")}
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(install_mold()) .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) .rev() .collect::<Vec<_>>() .join("::"), ) .defaults(Defaults::default().run(RunDefaults::default().shell(BASH_SHELL))) }
/// Returns a Job with the same name as the enclosing function. /// (note job names may not contain `::`) pub fn job<J: JobType>(job: Job<J>) -> NamedJob<J> { NamedJob { name: function_name(1).split("::").last().unwrap().to_owned(), job, } }
/// Returns the function name N callers above in the stack /// (typically 1). /// This only works because xtask always runs debug builds. pub fn function_name(i: usize) -> String { let mut name = "<unknown>".to_string(); let mut count = 0; backtrace::trace(|frame| { if count < i + 3 { count += 1; return true; } backtrace::resolve_frame(frame, |cb| { if let Some(s) = cb.name() { name = s.to_string() } }); false });
name.split("::") .skip_while(|s| s != &"workflows") .skip(1) .collect::<Vec<_>>() .join("::") }}
pub fn git_checkout(ref_name: &dyn std::fmt::Display) -> Step<Run> { named::bash(&format!( "git fetch origin {ref_name} && git checkout {ref_name}" ))}
pub fn authenticate_as_zippy() -> (Step<Use>, StepOutput) { let step = named::uses( "actions", "create-github-app-token", "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1", ) .add_with(("app-id", vars::ZED_ZIPPY_APP_ID)) .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY)) .id("get-app-token"); let output = StepOutput::new(&step, "token"); (step, output)}