updated build-script format

This commit is contained in:
2025-08-23 07:04:59 +03:00
parent 43a8696619
commit eeb4377bb3
7 changed files with 306 additions and 113 deletions

View File

@ -1,48 +1,21 @@
use std::fs;
use std::io;
use std::path::Path;
use rayon::prelude::*;
use std::os::unix::fs::MetadataExt;
use super::get_var_path;
use std::path::{Path,PathBuf};
use crate::utils::parser;
pub fn delete(repo: &str, pkgname: &str) {
let base_dir = Path::new("/pkg").join(repo);
let pkg_dir = base_dir.join(pkgname);
if pkg_dir.exists() {
match fs::remove_dir_all(&pkg_dir) {
Ok(()) => { println!("removed: {}", pkg_dir.display()) }
Err(e) => { eprintln!("Can't remove {}: {}", pkg_dir.display(), e)}
}
} else {
eprintln!("{} not installed in {}", pkgname, repo)
}
let subdirs = ["bin", "lib", "libexec", "include", "share"];
for subdir in &subdirs {
let dir_path = base_dir.join(subdir);
if dir_path.exists() {
match remove_unused_files(&dir_path) {
Ok(()) => {}
Err(_) => {}
}
}
}
pub fn delete(repo: &String, pkgname: &String) {
super::disable::disable(&repo, &pkgname).unwrap();
fs::remove_dir_all(PathBuf::from("/pkg").join(&repo).join(&pkgname)).unwrap();
}
pub fn delete_recursive(repo: &str, pkgname: &str) {
let base_dir = Path::new("/pkg").join(repo);
let pkg_dir = base_dir.join(pkgname);
pub fn delete_recursive(repo: &String, pkgname: &String) {
let pkg_dir = Path::new("/pkg").join(repo).join(pkgname);
if pkg_dir.exists() {
let var_path = get_var_path();
let var_path = super::get_var_path();
let pkg_md_path = var_path.join(format!("{}/{}.md", repo, pkgname));
match parser::get_deps(&pkg_md_path) {
match parser::get_build_deps(&pkg_md_path) {
Ok(deps) => {
for dependency in deps.lines() {
let dependency = dependency.trim();
@ -61,72 +34,9 @@ pub fn delete_recursive(repo: &str, pkgname: &str) {
}
}
match fs::remove_dir_all(&pkg_dir) {
Ok(()) => { println!("removed: {}", pkg_dir.display()) }
Err(e) => { eprintln!("Can't remove {}: {}", pkg_dir.display(), e)}
}
fs::remove_dir_all(&pkg_dir).unwrap();
} else {
eprintln!("{} not installed in {}", pkgname, repo)
}
let subdirs = ["lib", "include", "bin"];
for subdir in &subdirs {
let dir_path = base_dir.join(subdir);
if dir_path.exists() {
match remove_unused_files(&dir_path) {
Ok(()) => {}
Err(_) => {}
}
match remove_unused_dirs_and_symlink(&dir_path) {
Ok(()) => {}
Err(_) => {}
}
}
}
}
fn remove_unused_files(path: &Path) -> io::Result<()> {
let metadata = fs::symlink_metadata(path)?;
if metadata.file_type().is_file() {
if get_nlink(path)? == 1 {
fs::remove_file(path)?
}
} else if metadata.file_type().is_dir() {
let entries: Vec<_> = fs::read_dir(path)?.collect::<io::Result<Vec<_>>>()?;
entries.par_iter().try_for_each(|entry| {
remove_unused_files(&entry.path())
})?;
}
Ok(())
}
fn remove_unused_dirs_and_symlink(path: &Path) -> io::Result<()> {
let metadata = fs::symlink_metadata(path)?;
if metadata.file_type().is_dir() {
let entries: Vec<_> = fs::read_dir(path)?.collect::<io::Result<Vec<_>>>()?;
entries.par_iter().try_for_each(|entry| {
remove_unused_dirs_and_symlink(&entry.path())
})?;
if fs::read_dir(path)?.next().is_none() {
fs::remove_dir(path)?
}
} else if metadata.file_type().is_symlink() {
if let Err(_) = fs::metadata(path) {
fs::remove_file(path)?
}
}
Ok(())
}
fn get_nlink(path: &Path) -> io::Result<u64> {
Ok(fs::metadata(path)?.nlink())
}

View File

@ -3,20 +3,20 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::process;
use super::*;
use crate::utils::parser;
use crate::commands::pkglink::pkglink;
pub fn install(repo: &String, pkgname: &String) -> Result<(), bool> {
let var_path = get_var_path();
let var_path = super::get_var_path();
let pkg_md_path = var_path.join(format!("{}/{}.md", repo, pkgname));
if !pkg_md_path.exists() {
upload_from_repo(&repo, &pkgname, &pkg_md_path)?;
}
check_dependency(&repo, &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);
@ -94,8 +94,8 @@ fn upload_from_repo(repo: &String, pkgname: &String, pkg_md_path: &Path) -> Resu
}
}
fn check_dependency(repo: &String, pkg_md_path: &Path) -> Result<(), bool> {
let deps = match parser::get_deps(&pkg_md_path) {
fn check_build_dependency(repo: &String, pkg_md_path: &Path) -> Result<(), bool> {
let deps = match parser::get_build_deps(&pkg_md_path) {
Ok(deps) => deps,
Err(e) => {
eprintln!("Failed to parse dependencies {}: {}", &pkg_md_path.to_str().unwrap(), e);
@ -117,6 +117,41 @@ fn check_dependency(repo: &String, pkg_md_path: &Path) -> Result<(), bool> {
Ok(())
}
fn check_run_dependency(pkg_md_path: &Path) -> Result<(), bool> {
let deps = match parser::get_run_deps(pkg_md_path) {
Ok(deps) => deps,
Err(e) => {
eprintln!("Failed to parse dependencies {}: {}", pkg_md_path.display(), e);
return Err(false);
}
};
let repo_list = match parser::get_repo_list() {
Ok(repos) => repos,
Err(e) => {
eprintln!("Failed to get repository list: {}", e);
return Err(false)
}
};
for dependency in deps.split_whitespace() {
let mut found = false;
for repo_name in &repo_list {
let path = format!("/pkg/{}/{}/", repo_name, dependency);
if Path::new(&path).exists() {
found = true;
break;
}
}
if !found {
install_all(&dependency.to_string());
}
}
Ok(())
}
fn download(pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
let url = match parser::get_url(pkg_md_path) {
@ -259,6 +294,8 @@ 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);
@ -266,3 +303,33 @@ fn build(
Ok(())
}
fn config(
src_dir: &Path,
pkg_md_path: &Path,
) -> Result<(), bool> {
let config_script = match parser::get_config_script(pkg_md_path) {
Ok(script) => script,
Err(_) => { return Ok(()) }
};
let output = Command::new("zsh")
.arg("-c")
.arg(&config_script)
.current_dir(src_dir)
.output();
if let Err(e) = output {
eprintln!("Failed to execute config 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);
}
Ok(())
}

View File

@ -38,8 +38,12 @@ pub fn get_url<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
}
pub fn get_deps<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
extract_block(file_path, "``` sh dependencies", "```")
pub fn get_build_deps<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
extract_block(file_path, "``` cfg build dependencies", "```")
}
pub fn get_run_deps<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
extract_block(file_path, "``` cfg build dependencies", "```")
}
@ -47,6 +51,10 @@ pub fn get_build_script<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
extract_block(file_path, "``` sh build.sctipt", "```")
}
pub fn get_config_script<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
extract_block(file_path, "``` sh build.config", "```")
}
pub fn get_repo_list() -> io::Result<Vec<String>> {
let exe_path = env::current_exe()?;