Files
AeroPkg/src/commands/install.rs
2025-08-29 17:25:21 +03:00

330 lines
8.9 KiB
Rust

use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::process;
use crate::utils::parser;
use crate::commands::pkglink::pkglink;
pub fn install(repo: &String, pkgname: &String) -> Result<(), bool> {
let var_path = super::get_var_path();
let pkg_md_path = var_path.join(format!("{}/{}.md", repo, pkgname));
if !pkg_md_path.exists() {
upload_from_repo(&repo, &pkgname, &pkg_md_path)?;
}
check_build_dependency(&repo, &pkg_md_path)?;
check_run_dependency(&pkg_md_path)?;
download(&pkgname, &pkg_md_path)?;
let src_dir = PathBuf::from("/pkg/src").join(&pkgname);
build(&repo, &pkgname, &src_dir, &pkg_md_path)?;
pkglink(&repo, &pkgname).expect("Failed link package");
println!("Package {} installed successfully from repo {}", pkgname, repo);
Ok(())
}
pub fn install_all(pkgname: &String) {
let repos = match parser::get_repo_list() {
Ok(repos) => repos,
Err(e) => {
eprintln!("Failed to get repository list: {}", e);
return;
}
};
let mut success = false;
for repo in repos {
println!("Trying to install {} from repo {}...", pkgname, repo);
match install(&repo, pkgname) {
Ok(()) => {
success = true;
break;
}
Err(no_repo_package) => {
if no_repo_package {
continue;
} else {
process::exit(1)
}
}
}
}
if !success {
eprintln!("Package {} not found in any available repository", pkgname);
}
}
fn upload_from_repo(repo: &String, pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
match parser::get_repo_addr(repo) {
Ok(repo_addr) => {
let rsync_command = format!(
"rsync --include='{}.md' --exclude='*' {} {}",
pkgname,
repo_addr,
pkg_md_path.to_str().unwrap()
);
let rsync_output = Command::new("sh")
.arg("-c")
.arg(rsync_command)
.output()
.expect("Failed to execute rsync");
if !rsync_output.status.success() {
eprintln!("broken repo: {}", repo);
return Err(false);
}
if !pkg_md_path.exists() {
eprintln!("not found {} in {} repo", pkgname, repo);
return Err(true);
}
Ok(())
}
Err(e) => {
eprintln!("Repository {} not found: {}", repo, e);
return Err(true);
}
}
}
fn check_build_dependency(repo: &String, pkg_md_path: &Path) -> Result<(), bool> {
let deps = match parser::get_build_deps(&pkg_md_path) {
Ok(deps) => deps,
Err(_) => { return Ok(()) }
};
for dependency in deps.lines() {
if !dependency.trim().is_empty() {
if !Path::new("/pkg").join(repo).join(dependency).exists() {
match install(repo, &dependency.to_string()) {
Ok(()) => {}
Err(_) => {process::exit(1) }
}
}
}
}
Ok(())
}
fn check_run_dependency(pkg_md_path: &Path) -> Result<(), bool> {
let deps = match parser::get_run_deps(pkg_md_path) {
Ok(deps) => deps,
Err(_) => { return Ok(()) }
};
let repo_list = match parser::get_repo_list() {
Ok(repos) => repos,
Err(e) => {
eprintln!("Failed to get repository list: {}", e);
return Err(false)
}
};
for dependency in deps.split_whitespace() {
let mut found = false;
for repo_name in &repo_list {
let path = format!("/pkg/{}/{}/", repo_name, dependency);
if Path::new(&path).exists() {
found = true;
break;
}
}
if !found {
install_all(&dependency.to_string());
}
}
Ok(())
}
fn download(pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
let url = match parser::get_url(pkg_md_path) {
Ok(url) => url,
Err(e) => {
eprintln!("Failed to parse URL: {}", e);
return Err(false);
}
};
let src = PathBuf::from("/pkg/src").join(pkgname);
if let Err(e) = fs::create_dir_all(&src) {
eprintln!("Failed to create directory {}: {}", src.display(), e);
return Err(false);
}
if !url.ends_with(".git") {
let compress_flag = if url.ends_with(".bz2") {
"--bzip2"
} else if url.ends_with(".xz") {
"--xz"
} else if url.ends_with(".lz") {
"--lzip"
} else if url.ends_with(".lzma") {
"--lzma"
} else if url.ends_with(".lzo") {
"--lzop"
} else if url.ends_with(".zst") {
"--zstd"
} else if url.ends_with(".gz") {
"--gzip"
} else {
eprintln!("Unsupported compression format for URL: {}", url);
return Err(false);
};
let wget_output = Command::new("wget")
.arg("-O-")
.arg(&url)
.stdout(Stdio::piped())
.spawn();
let tar_input = match wget_output {
Ok(child) => child.stdout.unwrap(),
Err(e) => {
eprintln!("Failed to execute wget: {}", e);
return Err(false);
}
};
let tar_status = Command::new("tar")
.arg("-x")
.arg(compress_flag)
.arg("-C")
.arg(&src)
.stdin(tar_input)
.status();
if tar_status.is_err() || !tar_status.unwrap().success() {
eprintln!("Failed to extract archive from URL: {}", url);
return Err(false);
}
let entries = fs::read_dir(&src).unwrap();
let dirs: Vec<_> = entries
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_type().map_or(false, |ft| ft.is_dir()))
.collect();
if dirs.len() == 1 {
let single_dir = dirs[0].path();
for entry in fs::read_dir(&single_dir).unwrap() {
let entry = entry.unwrap();
let dest = src.join(entry.file_name());
fs::rename(entry.path(), dest).unwrap();
}
fs::remove_dir(single_dir).unwrap();
}
} else {
let git_status = Command::new("git")
.arg("clone")
.arg(&url)
.arg(&src)
.status();
if git_status.is_err() || !git_status.unwrap().success() {
eprintln!("Failed to clone git repository from URL: {}", url);
return Err(false);
}
}
Ok(())
}
fn build(
repo: &String,
pkgname: &String,
src_dir: &Path,
pkg_md_path: &Path,
) -> Result<(), bool> {
let build_script = match parser::get_build_script(pkg_md_path) {
Ok(script) => script,
Err(error) => {
eprintln!("Failed to parse build script: {}", error);
return Err(false);
}
};
let output = Command::new("zsh")
.arg("-c")
.arg(&build_script)
.current_dir(src_dir)
.output();
if let Err(e) = output {
eprintln!("Failed to execute build script: {}", e);
return Err(false);
}
let output = output.unwrap();
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("Script failed with error: {}", stderr);
return Err(false);
}
let dest_dir = PathBuf::from("/pkg").join(repo).join(pkgname);
if let Err(e) = fs::create_dir_all(&dest_dir) {
eprintln!("Failed to create destination directory: {}", e);
return Err(false);
}
let dest_path = dest_dir.join("build-script.md");
if let Err(e) = fs::copy(pkg_md_path, &dest_path) {
eprintln!("Failed to copy build script to destination: {}", e);
return Err(false);
}
config(&src_dir, &pkg_md_path)?;
if let Err(e) = fs::remove_dir_all(src_dir) {
eprintln!("Failed to remove source directory: {}", e);
return Err(false);
}
Ok(())
}
fn config(
src_dir: &Path,
pkg_md_path: &Path,
) -> Result<(), bool> {
let config_script = match parser::get_config_script(pkg_md_path) {
Ok(script) => script,
Err(_) => { return Ok(()) }
};
let output = Command::new("zsh")
.arg("-c")
.arg(&config_script)
.current_dir(src_dir)
.output();
if let Err(e) = output {
eprintln!("Failed to execute config script: {}", e);
return Err(false);
}
let output = output.unwrap();
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("Script failed with error: {}", stderr);
return Err(false);
}
Ok(())
}