delete uncorrect

This commit is contained in:
pivodevat
2025-06-02 18:00:32 +03:00
parent 5918b2c4b2
commit 51ffa435fb
16 changed files with 666 additions and 76 deletions

31
src/utils/hardcopy.rs Normal file
View File

@ -0,0 +1,31 @@
use rayon::prelude::*;
use std::fs;
use std::io;
use std::path::Path;
use std::os::unix;
pub fn hardcopy(source: &Path, destination: &Path) -> io::Result<()> {
let metadata = fs::symlink_metadata(source)?;
if metadata.file_type().is_file() {
fs::hard_link(source, destination)?;
} else if metadata.file_type().is_dir() {
fs::create_dir_all(destination)?;
let entries: Vec<_> = fs::read_dir(source)?.collect::<io::Result<Vec<_>>>()?;
entries.par_iter().try_for_each(|entry| {
let path = entry.path();
if let Some(file_name) = path.file_name() {
let dest_path = destination.join(file_name);
hardcopy(&path, &dest_path)
} else {
Ok(())
}
})?;
} else if metadata.file_type().is_symlink() {
let target = fs::read_link(source)?;
unix::fs::symlink(target, destination)?;
}
Ok(())
}

2
src/utils/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod hardcopy;
pub mod parser;

183
src/utils/parser.rs Normal file
View File

@ -0,0 +1,183 @@
use std::fs;
use std::env;
use std::io::{self, BufRead};
use std::path::Path;
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_deps<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
extract_block(file_path, "``` sh dependencies", "```")
}
pub fn get_build_script<P: AsRef<Path>>(file_path: P) -> io::Result<String> {
extract_block(file_path, "``` sh build.sctipt", "```")
}
pub fn get_repo_list() -> io::Result<Vec<String>> {
let exe_path = env::current_exe()?;
let file_path = exe_path
.parent()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Failed to get executable directory"))?
.join("../etc/sexpkg.md");
let block = extract_block(file_path, "``` sh *** 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 exe_path = env::current_exe()?;
let file_path = exe_path
.parent()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Failed to get executable directory"))?
.join("../etc/sexpkg.md");
let block = extract_block(file_path, "``` sh *** 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
}
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"))
}
}