before index-conflict changes

This commit is contained in:
pivodevat
2025-11-18 19:46:36 +03:00
parent 28ba2135ec
commit 30540602bd
66 changed files with 1403 additions and 879 deletions

93
src/commands/run/build.rs Normal file
View File

@ -0,0 +1,93 @@
use std::fs;
use std::os::unix::fs::MetadataExt;
use crate::utils::parser::{self, env::get_install_env};
use crate::utils::shell::{run_install_script,run_install_script_hook};
use crate::commands::link::link;
use super::download::download;
use super::patch::patch;
pub fn build(
repo: &String,
pkgname: &String,
) -> Result<(), bool> {
let src_dir = &crate::commands::get_aeropkg_base().join("src").join(&pkgname);
let pkg_md_path = &crate::commands::get_var_path().join(format!("{}/{}.md", &repo, &pkgname));
let builded_pkg_md_path = crate::commands::get_aeropkg_base().join(&repo).join(&pkgname).join("build-script.md");
if builded_pkg_md_path.exists() {
if fs::metadata(&builded_pkg_md_path).unwrap().ino() == fs::metadata(&pkg_md_path).unwrap().ino() {
println!("package {} already installed in {} repo", &pkgname, &repo);
return Ok(())
}
}
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);
}
};
if src_dir.join("aeropkg.applied-build").exists() {
let src_build = fs::read_to_string(src_dir.join("aeropkg.applied-build")).unwrap_or("".to_string());
if build_script == src_build {
return Ok(())
} else {
println!("build:\n{}\nend build", build_script);
println!("src build:\n{}\nend src build", src_build);
fs::remove_dir_all(src_dir).unwrap();
download(&repo, &pkgname)?;
patch(&repo, &pkgname)?;
}
}
let full_env = get_install_env(repo, pkgname, pkg_md_path, "build");
run_install_script(&build_script, src_dir, &full_env)?;
run_install_script_hook(repo, "build", &full_env)?;
let dest_dir = crate::commands::get_aeropkg_base().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");
fs::remove_file(&dest_path).ok();
if let Err(e) = fs::hard_link(pkg_md_path, &dest_path) {
eprintln!("Failed to copy build script to destination: {}", e);
return Err(false);
}
fs::write(src_dir.join("aeropkg.build-script"), &build_script).unwrap();
let link_flag = full_env.get("disable").map_or(true, |v| v != "true");
if !link_flag {
let _ = fs::File::create(crate::commands::get_aeropkg_base().join(repo).join(pkgname).join("disabled"));
}
link(&repo, &pkgname).expect("Failed link package");
let save_source_flag = full_env.get("save_source").map_or(false, |v| v == "true");
if !save_source_flag {
if let Err(e) = fs::remove_dir_all(&src_dir) {
eprintln!("Failed to remove source directory: {}", e);
return Err(false);
}
}
hook(&repo, &pkgname);
Ok(())
}
fn hook(repo: &String, pkgname: &String) {
let pkg_dir = crate::commands::get_aeropkg_base().join(&repo).join(&pkgname);
let lib_dir = &pkg_dir.join("lib");
let lib64_dir = &pkg_dir.join("lib64");
let lib32_dir = &pkg_dir.join("lib32");
crate::utils::fs::mv::mv(&lib64_dir, &lib_dir).ok();
crate::utils::fs::mv::mv(&lib32_dir, &lib_dir).ok();
fs::remove_dir_all(&lib64_dir).ok();
fs::remove_dir_all(&lib32_dir).ok();
}

View File

@ -0,0 +1,25 @@
use crate::commands::link::link;
use crate::utils::parser::{self, env::get_install_env};
use crate::utils::shell::{run_install_script,run_install_script_hook};
pub fn config(
repo: &String,
pkgname: &String
) -> Result<(), bool> {
let src_dir = &crate::commands::get_aeropkg_base().join("src").join(&pkgname);
let pkg_md_path = &crate::commands::get_var_path().join(format!("{}/{}.md", &repo, &pkgname));
let config_script = match parser::get_config_script(pkg_md_path) {
Ok(script) => script,
Err(_) => { return Ok(()) }
};
let full_env = get_install_env(repo, pkgname, pkg_md_path, "config");
run_install_script(&config_script, src_dir, &full_env)?;
run_install_script_hook(repo, "config", &full_env)?;
link(&repo, &pkgname).expect("Failed link package");
Ok(())
}

