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; 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) } let url = match parser::pkginfo::get_url(pkg_md_path) { Ok(url) => url, Err(e) => { panic!("Failed to parse URL: {}", e) } }; let pkgpath = &crate::commands::get_aeropkg_base().join(repo).join(pkgname); fs::create_dir_all(&pkgpath).ok(); let src = pkgpath.join("src"); 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() } } let pkgdata = crate::commands::get_aeropkg_var().join(repo).join(pkgname).join(".pkgdata.patch"); if pkgdata.exists() { let status = Command::new("cp").args(["-rPl", &format!("{}/.", pkgdata.to_string_lossy()), &src.to_string_lossy()]).status(); if let Err(e) = status { eprintln!("Warning: failed to copy .pkgdata for {}/{}: {}", repo, pkgname, e) } } let full_env = get_install_env(repo, pkgname, pkg_md_path, "download"); if url.ends_with(".git") || url.starts_with("git://") { let git_status = Command::new("git") .current_dir(&pkgpath) .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") || url.ends_with(".7z") { let ext = if url.ends_with(".zip") { "zip" } else { "7z" }; if let Err(e) = fs::create_dir_all(&src) { panic!("Failed to create directory {}: {}", &src.display(), e) } let archive_path = src.join(format!("archive.{}", ext)); let wget_status = Command::new("wget") .arg("-O") .arg(&archive_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(&archive_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(&archive_path).ok(); } else { if let Err(e) = fs::create_dir_all(&src) { panic!("Failed to create directory {}: {}", &src.display(), e) } 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) }; 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) } }; 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) } 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 old_path = dirs[0].path(); let single_dir = old_path.parent().unwrap() .join(format!("{}.aeropkg.bkp", old_path.file_name().unwrap().to_string_lossy())); fs::rename(&old_path, &single_dir).unwrap(); 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() } } fs::write(src.join(".aeropkg.download-url"), &url).unwrap(); shell::run_install_repo_stage_hook_script(repo, "download", &full_env); shell::run_install_stage_hook_script("download", &full_env); } fn upload_from_repo(repo: &String, pkgname: &String, pkg_md_path: &Path) { 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) } if !pkg_md_path.exists() { panic!("not found {} in {} repo", pkgname, repo) } }