before index-conflict changes
This commit is contained in:
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()
|
||||
}
|
||||
Reference in New Issue
Block a user