pkg download command and patches
This commit is contained in:
178
src/commands/download.rs
Normal file
178
src/commands/download.rs
Normal file
@ -0,0 +1,178 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::io::{self, Write};
|
||||
use std::cell::RefCell;
|
||||
|
||||
use super::get_aeropkg_base;
|
||||
use crate::utils::parser::{self, env::get_install_env, pkginfo, repoinfo};
|
||||
use crate::commands::link::link;
|
||||
|
||||
#[derive(Default)]
|
||||
struct DownloadSummary {
|
||||
no_changes: Vec<String>,
|
||||
updated: Vec<(String, u64)>,
|
||||
errors: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static SUMMARY: RefCell<DownloadSummary> = RefCell::new(DownloadSummary::default());
|
||||
}
|
||||
|
||||
pub fn print_download_summary() {
|
||||
print!("\r\x1B[K");
|
||||
io::stdout().flush().unwrap();
|
||||
|
||||
SUMMARY.with(|s| {
|
||||
let summary = s.borrow();
|
||||
|
||||
if !summary.no_changes.is_empty() {
|
||||
println!("No changes: {}", summary.no_changes.join(", "));
|
||||
}
|
||||
|
||||
if !summary.updated.is_empty() {
|
||||
println!("Updated:");
|
||||
for (pkg, count) in &summary.updated {
|
||||
println!(" {}: {} files", pkg, count);
|
||||
}
|
||||
}
|
||||
|
||||
if !summary.errors.is_empty() {
|
||||
eprintln!("Errors:");
|
||||
for (pkg, err) in &summary.errors {
|
||||
eprintln!(" {}: {}", pkg, err);
|
||||
}
|
||||
}
|
||||
|
||||
drop(summary);
|
||||
s.replace(DownloadSummary::default());
|
||||
});
|
||||
}
|
||||
|
||||
pub fn download_packages(
|
||||
repo: Option<&str>,
|
||||
packages: &[String],
|
||||
force: bool,
|
||||
recursive: bool,
|
||||
) {
|
||||
for pkgname in packages {
|
||||
let effective_repo = match repo {
|
||||
Some(r) => r.to_string(),
|
||||
None => pkginfo::get_priority_repo(pkgname),
|
||||
};
|
||||
|
||||
download(&effective_repo, pkgname, force, recursive);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn download(repo: &String, pkgname: &String, force: bool, recursive: bool) {
|
||||
let pkg_md_path = &crate::commands::get_aeropkg_var().join(format!("{}/{}.md", repo, pkgname));
|
||||
|
||||
check_build_dependency(repo, pkg_md_path, force, recursive);
|
||||
check_run_dependency(pkg_md_path, force, recursive);
|
||||
|
||||
let source = repoinfo::get_download_source(repo);
|
||||
|
||||
if source.is_empty() {
|
||||
let msg = format!("Download source not found for repo '{}'", repo);
|
||||
SUMMARY.with(|s| s.borrow_mut().errors.push((pkgname.clone(), msg)));
|
||||
return;
|
||||
}
|
||||
|
||||
let remote_path = format!("{}/{}/", source.trim_end_matches('/'), pkgname);
|
||||
let local_path = format!("/pkg/{}/{}", repo, pkgname);
|
||||
|
||||
if let Err(e) = std::fs::create_dir_all(&local_path) {
|
||||
let msg = format!("Failed to create directory {}: {}", local_path, e);
|
||||
SUMMARY.with(|s| s.borrow_mut().errors.push((pkgname.clone(), msg)));
|
||||
return;
|
||||
}
|
||||
let local_path_sync = format!("{}/", local_path);
|
||||
|
||||
print!("\r\x1B[KDownloading {}", pkgname);
|
||||
io::stdout().flush().unwrap();
|
||||
|
||||
let mut cmd = Command::new("rsync");
|
||||
cmd.arg("-azHX").arg("--stats");
|
||||
if force { cmd.arg("--delete"); }
|
||||
cmd.arg(&remote_path);
|
||||
cmd.arg(&local_path_sync);
|
||||
|
||||
let result = cmd.output();
|
||||
|
||||
match result {
|
||||
Ok(output) => {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
let transferred = stdout
|
||||
.lines()
|
||||
.find(|line| line.contains("Number of regular files transferred:"))
|
||||
.and_then(|line| line.split(':').nth(1))
|
||||
.map(|s| s.trim().parse::<u64>().unwrap_or(0))
|
||||
.unwrap_or(0);
|
||||
|
||||
if transferred == 0 && output.status.success() {
|
||||
SUMMARY.with(|s| s.borrow_mut().no_changes.push(pkgname.clone()));
|
||||
} else if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let err_msg = stderr.lines().rev().find(|l| !l.trim().is_empty()).unwrap_or("unknown error").to_string();
|
||||
SUMMARY.with(|s| s.borrow_mut().errors.push((pkgname.clone(), err_msg)));
|
||||
} else {
|
||||
SUMMARY.with(|s| s.borrow_mut().updated.push((pkgname.clone(), transferred)));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
SUMMARY.with(|s| s.borrow_mut().errors.push((pkgname.clone(), e.to_string())));
|
||||
}
|
||||
}
|
||||
|
||||
let full_env = get_install_env(repo, pkgname, pkg_md_path, "build");
|
||||
if full_env.get("disable").is_some() {
|
||||
let _ = fs::File::create(crate::commands::get_aeropkg_base().join(repo).join(pkgname).join("disabled"));
|
||||
} else {
|
||||
link(repo, pkgname)
|
||||
}
|
||||
}
|
||||
|
||||
fn check_build_dependency(repo: &String, pkg_md_path: &Path, force: bool, recursive: bool) {
|
||||
let deps = match parser::get_build_deps(pkg_md_path) {
|
||||
Ok(deps) => deps,
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
for dep in deps.split_whitespace() {
|
||||
let dep = dep.trim();
|
||||
if dep.is_empty() { continue }
|
||||
|
||||
if !get_aeropkg_base().join(repo).join(dep).exists() || recursive {
|
||||
download(repo, &dep.to_string(), force, recursive);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_run_dependency(pkg_md_path: &Path, force: bool, recursive: bool) {
|
||||
let deps = match parser::get_run_deps(pkg_md_path) {
|
||||
Ok(deps) => deps,
|
||||
Err(_) => return,
|
||||
};
|
||||
let repo_list = parser::repoinfo::get_repo_list();
|
||||
|
||||
for dep in deps.split_whitespace() {
|
||||
let dep = dep.trim();
|
||||
if dep.is_empty() { continue }
|
||||
|
||||
let found_repo = repo_list.iter().find(|repo_name| {
|
||||
get_aeropkg_base().join(repo_name).join(dep).exists()
|
||||
});
|
||||
|
||||
match found_repo {
|
||||
Some(found) if recursive => {
|
||||
download(found, &dep.to_string(), force, recursive);
|
||||
}
|
||||
None => {
|
||||
download(&pkginfo::get_priority_repo(&dep.to_string()), &dep.to_string(), force, recursive);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
use crate::utils::fs::hardcopy::hardcopy_handler;
|
||||
use crate::utils::shell::*;
|
||||
use crate::utils::parser::env::get_install_env;
|
||||
|
||||
pub fn link(repo: &String, pkgname: &String) {
|
||||
let source = crate::commands::get_aeropkg_base().join(repo).join(pkgname);
|
||||
@ -26,6 +27,12 @@ pub fn link(repo: &String, pkgname: &String) {
|
||||
}
|
||||
}
|
||||
|
||||
mount_overlay();
|
||||
let builded_pkg_md_path = &crate::commands::get_aeropkg_base().join(repo).join(pkgname).join("build-script.md");
|
||||
let full_env = get_install_env(repo, pkgname, builded_pkg_md_path, "link");
|
||||
|
||||
let mount_usr_dir_flag = full_env.get("mount_usr_dir").is_some();
|
||||
if mount_usr_dir_flag {
|
||||
mount_overlay();
|
||||
}
|
||||
shell_update()
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ pub mod disable;
|
||||
pub mod enable;
|
||||
pub mod trim;
|
||||
pub mod run;
|
||||
pub mod download;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
||||
@ -31,8 +31,8 @@ pub fn build(repo: &String, pkgname: &String) {
|
||||
if build_script == builded_script { return }
|
||||
}
|
||||
|
||||
if src.join("aeropkg.applied-build").exists() {
|
||||
let src_build = fs::read_to_string(src.join("aeropkg.applied-build")).unwrap_or("".to_string());
|
||||
if src.join(".aeropkg.applied-build").exists() {
|
||||
let src_build = fs::read_to_string(src.join(".aeropkg.applied-build")).unwrap_or("".to_string());
|
||||
if build_script == src_build {
|
||||
return
|
||||
} else {
|
||||
@ -51,21 +51,21 @@ pub fn build(repo: &String, pkgname: &String) {
|
||||
let dest_path = &dest_dir.join("build-script.md");
|
||||
fs::remove_file(dest_path).ok();
|
||||
if let Err(e) = fs::hard_link(pkg_md_path, dest_path) { panic!("Failed to copy build script to destination: {}", e) }
|
||||
fs::write(src.join("aeropkg.build-script"), &build_script).unwrap();
|
||||
fs::write(src.join(".aeropkg.applied-build"), &build_script).unwrap();
|
||||
|
||||
|
||||
let save_source_flag = full_env.get("save_source").map_or(false, |v| v == "true");
|
||||
let save_source_flag = full_env.get("save_source").is_some();
|
||||
if !save_source_flag {
|
||||
if let Err(e) = fs::remove_dir_all(&src) { panic!("Failed to remove source directory: {}", e) }
|
||||
}
|
||||
|
||||
hook(repo, pkgname);
|
||||
|
||||
let link_flag = full_env.get("disable").map_or(true, |v| v != "true");
|
||||
if !link_flag {
|
||||
if !full_env.get("disable").is_some() {
|
||||
let _ = fs::File::create(crate::commands::get_aeropkg_base().join(repo).join(pkgname).join("disabled"));
|
||||
} else {
|
||||
link(repo, pkgname)
|
||||
}
|
||||
link(repo, pkgname)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@ pub fn download(repo: &String, pkgname: &String) {
|
||||
|
||||
let src = pkgpath.join("src");
|
||||
if src.exists() {
|
||||
let src_url = fs::read_to_string(src.join("aeropkg.download-url")).unwrap_or("".to_string());
|
||||
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()
|
||||
@ -137,7 +137,10 @@ pub fn download(repo: &String, pkgname: &String) {
|
||||
.collect();
|
||||
|
||||
if dirs.len() == 1 {
|
||||
let single_dir = dirs[0].path();
|
||||
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();
|
||||
@ -149,7 +152,7 @@ pub fn download(repo: &String, pkgname: &String) {
|
||||
}
|
||||
}
|
||||
|
||||
fs::write(src.join("aeropkg.download-url"), &url).unwrap();
|
||||
fs::write(src.join(".aeropkg.download-url"), &url).unwrap();
|
||||
|
||||
run_install_script_hook(repo, "download", &full_env)
|
||||
}
|
||||
|
||||
@ -11,8 +11,8 @@ pub fn patch(repo: &String, pkgname: &String) {
|
||||
|
||||
let pkg_md_path = &crate::commands::get_aeropkg_var().join(format!("{}/{}.md", repo, pkgname));
|
||||
let patch_script = &parser::get_patch_script(pkg_md_path).unwrap_or("".to_string());
|
||||
if src.join("aeropkg.applied-patch").exists() {
|
||||
let src_patch = &fs::read_to_string(src.join("aeropkg.applied-patch")).unwrap_or("".to_string());
|
||||
if src.join(".aeropkg.applied-patch").exists() {
|
||||
let src_patch = &fs::read_to_string(src.join(".aeropkg.applied-patch")).unwrap_or("".to_string());
|
||||
if patch_script == src_patch { return }
|
||||
else if src_patch != "" {
|
||||
fs::remove_dir_all(src).unwrap();
|
||||
@ -23,5 +23,5 @@ pub fn patch(repo: &String, pkgname: &String) {
|
||||
let full_env = get_install_env(repo, pkgname, pkg_md_path, "patch");
|
||||
run_install_script(patch_script, src, &full_env);
|
||||
run_install_script_hook(repo, "patch", &full_env);
|
||||
fs::write(src.join("aeropkg.applied-patch"), &patch_script).unwrap()
|
||||
fs::write(src.join(".aeropkg.applied-patch"), &patch_script).unwrap()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user