use std::process::Command;
use std::fs;
use std::fs::write;
use std::path::Path;
use regex::Regex;
pub fn install_essential_equipments() {
let install_cmd = "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))";
let script_content = r#"Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))"#;
write("install_choco.ps1", script_content);
let install_choco = Command::new("powershell")
.args(&[
"-NoProfile",
"-ExecutionPolicy", "Bypass",
"-File", "install_choco.ps1"
])
.spawn();
match install_choco {
Ok(_) => {
println!("Choco has installed");
}
Err(e) => {
eprintln!("PowerShell 실행 중 오류 발생: {}", e);
}
}
let install_opencv_and_llvm = Command::new("powershell")
.args(&[
"-Command",
"Start-Process powershell -Verb runAs -ArgumentList '-Command \"choco install llvm opencv\"'",
])
.status();
match install_opencv_and_llvm {
Ok(status) if status.success() => {
println!("choco --help 명령 실행이 완료되었습니다.");
}
Ok(status) => {
eprintln!("choco --help 명령 실행이 실패했습니다. 종료 코드: {}", status);
}
Err(e) => {
eprintln!("choco --help 명령 실행 중 오류 발생: {}", e);
}
}
let opencv_version = get_opencv_num_and_version().unwrap_or(0);
set_opencv_env(opencv_version);
}
pub fn get_opencv_num_and_version() -> Option<u16> {
let opencv_lib_path = "C:\\tools\\opencv\\build\\x64\\lib"; let opencv_world_prefix = "opencv_world";
if !Path::new(opencv_lib_path).exists() {
eprintln!("OpenCV 라이브러리 경로가 존재하지 않습니다: {}", opencv_lib_path);
return None;
}
match fs::read_dir(opencv_lib_path) {
Ok(entries) => {
let re = Regex::new(&format!(r"{}(\d+)\.lib", opencv_world_prefix)).unwrap();
for entry in entries {
if let Ok(entry) = entry {
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();
if let Some(captures) = re.captures(&file_name) {
if let Some(version_str) = captures.get(1) {
if let Ok(version) = version_str.as_str().parse::<u16>() {
println!("발견된 OpenCV 라이브러리: {}", file_name);
println!("추출된 버전 숫자: {}", version);
return Some(version); } else {
eprintln!("버전 숫자가 u16 형식으로 변환되지 않습니다.");
return None;
}
}
}
}
}
println!("opencv_world 라이브러리를 찾을 수 없습니다.");
None
}
Err(e) => {
eprintln!("디렉토리를 읽을 수 없습니다: {}", e);
None
}
}
}
pub fn set_opencv_env(opencv_version: u16) { let opencv_include_paths = "C:\\tools\\opencv\\build\\include";
let opencv_link_string = format!("opencv_world{}", opencv_version); let opencv_link_libs = opencv_link_string.as_str();
let opencv_link_paths = "C:\\tools\\opencv\\build\\x64\\lib";
let opencv_bin_paths = "C:\\tools\\opencv\\build\\x64\\vc16\\bin";
let script = format!(
r#"
[Environment]::SetEnvironmentVariable("OPENCV_INCLUDE_PATHS", "{0}", [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable("OPENCV_LINK_LIBS", "{1}", [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable("OPENCV_LINK_PATHS", "{2}", [EnvironmentVariableTarget]::Machine)
$currentPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if (-not ($currentPath -like "*{3}*")) {{
[Environment]::SetEnvironmentVariable("Path", "$currentPath;{3}", [EnvironmentVariableTarget]::Machine)
}}
"#,
opencv_include_paths, opencv_link_libs, opencv_link_paths, opencv_bin_paths
);
let output = Command::new("powershell")
.args(["-Command", &script])
.output()
.expect("Failed to execute PowerShell command");
if output.status.success() {
println!("환경 변수가 성공적으로 설정되었습니다.");
} else {
eprintln!(
"오류 발생: {}",
String::from_utf8_lossy(&output.stderr)
);
}
}