Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
fix: install dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
iewnfod committed Mar 8, 2024
1 parent 8e7f41c commit cab4db6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub const PYTHON : &[&str] = &[
"python",
"python3",
];
pub const OS_TYPE: &str = std::env::consts::OS;

#[cfg(target_os = "macos")]
pub const INSTALL_SCRIPT: &str = include_str!("./scripts/macos-install.sh");
Expand Down
74 changes: 38 additions & 36 deletions src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ use std::io::Write;
use std::process::Command;
use std::path::{Path, PathBuf};

use crate::constants::{set_install_result, OS_TYPE};
use crate::constants::{set_install_result, get_install_result};
use crate::constants::INSTALL_SCRIPT;
use crate::constants::PYTHON;

fn save_tmp_file(content: &str) -> PathBuf {
let p = Path::new("/tmp/macos-install.sh");
let p = Path::new("/tmp/cpc-install.sh");
let mut f = File::create(p).unwrap();
f.write(content.as_bytes()).unwrap();
p.to_path_buf()
}

pub fn check_dependencies() -> bool {
// Check if Xcode Command Line Tools is installed
// 检查 git
let git_check = Command::new("which").arg("git").output().unwrap();
if !git_check.status.success() && String::from_utf8_lossy(&git_check.stdout).is_empty() {
set_install_result(Some(44));
return false;
}
// Check if commands in PYTHON can be run

// 检查 python
for py in PYTHON {
let python_check = Command::new("which").arg(py).output().unwrap();
if python_check.status.success() && !String::from_utf8_lossy(&python_check.stdout).is_empty() {
Expand All @@ -35,41 +36,42 @@ pub fn check_dependencies() -> bool {
true
}

pub fn dependencies_install() {
use crate::constants::get_install_result;

fn install_git() {
if OS_TYPE == "windows" {
Command::new("winget")
.arg("install")
.arg("Git.Git")
.arg("--accept-package-agreements")
.arg("--accept-source-agreements");
} else if cfg!(target_os = "macos"){
Command::new("git")
.arg("--version");
}

}
fn install_git() {
#[cfg(target_os = "windows")]
Command::new("winget")
.arg("install")
.arg("Git.Git")
.arg("--accept-package-agreements")
.arg("--accept-source-agreements")
.spawn().unwrap();

fn install_python() {
if OS_TYPE == "windows" {
Command::new("winget")
.arg("install")
.arg("Python.Python.3.12")
.arg("--accept-package-agreements")
.arg("--accept-source-agreements");
} else if cfg!(target_os = "macos"){
Command::new("python3")
.arg("--version");
}
}
#[cfg(target_os = "macos")]
Command::new("git")
.arg("--version")
.spawn().unwrap();
}

fn install_python() {
#[cfg(target_os = "windows")]
Command::new("winget")
.arg("install")
.arg("Python.Python.3.12")
.arg("--accept-package-agreements")
.arg("--accept-source-agreements")
.spawn().unwrap();

#[cfg(target_os = "macos")]
Command::new("python3")
.arg("--version")
.spawn().unwrap();
}

pub fn dependencies_install() {
if let Some(result) = get_install_result() {
if result == 44 {
install_git();
} else if result == 45{
install_python();
match result {
44 => install_git(),
45 => install_python(),
_ => ()
}
}
}
Expand Down

0 comments on commit cab4db6

Please sign in to comment.