before index-conflict changes
This commit is contained in:
36
src/utils/command_handler/delete.rs
Normal file
36
src/utils/command_handler/delete.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use crate::commands;
|
||||
|
||||
pub fn delete(matches: &clap::ArgMatches) {
|
||||
let repo = matches.get_one::<String>("repo").unwrap();
|
||||
let pkgname = matches.get_one::<String>("pkgname").unwrap();
|
||||
let recursive = matches.get_flag("recursive");
|
||||
|
||||
if recursive {
|
||||
commands::delete::delete_recursive(repo, pkgname);
|
||||
} else {
|
||||
commands::delete::delete(repo, pkgname);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn command() -> clap::Command {
|
||||
clap::Command::new("delete")
|
||||
.about("Delete a package from a repository")
|
||||
.arg(
|
||||
clap::Arg::new("repo")
|
||||
.help("Repository name")
|
||||
.required(true),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("pkgname")
|
||||
.help("Package name")
|
||||
.required(true),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("recursive")
|
||||
.short('R')
|
||||
.long("recursive")
|
||||
.help("Recursively delete the package")
|
||||
.action(clap::ArgAction::SetTrue),
|
||||
)
|
||||
}
|
||||
29
src/utils/command_handler/disable.rs
Normal file
29
src/utils/command_handler/disable.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use crate::commands;
|
||||
|
||||
pub fn disable(matches: &clap::ArgMatches) {
|
||||
let repo = matches.get_one::<String>("repo").unwrap();
|
||||
let pkgname = matches.get_one::<String>("pkgname").unwrap();
|
||||
|
||||
match commands::disable::disable(&repo, &pkgname) {
|
||||
Ok(_) => println!("disable completed successfully."),
|
||||
Err(e) => eprintln!("Error during disable: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn command() -> clap::Command {
|
||||
clap::Command::new("disable")
|
||||
.about("Disable package")
|
||||
.arg(
|
||||
clap::Arg::new("repo")
|
||||
.help("Repository name")
|
||||
.required(true)
|
||||
.index(1),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("pkgname")
|
||||
.help("Package name")
|
||||
.required(true)
|
||||
.index(2),
|
||||
)
|
||||
}
|
||||
29
src/utils/command_handler/enable.rs
Normal file
29
src/utils/command_handler/enable.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use crate::commands;
|
||||
|
||||
pub fn enable(matches: &clap::ArgMatches) {
|
||||
let repo = matches.get_one::<String>("repo").unwrap();
|
||||
let pkgname = matches.get_one::<String>("pkgname").unwrap();
|
||||
|
||||
match commands::enable::enable(&repo, &pkgname) {
|
||||
Ok(_) => println!("enable completed successfully."),
|
||||
Err(e) => eprintln!("Error during enable: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn command() -> clap::Command {
|
||||
clap::Command::new("enable")
|
||||
.about("Enable package")
|
||||
.arg(
|
||||
clap::Arg::new("repo")
|
||||
.help("Repository name")
|
||||
.required(true)
|
||||
.index(1),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("pkgname")
|
||||
.help("Package name")
|
||||
.required(true)
|
||||
.index(2),
|
||||
)
|
||||
}
|
||||
33
src/utils/command_handler/install.rs
Normal file
33
src/utils/command_handler/install.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use crate::commands;
|
||||
use crate::utils::parser::pkginfo;
|
||||
|
||||
|
||||
pub fn install(matches: &clap::ArgMatches) {
|
||||
let args: Vec<&String> = matches.get_many::<String>("args").unwrap().collect();
|
||||
|
||||
match args.len() {
|
||||
1 => {
|
||||
let pkgname = args[0];
|
||||
commands::install::install(&pkginfo::get_priority_repo(pkgname.to_string()), &pkgname.to_string());
|
||||
}
|
||||
2 => {
|
||||
let repo = args[0];
|
||||
let pkgname = args[1];
|
||||
if let Err(_) = commands::install::install(repo, pkgname) { std::process::exit(1) }
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn command() -> clap::Command {
|
||||
clap::Command::new("install")
|
||||
.about("Install a package")
|
||||
.arg(
|
||||
clap::Arg::new("args")
|
||||
.help("Repository and package name (optional repo)")
|
||||
.required(true)
|
||||
.num_args(1..=2)
|
||||
.value_names(["repo", "pkgname"]),
|
||||
)
|
||||
}
|
||||
29
src/utils/command_handler/link.rs
Normal file
29
src/utils/command_handler/link.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use crate::commands;
|
||||
|
||||
pub fn link(matches: &clap::ArgMatches) {
|
||||
let repo = matches.get_one::<String>("repo").unwrap();
|
||||
let pkgname = matches.get_one::<String>("pkgname").unwrap();
|
||||
|
||||
match commands::link::link(&repo, &pkgname) {
|
||||
Ok(_) => println!("link completed successfully."),
|
||||
Err(e) => eprintln!("Error during link: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn command() -> clap::Command {
|
||||
clap::Command::new("link")
|
||||
.about("Create package links and mount overlays")
|
||||
.arg(
|
||||
clap::Arg::new("repo")
|
||||
.help("Repository name")
|
||||
.required(true)
|
||||
.index(1),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("pkgname")
|
||||
.help("Package name")
|
||||
.required(true)
|
||||
.index(2),
|
||||
)
|
||||
}
|
||||
7
src/utils/command_handler/mod.rs
Normal file
7
src/utils/command_handler/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
pub mod install;
|
||||
pub mod delete;
|
||||
pub mod disable;
|
||||
pub mod enable;
|
||||
pub mod link;
|
||||
pub mod run;
|
||||
pub mod trim;
|
||||
43
src/utils/command_handler/run.rs
Normal file
43
src/utils/command_handler/run.rs
Normal file
@ -0,0 +1,43 @@
|
||||
use crate::commands;
|
||||
use crate::utils::parser::pkginfo;
|
||||
|
||||
// pkg run <script> [repo] <pkgname>
|
||||
// pkg run kernel_change gnu linux-6.17
|
||||
// pkg run kernel_change linux-6.17
|
||||
//
|
||||
// pkg md_path должен брать из уже установленного ?
|
||||
// Или пусть проверяет
|
||||
|
||||
|
||||
pub fn run(matches: &clap::ArgMatches) {
|
||||
let args: Vec<&String> = matches.get_many::<String>("args").unwrap().collect();
|
||||
|
||||
match args.len() {
|
||||
2 => {
|
||||
let pkgname = args[1];
|
||||
let scriptname = args[0];
|
||||
commands::run::custom::custom(scriptname , &pkginfo::get_priority_repo(pkgname.to_string()), &pkgname.to_string());
|
||||
}
|
||||
3 => {
|
||||
let scriptname = args[0];
|
||||
let repo = args[0];
|
||||
let pkgname = args[1];
|
||||
commands::run::custom::custom(scriptname , &repo, &pkgname.to_string());
|
||||
if let Err(_) = commands::install::install(repo, pkgname) { std::process::exit(1) }
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn command() -> clap::Command {
|
||||
clap::Command::new("run")
|
||||
.about("Install a package")
|
||||
.arg(
|
||||
clap::Arg::new("args")
|
||||
.help("Repository and package name (optional repo)")
|
||||
.required(true)
|
||||
.num_args(1..=2)
|
||||
.value_names(["repo", "pkgname"]),
|
||||
)
|
||||
}
|
||||
40
src/utils/command_handler/trim.rs
Normal file
40
src/utils/command_handler/trim.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use crate::commands;
|
||||
use chrono::NaiveDateTime;
|
||||
|
||||
pub fn trim(matches: &clap::ArgMatches) {
|
||||
let repo = matches.get_one::<String>("repo").unwrap();
|
||||
let date = matches.get_one::<String>("date").unwrap();
|
||||
let time = matches.get_one::<String>("time").map(|s| s.as_str()).unwrap_or("00:00:00");
|
||||
|
||||
let datetime_str = format!("{} {}", date, time);
|
||||
|
||||
let datetime = NaiveDateTime::parse_from_str(&datetime_str, "%d.%m.%Y %H:%M:%S")
|
||||
.expect("Invalid date or time format. Expected format: DD.MM.YYYY HH:mm:ss");
|
||||
|
||||
let trim_date = datetime.and_utc().timestamp();
|
||||
|
||||
commands::trim::trim_handler(&repo, trim_date);
|
||||
}
|
||||
|
||||
pub fn command() -> clap::Command {
|
||||
clap::Command::new("trim")
|
||||
.about("Remove unused files within a specified period")
|
||||
.arg(
|
||||
clap::Arg::new("repo")
|
||||
.help("Repository name")
|
||||
.required(true)
|
||||
.index(1),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("date")
|
||||
.help("DD.MM.YYYY")
|
||||
.required(true)
|
||||
.index(2),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("time")
|
||||
.help("HH:mm:ss")
|
||||
.required(false)
|
||||
.index(3),
|
||||
)
|
||||
}
|
||||
@ -11,7 +11,7 @@ pub fn deletecopy(source: &Path, destination: &Path) -> io::Result<()> {
|
||||
if metadata.file_type().is_file() {
|
||||
if let Ok(dest_metadata) = fs::metadata(destination) {
|
||||
if dest_metadata.ino() == metadata.ino() {
|
||||
fs::remove_file(destination)?;
|
||||
fs::remove_file(destination).ok();
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@ -26,7 +26,7 @@ pub fn deletecopy(source: &Path, destination: &Path) -> io::Result<()> {
|
||||
}
|
||||
})?;
|
||||
} else if metadata.file_type().is_symlink() {
|
||||
fs::remove_file(destination)?;
|
||||
fs::remove_file(destination).ok();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -26,7 +26,7 @@ fn hardcopy(
|
||||
}
|
||||
}
|
||||
|
||||
match crate::utils::parser::get_index_conflict(destination) {
|
||||
match crate::utils::parser::pkginfo::get_index_conflict(destination) {
|
||||
Ok(index_source) => {
|
||||
if index_source == source {
|
||||
fs::remove_file(destination)?;
|
||||
@ -172,7 +172,7 @@ fn append_index_block(source: &Path, destination: &Path) -> io::Result<()> {
|
||||
let source_components: Vec<_> = source.iter().collect();
|
||||
let base_system_folder = source_components[4].to_str().unwrap();
|
||||
|
||||
let index_conflict_path = Path::new("/pkg/gnu/aeropkg/etc/index-conflict.md");
|
||||
let index_conflict_path = crate::commands::get_etc_path().join("index-conflict.md");
|
||||
let content = fs::read_to_string(&index_conflict_path)?;
|
||||
|
||||
let start_marker = format!("``` cfg *** {} ***", base_system_folder);
|
||||
3
src/utils/fs/mod.rs
Normal file
3
src/utils/fs/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod hardcopy;
|
||||
pub mod deletecopy;
|
||||
pub mod mv;
|
||||
@ -1,5 +1,5 @@
|
||||
pub mod hardcopy;
|
||||
pub mod fs;
|
||||
|
||||
pub mod parser;
|
||||
pub mod deletecopy;
|
||||
pub mod shell;
|
||||
pub mod mv;
|
||||
pub mod command_handler;
|
||||
|
||||
@ -1,240 +0,0 @@
|
||||
use std::fs;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::{Path,PathBuf};
|
||||
|
||||
|
||||
pub fn get_name<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
let first_line = read_first_line(file_path)?;
|
||||
Ok(first_line.split_whitespace().next().unwrap_or("").to_string())
|
||||
}
|
||||
|
||||
|
||||
pub fn get_version<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
let first_line = read_first_line(file_path)?;
|
||||
Ok(first_line
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.unwrap_or("")
|
||||
.to_string())
|
||||
}
|
||||
|
||||
|
||||
pub fn get_url<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
let lines = read_lines(&file_path)?;
|
||||
let third_line = lines.get(2)
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "File has less than 3 lines"))?
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
let name = get_name(&file_path)?;
|
||||
let version = get_version(&file_path)?;
|
||||
|
||||
let url = third_line
|
||||
.replace("{name}", &name)
|
||||
.replace("{version}", &version);
|
||||
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
|
||||
pub fn get_build_deps<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
extract_block(file_path, "``` cfg *** build deps ***", "```")
|
||||
}
|
||||
|
||||
pub fn get_run_deps<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
extract_block(file_path, "``` cfg *** run deps ***", "```")
|
||||
}
|
||||
|
||||
|
||||
pub fn get_build_script<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
extract_block(file_path, "``` sh *** build ***", "```")
|
||||
}
|
||||
|
||||
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_trim_rules<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
extract_block(file_path, "``` cfg *** Trim rules ***", "```")
|
||||
}
|
||||
|
||||
pub fn get_repo_list() -> io::Result<Vec<String>> {
|
||||
let file_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
|
||||
let block = extract_block(file_path, "``` cfg *** Repository list and priority ***", "```")?;
|
||||
|
||||
let mut repo_list = Vec::new();
|
||||
for line in block.lines() {
|
||||
let trimmed_line = line.trim();
|
||||
if !trimmed_line.is_empty() {
|
||||
let parts: Vec<&str> = trimmed_line.split_whitespace().collect();
|
||||
if let Some(repo) = parts.first() {
|
||||
repo_list.push(repo.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(repo_list)
|
||||
}
|
||||
|
||||
|
||||
pub fn get_repo_addr(repo: &str) -> io::Result<String> {
|
||||
let file_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
|
||||
let block = extract_block(file_path, "``` cfg *** Repository list and priority ***", "```")?;
|
||||
|
||||
for line in block.lines() {
|
||||
let trimmed_line = line.trim();
|
||||
if !trimmed_line.is_empty() {
|
||||
let parts: Vec<&str> = trimmed_line.split_whitespace().collect();
|
||||
if parts.len() >= 2 && parts[0] == repo {
|
||||
return Ok(parts[1].to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Repository '{}' not found in the repository list", repo),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn get_use_status(repo: &str, dependency: &str) -> bool {
|
||||
let base_path = format!("/pkg/{}", repo);
|
||||
let path = Path::new(&base_path);
|
||||
|
||||
if !path.exists() || !path.is_dir() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut match_count = 0;
|
||||
|
||||
if let Ok(entries) = fs::read_dir(path) {
|
||||
for entry in entries.flatten() {
|
||||
let subdir_path = entry.path();
|
||||
if subdir_path.is_dir() {
|
||||
let script_path = subdir_path.join("build-script.md");
|
||||
if let Ok(lines) = read_lines(&script_path) {
|
||||
for line in lines.iter() {
|
||||
if line.trim() == format!("={}", dependency) {
|
||||
match_count += 1;
|
||||
if match_count > 1 {
|
||||
return true
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
pub fn get_index_conflict<P: AsRef<Path>>(destination: P) -> io::Result<PathBuf> {
|
||||
let destination_path = destination.as_ref();
|
||||
|
||||
let parts: Vec<&str> = destination_path
|
||||
.iter()
|
||||
.map(|component| component.to_str().unwrap_or(""))
|
||||
.collect();
|
||||
|
||||
if parts.len() < 4 {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"Invalid destination path format",
|
||||
));
|
||||
}
|
||||
|
||||
let system_struct_folder = parts[3]; // bin, sbin, include, lib, share
|
||||
|
||||
let etc = Path::new("/pkg/gnu/aeropkg/etc");
|
||||
let cfg_path = etc.join("index-conflict.md");
|
||||
|
||||
let start_marker = format!("``` cfg *** {} ***", system_struct_folder);
|
||||
let end_marker = "```";
|
||||
|
||||
let block_content = extract_block(&cfg_path, &start_marker, end_marker)?;
|
||||
|
||||
let destination_str = destination_path.to_str().ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidInput, "Failed to convert destination path to string")
|
||||
})?;
|
||||
|
||||
for line in block_content.lines() {
|
||||
let trimmed_line = line.trim();
|
||||
if trimmed_line.starts_with(destination_str) {
|
||||
let mut words = trimmed_line.split_whitespace();
|
||||
if let Some(_) = words.next() {
|
||||
if let Some(path_source) = words.next() {
|
||||
return Ok(PathBuf::from(path_source));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
format!(
|
||||
"No matching line found for destination: {}",
|
||||
destination_path.display()
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
fn read_first_line<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
let file = fs::File::open(file_path)?;
|
||||
let reader = io::BufReader::new(file);
|
||||
if let Some(line) = reader.lines().next() {
|
||||
line
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::InvalidData, "File is empty"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn read_lines<P: AsRef<Path>>(file_path: P) -> io::Result<Vec<String>> {
|
||||
let file = fs::File::open(file_path)?;
|
||||
let reader = io::BufReader::new(file);
|
||||
reader.lines().collect()
|
||||
}
|
||||
|
||||
|
||||
fn extract_block<P: AsRef<Path>>(
|
||||
file_path: P,
|
||||
start_marker: &str,
|
||||
end_marker: &str,
|
||||
) -> io::Result<String> {
|
||||
let lines = read_lines(file_path)?;
|
||||
let mut block_started = false;
|
||||
let mut result = Vec::new();
|
||||
|
||||
for line in lines {
|
||||
if line.trim() == start_marker {
|
||||
block_started = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if block_started {
|
||||
if line.trim() == end_marker {
|
||||
break;
|
||||
}
|
||||
result.push(line.trim().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if result.is_empty() {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Block between '{}' and '{}' not found", start_marker, end_marker),
|
||||
))
|
||||
} else {
|
||||
Ok(result.join("\n"))
|
||||
}
|
||||
}
|
||||
86
src/utils/parser/env.rs
Normal file
86
src/utils/parser/env.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use std::io;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
pub fn get_install_env(repo: &String, pkgname: &String, pkg_md_path: &Path, stage: &str) -> HashMap<String, String> {
|
||||
let mut full_env = HashMap::new();
|
||||
full_env.insert("repo".to_string(), repo.clone());
|
||||
full_env.insert("pkgname".to_string(), pkgname.clone());
|
||||
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
|
||||
if let Some(pkg_env) = get_pkg_env(pkg_md_path).ok() { full_env.extend(pkg_env) }
|
||||
if let Some(repo_env) = get_repo_env(repo).ok() { full_env.extend(repo_env) }
|
||||
if let Some(stage_env) = get_stage_env(stage).ok() { full_env.extend(stage_env) }
|
||||
if let Some(repo_stage_env) = get_repo_and_stage_env(repo, stage).ok() { full_env.extend(repo_stage_env) }
|
||||
full_env.extend(env::vars().map(|(k, v)| (k, v)));
|
||||
return full_env
|
||||
}
|
||||
|
||||
pub fn get_custom_env(repo: &String, pkgname: &String, pkg_md_path: &Path) -> HashMap<String, String> {
|
||||
let mut full_env = HashMap::new();
|
||||
full_env.insert("repo".to_string(), repo.clone());
|
||||
full_env.insert("pkgname".to_string(), pkgname.clone());
|
||||
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
|
||||
if let Some(pkg_env) = get_pkg_env(pkg_md_path).ok() { full_env.extend(pkg_env) }
|
||||
if let Some(repo_env) = get_repo_env(repo).ok() { full_env.extend(repo_env) }
|
||||
full_env.extend(env::vars().map(|(k, v)| (k, v)));
|
||||
return full_env
|
||||
}
|
||||
|
||||
pub fn get_global_env() -> HashMap<String, String> {
|
||||
let mut full_env = HashMap::new();
|
||||
full_env.insert("repo".to_string(), repo.clone());
|
||||
full_env.insert("pkgname".to_string(), pkgname.clone());
|
||||
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
|
||||
return full_env
|
||||
}
|
||||
|
||||
fn get_global_env_string() -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env ***"), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
fn parse_env_vars(content: &str) -> HashMap<String, String> {
|
||||
content
|
||||
.lines()
|
||||
.filter_map(|line| {
|
||||
let line = line.trim();
|
||||
if line.is_empty() || line.starts_with('#') {
|
||||
return None;
|
||||
}
|
||||
let parts: Vec<&str> = line.splitn(2, '=').collect();
|
||||
if parts.len() != 2 {
|
||||
return None;
|
||||
}
|
||||
Some((parts[0].to_string(), parts[1].to_string()))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn get_pkg_env(file_path: &Path) -> io::Result<HashMap<String, String>> {
|
||||
let content = extract_block(file_path, "``` env *** env ***", "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
|
||||
pub fn get_repo_env(repo: &str) -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env {} ***", repo), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
pub fn get_stage_env(stage: &str) -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env {} ***", stage), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
pub fn get_repo_and_stage_env(repo: &str, stage: &str) -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env {} {} ***", repo, stage), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
77
src/utils/parser/mod.rs
Normal file
77
src/utils/parser/mod.rs
Normal file
@ -0,0 +1,77 @@
|
||||
pub mod env;
|
||||
pub mod pkginfo;
|
||||
pub mod repoinfo;
|
||||
|
||||
use std::fs;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
|
||||
|
||||
pub fn get_stage_hook(stage: &str) -> io::Result<String> {
|
||||
let cfg_path = &crate::commands::get_etc_path().join("aeropkg.md");
|
||||
extract_block(cfg_path, &format!("``` sh *** hook {} ***", &stage), "```")
|
||||
}
|
||||
|
||||
pub fn get_repo_and_stage_hook(repo: &str, stage: &str) -> io::Result<String> {
|
||||
let cfg_path = &crate::commands::get_etc_path().join("aeropkg.md");
|
||||
extract_block(cfg_path, &format!("``` sh *** hook {} {} ***", &repo, &stage), "```")
|
||||
}
|
||||
|
||||
pub fn get_trim_rules(file_path: &Path) -> io::Result<String> { extract_block(file_path, "``` cfg *** Trim rules ***", "```") }
|
||||
|
||||
pub fn get_build_deps(file_path: &Path) -> io::Result<String> { extract_block(file_path, "``` cfg *** build deps ***", "```") }
|
||||
pub fn get_run_deps(file_path: &Path) -> io::Result<String> { extract_block(file_path, "``` cfg *** run deps ***", "```") }
|
||||
pub fn get_build_script(file_path: &Path) -> io::Result<String> { extract_block(file_path, "``` sh *** build ***", "```") }
|
||||
pub fn get_config_script(file_path: &Path) -> io::Result<String> { extract_block(file_path, "``` sh *** config ***", "```") }
|
||||
pub fn get_patch_script(file_path: &Path) -> io::Result<String> { extract_block(file_path, "``` sh *** config ***", "```") }
|
||||
pub fn get_custom_script(file_path: &Path, scriptname: &String) -> String { extract_block(file_path, &format!("``` sh *** {} ***", scriptname), "```").expect(&format!("Can't get custom script: {}", &scriptname)) }
|
||||
|
||||
fn extract_block(
|
||||
file_path: &Path,
|
||||
start_marker: &str,
|
||||
end_marker: &str,
|
||||
) -> io::Result<String> {
|
||||
let lines = read_lines(file_path)?;
|
||||
let mut block_started = false;
|
||||
let mut result = Vec::new();
|
||||
|
||||
for line in lines {
|
||||
if line.trim() == start_marker {
|
||||
block_started = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if block_started {
|
||||
if line.trim() == end_marker {
|
||||
break;
|
||||
}
|
||||
result.push(line.trim().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if result.is_empty() {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Block between '{}' and '{}' not found", start_marker, end_marker),
|
||||
))
|
||||
} else {
|
||||
Ok(result.join("\n"))
|
||||
}
|
||||
}
|
||||
|
||||
fn read_first_line<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
|
||||
let file = fs::File::open(file_path)?;
|
||||
let reader = io::BufReader::new(file);
|
||||
if let Some(line) = reader.lines().next() {
|
||||
line
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::InvalidData, "File is empty"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn read_lines<P: AsRef<Path>>(file_path: P) -> io::Result<Vec<String>> {
|
||||
let file = fs::File::open(file_path)?;
|
||||
let reader = io::BufReader::new(file);
|
||||
reader.lines().collect()
|
||||
}
|
||||
133
src/utils/parser/pkginfo.rs
Normal file
133
src/utils/parser/pkginfo.rs
Normal file
@ -0,0 +1,133 @@
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path,PathBuf};
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn get_name<P: AsRef<Path>>(pkg_file_path: P) -> io::Result<String> {
|
||||
let first_line = read_first_line(pkg_file_path)?;
|
||||
Ok(first_line.split_whitespace().next().unwrap_or("").to_string())
|
||||
}
|
||||
|
||||
pub fn get_version<P: AsRef<Path>>(pkg_file_path: P) -> io::Result<String> {
|
||||
let first_line = read_first_line(pkg_file_path)?;
|
||||
Ok(first_line
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.unwrap_or("")
|
||||
.to_string())
|
||||
}
|
||||
|
||||
pub fn get_url(pkg_file_path: &Path) -> io::Result<String> {
|
||||
let lines = read_lines(&pkg_file_path)?;
|
||||
let third_line = lines.get(2)
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "File has less than 3 lines"))?
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
let name = get_name(&pkg_file_path)?;
|
||||
let version = get_version(&pkg_file_path)?;
|
||||
|
||||
let url = third_line
|
||||
.replace("{name}", &name)
|
||||
.replace("{version}", &version);
|
||||
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
pub fn get_use_status(repo: &str, pkgname: &str) -> bool {
|
||||
let base_path = crate::commands::get_aeropkg_base().join(repo);
|
||||
let path = Path::new(&base_path);
|
||||
|
||||
if !path.exists() || !path.is_dir() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut match_count = 0;
|
||||
|
||||
if let Ok(entries) = fs::read_dir(path) {
|
||||
for entry in entries.flatten() {
|
||||
let subdir_path = entry.path();
|
||||
if subdir_path.is_dir() {
|
||||
let script_path = subdir_path.join("build-script.md");
|
||||
if let Ok(lines) = read_lines(&script_path) {
|
||||
for line in lines.iter() {
|
||||
if line.trim() == format!("={}", pkgname) {
|
||||
match_count += 1;
|
||||
if match_count > 1 {
|
||||
return true
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
pub fn get_priority_repo(pkgname: String) -> String {
|
||||
let repo_list = repoinfo::get_repo_list();
|
||||
let var_path = crate::commands::get_var_path();
|
||||
|
||||
for repo in repo_list {
|
||||
let pkg_path = var_path.join(&repo).join(format!("{}.md", &pkgname));
|
||||
if pkg_path.exists() {
|
||||
return repo;
|
||||
}
|
||||
}
|
||||
|
||||
panic!("Package {} not found in any available repository", pkgname);
|
||||
}
|
||||
|
||||
pub fn get_index_conflict<P: AsRef<Path>>(pkg_file_path: P) -> io::Result<PathBuf> {
|
||||
let pkg_file_path_path = pkg_file_path.as_ref();
|
||||
|
||||
let parts: Vec<&str> = pkg_file_path_path
|
||||
.iter()
|
||||
.map(|component| component.to_str().unwrap_or(""))
|
||||
.collect();
|
||||
|
||||
if parts.len() < 4 {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"Invalid pkg_file_path path format",
|
||||
));
|
||||
}
|
||||
|
||||
let system_struct_folder = parts[3]; // bin, sbin, include, lib, share
|
||||
|
||||
let etc = crate::commands::get_etc_path();
|
||||
let cfg_path = etc.join("index-conflict.md");
|
||||
|
||||
let start_marker = format!("``` cfg *** {} ***", system_struct_folder);
|
||||
let end_marker = "```";
|
||||
|
||||
let block_content = extract_block(&cfg_path, &start_marker, end_marker)?;
|
||||
|
||||
let pkg_file_path_str = pkg_file_path_path.to_str().ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidInput, "Failed to convert pkg_file_path path to string")
|
||||
})?;
|
||||
|
||||
for line in block_content.lines() {
|
||||
let trimmed_line = line.trim();
|
||||
if trimmed_line.starts_with(pkg_file_path_str) {
|
||||
let mut words = trimmed_line.split_whitespace();
|
||||
if let Some(_) = words.next() {
|
||||
if let Some(path_source) = words.next() {
|
||||
return Ok(PathBuf::from(path_source));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
format!(
|
||||
"No matching line found for pkg_file_path: {}",
|
||||
pkg_file_path_path.display()
|
||||
),
|
||||
))
|
||||
}
|
||||
38
src/utils/parser/repoinfo.rs
Normal file
38
src/utils/parser/repoinfo.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use super::*;
|
||||
|
||||
pub fn get_repo_addr(repo: &str) -> String {
|
||||
let file_path = &crate::commands::get_etc_path().join("aeropkg.md");
|
||||
|
||||
let block = extract_block(file_path, "``` cfg *** Repository list and priority ***", "```").expect("Can't parse repo list block");
|
||||
|
||||
for line in block.lines() {
|
||||
let trimmed_line = line.trim();
|
||||
if !trimmed_line.is_empty() {
|
||||
let parts: Vec<&str> = trimmed_line.split_whitespace().collect();
|
||||
if parts.len() >= 2 && parts[0] == repo {
|
||||
return parts[1].to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panic!("Repository '{}' not found in the repository list", repo);
|
||||
}
|
||||
|
||||
pub fn get_repo_list() -> Vec<String> {
|
||||
let file_path = &crate::commands::get_etc_path().join("aeropkg.md");
|
||||
|
||||
let block = extract_block(file_path, "``` cfg *** Repository list and priority ***", "```").expect("Can't parse repo list block");
|
||||
|
||||
let mut repo_list = Vec::new();
|
||||
for line in block.lines() {
|
||||
let trimmed_line = line.trim();
|
||||
if !trimmed_line.is_empty() {
|
||||
let parts: Vec<&str> = trimmed_line.split_whitespace().collect();
|
||||
if let Some(repo) = parts.first() {
|
||||
repo_list.push(repo.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
repo_list
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::env;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::utils::parser;
|
||||
|
||||
pub fn mount_overlay(path_repo: &Path) -> Result<(), String> {
|
||||
let lowerdirs = vec![
|
||||
@ -71,3 +74,58 @@ pub fn shell_update() -> Result<(), String> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_install_script_hook(repo: &str, stage: &str, full_env: &HashMap<String, String>) -> Result<(), bool> {
|
||||
let hook_script = match parser::get_repo_and_stage_hook(repo, stage) {
|
||||
Ok(script) => Ok(script),
|
||||
Err(_) => parser::get_stage_hook(stage),
|
||||
};
|
||||
|
||||
let shell = env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string());
|
||||
|
||||
if let Ok(script) = hook_script {
|
||||
let output = Command::new(&shell)
|
||||
.arg("-c")
|
||||
.arg(&script)
|
||||
.envs(full_env)
|
||||
.output();
|
||||
|
||||
if let Err(e) = output {
|
||||
eprintln!("Failed to execute hook script: {}", e);
|
||||
return Err(false);
|
||||
}
|
||||
let output = output.unwrap();
|
||||
|
||||
if !output.status.success() {
|
||||
eprintln!("Hook script failed: {:?}", output);
|
||||
return Err(false);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_install_script(script: &str, work_dir: &Path, full_env: &HashMap<String, String>) -> Result<(), bool> {
|
||||
let shell = env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string());
|
||||
|
||||
let output = Command::new(&shell)
|
||||
.arg("-c")
|
||||
.arg(&script)
|
||||
.current_dir(work_dir)
|
||||
.envs(full_env)
|
||||
.output();
|
||||
|
||||
if let Err(e) = output {
|
||||
eprintln!("Failed to execute shell script: {}", e);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
let output = output.unwrap();
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
eprintln!("Failed to execute script:\n``` sh\n{}\n```\nError: {}", script, stderr);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user