Skip to content

Commit

Permalink
Add install via git
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Oct 23, 2023
1 parent 5445241 commit 6c2ef1d
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 30 deletions.
85 changes: 85 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ regex = "1.10.2"
async-recursion = "1.0.5"
openssl = { version = "0.10.45", features = ["vendored"] }
url = "2.2"
git2 = "0.18.1"
walkdir = "2.4.0"

[dependencies.confy]
features = ["toml_conf"]
Expand Down
128 changes: 101 additions & 27 deletions src/cmd/install.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
extern crate serde;
extern crate serde_ini;

use crate::utils::{confirm, download_from_url, get_last, get_mmrl_json, is_url};
use crate::android_root::{get_downloads_dir, get_install_cli};
use crate::repo::{find_module, find_version, get_id_details, Module};
use reqwest::Client;
use crate::utils::{confirm, download_from_url, get_last, get_mmrl_json, is_git, is_url, zip_dir};
use async_recursion::async_recursion;
use git2::Repository;
use reqwest::Client;
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Error, ErrorKind};
use std::path::Path;
use std::process::{exit, Command, Stdio};
use walkdir::WalkDir;

#[async_recursion]
async fn check_requires(
_is_git: bool,
path: String,
install_requires: bool,
client: Client,
yes: bool,
modules: &Vec<Module>,
) {
let mini = get_mmrl_json(&path);
) -> () {
let mini: Result<crate::utils::MMRLJSON, serde_json::Error>;

if _is_git {
mini = match File::open(path) {
Ok(file) => serde_json::from_reader(file),
Err(..) => serde_json::from_str("{\"require\":[]}"),
};
} else {
mini = get_mmrl_json(&path);
}

for req in mini.unwrap().require {
let dep_path = Path::new("/data/adb/modules")
Expand All @@ -30,7 +43,7 @@ async fn check_requires(
if !(dep_path_update.exists() || dep_path.exists()) {
if install_requires {
println!("Install requires");
install(client.clone(), yes, install_requires, modules, req).await;
install(client.clone(), yes, install_requires, modules, req).await;
} else {
println!("This module requires {} to be installed", req.clone());
exit(1)
Expand All @@ -39,31 +52,82 @@ async fn check_requires(
}
}

const METHOD_STORED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Stored);

#[async_recursion]
pub async fn install(client: Client, yes: bool, requires: bool, modules: &Vec<Module>, id: String) {
let _url = &id.to_owned()[..];
if !is_url(_url) {
let (_id, _ver) = get_id_details(id);
let module = find_module(&modules, _id.clone());
let version = find_version(module.versions.clone(), _ver);
if is_git(_url) {
let name = get_last(_url);
let path = &[get_downloads_dir(), name.unwrap()].join("/");
match Repository::clone(_url, path) {
Ok(repo) => repo,
Err(e) => panic!("failed to clone: {}", e),
};

check_requires(
true,
[path, "mmrl.json"].join("/"),
requires,
client.clone(),
yes,
modules,
)
.await;

let file = File::create([path, "zip"].join(".")).unwrap();

let walkdir = WalkDir::new(path);
let it = walkdir.into_iter();

zip_dir(
&mut it.filter_map(|e| e.ok()),
path,
file,
METHOD_STORED.unwrap(),
)
.unwrap();

if Path::new(path).exists() {
fs::remove_dir_all(path).expect("File delete failed");
}

println!("Info not availabe in git install");
let success = yes || confirm("Do you want to continue [y/N] ");

if success {
let (bin, args) = get_install_cli(&path);

let stdout = Command::new(bin)
.args(args)
.stdout(Stdio::piped())
.spawn()
.unwrap()
.stdout
.ok_or_else(|| Error::new(ErrorKind::Other, "Could not capture standard output."))
.unwrap();

let reader = BufReader::new(stdout);

reader
.lines()
.filter_map(|line| line.ok())
.for_each(|line| println!("{}", line));
} else {
exit(0);
}
} else if is_url(_url) {
let name = get_last(_url);
let path = &[
get_downloads_dir(),
[
[version.version.clone(), module.id].join("-"),
"zip".to_string(),
]
.join("."),
[name.clone().unwrap().to_string(), "zip".to_string()].join("."),
]
.join("/");
download_from_url(client.clone(), id.clone(), name.unwrap(), path).await;
check_requires(false, path.clone(), requires, client.clone(), yes, modules).await;

println!("Downloading {}", module.name);
println!("Version: {}", &version.version);

download_from_url(client.clone(), version.zip_url, module.name, path).await;
check_requires(path.clone(), requires, client.clone(), yes, modules).await;

let success = yes || confirm("Do you want to continue [y/N]? ");
println!("Info not availabe in url install");
let success = yes || confirm("Do you want to continue [y/N] ");

if success {
let (bin, args) = get_install_cli(&path);
Expand All @@ -87,17 +151,27 @@ pub async fn install(client: Client, yes: bool, requires: bool, modules: &Vec<Mo
exit(0);
}
} else {
let name = get_last(_url);
let (_id, _ver) = get_id_details(id);
let module = find_module(&modules, _id.clone());
let version = find_version(module.versions.clone(), _ver);

let path = &[
get_downloads_dir(),
[name.clone().unwrap().to_string(), "zip".to_string()].join("."),
[
[version.version.clone(), module.id].join("-"),
"zip".to_string(),
]
.join("."),
]
.join("/");
download_from_url(client.clone(), id.clone(), name.unwrap(), path).await;
check_requires(path.clone(), requires, client.clone(), yes, modules).await;

println!("Info not availabe in url install");
let success = yes || confirm("\nDo you want to continue [y/N]? ");
println!("Downloading {}", module.name);
println!("Version: {}", &version.version);

download_from_url(client.clone(), version.zip_url, module.name, path).await;
check_requires(false, path.clone(), requires, client.clone(), yes, modules).await;

let success = yes || confirm("Do you want to continue [y/N] ");

if success {
let (bin, args) = get_install_cli(&path);
Expand Down
Loading

0 comments on commit 6c2ef1d

Please sign in to comment.