|
1 | 1 | use std::path::Path; |
2 | 2 | use std::process::Command; |
3 | 3 |
|
4 | | -use tempfile::TempDir; |
5 | | - |
6 | | -use crate::paths::{legacy_binary_path, shared_folder_dir}; |
7 | | - |
8 | | -pub fn install_compiler_binary( |
9 | | - binary_name: &str, |
10 | | - required_version: &str, |
11 | | - cargo_install_args: &[&str], |
12 | | - out_dir: &std::path::Path, |
13 | | -) { |
14 | | - let binary_path = legacy_binary_path(out_dir, binary_name); |
15 | | - match Command::new(&binary_path).args(["--version"]).output() { |
16 | | - Ok(binary_version) => { |
17 | | - let binary_version = String::from_utf8(binary_version.stdout) |
18 | | - .expect("Failed to convert the binary version to a string."); |
19 | | - if binary_version.contains(required_version) { |
20 | | - println!("The {binary_name} binary is up to date."); |
21 | | - return; |
22 | | - } else { |
23 | | - println!( |
24 | | - "The {binary_name} binary is not up to date. Installing the required version." |
25 | | - ); |
26 | | - std::fs::remove_file(&binary_path).expect("Failed to remove the old binary."); |
27 | | - } |
28 | | - } |
29 | | - Err(_) => { |
30 | | - println!("The {binary_name} binary is not installed. Installing the required version."); |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - let temp_cargo_path = TempDir::new().expect("Failed to create a temporary directory."); |
35 | | - let post_install_file_path = temp_cargo_path.path().join("bin").join(binary_name); |
36 | | - |
37 | | - let install_command_status = Command::new("cargo") |
38 | | - .args([ |
39 | | - "install", |
40 | | - "--root", |
41 | | - temp_cargo_path.path().to_str().expect("Failed to convert cargo_path to str"), |
42 | | - "--locked", |
43 | | - ]) |
44 | | - .args(cargo_install_args) |
45 | | - .status() |
46 | | - .unwrap_or_else(|_| panic!("Failed to install {binary_name}")); |
47 | | - |
48 | | - if !install_command_status.success() { |
49 | | - panic!("Failed to install {binary_name}"); |
50 | | - } |
51 | | - |
52 | | - // Move the '{binary_name}' executable to a shared location. |
53 | | - std::fs::create_dir_all(shared_folder_dir(out_dir)) |
54 | | - .expect("Failed to create shared executables folder"); |
55 | | - let move_command_status = Command::new("mv") |
56 | | - .args([post_install_file_path.as_os_str(), binary_path.as_os_str()]) |
57 | | - .status() |
58 | | - .expect("Failed to perform mv command."); |
59 | | - |
60 | | - if !move_command_status.success() { |
61 | | - panic!("Failed to move the {binary_name} binary to the shared folder."); |
62 | | - } |
63 | | - |
64 | | - std::fs::remove_dir_all(temp_cargo_path).expect("Failed to remove the cargo directory."); |
65 | | - |
66 | | - println!("Successfully set executable file: {:?}", binary_path.display()); |
67 | | -} |
68 | | - |
69 | 4 | /// Verifies that a compiler binary is installed and has the required version. |
70 | 5 | /// Panics with installation instructions if the binary is missing or has the wrong version. |
71 | 6 | pub fn verify_compiler_binary(binary_path: &Path, required_version: &str) { |
|
0 commit comments