forked from dylan-sutton-chavez/edge-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
251 lines (226 loc) · 8.83 KB
/
build.rs
File metadata and controls
251 lines (226 loc) · 8.83 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
`edge build`: vendor the runtime, compiler.wasm, used packages, and user scripts into a self-contained dist/.
*/
use anyhow::{anyhow, Context, Result};
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Instant;
use crate::pkg::{self, Kind, Manifest};
// Production layout we mirror into dist/runtime/ and dist/.
const RUNTIME_BASE: &str = "https://cdn.edgepython.com/runtime/";
const COMPILER_WASM: &str = "https://cdn.edgepython.com/compiler.wasm";
const RUNTIME_FILES: &[&str] = &[
"src/index.js",
"src/element.js",
"src/env.js",
"src/fetch.js",
"src/native.js",
"src/prefetch.js",
"src/rt.js",
"src/specs.js",
"src/defaults.js",
"src/cache/idb.js",
"src/cache/memory.js",
"worker/worker.js",
"worker/engine.js",
];
const INDEX_HTML: &str = include_str!("templates/dist.html");
pub fn run(manifest_path: &Path, out_dir: PathBuf) -> Result<()> {
let t0 = Instant::now();
let manifest = Manifest::load(manifest_path)?;
// `Path::parent` returns Some("") for a bare filename, so collapse that to "." explicitly.
let project = match manifest_path.parent() {
Some(p) if !p.as_os_str().is_empty() => p.to_path_buf(),
_ => PathBuf::from("."),
};
fs::create_dir_all(&out_dir).with_context(|| format!("creating {}", out_dir.display()))?;
let sp = crate::ui::spinner("vendoring runtime");
match vendor_runtime(&out_dir) {
Ok(()) => sp.done("vendored runtime"),
Err(e) => { sp.fail("failed to vendor runtime"); return Err(e); }
}
let sp = crate::ui::spinner("fetching compiler.wasm");
let compiler_bytes = match fetch(COMPILER_WASM).context("fetching compiler.wasm") {
Ok(b) => b,
Err(e) => { sp.fail("failed to fetch compiler.wasm"); return Err(e); }
};
fs::write(out_dir.join("compiler.wasm"), &compiler_bytes)?;
sp.done("fetched compiler.wasm");
let scripts = collect_scripts(&project, &out_dir);
let imports = crawl_imports(&scripts);
let sp = crate::ui::spinner("vendoring packages");
let (vendored_imports, vendored_host) = match vendor_packages(&manifest, &imports, &out_dir) {
Ok(v) => v,
Err(e) => { sp.fail("failed to vendor packages"); return Err(e); }
};
sp.done("vendored packages");
let script_count = copy_scripts(&scripts, &project, &out_dir)?;
let rewritten = rewrite_manifest(&manifest, &vendored_imports, &vendored_host);
let pretty = serde_json::to_string_pretty(&rewritten)?;
fs::write(out_dir.join("packages.json"), format!("{pretty}\n"))?;
let entry = find_entry(&scripts, &project);
fs::write(out_dir.join("index.html"), index_html(&entry))?;
crate::ui::build_report(
&out_dir,
RUNTIME_FILES.len(),
vendored_imports.len() + vendored_host.len(),
script_count,
dir_size(&out_dir)?,
t0.elapsed(),
);
Ok(())
}
/// Fetch the runtime JS modules into `dist/runtime/` mirroring their CDN layout.
fn vendor_runtime(out_dir: &Path) -> Result<()> {
for rel in RUNTIME_FILES {
let url = format!("{RUNTIME_BASE}{rel}");
let bytes = fetch(&url).with_context(|| format!("fetching {url}"))?;
let path = out_dir.join("runtime").join(rel);
if let Some(p) = path.parent() {
fs::create_dir_all(p)?;
}
fs::write(&path, bytes)?;
}
Ok(())
}
/// Walk the project for `.py` files; skip hidden dirs and the output directory itself.
fn collect_scripts(project: &Path, out_dir: &Path) -> Vec<PathBuf> {
let mut scripts = Vec::new();
walk(project, out_dir, &mut scripts);
scripts
}
fn walk(dir: &Path, out_dir: &Path, found: &mut Vec<PathBuf>) {
let Ok(entries) = fs::read_dir(dir) else { return };
for entry in entries.flatten() {
let path = entry.path();
if path == out_dir { continue; }
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if name.starts_with('.') || name == "node_modules" || name == "target" {
continue;
}
if path.is_dir() {
walk(&path, out_dir, found);
} else if path.extension().and_then(|e| e.to_str()) == Some("py") {
found.push(path);
}
}
}
/// Cheap import scanner: regex-free, picks up `import X` and `from X import …` at the top level of a line.
fn crawl_imports(scripts: &[PathBuf]) -> BTreeSet<String> {
let mut imports = BTreeSet::new();
for path in scripts {
let Ok(text) = fs::read_to_string(path) else { continue };
for line in text.lines() {
let line = line.trim();
let rest = if let Some(r) = line.strip_prefix("from ") {
r
} else if let Some(r) = line.strip_prefix("import ") {
r
} else {
continue;
};
for tok in rest.split(|c: char| c == ',' || c.is_whitespace()) {
if tok.is_empty() { continue; }
let name = tok.split('.').next().unwrap_or(tok);
if !name.is_empty() { imports.insert(name.to_string()); }
break; // first token after `import`/`from` is the module
}
}
}
imports
}
/// For each imported name, resolve via the shared registry, fetch, and stash under dist/vendor/.
fn vendor_packages(
manifest: &Manifest,
imports: &BTreeSet<String>,
out_dir: &Path,
) -> Result<(BTreeMap<String, String>, BTreeMap<String, String>)> {
let mut std_local = BTreeMap::new();
let mut host_local = BTreeMap::new();
for name in imports {
// Unknown names are project-local .py modules; let the runtime resolve them at run time.
let Some((kind, url)) = pkg::resolve(name, manifest) else { continue };
let bytes = fetch(&url).with_context(|| format!("fetching {url}"))?;
let local = match kind {
// std packages are .wasm, except pure-Python ones (test) served as .py; preserve the real extension.
Kind::Std => format!("vendor/{name}.{}", if url.ends_with(".py") { "py" } else { "wasm" }),
Kind::Host => format!("vendor/{name}/index.js"),
};
write_under(out_dir, &local, &bytes)?;
match kind {
Kind::Std => { std_local.insert(name.clone(), local); }
Kind::Host => { host_local.insert(name.clone(), local); }
}
}
Ok((std_local, host_local))
}
fn write_under(root: &Path, rel: &str, bytes: &[u8]) -> Result<()> {
let path = root.join(rel);
if let Some(p) = path.parent() {
fs::create_dir_all(p)?;
}
fs::write(&path, bytes)?;
Ok(())
}
/// Copy each `.py` preserving its path under the project root.
fn copy_scripts(scripts: &[PathBuf], project: &Path, out_dir: &Path) -> Result<usize> {
let mut count = 0usize;
for s in scripts {
let rel = s.strip_prefix(project).unwrap_or(s);
let dest = out_dir.join(rel);
if let Some(p) = dest.parent() {
fs::create_dir_all(p)?;
}
fs::copy(s, &dest)?;
count += 1;
}
Ok(count)
}
/// Overlay vendored entries on top of the user's manifest; vendored paths win.
fn rewrite_manifest(
manifest: &Manifest,
vendored_imports: &BTreeMap<String, String>,
vendored_host: &BTreeMap<String, String>,
) -> Manifest {
let mut out = Manifest::default();
for (k, v) in &manifest.imports { out.imports.insert(k.clone(), v.clone()); }
for (k, v) in &manifest.host { out.host.insert(k.clone(), v.clone()); }
for (k, v) in vendored_imports { out.imports.insert(k.clone(), v.clone()); }
for (k, v) in vendored_host { out.host.insert(k.clone(), v.clone()); }
out
}
/// Pick `main.py`/`app.py`/`index.py` if present; otherwise the first script found.
fn find_entry(scripts: &[PathBuf], project: &Path) -> String {
for c in ["main.py", "app.py", "index.py"] {
if scripts.iter().any(|s| s.file_name().and_then(|n| n.to_str()) == Some(c)) {
return c.to_string();
}
}
scripts
.first()
.and_then(|s| s.strip_prefix(project).ok())
.and_then(|p| p.to_str())
.map(String::from)
.unwrap_or_else(|| "main.py".to_string())
}
fn dir_size(dir: &Path) -> Result<u64> {
let mut total = 0u64;
let entries = fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))?;
for entry in entries.flatten() {
let m = entry.metadata()?;
if m.is_dir() {
total = total.saturating_add(dir_size(&entry.path())?);
} else {
total = total.saturating_add(m.len());
}
}
Ok(total)
}
fn index_html(entry: &str) -> String {
INDEX_HTML.replace("__EDGE_ENTRY__", entry)
}
fn fetch(url: &str) -> Result<Vec<u8>> {
let mut resp = ureq::get(url).call().map_err(|e| anyhow!("HTTP error: {e}"))?;
resp.body_mut().read_to_vec().map_err(|e| anyhow!("reading body: {e}"))
}