|
1 | 1 | //! Tools required by us to build stuff. |
2 | 2 |
|
3 | | -use crate::util::{error, execute_command, unwrap_or_exit, warn}; |
| 3 | +use crate::util::{debug1, error, execute_command, unwrap_or_exit, warn}; |
| 4 | +use std::fs::File; |
| 5 | +use std::io::Write; |
4 | 6 | use std::process::{exit, Command}; |
5 | 7 |
|
6 | 8 | /// Required tools. |
7 | 9 | static TOOLS: &[&str] = &["sass", "rollup"]; |
| 10 | +static NVM_EXEC: &'static str = "/tmp/pgml-components-nvm.sh"; |
| 11 | +static NVM_SOURCE: &'static str = "https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh"; |
| 12 | +static NVM_SOURCE_DOWNLOADED: &'static str = "/tmp/pgml-components-nvm-source.sh"; |
8 | 13 |
|
9 | 14 | /// Install any missing tools. |
10 | 15 | pub fn install() { |
11 | | - if let Err(err) = execute_command(Command::new("node").arg("--version")) { |
12 | | - error("Node is not installed. Install it with nvm or your system package manager."); |
13 | | - debug!("{}", err); |
14 | | - exit(1); |
15 | | - } |
| 16 | + install_nvm_entrypoint(); |
| 17 | + debug!("installed node entrypoint"); |
| 18 | + install_node(); |
| 19 | + debug!("installed node"); |
16 | 20 |
|
17 | 21 | for tool in TOOLS { |
18 | | - match execute_command(Command::new(tool).arg("--version")) { |
| 22 | + match execute_with_nvm(Command::new(tool).arg("--version")) { |
19 | 23 | Ok(_) => (), |
20 | 24 | Err(err) => { |
21 | | - debug!("{}", err); |
| 25 | + debug1!(err); |
22 | 26 | warn(&format!("installing {}", tool)); |
23 | | - unwrap_or_exit!(execute_command( |
| 27 | + unwrap_or_exit!(execute_with_nvm( |
24 | 28 | Command::new("npm").arg("install").arg("-g").arg(tool) |
25 | 29 | )); |
26 | 30 | } |
27 | 31 | } |
28 | 32 | } |
29 | 33 | } |
| 34 | + |
| 35 | +/// Execute a command making sure that nvm is available. |
| 36 | +pub fn execute_with_nvm(command: &mut Command) -> std::io::Result<String> { |
| 37 | + let mut cmd = Command::new(NVM_EXEC); |
| 38 | + cmd.arg(command.get_program()); |
| 39 | + for arg in command.get_args() { |
| 40 | + cmd.arg(arg); |
| 41 | + } |
| 42 | + execute_command(&mut cmd) |
| 43 | +} |
| 44 | + |
| 45 | +/// Install the nvm entrypoint we provide into /tmp |
| 46 | +fn install_nvm_entrypoint() { |
| 47 | + let mut file = unwrap_or_exit!(File::create(NVM_EXEC)); |
| 48 | + unwrap_or_exit!(writeln!(&mut file, "{}", include_str!("nvm.sh"))); |
| 49 | + drop(file); |
| 50 | + |
| 51 | + unwrap_or_exit!(execute_command( |
| 52 | + Command::new("chmod").arg("+x").arg(NVM_EXEC) |
| 53 | + )); |
| 54 | +} |
| 55 | + |
| 56 | +/// Install node using nvm |
| 57 | +fn install_node() { |
| 58 | + debug!("installing node"); |
| 59 | + // Node is already installed. |
| 60 | + if let Ok(_) = execute_with_nvm(Command::new("node").arg("--version")) { |
| 61 | + debug!("node is available"); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + warn("installing node using nvm"); |
| 66 | + |
| 67 | + debug!("node is not available"); |
| 68 | + |
| 69 | + if let Err(err) = execute_command(Command::new("nvm").arg("--version")) { |
| 70 | + debug!("nvm is not available"); |
| 71 | + debug1!(err); |
| 72 | + // Install Node Version Manager. |
| 73 | + if let Err(err) = execute_command( |
| 74 | + Command::new("curl") |
| 75 | + .arg("-Ls") |
| 76 | + .arg(NVM_SOURCE) |
| 77 | + .arg("-o") |
| 78 | + .arg(NVM_SOURCE_DOWNLOADED), |
| 79 | + ) { |
| 80 | + debug!("curl is not available"); |
| 81 | + error("couldn't not download nvm from Github, please do so manually before proceeding"); |
| 82 | + debug1!(err); |
| 83 | + exit(1); |
| 84 | + } else { |
| 85 | + if let Err(err) = execute_command(Command::new("bash").arg(NVM_SOURCE_DOWNLOADED)) { |
| 86 | + error("couldn't install nvm, please do so manually before proceeding"); |
| 87 | + debug1!(err); |
| 88 | + exit(1); |
| 89 | + } else { |
| 90 | + warn("installed nvm"); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if let Err(err) = execute_with_nvm(Command::new("nvm").arg("install").arg("stable")) { |
| 96 | + error("couldn't install Node, please do so manually before proceeding"); |
| 97 | + debug1!(err); |
| 98 | + exit(1); |
| 99 | + } else { |
| 100 | + warn("installed node") |
| 101 | + } |
| 102 | +} |
0 commit comments