-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmod.rs
More file actions
543 lines (476 loc) · 17.4 KB
/
Copy pathmod.rs
File metadata and controls
543 lines (476 loc) · 17.4 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use std::env;
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::str;
use color_eyre::{eyre::eyre, Result};
use fs_err as fs;
use tracing::{info, instrument, warn};
use crate::build::run_command;
use crate::publish::make_remote_link;
use crate::run_tests::types::BroadcastRecvBool;
const FETCH_NVM_VERSION: &str = "v0.39.7";
const REQUIRED_NODE_MAJOR: u32 = 20;
const MINIMUM_NODE_MINOR: u32 = 0;
const MINIMUM_NPM_MAJOR: u32 = 9;
const MINIMUM_NPM_MINOR: u32 = 0;
pub const REQUIRED_PY_MAJOR: u32 = 3;
pub const MINIMUM_PY_MINOR: u32 = 10;
pub const REQUIRED_PY_PACKAGE: &str = "componentize-py==0.11.0";
const WASM_TOOLS_VERSION: &str = "1.225.0";
#[derive(Clone)]
pub enum Dependency {
Foundry,
Nvm,
Npm,
Node,
Rust,
RustWasm32Wasi,
WasmTools,
Docker,
}
impl std::fmt::Display for Dependency {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Dependency::Foundry => write!(f, "foundry"),
Dependency::Nvm => write!(f, "nvm {}", FETCH_NVM_VERSION),
Dependency::Npm => write!(f, "npm {}.{}", MINIMUM_NPM_MAJOR, MINIMUM_NPM_MINOR),
Dependency::Node => write!(f, "node {}.{}", REQUIRED_NODE_MAJOR, MINIMUM_NODE_MINOR),
Dependency::Rust => write!(f, "rust"),
Dependency::RustWasm32Wasi => write!(f, "rust wasm32-wasip1 target"),
Dependency::WasmTools => write!(f, "wasm-tools"),
Dependency::Docker => write!(f, "docker"),
}
}
}
// use Display
impl std::fmt::Debug for Dependency {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
// hack to allow definition of Display
struct Dependencies(Vec<Dependency>);
impl std::fmt::Display for Dependencies {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let deps: Vec<String> = self.0.iter().map(|d| d.to_string()).collect();
write!(f, "{}", deps.join(", "))
}
}
#[instrument(level = "trace", skip_all)]
fn is_nvm_installed() -> Result<bool> {
let home_dir = env::var("HOME")?;
let nvm_dir = format!("{}/.nvm", home_dir);
Ok(std::path::Path::new(&nvm_dir).exists())
}
#[instrument(level = "trace", skip_all)]
fn install_nvm(verbose: bool) -> Result<()> {
info!("Getting nvm...");
let install_nvm = format!(
"curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/{}/install.sh | bash",
FETCH_NVM_VERSION,
);
run_command(Command::new("bash").args(&["-c", &install_nvm]), verbose)?;
info!("Done getting nvm.");
Ok(())
}
#[instrument(level = "trace", skip_all)]
fn install_rust(verbose: bool) -> Result<()> {
info!("Getting rust...");
let install_rust = "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh";
run_command(Command::new("bash").args(&["-c", install_rust]), verbose)?;
info!("Done getting rust.");
Ok(())
}
#[instrument(level = "trace", skip_all)]
fn check_python_venv(python: &str) -> Result<()> {
info!("Checking for python venv...");
let venv_result = run_command(
Command::new(python)
.args(&["-m", "venv", "hyperware-test-venv"])
.current_dir("/tmp"),
false,
);
let venv_dir = PathBuf::from("/tmp/hyperware-test-venv");
if venv_dir.exists() {
fs::remove_dir_all(&venv_dir)?;
}
match venv_result {
Ok(_) => {
info!("Found python venv.");
Ok(())
}
Err(_) => Err(eyre!("Check for python venv failed.")),
}
}
#[instrument(level = "trace", skip_all)]
fn is_command_installed(cmd: &str) -> Result<bool> {
Ok(Command::new("which")
.arg(cmd)
.stdout(Stdio::null())
.status()?
.success())
}
#[instrument(level = "trace", skip_all)]
fn is_npm_version_correct(node_version: String, required_version: (u32, u32)) -> Result<bool> {
let version = call_with_nvm_output(&format!("nvm use {node_version} && npm --version"))?;
let version = version
.split('\n')
.filter(|s| !s.is_empty())
.collect::<Vec<&str>>();
let version = version.last().unwrap_or_else(|| &"");
Ok(parse_version(version)
.and_then(|v| Some(compare_versions_min_major(v, required_version)))
.unwrap_or(false))
}
fn strip_color_codes(input: &str) -> String {
let re = regex::Regex::new("\x1b\\[[^m]*m").unwrap();
re.replace_all(input, "").into_owned()
}
/// Get the newest valid `node` via `nvm`, provided
/// that version is at least as new as `required_major`.
///
/// Returns `None` if no valid version; `Some(String)`:
/// the valid version, as a String.
#[instrument(level = "trace", skip_all)]
pub fn get_newest_valid_node_version(
required_major: Option<u32>,
minimum_minor: Option<u32>,
) -> Result<Option<String>> {
let required_major = required_major.unwrap_or(REQUIRED_NODE_MAJOR);
let minimum_minor = minimum_minor.unwrap_or(MINIMUM_NODE_MINOR);
let nvm_ls = call_with_nvm_output("nvm ls --no-alias")?;
let mut versions = Vec::new();
for line in nvm_ls.lines() {
let line = strip_color_codes(line);
let fields: Vec<&str> = line.split_whitespace().collect();
if fields.len() == 1 {
versions.push(fields[0].to_string());
} else if fields.len() == 2 {
if "->" == fields[0] {
versions.push(fields[1].to_string());
} else if fields[1] == "*" {
versions.push(fields[0].to_string());
} else {
warn!("unexpected line {line} in `nvm ls --no-alias`; skipping:\n{nvm_ls}");
}
}
}
let mut newest_node = None;
let mut max_version = (0, 0); // (major, minor)
for version in versions {
if let Some((major, minor)) = parse_version(&version) {
if major == required_major && minor >= minimum_minor && (major, minor) > max_version {
max_version = (major, minor);
newest_node = Some(version.to_string());
}
}
}
Ok(newest_node)
}
#[instrument(level = "trace", skip_all)]
fn call_with_nvm_output(arg: &str) -> Result<String> {
let output = Command::new("bash")
.args(&["-c", &format!("source ~/.nvm/nvm.sh && {}", arg)])
.output()?
.stdout;
Ok(String::from_utf8_lossy(&output).to_string())
}
#[instrument(level = "trace", skip_all)]
fn call_with_nvm(arg: &str, verbose: bool) -> Result<()> {
run_command(
Command::new("bash").args(&["-c", &format!("source ~/.nvm/nvm.sh && {}", arg)]),
verbose,
)?;
Ok(())
}
#[instrument(level = "trace", skip_all)]
fn call_rustup(arg: &str, verbose: bool, toolchain: &str) -> Result<()> {
run_command(
Command::new("bash").args(&["-c", &format!("rustup {} {}", toolchain, arg)]),
verbose,
)?;
Ok(())
}
#[instrument(level = "trace", skip_all)]
fn call_cargo(arg: &str, verbose: bool, toolchain: &str) -> Result<()> {
let command = if arg.contains("--color=always") {
format!("cargo {} {}", toolchain, arg)
} else {
format!("cargo {} --color=always {}", toolchain, arg)
};
run_command(Command::new("bash").args(&["-c", &command]), verbose)?;
Ok(())
}
fn compare_versions_min_major(installed_version: (u32, u32), required_version: (u32, u32)) -> bool {
installed_version.0 >= required_version.0 && installed_version.1 >= required_version.1
}
fn parse_version(version_str: &str) -> Option<(u32, u32)> {
let mut parts: Vec<&str> = version_str.split('.').collect();
if parts.is_empty() {
return None;
}
// Remove leading 'v' from the first part if present
parts[0] = parts[0].trim_start_matches('v');
if parts.len() >= 2 {
if let (Ok(major), Ok(minor)) = (parts[0].parse(), parts[1].parse()) {
return Some((major, minor));
}
}
None
}
#[instrument(level = "trace", skip_all)]
fn check_rust_toolchains_targets(toolchain: &str) -> Result<Vec<Dependency>> {
let mut missing_deps = Vec::new();
let output = Command::new("rustup")
.arg(toolchain)
.arg("show")
.output()?
.stdout;
let output = String::from_utf8_lossy(&output);
let has_wasm32_wasi = output
.split('\n')
.fold(false, |acc, item| acc || item.contains("wasm32-wasip1"));
if !has_wasm32_wasi {
missing_deps.push(Dependency::RustWasm32Wasi);
}
Ok(missing_deps)
}
/// Find the newest Python version (>= 3.10 or given major, minor)
#[instrument(level = "trace", skip_all)]
pub fn get_python_version(
required_major: Option<u32>,
minimum_minor: Option<u32>,
) -> Result<Option<String>> {
let required_major = required_major.unwrap_or(REQUIRED_PY_MAJOR);
let minimum_minor = minimum_minor.unwrap_or(MINIMUM_PY_MINOR);
let output = Command::new("bash")
.arg("-c")
.arg("for dir in $(echo $PATH | tr ':' ' '); do for cmd in $(echo $dir/python3*); do which $(basename $cmd) 2>/dev/null; done; done")
.output()?;
let commands = str::from_utf8(&output.stdout)?;
let python_versions = commands.split_whitespace();
let mut newest_python = None;
let mut max_version = (0, 0); // (major, minor)
for python in python_versions {
let version_output = Command::new(python).arg("--version").output()?;
let version_str = str::from_utf8(&version_output.stdout).unwrap_or("");
if version_str.is_empty() {
continue;
}
if let Some(version) = version_str.split_whitespace().nth(1) {
if let Some((major, minor)) = parse_version(version) {
if major == required_major && minor >= minimum_minor && (major, minor) > max_version
{
max_version = (major, minor);
newest_python = Some(python.to_string());
}
}
}
}
Ok(newest_python)
}
/// Check for Python deps, erroring if not found: python deps cannot be automatically fetched
#[instrument(level = "trace", skip_all)]
pub fn check_py_deps() -> Result<String> {
let python = get_python_version(Some(REQUIRED_PY_MAJOR), Some(MINIMUM_PY_MINOR))?
.ok_or_else(|| eyre!("kit requires Python 3.10 or newer"))?;
check_python_venv(&python)?;
Ok(python)
}
/// Check for Javascript deps, returning a Vec of not found: can be automatically fetched
#[instrument(level = "trace", skip_all)]
pub fn check_js_deps() -> Result<Vec<Dependency>> {
let mut missing_deps = Vec::new();
if !is_nvm_installed()? {
missing_deps.push(Dependency::Nvm);
}
let valid_node = get_newest_valid_node_version(None, None)?;
match valid_node {
None => missing_deps.extend_from_slice(&[Dependency::Node, Dependency::Npm]),
Some(vn) => {
if !is_command_installed("npm")?
|| !is_npm_version_correct(vn, (MINIMUM_NPM_MAJOR, MINIMUM_NPM_MINOR))?
{
missing_deps.push(Dependency::Npm);
}
}
}
Ok(missing_deps)
}
/// Check for Foundry deps, returning a Vec of Dependency if not found: can be automatically fetched?
#[instrument(level = "trace", skip_all)]
pub fn check_foundry_deps() -> Result<Vec<Dependency>> {
if !is_command_installed("anvil")? {
return Ok(vec![Dependency::Foundry]);
}
Ok(vec![])
}
/// install Foundry, could be separated into binary extractions from github releases.
#[instrument(level = "trace", skip_all)]
fn install_foundry(verbose: bool) -> Result<()> {
let download_cmd = "curl -L https://foundry.paradigm.xyz | bash";
let install_cmd = "export PATH=\"$PATH:$HOME/.foundry/bin\" && foundryup";
run_command(Command::new("bash").args(&["-c", download_cmd]), verbose)?;
run_command(Command::new("bash").args(&["-c", install_cmd]), verbose)?;
Ok(())
}
/// Check for Rust deps, returning a Vec of not found: can be automatically fetched
#[instrument(level = "trace", skip_all)]
pub fn check_rust_deps(toolchain: &str) -> Result<Vec<Dependency>> {
if !is_command_installed("rustup")? {
// don't have rust -> missing all
return Ok(vec![
Dependency::Rust,
Dependency::RustWasm32Wasi,
Dependency::WasmTools,
]);
}
let mut missing_deps = check_rust_toolchains_targets(toolchain)?;
if !is_command_installed("wasm-tools")? {
missing_deps.push(Dependency::WasmTools);
}
Ok(missing_deps)
}
// Check for Foundry deps, returning a Vec of not found: can be automatically fetched?
#[instrument(level = "trace", skip_all)]
pub fn check_docker_deps() -> Result<Vec<Dependency>> {
let mut missing_deps = Vec::new();
if !is_command_installed("docker")? {
missing_deps.push(Dependency::Docker);
// TODO: automated get docker
return Err(eyre!(
"docker not found: please {} and try again",
make_remote_link("https://docs.docker.com/engine/install", "install Docker"),
));
}
Ok(missing_deps)
}
#[instrument(level = "trace", skip_all)]
pub async fn get_deps(
deps: Vec<Dependency>,
recv_kill: &mut BroadcastRecvBool,
non_interactive: bool,
verbose: bool,
toolchain: &str,
) -> Result<()> {
if deps.is_empty() {
return Ok(());
}
if non_interactive {
install_deps(deps, verbose, toolchain)?;
} else {
// If setup required, request user permission
print!(
"kit requires {} missing {}: {}. Install? [Y/n]: ",
if deps.len() == 1 { "this" } else { "these" },
if deps.len() == 1 {
"dependency"
} else {
"dependencies"
},
Dependencies(deps.clone()),
);
// Flush to ensure the prompt is displayed before input
io::stdout().flush().unwrap();
// Read the user's response
let (sender, mut receiver) = tokio::sync::mpsc::channel(1);
tokio::spawn(async move {
let mut response = String::new();
io::stdin().read_line(&mut response).unwrap();
sender.send(response).await.unwrap();
});
// Process the response
let response = tokio::select! {
Some(response) = receiver.recv() => response,
k = recv_kill.recv() => {
match k {
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
// some systems drop the fake sender produced in build/mod.rs:57
// make_fake_kill_chan() and so we handle this by ignoring the
// Closed message that comes through
// https://docs.rs/tokio/latest/tokio/sync/broadcast/struct.Receiver.html#method.recv
receiver.recv().await.unwrap()
}
_ => return Err(eyre!("got exit code")),
}
}
};
let response = response.trim().to_lowercase();
match response.as_str() {
"y" | "yes" | "" => install_deps(deps, verbose, toolchain)?,
r => warn!("Got '{}'; not getting deps.", r),
}
}
Ok(())
}
#[instrument(level = "trace", skip_all)]
fn install_deps(deps: Vec<Dependency>, verbose: bool, toolchain: &str) -> Result<()> {
for dep in deps {
match dep {
Dependency::Nvm => install_nvm(verbose)?,
Dependency::Npm => call_with_nvm(&format!("nvm install-latest-npm"), verbose)?,
Dependency::Node => call_with_nvm(
&format!("nvm install {}.{}", REQUIRED_NODE_MAJOR, MINIMUM_NODE_MINOR,),
verbose,
)?,
Dependency::Rust => install_rust(verbose)?,
Dependency::RustWasm32Wasi => {
call_rustup("target add wasm32-wasip1", verbose, toolchain)?
}
Dependency::WasmTools => call_cargo(
&format!("install wasm-tools --locked --version {WASM_TOOLS_VERSION}"),
verbose,
toolchain,
)?,
Dependency::Foundry => install_foundry(verbose)?,
Dependency::Docker => {}
}
}
Ok(())
}
#[instrument(level = "trace", skip_all)]
pub async fn execute(
recv_kill: &mut BroadcastRecvBool,
docker_optional: bool,
python_optional: bool,
foundry_optional: bool,
javascript_optional: bool,
non_interactive: bool,
verbose: bool,
toolchain: &str,
) -> Result<()> {
info!("Setting up...");
let py_result = check_py_deps();
if !python_optional {
py_result?;
} else {
if let Err(e) = py_result {
warn!("Python deps are not satisfied: {e}");
}
}
let mut missing_deps = check_rust_deps(toolchain)?;
let mut js_deps = check_js_deps()?;
if !javascript_optional {
missing_deps.append(&mut js_deps);
} else {
warn!("JavaScript deps are not satisfied: {js_deps:?}");
}
let docker_result = check_docker_deps();
if !docker_optional {
missing_deps.append(&mut docker_result?);
} else {
if let Err(e) = docker_result {
warn!("Docker deps are not satisfied: {e}");
}
}
let mut foundry_deps = check_foundry_deps()?;
if !foundry_optional {
missing_deps.append(&mut foundry_deps);
} else {
warn!("Foundry deps are not satisfied: {foundry_deps:?}");
}
get_deps(missing_deps, recv_kill, non_interactive, verbose, toolchain).await?;
info!("Done setting up.");
Ok(())
}