bkp
This commit is contained in:
@ -1,47 +1,100 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::process;
|
||||
|
||||
use super::*;
|
||||
use crate::utils::parser;
|
||||
use crate::utils::hardcopy::hardcopy;
|
||||
use crate::commands::pkglink::pkglink;
|
||||
|
||||
|
||||
pub fn install(repo: &String, pkgname: &String) -> Result<(), bool> {
|
||||
let var_path = get_var_path();
|
||||
let pkg_md_path = var_path.join(format!("{}/{}.md", repo, pkgname));
|
||||
|
||||
if !pkg_md_path.exists() {
|
||||
match parser::get_repo_addr(repo) {
|
||||
Ok(repo_addr) => {
|
||||
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");
|
||||
upload_from_repo(&repo, &pkgname, &pkg_md_path)?;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
check_dependency(&repo, &pkg_md_path)?;
|
||||
download(&pkgname, &pkg_md_path)?;
|
||||
|
||||
let src_dir = PathBuf::from("/pkg/src").join(&pkgname);
|
||||
build(&repo, &pkgname, &src_dir, &pkg_md_path)?;
|
||||
pkglink(&repo, &pkgname).expect("Failed link package");
|
||||
|
||||
println!("Package {} installed successfully from repo {}", pkgname, repo);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub fn install_all(pkgname: &String) {
|
||||
let repos = match parser::get_repo_list() {
|
||||
Ok(repos) => repos,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to get repository list: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut success = false;
|
||||
for repo in repos {
|
||||
println!("Trying to install {} from repo {}...", pkgname, repo);
|
||||
|
||||
match install(&repo, pkgname) {
|
||||
Ok(()) => {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Repository {} not found: {}", repo, e);
|
||||
return Err(false);
|
||||
Err(no_repo_package) => {
|
||||
if no_repo_package {
|
||||
continue;
|
||||
} else {
|
||||
process::exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !success {
|
||||
eprintln!("Package {} not found in any available repository", pkgname);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn upload_from_repo(repo: &String, pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
|
||||
match parser::get_repo_addr(repo) {
|
||||
Ok(repo_addr) => {
|
||||
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(())
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Repository {} not found: {}", repo, e);
|
||||
return Err(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_dependency(repo: &String, pkg_md_path: &Path) -> Result<(), bool> {
|
||||
let deps = match parser::get_deps(&pkg_md_path) {
|
||||
Ok(deps) => deps,
|
||||
Err(e) => {
|
||||
@ -51,10 +104,8 @@ pub fn install(repo: &String, pkgname: &String) -> Result<(), bool> {
|
||||
};
|
||||
|
||||
for dependency in deps.lines() {
|
||||
let dependency = dependency.trim();
|
||||
if !dependency.is_empty() {
|
||||
let pkg_dir = Path::new("/pkg").join(repo).join(dependency);
|
||||
if !pkg_dir.exists() {
|
||||
if !dependency.trim().is_empty() {
|
||||
if !Path::new("/pkg").join(repo).join(dependency).exists() {
|
||||
match install(repo, &dependency.to_string()) {
|
||||
Ok(()) => {}
|
||||
Err(_) => {process::exit(1) }
|
||||
@ -63,9 +114,12 @@ pub fn install(repo: &String, pkgname: &String) -> Result<(), bool> {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
let url = match parser::get_url(&pkg_md_path) {
|
||||
fn download(pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
|
||||
let url = match parser::get_url(pkg_md_path) {
|
||||
Ok(url) => url,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to parse URL: {}", e);
|
||||
@ -73,93 +127,142 @@ pub fn install(repo: &String, pkgname: &String) -> Result<(), bool> {
|
||||
}
|
||||
};
|
||||
|
||||
let src = PathBuf::from("/pkg/src").join(pkgname);
|
||||
|
||||
let src = Path::new("/pkg/src");
|
||||
fs::create_dir_all(&src).expect("Failed to create src directory");
|
||||
let wget_command: String;
|
||||
let src_dir: String;
|
||||
if url.ends_with(".tar.gz") {
|
||||
wget_command = format!("wget -O- {} | tar -xz -C {}", url, src.to_str().unwrap());
|
||||
src_dir = format!(
|
||||
"{}/{}",
|
||||
src.to_str().unwrap(),
|
||||
url
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.expect("Failed to extract archive name from URL")
|
||||
.trim_end_matches(".tar.gz"))
|
||||
} else if url.ends_with(".tar.xz") {
|
||||
wget_command = format!("wget -O- {} | tar -xJ -C {}", url, src.to_str().unwrap());
|
||||
src_dir = format!(
|
||||
"{}/{}",
|
||||
src.to_str().unwrap(),
|
||||
url
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.expect("Failed to extract archive name from URL")
|
||||
.trim_end_matches(".tar.xz"))
|
||||
if let Err(e) = fs::create_dir_all(&src) {
|
||||
eprintln!("Failed to create directory {}: {}", src.display(), e);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
if !url.ends_with(".git") {
|
||||
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(&url)
|
||||
.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)
|
||||
.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 {
|
||||
eprintln!("Unsupported archive format for URL: {}", url);
|
||||
return Err(false);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
println!("Downloading and extracting package from: {}", url);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
let wget_output = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(wget_command)
|
||||
.output()
|
||||
.expect("Failed to execute wget and tar");
|
||||
|
||||
if !wget_output.status.success() {
|
||||
eprintln!("Failed to download and extract package: {}", String::from_utf8_lossy(&wget_output.stderr));
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
// 3. Выполнение bash-скрипта сборки
|
||||
let build_script = match parser::get_build_script(&pkg_md_path) {
|
||||
fn build(
|
||||
repo: &String,
|
||||
pkgname: &String,
|
||||
src_dir: &Path,
|
||||
pkg_md_path: &Path,
|
||||
) -> Result<(), bool> {
|
||||
let build_script = match parser::get_build_script(pkg_md_path) {
|
||||
Ok(script) => script,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to parse build script: {}", e);
|
||||
Err(error) => {
|
||||
eprintln!("Failed to parse build script: {}", error);
|
||||
return Err(false);
|
||||
}
|
||||
};
|
||||
|
||||
println!("{}", &src_dir);
|
||||
let build_output = Command::new("sh")
|
||||
let output = Command::new("zsh")
|
||||
.arg("-c")
|
||||
.arg(build_script)
|
||||
.current_dir(&src_dir)
|
||||
.output()
|
||||
.expect("Failed to execute build script");
|
||||
.arg(&build_script)
|
||||
.current_dir(src_dir)
|
||||
.output();
|
||||
|
||||
if !build_output.status.success() {
|
||||
eprintln!("Build failed: {}", String::from_utf8_lossy(&build_output.stderr));
|
||||
if let Err(e) = output {
|
||||
eprintln!("Failed to execute build script: {}", e);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
let build_script_dest = Path::new("/pkg")
|
||||
.join(repo)
|
||||
.join(pkgname)
|
||||
.join("build-script.md");
|
||||
fs::copy(&pkg_md_path, &build_script_dest).expect("Failed to copy build script");
|
||||
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);
|
||||
}
|
||||
|
||||
let pkg_dir = Path::new("/pkg").join(repo).join(pkgname);
|
||||
hardcopy(
|
||||
&pkg_dir.join("bin"),
|
||||
&Path::new("/pkg").join(repo).join("bin"),
|
||||
).expect("Failed to copy bin directory");
|
||||
hardcopy(
|
||||
&pkg_dir.join("include"),
|
||||
&Path::new("/pkg").join(repo).join("include"),
|
||||
).expect("Failed to copy include directory");
|
||||
hardcopy(
|
||||
&pkg_dir.join("lib"),
|
||||
&Path::new("/pkg").join(repo).join("lib"),
|
||||
).expect("Failed to copy lib directory");
|
||||
let dest_dir = PathBuf::from("/pkg").join(repo).join(pkgname);
|
||||
if let Err(e) = fs::create_dir_all(&dest_dir) {
|
||||
eprintln!("Failed to create destination directory: {}", e);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
fs::remove_dir_all(&src_dir).expect("Failed to clean up src directory");
|
||||
let dest_path = dest_dir.join("build-script.md");
|
||||
if let Err(e) = fs::copy(pkg_md_path, &dest_path) {
|
||||
eprintln!("Failed to copy build script to destination: {}", e);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
if let Err(e) = fs::remove_dir_all(src_dir) {
|
||||
eprintln!("Failed to remove source directory: {}", e);
|
||||
return Err(false);
|
||||
}
|
||||
|
||||
println!("Package {} installed successfully from repo {}", pkgname, repo);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user