forked from cfourney/OpenHarmony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenHarmony_install.js
282 lines (221 loc) · 8.48 KB
/
openHarmony_install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
function openHarmony_install_main(){
try{
var d = new Dialog();
d.title = "Install/Update OpenHarmony";
d.cancelButtonText = "Cancel";
//Standard install paths.
var install_base = specialFolders.userScripts;
var library_base = install_base;
var libdir = new Dir(library_base);
var label = new Label;
label.text = "Install openHarmony libraries?\n";
d.add( label );
if ( !d.exec() ){
return;
}
if( !libdir.exists ){
MessageBox.information( "Failed to create folders for openHarmony - please check permissions." );
}
var apic = api_call( "https://api.github.com/repos/cfourney/OpenHarmony/branches" );
if( !apic ){
MessageBox.information( "API Error - Failed to get available branches." );
return;
}
var branch_names = apic.map(function(x, index){return index+": "+x.name.toUpperCase()})
var res = Input.getItem( "Which Branch", branch_names );
if( !res ){ return; }
var branch_data = false;
var fnd = false;
for( var x=0;x<apic.length;x++ ){
var fname = (x+1)+": "+apic[x].name.toUpperCase();
if( fname == res ){
fnd = apic[x]["commit"]["sha"];
branch_data = apic[x].name.toUpperCase();
branch_data = branch_data + "\n" + fnd;
branch_data = branch_data + "\n" + new Date();
break;
}
}
if( !fnd ){
MessageBox.information( "Error - This shouldn't have happened." );
return;
}
var contents_url = "https://api.github.com/repos/cfourney/OpenHarmony/contents?ref="+fnd;
var apic = api_call( contents_url );
if( !apic ){
MessageBox.information( "API Error - Failed to get available branch content." );
return;
}
if( apic && apic.length ){
//Download the files.
var progress = new QProgressDialog();
progress.setLabelText( "Downloading files..." );
progress.show();
progress.setRange( 0, 100 );
var file_download_listing = recurse_files( apic, [] );
progress.setRange( 0, file_download_listing.length );
var oh_install = specialFolders.userScripts + "/" + "openHarmony_install";
var libdir = new Dir( oh_install );
if( !libdir.exists ) libdir.mkdirs();
if( libdir.exists ){
var oh_install_file = oh_install + "/" + "OpenHarmony";
var file = new File( oh_install_file );
try {
var content = [ fnd ];
for( var n=0;n<file_download_listing.length;n++ ){
var path = file_download_listing[n][1];
var local_path = library_base+'/'+path;
content.push( local_path );
}
file.open(FileAccess.WriteOnly);
file.write( content.join("\n") );
file.close();
} catch (err) {}
}
for( var n=0;n<file_download_listing.length;n++ ){
progress.setValue( n );
QCoreApplication.processEvents();
var raw_url = file_download_listing[n][0];
var path = file_download_listing[n][1];
var local_path = library_base+'/'+path;
if( path.slice( 0, path.indexOf("/") ) == "tools" ){
//Dont include the tools directory, these will be installed as needed.
continue;
}
if( path.slice( 0, path.indexOf("/") ) == "docs" ){
//Dont include the docs directory, these will be installed as needed.
continue;
}
var local_dir = local_path.slice( 0, local_path.lastIndexOf("/") );
var libdir = new Dir(local_dir);
if( !libdir.exists ){
libdir.mkdirs();
}
if( !libdir.exists ){
MessageBox.information( "Failed to create openHarmony directory: " + local_dir );
progress.accept();
return;
}
//Override path of active scripts to ensure they're accessible to the user.
//----------------------------------------------------------------------------
var downloaded = download( raw_url, local_path );
if( !downloaded ){
MessageBox.information( "Failed to create openHarmony file: " + local_path );
progress.accept();
return;
}
}
progress.accept();
if( (new File( local_dir + "/" + "openHarmony.js" )).exists ){
MessageBox.information( "OpenHarmony successfully installed." );
preferences.setString( 'openHarmonyInclude', ( local_dir + "/" + "openHarmony.js" ) );
preferences.setString( 'openHarmonyPath', ( local_dir ) );
try {
var install_path = local_dir + "/" + "INSTALL";
var file = new File(install_path);
file.open(FileAccess.Append);
file.write( branch_data );
file.close();
}catch (err){
}
}else{
MessageBox.information( "Unable to find OpenHarmony in install path: " + (local_dir + "/" + "openHarmony.js") );
}
}
}catch( err ){
MessageBox.information( "Failed to install: " + err + " ("+ err.lineNumber +")" );
}
function recurse_files( contents, arr_files ){
for( var n=0;n<contents.length;n++ ){
var tfl = contents[n];
if( contents[n].type == "file" ){
arr_files.push( [tfl["download_url"], tfl["path"]] );
}else if( contents[n].type == "dir" ){
//Get more contents.
QCoreApplication.processEvents();
var apic = api_call( tfl["url"] );
if( apic ){
arr_files = recurse_files( apic, arr_files );
}else{
return false;
}
}
}
return arr_files;
}
function api_call( address ){
try{
var avail_paths = [
"c:\\Windows\\System32\\curl.exe"
];
if( !about.isWindowsArch() ){
avail_paths = [
"/usr/bin/curl",
"/usr/local/bin/curl"
];
}
var curl_path = false;
for( var n=0;n<avail_paths.length;n++ ){
if( ( new File(avail_paths[n]) ).exists ){
curl_path = avail_paths[n];
break;
}
}
if (!curl_path){
MessageBox.information("curl is required and is not found on this machine. Install curl to continue.\n\n Download available at : "+(about.isWindowsArch?"https://curl.haxx.se/windows/":"https://curl.haxx.se/download.html"))
return false;
}
var cmdline = [ "-L", address ];
var p = new QProcess();
// p.setWorkingDirectory( pathToTemplate );
p.start( curl_path, cmdline );
p.waitForFinished( 10000 );
try{
var readOut = JSON.parse((new QTextStream(p.readAllStandardOutput())).readAll());
return readOut;
}catch(err){
return false;
}
}catch( err ){
System.println( err + " ("+address.lineNumber+")" );
MessageLog.trace( err + " ("+address.lineNumber+")" );
}
}
function download( address, target ){
try{
var avail_paths = [
"c:\\Windows\\System32\\curl.exe"
];
if( !about.isWindowsArch() ){
avail_paths = [
"/usr/bin/curl",
"/usr/local/bin/curl"
];
}
var curl_path = false;
for( var n=0;n<avail_paths.length;n++ ){
if( ( new File(avail_paths[n]) ).exists ){
curl_path = avail_paths[n];
break;
}
}
if( !curl_path ){
MessageBox.information( "Unable to find application for web hook." );
return false;
}
var file = new File( target );
if( file.exists ){
file.remove();
}
var cmdline = [ "-L", "-o", target, address ];
var p = new QProcess();
p.start( curl_path, cmdline );
p.waitForFinished( 10000 );
var file = new File( target );
return file.exists;
}catch( err ){
System.println( err + "("+err.lineNumber+")" );
MessageLog.trace( err + " ("+address.lineNumber+")" );
}
}
}