View File

@ -0,0 +1,15 @@
use crate::utils::parser::{self, env::get_custom_env};
use crate::utils::shell::run_install_script;
pub fn custom(scriptname: &String, repo: &String, pkgname: &String) -> Result<(), bool> {
let src_dir = &crate::commands::get_aeropkg_base().join("src").join(&pkgname);
let pkg_md_path = &crate::commands::get_var_path().join(format!("{}/{}.md", &repo, &pkgname));
let config_script = parser::get_custom_script(pkg_md_path, scriptname);
let env = get_custom_env(repo, pkgname, pkg_md_path);
run_install_script(&config_script, src_dir, &env)?;
Ok(())
}

View File

@ -0,0 +1,149 @@
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) -> Result<(), bool> {
let pkg_md_path = &crate::commands::get_var_path().join(format!("{}/{}.md", &repo, &pkgname));
if !pkg_md_path.exists() { upload_from_repo(&repo, &pkgname, &pkg_md_path)? }
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) => {
eprintln!("Failed to parse URL: {}", e);
return Err(false);
}
};
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 Ok(())
} else {
fs::remove_dir_all(&src).unwrap();
}
}
if !url.ends_with(".git") {
if let Err(e) = fs::create_dir_all(&src) {
eprintln!("Failed to create directory {}: {}", &src.display(), e);
return Err(false);
}
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("-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) => {
eprintln!("Failed to execute wget: {}", e);
return Err(false);
}
};
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() {
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);
}
}
fs::write(src.join("aeropkg.download-url"), &url).unwrap();
run_install_script_hook(repo, "download", &full_env)?;
Ok(())
}
fn upload_from_repo(repo: &String, pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
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() {
eprintln!("broken repo: {}", repo);
return Err(false);
}
if !pkg_md_path.exists() {
eprintln!("not found {} in {} repo", pkgname, repo);
return Err(true);
}
Ok(())
}

6
src/commands/run/mod.rs Normal file
View File

@ -0,0 +1,6 @@
pub mod run;
pub mod download;
pub mod patch;
pub mod build;
pub mod config;
pub mod custom;

32
src/commands/run/patch.rs Normal file
View File

@ -0,0 +1,32 @@
use std::fs;
use crate::utils::parser::{self, env::get_install_env};
use crate::utils::shell::{run_install_script,run_install_script_hook};
use super::download::download;
pub fn patch(
repo: &String,
pkgname: &String,
) -> Result<(), bool> {
let src_dir = &crate::commands::get_aeropkg_base().join("src").join(&pkgname);
let pkg_md_path = &crate::commands::get_var_path().join(format!("{}/{}.md", &repo, &pkgname));
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 {
fs::remove_dir_all(src_dir).unwrap();
download(&repo, &pkgname)?;
}
}
if patch_script == "" { return Ok(()) }
let full_env = get_install_env(repo, pkgname, pkg_md_path, "patch");
run_install_script(patch_script, src_dir, &full_env)?;
run_install_script_hook(repo, "patch", &full_env)?;
fs::write(src_dir.join("aeropkg.applied-patch"), &patch_script).unwrap();
Ok(())
}

14
src/commands/run/run.rs Normal file
View File

@ -0,0 +1,14 @@
use super::download::download;
use super::patch::patch;
use super::build::build;
use super::config::config;
pub fn run(script: &String, repo: &String, pkgname: &String) -> Result<(), bool> {
if script == "download" { download(&repo, &pkgname)?; return Ok(()) }
if script == "patch" { patch(&repo, &pkgname)?; return Ok(()) }
if script == "build" { build(&repo, &pkgname)?; return Ok(()) }
if script == "config" { config(&repo, &pkgname)?; return Ok(()) }
Ok(())
}