Files
AeroPkg/src/commands/run/download.rs

174 lines
5.3 KiB
Rust
Raw Normal View History

2025-11-18 19:46:36 +03:00
use std::fs;
use std::path::Path;
use std::process::{Command, Stdio};
use crate::utils::parser::{self, env::get_install_env};
use crate::utils::shell::run_install_script_hook;
pub fn download(repo: &String, pkgname: &String) {
let pkg_md_path = &crate::commands::get_aeropkg_var().join(format!("{}/{}.md", repo, pkgname));
if !pkg_md_path.exists() { upload_from_repo(repo, pkgname, pkg_md_path) }
2025-11-18 19:46:36 +03:00
let full_env = get_install_env(repo, pkgname, pkg_md_path, "download");
let url = match parser::pkginfo::get_url(pkg_md_path) {
Ok(url) => url,
Err(e) => {
panic!("Failed to parse URL: {}", e)
2025-11-18 19:46:36 +03:00
}
};
let src = crate::commands::get_aeropkg_base().join("src").join(pkgname);
if src.exists() {
let src_url = fs::read_to_string(src.join("aeropkg.download-url")).unwrap_or("".to_string());
if url == src_url { return }
else {
fs::remove_dir_all(&src).unwrap()
2025-11-18 19:46:36 +03:00
}
}
if url.ends_with(".git") || url.starts_with("git://") {
let git_status = Command::new("git")
.arg("clone")
.arg(&url)
.arg(&src)
.status();
if git_status.is_err() || !git_status.unwrap().success() {
panic!("Failed to clone git repository from URL: {}", url)
}
}
else if url.is_empty() { return }
else if url.ends_with(".zip") {
if let Err(e) = fs::create_dir_all(&src) {
panic!("Failed to create directory {}: {}", &src.display(), e)
}
2025-11-18 19:46:36 +03:00
let zip_path = src.join("archive.zip");
let wget_status = Command::new("wget")
.arg("-O")
.arg(&zip_path)
.arg("-q")
.arg("--show-progress")
.arg(&url)
.envs(&full_env)
.status();
if wget_status.is_err() || !wget_status.unwrap().success() {
panic!("Failed to download zip archive from URL: {}", url)
}
let output_flag = format!("-o{}", src.display());
let sevenz_status = Command::new("7z")
.arg("x")
.arg(&zip_path)
.arg(output_flag)
.arg("-y")
.arg("-bd")
.arg("-bso0")
.arg("-bse0")
.arg("-bsp0")
.envs(&full_env)
.status();
if sevenz_status.is_err() || !sevenz_status.unwrap().success() {
panic!("Failed to extract zip archive from URL: {}", url)
}
fs::remove_file(&zip_path).ok();
}
else {
2025-11-18 19:46:36 +03:00
if let Err(e) = fs::create_dir_all(&src) {
panic!("Failed to create directory {}: {}", &src.display(), e)
2025-11-18 19:46:36 +03:00
}
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 {
panic!("Unsupported compression format for URL: {}", url)
2025-11-18 19:46:36 +03:00
};
let wget_output = Command::new("wget")
.arg("-O-")
.arg("-q")
.arg("--show-progress")
.arg(&url)
.envs(&full_env)
.stdout(Stdio::piped())
.spawn();
let tar_input = match wget_output {
Ok(child) => child.stdout.unwrap(),
Err(e) => { panic!("Failed to execute wget: {}", e) }
2025-11-18 19:46:36 +03:00
};
let tar_status = Command::new("tar")
.arg("-x")
.arg(compress_flag)
.arg("-C")
.arg(&src)
.envs(&full_env)
.stdin(tar_input)
.status();
if tar_status.is_err() || !tar_status.unwrap().success() {
panic!("Failed to extract archive from URL: {}", url)
2025-11-18 19:46:36 +03:00
}
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()
2025-11-18 19:46:36 +03:00
}
}
fs::write(src.join("aeropkg.download-url"), &url).unwrap();
run_install_script_hook(repo, "download", &full_env)
2025-11-18 19:46:36 +03:00
}
fn upload_from_repo(repo: &String, pkgname: &String, pkg_md_path: &Path) {
2025-11-18 19:46:36 +03:00
let repo_addr = parser::repoinfo::get_repo_addr(repo);
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() {
panic!("broken repo: {}", repo)
2025-11-18 19:46:36 +03:00
}
if !pkg_md_path.exists() {
panic!("not found {} in {} repo", pkgname, repo)
2025-11-18 19:46:36 +03:00
}
}