forked from iii-hq/workers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
175 lines (160 loc) · 5.35 KB
/
Copy pathbuild.rs
File metadata and controls
175 lines (160 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Build script for the `github` worker.
//!
//! Ensures the injected console UI assets exist: `src/ui.rs` embeds
//! `ui/dist/page.js` and `ui/dist/styles.css` via `include_str!`, so if
//! either is missing or stale we run `pnpm install && pnpm build` inside
//! `ui/` first (the state worker's precedent). Set `SKIP_UI_BUILD=1` to use
//! the existing `ui/dist/` outputs as-is.
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::SystemTime;
fn main() {
// `dist/` itself is not listed: include_str! reads it directly, and
// listing it would rebuild-loop on our own output.
println!("cargo:rerun-if-changed=ui/page.tsx");
println!("cargo:rerun-if-changed=ui/styles.css");
println!("cargo:rerun-if-changed=ui/src");
println!("cargo:rerun-if-changed=ui/build.mjs");
println!("cargo:rerun-if-changed=ui/package.json");
// The lockfile lives at the workers-repo root (pnpm workspace: the ui
// project links @iii-dev/console-ui from packages/console-ui).
println!("cargo:rerun-if-changed=../pnpm-lock.yaml");
println!("cargo:rerun-if-changed=ui/tsconfig.json");
// build.rs branches on SKIP_UI_BUILD below; declare it so toggling the var
// re-runs this script (and refreshes the embedded assets accordingly).
println!("cargo:rerun-if-env-changed=SKIP_UI_BUILD");
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let ui_dir = manifest_dir.join("ui");
let dist_assets = [
ui_dir.join("dist").join("page.js"),
ui_dir.join("dist").join("styles.css"),
];
if dist_assets
.iter()
.all(|a| a.exists() && dist_is_fresh(a, &ui_dir))
{
return;
}
if std::env::var_os("SKIP_UI_BUILD").is_some() {
for asset in &dist_assets {
if !asset.exists() {
panic!(
"SKIP_UI_BUILD set but {} is missing — build the UI manually \
(cd ui && pnpm install && pnpm build) or unset the env var",
asset.display()
);
}
}
return;
}
let pnpm = locate_pnpm();
let status = Command::new(&pnpm)
.args(["install"])
.current_dir(&ui_dir)
.status()
.unwrap_or_else(|e| {
panic!(
"failed to spawn `pnpm install` in {}: {e}",
ui_dir.display()
)
});
if !status.success() {
panic!("`pnpm install` exited with {status} — see logs above");
}
let status = Command::new(&pnpm)
.args(["build"])
.current_dir(&ui_dir)
.status()
.unwrap_or_else(|e| panic!("failed to spawn `pnpm build` in {}: {e}", ui_dir.display()));
if !status.success() {
panic!("`pnpm build` exited with {status} — see logs above");
}
for asset in &dist_assets {
if !asset.exists() {
panic!(
"`pnpm build` finished but {} is still missing — check the esbuild \
output above",
asset.display()
);
}
}
}
/// `true` when the built asset is at least as new as every source that
/// contributes to it. Conservative: any I/O failure forces a rebuild.
fn dist_is_fresh(dist_asset: &Path, ui_dir: &Path) -> bool {
let Ok(dist_mtime) = dist_asset.metadata().and_then(|m| m.modified()) else {
return false;
};
let watched_files = [
ui_dir.join("page.tsx"),
ui_dir.join("styles.css"),
ui_dir.join("build.mjs"),
ui_dir.join("package.json"),
ui_dir.join("../../pnpm-lock.yaml"),
ui_dir.join("tsconfig.json"),
];
for f in watched_files.iter() {
if !f.exists() {
continue;
}
let Ok(m) = f.metadata().and_then(|m| m.modified()) else {
return false;
};
if m > dist_mtime {
return false;
}
}
for dir in [ui_dir.join("src")] {
if dir.exists() && !subtree_older_than(&dir, dist_mtime) {
return false;
}
}
true
}
fn subtree_older_than(root: &Path, ceiling: SystemTime) -> bool {
let Ok(read) = std::fs::read_dir(root) else {
return false;
};
for entry in read.flatten() {
let path = entry.path();
let Ok(meta) = entry.metadata() else {
return false;
};
if meta.is_dir() {
if !subtree_older_than(&path, ceiling) {
return false;
}
} else {
let Ok(m) = meta.modified() else {
return false;
};
if m > ceiling {
return false;
}
}
}
true
}
fn locate_pnpm() -> PathBuf {
if let Ok(explicit) = std::env::var("PNPM") {
return PathBuf::from(explicit);
}
let candidates = if cfg!(windows) {
["pnpm.cmd", "pnpm.exe", "pnpm"].as_slice()
} else {
["pnpm"].as_slice()
};
let path = std::env::var_os("PATH").unwrap_or_default();
for dir in std::env::split_paths(&path) {
for name in candidates {
let candidate = dir.join(name);
if candidate.is_file() {
return candidate;
}
}
}
panic!(
"pnpm not found on PATH — install Node + pnpm, or set SKIP_UI_BUILD=1 \
after building the UI manually with `cd ui && pnpm install && pnpm build`"
);
}