Skip to content

Commit 6c2ef1d

Browse files
committed
Add install via git
1 parent 5445241 commit 6c2ef1d

File tree

4 files changed

+246
-30
lines changed

4 files changed

+246
-30
lines changed

Cargo.lock

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ regex = "1.10.2"
2424
async-recursion = "1.0.5"
2525
openssl = { version = "0.10.45", features = ["vendored"] }
2626
url = "2.2"
27+
git2 = "0.18.1"
28+
walkdir = "2.4.0"
2729

2830
[dependencies.confy]
2931
features = ["toml_conf"]

src/cmd/install.rs

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
extern crate serde;
22
extern crate serde_ini;
33

4-
use crate::utils::{confirm, download_from_url, get_last, get_mmrl_json, is_url};
54
use crate::android_root::{get_downloads_dir, get_install_cli};
65
use crate::repo::{find_module, find_version, get_id_details, Module};
7-
use reqwest::Client;
6+
use crate::utils::{confirm, download_from_url, get_last, get_mmrl_json, is_git, is_url, zip_dir};
87
use async_recursion::async_recursion;
8+
use git2::Repository;
9+
use reqwest::Client;
10+
use std::fs::{self, File};
911
use std::io::{BufRead, BufReader, Error, ErrorKind};
1012
use std::path::Path;
1113
use std::process::{exit, Command, Stdio};
14+
use walkdir::WalkDir;
1215

1316
#[async_recursion]
1417
async fn check_requires(
18+
_is_git: bool,
1519
path: String,
1620
install_requires: bool,
1721
client: Client,
1822
yes: bool,
1923
modules: &Vec<Module>,
20-
) {
21-
let mini = get_mmrl_json(&path);
24+
) -> () {
25+
let mini: Result<crate::utils::MMRLJSON, serde_json::Error>;
26+
27+
if _is_git {
28+
mini = match File::open(path) {
29+
Ok(file) => serde_json::from_reader(file),
30+
Err(..) => serde_json::from_str("{\"require\":[]}"),
31+
};
32+
} else {
33+
mini = get_mmrl_json(&path);
34+
}
2235

2336
for req in mini.unwrap().require {
2437
let dep_path = Path::new("/data/adb/modules")
@@ -30,7 +43,7 @@ async fn check_requires(
3043
if !(dep_path_update.exists() || dep_path.exists()) {
3144
if install_requires {
3245
println!("Install requires");
33-
install(client.clone(), yes, install_requires, modules, req).await;
46+
install(client.clone(), yes, install_requires, modules, req).await;
3447
} else {
3548
println!("This module requires {} to be installed", req.clone());
3649
exit(1)
@@ -39,31 +52,82 @@ async fn check_requires(
3952
}
4053
}
4154

55+
const METHOD_STORED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Stored);
56+
4257
#[async_recursion]
4358
pub async fn install(client: Client, yes: bool, requires: bool, modules: &Vec<Module>, id: String) {
4459
let _url = &id.to_owned()[..];
45-
if !is_url(_url) {
46-
let (_id, _ver) = get_id_details(id);
47-
let module = find_module(&modules, _id.clone());
48-
let version = find_version(module.versions.clone(), _ver);
60+
if is_git(_url) {
61+
let name = get_last(_url);
62+
let path = &[get_downloads_dir(), name.unwrap()].join("/");
63+
match Repository::clone(_url, path) {
64+
Ok(repo) => repo,
65+
Err(e) => panic!("failed to clone: {}", e),
66+
};
67+
68+
check_requires(
69+
true,
70+
[path, "mmrl.json"].join("/"),
71+
requires,
72+
client.clone(),
73+
yes,
74+
modules,
75+
)
76+
.await;
77+
78+
let file = File::create([path, "zip"].join(".")).unwrap();
79+
80+
let walkdir = WalkDir::new(path);
81+
let it = walkdir.into_iter();
82+
83+
zip_dir(
84+
&mut it.filter_map(|e| e.ok()),
85+
path,
86+
file,
87+
METHOD_STORED.unwrap(),
88+
)
89+
.unwrap();
90+
91+
if Path::new(path).exists() {
92+
fs::remove_dir_all(path).expect("File delete failed");
93+
}
94+
95+
println!("Info not availabe in git install");
96+
let success = yes || confirm("Do you want to continue [y/N] ");
97+
98+
if success {
99+
let (bin, args) = get_install_cli(&path);
100+
101+
let stdout = Command::new(bin)
102+
.args(args)
103+
.stdout(Stdio::piped())
104+
.spawn()
105+
.unwrap()
106+
.stdout
107+
.ok_or_else(|| Error::new(ErrorKind::Other, "Could not capture standard output."))
108+
.unwrap();
109+
110+
let reader = BufReader::new(stdout);
49111

112+
reader
113+
.lines()
114+
.filter_map(|line| line.ok())
115+
.for_each(|line| println!("{}", line));
116+
} else {
117+
exit(0);
118+
}
119+
} else if is_url(_url) {
120+
let name = get_last(_url);
50121
let path = &[
51122
get_downloads_dir(),
52-
[
53-
[version.version.clone(), module.id].join("-"),
54-
"zip".to_string(),
55-
]
56-
.join("."),
123+
[name.clone().unwrap().to_string(), "zip".to_string()].join("."),
57124
]
58125
.join("/");
126+
download_from_url(client.clone(), id.clone(), name.unwrap(), path).await;
127+
check_requires(false, path.clone(), requires, client.clone(), yes, modules).await;
59128

60-
println!("Downloading {}", module.name);
61-
println!("Version: {}", &version.version);
62-
63-
download_from_url(client.clone(), version.zip_url, module.name, path).await;
64-
check_requires(path.clone(), requires, client.clone(), yes, modules).await;
65-
66-
let success = yes || confirm("Do you want to continue [y/N]? ");
129+
println!("Info not availabe in url install");
130+
let success = yes || confirm("Do you want to continue [y/N] ");
67131

68132
if success {
69133
let (bin, args) = get_install_cli(&path);
@@ -87,17 +151,27 @@ pub async fn install(client: Client, yes: bool, requires: bool, modules: &Vec<Mo
87151
exit(0);
88152
}
89153
} else {
90-
let name = get_last(_url);
154+
let (_id, _ver) = get_id_details(id);
155+
let module = find_module(&modules, _id.clone());
156+
let version = find_version(module.versions.clone(), _ver);
157+
91158
let path = &[
92159
get_downloads_dir(),
93-
[name.clone().unwrap().to_string(), "zip".to_string()].join("."),
160+
[
161+
[version.version.clone(), module.id].join("-"),
162+
"zip".to_string(),
163+
]
164+
.join("."),
94165
]
95166
.join("/");
96-
download_from_url(client.clone(), id.clone(), name.unwrap(), path).await;
97-
check_requires(path.clone(), requires, client.clone(), yes, modules).await;
98167

99-
println!("Info not availabe in url install");
100-
let success = yes || confirm("\nDo you want to continue [y/N]? ");
168+
println!("Downloading {}", module.name);
169+
println!("Version: {}", &version.version);
170+
171+
download_from_url(client.clone(), version.zip_url, module.name, path).await;
172+
check_requires(false, path.clone(), requires, client.clone(), yes, modules).await;
173+
174+
let success = yes || confirm("Do you want to continue [y/N] ");
101175

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

0 commit comments

Comments
 (0)