| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use anyhow::{Context, Result}; |
| 23 |
use ops_exec::{Action, Executor, LogSink, ObserveKind, RunOutput, Step}; |
| 24 |
use std::path::PathBuf; |
| 25 |
|
| 26 |
|
| 27 |
#[derive(Clone, Debug)] |
| 28 |
pub struct ReleasePlan { |
| 29 |
|
| 30 |
pub app: String, |
| 31 |
|
| 32 |
pub version: String, |
| 33 |
|
| 34 |
pub repo_path: String, |
| 35 |
|
| 36 |
|
| 37 |
pub env_file: String, |
| 38 |
|
| 39 |
|
| 40 |
pub dist_root: String, |
| 41 |
|
| 42 |
pub dmg_name: String, |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
fn reject_tilde(field: &str, value: &str) -> Result<()> { |
| 54 |
anyhow::ensure!( |
| 55 |
!value.starts_with('~'), |
| 56 |
"{field} = {value:?} uses `~`, which is never expanded: this path is used on \ |
| 57 |
the build host, and its home directory is not this machine's. Write it \ |
| 58 |
absolute, e.g. /Users/<user>/{}", |
| 59 |
value.trim_start_matches("~/") |
| 60 |
); |
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub fn expand_local_tilde(path: &str) -> Result<PathBuf> { |
| 74 |
let Some(rest) = path.strip_prefix('~') else { |
| 75 |
return Ok(PathBuf::from(path)); |
| 76 |
}; |
| 77 |
let home = std::env::var_os("HOME") |
| 78 |
.filter(|h| !h.is_empty()) |
| 79 |
.context("expanding `~`: HOME is not set")?; |
| 80 |
match rest { |
| 81 |
"" => Ok(PathBuf::from(home)), |
| 82 |
_ => match rest.strip_prefix('/') { |
| 83 |
Some(tail) => Ok(PathBuf::from(home).join(tail)), |
| 84 |
|
| 85 |
None => anyhow::bail!( |
| 86 |
"{path:?} uses `~{}`, which is not supported: only `~/` (your own home) is \ |
| 87 |
expanded. Write it absolute.", |
| 88 |
rest.split('/').next().unwrap_or(rest) |
| 89 |
), |
| 90 |
}, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl ReleasePlan { |
| 95 |
|
| 96 |
pub fn validate(&self) -> Result<()> { |
| 97 |
reject_tilde("repo_path", &self.repo_path)?; |
| 98 |
reject_tilde("env_file", &self.env_file)?; |
| 99 |
reject_tilde("dist_root", &self.dist_root)?; |
| 100 |
Ok(()) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
pub fn default_dmg_name(app_product_name: &str, version: &str) -> String { |
| 105 |
format!("{app_product_name}_{version}_aarch64.dmg") |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
pub fn checkout_step(&self) -> Step { |
| 119 |
Step::shell( |
| 120 |
Action::Build, |
| 121 |
format!( |
| 122 |
"set -e; git fetch --all --tags --prune && git checkout v{}", |
| 123 |
self.version |
| 124 |
), |
| 125 |
) |
| 126 |
.with_cwd(&self.repo_path) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
pub fn release_step(&self) -> Step { |
| 134 |
Step::shell( |
| 135 |
Action::Sign, |
| 136 |
format!(". {} && ./dist/release-macos.sh --keychain", self.env_file), |
| 137 |
) |
| 138 |
.with_cwd(&self.repo_path) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
pub fn verify_step(&self) -> Step { |
| 145 |
Step::shell( |
| 146 |
Action::Observe(ObserveKind::Custom("gatekeeper".into())), |
| 147 |
format!( |
| 148 |
"spctl --assess -vv --type install {} 2>&1", |
| 149 |
shell_quote(&self.dmg_remote_path()) |
| 150 |
), |
| 151 |
) |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
pub fn dmg_remote_path(&self) -> String { |
| 156 |
format!("{}/{}", self.dist_root.trim_end_matches('/'), self.dmg_name) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
fn shell_quote(s: &str) -> String { |
| 164 |
format!("'{}'", s.replace('\'', r"'\''")) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
#[derive(Debug)] |
| 169 |
pub struct ReleaseOutcome { |
| 170 |
|
| 171 |
pub gatekeeper_accepted: bool, |
| 172 |
|
| 173 |
pub dmg_remote: String, |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
async fn run_step( |
| 178 |
exec: &dyn Executor, |
| 179 |
step: &Step, |
| 180 |
sink: &mut dyn LogSink, |
| 181 |
label: &str, |
| 182 |
) -> Result<RunOutput> { |
| 183 |
let out = exec |
| 184 |
.run_streaming(step, sink) |
| 185 |
.await |
| 186 |
.with_context(|| format!("running {label}"))?; |
| 187 |
anyhow::ensure!( |
| 188 |
out.status.success(), |
| 189 |
"{label} failed (exit {}): {}", |
| 190 |
out.status |
| 191 |
.code() |
| 192 |
.map_or_else(|| "signal".into(), |c| c.to_string()), |
| 193 |
failure_tail(&out), |
| 194 |
); |
| 195 |
Ok(out) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
fn failure_tail(out: &RunOutput) -> String { |
| 205 |
const MAX_LINES: usize = 5; |
| 206 |
let stderr = String::from_utf8_lossy(&out.stderr); |
| 207 |
let stdout = String::from_utf8_lossy(&out.stdout); |
| 208 |
let source = if stderr.trim().is_empty() { |
| 209 |
stdout |
| 210 |
} else { |
| 211 |
stderr |
| 212 |
}; |
| 213 |
let tail: Vec<&str> = source |
| 214 |
.lines() |
| 215 |
.filter(|l| !l.trim().is_empty()) |
| 216 |
.rev() |
| 217 |
.take(MAX_LINES) |
| 218 |
.collect(); |
| 219 |
if tail.is_empty() { |
| 220 |
return "(no output captured; see the streamed log above)".into(); |
| 221 |
} |
| 222 |
tail.into_iter().rev().collect::<Vec<_>>().join("\n") |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
pub async fn run_release( |
| 230 |
exec: &dyn Executor, |
| 231 |
plan: &ReleasePlan, |
| 232 |
sink: &mut dyn LogSink, |
| 233 |
) -> Result<ReleaseOutcome> { |
| 234 |
run_step(exec, &plan.checkout_step(), sink, "checkout").await?; |
| 235 |
run_step( |
| 236 |
exec, |
| 237 |
&plan.release_step(), |
| 238 |
sink, |
| 239 |
"release-macos.sh --keychain", |
| 240 |
) |
| 241 |
.await?; |
| 242 |
let verify = run_step(exec, &plan.verify_step(), sink, "verify gatekeeper").await?; |
| 243 |
|
| 244 |
|
| 245 |
let combined = format!( |
| 246 |
"{}{}", |
| 247 |
String::from_utf8_lossy(&verify.stdout), |
| 248 |
String::from_utf8_lossy(&verify.stderr) |
| 249 |
); |
| 250 |
Ok(ReleaseOutcome { |
| 251 |
gatekeeper_accepted: gatekeeper_accepted(&combined), |
| 252 |
dmg_remote: plan.dmg_remote_path(), |
| 253 |
}) |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
pub fn gatekeeper_accepted(spctl_output: &str) -> bool { |
| 260 |
spctl_output.contains("source=Notarized Developer ID") |
| 261 |
|| (spctl_output.contains("accepted") && spctl_output.contains("source=")) |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
pub struct StdoutSink; |
| 267 |
|
| 268 |
#[async_trait::async_trait] |
| 269 |
impl LogSink for StdoutSink { |
| 270 |
async fn write_chunk(&mut self, bytes: &[u8]) { |
| 271 |
use tokio::io::AsyncWriteExt; |
| 272 |
let mut out = tokio::io::stdout(); |
| 273 |
let _ = out.write_all(bytes).await; |
| 274 |
let _ = out.flush().await; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
#[cfg(test)] |
| 279 |
mod tests { |
| 280 |
use super::*; |
| 281 |
use ops_exec::{CapabilitySet, LocalExec}; |
| 282 |
|
| 283 |
fn go_plan(repo: &str, dist: &str) -> ReleasePlan { |
| 284 |
ReleasePlan { |
| 285 |
app: "goingson".into(), |
| 286 |
version: "0.4.1".into(), |
| 287 |
repo_path: repo.into(), |
| 288 |
env_file: "~/.tauri/passwords.env".into(), |
| 289 |
dist_root: dist.into(), |
| 290 |
dmg_name: ReleasePlan::default_dmg_name("GoingsOn", "0.4.1"), |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn checkout_step_fetches_and_checks_out_the_tag() { |
| 296 |
let s = go_plan("/repo", "/dist").checkout_step(); |
| 297 |
assert_eq!(s.action, Action::Build); |
| 298 |
let script = s.argv.last().unwrap(); |
| 299 |
assert!(script.contains("git checkout v0.4.1"), "{script}"); |
| 300 |
assert_eq!(s.cwd.as_deref(), Some(std::path::Path::new("/repo"))); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
#[test] |
| 306 |
fn checkout_step_does_not_depend_on_a_tracking_branch() { |
| 307 |
let script = go_plan("/repo", "/dist") |
| 308 |
.checkout_step() |
| 309 |
.argv |
| 310 |
.last() |
| 311 |
.unwrap() |
| 312 |
.clone(); |
| 313 |
assert!(script.contains("git fetch"), "{script}"); |
| 314 |
assert!(!script.contains("git pull"), "must not use pull: {script}"); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn validate_rejects_tilde_in_build_host_paths() { |
| 319 |
|
| 320 |
for plan in [ |
| 321 |
go_plan("~/Code/Apps/goingson", "/dist"), |
| 322 |
go_plan("/repo", "~/Dist/goingson/macos"), |
| 323 |
] { |
| 324 |
let err = plan.validate().unwrap_err().to_string(); |
| 325 |
assert!(err.contains('~'), "should name the offending value: {err}"); |
| 326 |
} |
| 327 |
|
| 328 |
let mut env_tilde = go_plan("/repo", "/dist"); |
| 329 |
env_tilde.env_file = "~/.tauri/passwords.env".into(); |
| 330 |
assert!(env_tilde.validate().is_err()); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
#[test] |
| 336 |
fn expand_local_tilde_resolves_against_our_own_home() { |
| 337 |
let home = std::env::var("HOME").expect("HOME set in the test env"); |
| 338 |
assert_eq!( |
| 339 |
expand_local_tilde("~/Dist/goingson").unwrap(), |
| 340 |
PathBuf::from(&home).join("Dist/goingson") |
| 341 |
); |
| 342 |
assert_eq!(expand_local_tilde("~").unwrap(), PathBuf::from(&home)); |
| 343 |
|
| 344 |
assert!( |
| 345 |
!expand_local_tilde("~/Dist/goingson") |
| 346 |
.unwrap() |
| 347 |
.starts_with("~") |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
#[test] |
| 352 |
fn expand_local_tilde_leaves_other_paths_alone() { |
| 353 |
assert_eq!( |
| 354 |
expand_local_tilde("/Users/max/Dist").unwrap(), |
| 355 |
PathBuf::from("/Users/max/Dist") |
| 356 |
); |
| 357 |
assert_eq!( |
| 358 |
expand_local_tilde("Dist/goingson").unwrap(), |
| 359 |
PathBuf::from("Dist/goingson") |
| 360 |
); |
| 361 |
|
| 362 |
assert_eq!( |
| 363 |
expand_local_tilde("/tmp/a~b").unwrap(), |
| 364 |
PathBuf::from("/tmp/a~b") |
| 365 |
); |
| 366 |
} |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn expand_local_tilde_refuses_another_users_home() { |
| 370 |
let err = expand_local_tilde("~max/Dist").unwrap_err().to_string(); |
| 371 |
assert!(err.contains("~max"), "should name what it saw: {err}"); |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn validate_accepts_absolute_build_host_paths() { |
| 376 |
let mut plan = go_plan( |
| 377 |
"/Users/max/Code/Apps/goingson", |
| 378 |
"/Users/max/Dist/goingson/macos", |
| 379 |
); |
| 380 |
plan.env_file = "/Users/max/.tauri/passwords.env".into(); |
| 381 |
assert!(plan.validate().is_ok()); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
#[test] |
| 387 |
fn failure_tail_falls_back_to_stdout_when_stderr_is_empty() { |
| 388 |
let out = RunOutput { |
| 389 |
status: std::process::ExitStatus::default(), |
| 390 |
stdout: b"fatal: not a git repository\n".to_vec(), |
| 391 |
stderr: Vec::new(), |
| 392 |
}; |
| 393 |
assert!(failure_tail(&out).contains("not a git repository")); |
| 394 |
} |
| 395 |
|
| 396 |
#[test] |
| 397 |
fn failure_tail_reports_when_nothing_was_captured() { |
| 398 |
let out = RunOutput { |
| 399 |
status: std::process::ExitStatus::default(), |
| 400 |
stdout: Vec::new(), |
| 401 |
stderr: Vec::new(), |
| 402 |
}; |
| 403 |
assert!(failure_tail(&out).contains("no output captured")); |
| 404 |
} |
| 405 |
|
| 406 |
#[test] |
| 407 |
fn release_step_is_gated_as_sign_and_sources_secrets() { |
| 408 |
let s = go_plan("/repo", "/dist").release_step(); |
| 409 |
assert_eq!(s.action, Action::Sign); |
| 410 |
let script = s.argv.last().unwrap(); |
| 411 |
assert!(script.contains("passwords.env")); |
| 412 |
assert!(script.contains("release-macos.sh --keychain")); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
fn verify_step_is_an_observe_action_on_the_dmg() { |
| 417 |
let s = go_plan("/repo", "/dist").verify_step(); |
| 418 |
assert_eq!( |
| 419 |
s.action, |
| 420 |
Action::Observe(ObserveKind::Custom("gatekeeper".into())) |
| 421 |
); |
| 422 |
let script = s.argv.last().unwrap(); |
| 423 |
assert!(script.contains("spctl --assess")); |
| 424 |
assert!(script.contains("/dist/GoingsOn_0.4.1_aarch64.dmg")); |
| 425 |
} |
| 426 |
|
| 427 |
#[test] |
| 428 |
fn dmg_path_trims_trailing_slash() { |
| 429 |
let p = go_plan("/repo", "/dist/").dmg_remote_path(); |
| 430 |
assert_eq!(p, "/dist/GoingsOn_0.4.1_aarch64.dmg"); |
| 431 |
} |
| 432 |
|
| 433 |
#[test] |
| 434 |
fn gatekeeper_banner_parsing() { |
| 435 |
assert!(gatekeeper_accepted( |
| 436 |
"GoingsOn.dmg: accepted\nsource=Notarized Developer ID\norigin=Developer ID Application: ..." |
| 437 |
)); |
| 438 |
assert!(gatekeeper_accepted( |
| 439 |
"X.dmg: accepted\nsource=Notarized Developer ID" |
| 440 |
)); |
| 441 |
assert!(!gatekeeper_accepted( |
| 442 |
"X.dmg: rejected\nsource=no usable signature" |
| 443 |
)); |
| 444 |
assert!(!gatekeeper_accepted("")); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
#[tokio::test] |
| 453 |
async fn run_release_drives_the_full_recipe_against_a_local_fake() { |
| 454 |
let _guard = PATH_LOCK.lock().await; |
| 455 |
let dir = tempfile::tempdir().unwrap(); |
| 456 |
let repo = dir.path().join("repo"); |
| 457 |
let dist = dir.path().join("dist"); |
| 458 |
let bindir = dir.path().join("bin"); |
| 459 |
tokio::fs::create_dir_all(repo.join("dist")).await.unwrap(); |
| 460 |
tokio::fs::create_dir_all(&dist).await.unwrap(); |
| 461 |
tokio::fs::create_dir_all(&bindir).await.unwrap(); |
| 462 |
|
| 463 |
let dmg_name = ReleasePlan::default_dmg_name("GoingsOn", "0.4.1"); |
| 464 |
let dmg = dist.join(&dmg_name); |
| 465 |
|
| 466 |
|
| 467 |
write_shim(&bindir.join("git"), "#!/bin/sh\nexit 0\n").await; |
| 468 |
write_shim( |
| 469 |
&bindir.join("spctl"), |
| 470 |
"#!/bin/sh\necho 'accepted'\necho 'source=Notarized Developer ID'\n", |
| 471 |
) |
| 472 |
.await; |
| 473 |
|
| 474 |
write_shim( |
| 475 |
&repo.join("dist/release-macos.sh"), |
| 476 |
&format!( |
| 477 |
"#!/bin/sh\nset -e\nprintf 'building %s\\n' \"$PWD\"\n: > '{}'\n", |
| 478 |
dmg.display() |
| 479 |
), |
| 480 |
) |
| 481 |
.await; |
| 482 |
|
| 483 |
let orig_path = std::env::var("PATH").unwrap_or_default(); |
| 484 |
|
| 485 |
unsafe { |
| 486 |
std::env::set_var("PATH", format!("{}:{}", bindir.display(), orig_path)); |
| 487 |
} |
| 488 |
|
| 489 |
let plan = ReleasePlan { |
| 490 |
app: "goingson".into(), |
| 491 |
version: "0.4.1".into(), |
| 492 |
repo_path: repo.to_string_lossy().into_owned(), |
| 493 |
env_file: "/dev/null".into(), |
| 494 |
dist_root: dist.to_string_lossy().into_owned(), |
| 495 |
dmg_name, |
| 496 |
}; |
| 497 |
let exec = LocalExec::new(CapabilitySet::from_tokens( |
| 498 |
["build", "sign", "notarize", "staple"], |
| 499 |
["gatekeeper"], |
| 500 |
)); |
| 501 |
|
| 502 |
let mut sink = Discard; |
| 503 |
let outcome = run_release(&exec, &plan, &mut sink) |
| 504 |
.await |
| 505 |
.expect("recipe should run"); |
| 506 |
|
| 507 |
|
| 508 |
unsafe { |
| 509 |
std::env::set_var("PATH", orig_path); |
| 510 |
} |
| 511 |
|
| 512 |
assert!(dmg.exists(), "release step should produce the DMG"); |
| 513 |
assert!(outcome.gatekeeper_accepted, "fake spctl reports notarized"); |
| 514 |
assert_eq!( |
| 515 |
outcome.dmg_remote, |
| 516 |
dist.join("GoingsOn_0.4.1_aarch64.dmg").to_string_lossy() |
| 517 |
); |
| 518 |
} |
| 519 |
|
| 520 |
static PATH_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); |
| 521 |
|
| 522 |
async fn write_shim(path: &std::path::Path, body: &str) { |
| 523 |
tokio::fs::write(path, body).await.unwrap(); |
| 524 |
let out = tokio::process::Command::new("chmod") |
| 525 |
.arg("+x") |
| 526 |
.arg(path) |
| 527 |
.output() |
| 528 |
.await |
| 529 |
.unwrap(); |
| 530 |
assert!(out.status.success()); |
| 531 |
} |
| 532 |
|
| 533 |
struct Discard; |
| 534 |
#[async_trait::async_trait] |
| 535 |
impl LogSink for Discard { |
| 536 |
async fn write_chunk(&mut self, _b: &[u8]) {} |
| 537 |
} |
| 538 |
} |
| 539 |
|