update install module
This commit is contained in:
@ -15,13 +15,28 @@ pub fn install(repo: &String, pkgname: &String) -> Result<(), bool> {
|
||||
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);
|
||||
patch(&pkgname, &src_dir, &pkg_md_path)?;
|
||||
build(&repo, &pkgname, &src_dir, &pkg_md_path)?;
|
||||
pkglink(&repo, &pkgname).expect("Failed link package");
|
||||
config(&src_dir, &pkg_md_path)?;
|
||||
|
||||
let src_remove_flag = match std::env::var("pkg_src_remove") {
|
||||
Ok(value) => value != "false",
|
||||
Err(_) => true,
|
||||
};
|
||||
|
||||
if src_remove_flag {
|
||||
if let Err(e) = fs::remove_dir_all(&src_dir) {
|
||||
eprintln!("Failed to remove source directory: {}", e);
|
||||
return Err(false);
|
||||
}
|
||||
}
|
||||
|
||||
println!("Package {} installed successfully from repo {}", pkgname, repo);
|
||||
Ok(())
|
||||
@ -157,9 +172,19 @@ fn download(pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
|
||||
};
|
||||
|
||||
let src = PathBuf::from("/pkg/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 Ok(())
|
||||
} else {
|
||||
println!("url:\n{}\nend url", url);
|
||||
println!("src url:\n{}\nend src url", src_url);
|
||||
fs::remove_dir_all(&src).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = fs::create_dir_all(&src) {
|
||||
eprintln!("Failed to create directory {}: {}", src.display(), e);
|
||||
eprintln!("Failed to create directory {}: {}", &src.display(), e);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
@ -185,6 +210,8 @@ fn download(pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
|
||||
|
||||
let wget_output = Command::new("wget")
|
||||
.arg("-O-")
|
||||
.arg("-q")
|
||||
.arg("--show-progress")
|
||||
.arg(&url)
|
||||
.stdout(Stdio::piped())
|
||||
.spawn();
|
||||
@ -240,10 +267,57 @@ fn download(pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
|
||||
}
|
||||
}
|
||||
|
||||
fs::write(src.join("aeropkg.download-url"), &url).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn patch(
|
||||
pkgname: &String,
|
||||
src_dir: &Path,
|
||||
pkg_md_path: &Path,
|
||||
) -> Result<(), bool> {
|
||||
let patch_script = parser::get_patch_script(pkg_md_path).unwrap_or("".to_string());
|
||||
|
||||
if src_dir.join("aeropkg.applied-patch").exists() {
|
||||
let src_patch = fs::read_to_string(src_dir.join("aeropkg.applied-patch")).unwrap_or("".to_string());
|
||||
if patch_script == src_patch {
|
||||
return Ok(())
|
||||
} else {
|
||||
println!("patch:\n{}\nend patch", patch_script);
|
||||
println!("src patch:\n{}\nend src patch", src_patch);
|
||||
fs::remove_dir_all(src_dir).unwrap();
|
||||
download(&pkgname, &pkg_md_path)?;
|
||||
}
|
||||
}
|
||||
|
||||
if patch_script == "" { return Ok(()) }
|
||||
let output = Command::new("zsh")
|
||||
.arg("-c")
|
||||
.arg(&patch_script)
|
||||
.current_dir(src_dir)
|
||||
.output();
|
||||
|
||||
if let Err(e) = output {
|
||||
eprintln!("Failed to execute patch 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);
|
||||
}
|
||||
|
||||
fs::write(src_dir.join("aeropkg.applied-patch"), &patch_script).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn build(
|
||||
repo: &String,
|
||||
pkgname: &String,
|
||||
@ -288,19 +362,13 @@ fn build(
|
||||
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,
|
||||
pkg_md_path: &Path
|
||||
) -> Result<(), bool> {
|
||||
let config_script = match parser::get_config_script(pkg_md_path) {
|
||||
Ok(script) => script,
|
||||
|
@ -55,6 +55,11 @@ pub fn get_config_script<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
extract_block(file_path, "``` sh *** config ***", "```")
|
||||
}
|
||||
|
||||
pub fn get_patch_script<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
extract_block(file_path, "``` sh *** config ***", "```")
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn get_repo_list() -> io::Result<Vec<String>> {
|
||||
let file_path = crate::commands::get_var_path().join("sexpkg.md");
|
||||
|
Reference in New Issue
Block a user