1
1
extern crate serde;
2
2
extern crate serde_ini;
3
3
4
- use crate :: utils:: { confirm, download_from_url, get_last, get_mmrl_json, is_url} ;
5
4
use crate :: android_root:: { get_downloads_dir, get_install_cli} ;
6
5
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 } ;
8
7
use async_recursion:: async_recursion;
8
+ use git2:: Repository ;
9
+ use reqwest:: Client ;
10
+ use std:: fs:: { self , File } ;
9
11
use std:: io:: { BufRead , BufReader , Error , ErrorKind } ;
10
12
use std:: path:: Path ;
11
13
use std:: process:: { exit, Command , Stdio } ;
14
+ use walkdir:: WalkDir ;
12
15
13
16
#[ async_recursion]
14
17
async fn check_requires (
18
+ _is_git : bool ,
15
19
path : String ,
16
20
install_requires : bool ,
17
21
client : Client ,
18
22
yes : bool ,
19
23
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
+ }
22
35
23
36
for req in mini. unwrap ( ) . require {
24
37
let dep_path = Path :: new ( "/data/adb/modules" )
@@ -30,7 +43,7 @@ async fn check_requires(
30
43
if !( dep_path_update. exists ( ) || dep_path. exists ( ) ) {
31
44
if install_requires {
32
45
println ! ( "Install requires" ) ;
33
- install ( client. clone ( ) , yes, install_requires, modules, req) . await ;
46
+ install ( client. clone ( ) , yes, install_requires, modules, req) . await ;
34
47
} else {
35
48
println ! ( "This module requires {} to be installed" , req. clone( ) ) ;
36
49
exit ( 1 )
@@ -39,31 +52,82 @@ async fn check_requires(
39
52
}
40
53
}
41
54
55
+ const METHOD_STORED : Option < zip:: CompressionMethod > = Some ( zip:: CompressionMethod :: Stored ) ;
56
+
42
57
#[ async_recursion]
43
58
pub async fn install ( client : Client , yes : bool , requires : bool , modules : & Vec < Module > , id : String ) {
44
59
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) ;
49
111
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) ;
50
121
let path = & [
51
122
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 ( "." ) ,
57
124
]
58
125
. 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 ;
59
128
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] " ) ;
67
131
68
132
if success {
69
133
let ( bin, args) = get_install_cli ( & path) ;
@@ -87,17 +151,27 @@ pub async fn install(client: Client, yes: bool, requires: bool, modules: &Vec<Mo
87
151
exit ( 0 ) ;
88
152
}
89
153
} 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
+
91
158
let path = & [
92
159
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 ( "." ) ,
94
165
]
95
166
. join ( "/" ) ;
96
- download_from_url ( client. clone ( ) , id. clone ( ) , name. unwrap ( ) , path) . await ;
97
- check_requires ( path. clone ( ) , requires, client. clone ( ) , yes, modules) . await ;
98
167
99
- println ! ( "Info not availabe in url install" ) ;
100
- let success = yes || confirm ( "\n Do 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] " ) ;
101
175
102
176
if success {
103
177
let ( bin, args) = get_install_cli ( & path) ;
0 commit comments