forked from joshmarinacci/ElectronIDE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibraries.js
More file actions
190 lines (173 loc) · 5.74 KB
/
Copy pathlibraries.js
File metadata and controls
190 lines (173 loc) · 5.74 KB
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
var fs = require('fs');
var spawn = require('child_process').spawn;
var http = require('http');
var AdmZip = require('adm-zip');
var platform = require('./platform');
var settings = require('./settings.js');
var master = null;
var libs = null;
var plat = platform.getDefaultPlatform();
function isInstalled() {
console.log('checking if',this.id,'is installed');
if(this.source == 'ide') return true;
var path = plat.getReposPath()+'/'+this.id;
console.log('checking if path exists',path);
if(fs.existsSync(plat.getReposPath()+'/'+this.id)) return true;
return false;
}
function getIncludePaths(platform) {
if(this.source == 'ide') {
var path = platform.getStandardLibraryPath()+'/'+this.location;
//console.log("files = ",fs.readdirSync(path));
var paths = [];
paths.push(path);
fs.readdirSync(path).forEach(function(filename) {
if(fs.statSync(path+'/'+filename).isDirectory()) {
if(filename != 'examples') {
//console.log("found a subdir of files. use it",filename);
paths.push(path+'/'+filename);
}
}
});
return paths;
}
if(this.path) {
return [plat.getReposPath()+'/'+this.id+'/'+this.path];
}
return [plat.getReposPath()+'/'+this.id];
}
function install(cb) {
if(!fs.existsSync(plat.getReposPath())) {
fs.mkdirSync(plat.getReposPath());
}
console.log('installing',this.id);
if(this.source == 'git') {
var bin = 'git';
var cmd = [
'clone',
this.location,
plat.getReposPath()+'/'+this.id,
];
console.log("execing",bin,cmd);
var proc = spawn(bin,cmd);
proc.stdout.on('data',function(data) {
console.log("STDOUT",data.toString());
});
proc.stderr.on('data',function(data) {
console.log("STDERR",data.toString());
});
proc.on('close',function(code) {
console.log("exited with code",code);
if(cb) cb(null);
});
}
if(this.source == 'http'){
console.log("source is http",this.location);
var outpath = plat.getReposPath();
var outfile = plat.getReposPath()+'/'+this.location.substring(this.location.lastIndexOf('/')+1);
console.log("output file = ",outfile);
var req = http.get(this.location)
.on('response',function(res){
console.log("response");
res.pipe(fs.createWriteStream(outfile)).on('close',function(){
console.log('finished downloading');
var zip = new AdmZip(outfile);
var zipEntries = zip.getEntries();
var rootpath = zipEntries[0].entryName;
rootpath = rootpath.substring(0,rootpath.indexOf('/'));
console.log("rootpath of the zip is",rootpath);
zip.extractAllTo(plat.getReposPath(),true);
console.log('done extracting from ',outfile, 'to',plat.getReposPath());
fs.renameSync(plat.getReposPath()+'/'+rootpath, plat.getReposPath()+'/'+rootpath.toLowerCase());
if(cb) cb(null);
});
});
req.end();
}
}
function init() {
if(libs == null) {
libs = [];
fs.readdirSync(settings.datapath).forEach(function(file){
var str = fs.readFileSync(settings.datapath+'/'+file).toString();
var lib = JSON.parse(str);
lib.isInstalled = isInstalled;
lib.install = install;
lib.getIncludePaths = getIncludePaths;
libs.push(lib);
});
}
}
init();
function collectdeps(lib,deps) {
if(lib.dependencies) {
for(var i=0; i<lib.dependencies.length; i++) {
var dep = lib.dependencies[i];
var deplib = exports.getById(dep);
console.log("dep lib = ",dep,deplib);
if(deplib && !deplib.isInstalled()) {
deps.push(deplib);
};
collectdeps(deplib,deps);
}
}
}
exports.install = function(targets, cb) {
console.log("installing libraries: ", targets);
var toinstall = targets
.map(function(libname) {
console.log("looking at libname", libname);
return exports.getById(libname); })
.filter(function(lib) {
if(lib == null) return false;
return !lib.isInstalled();
});
//console.log("need to install", toinstall);
var deps = [];
toinstall.forEach(function(lib) {
collectdeps(lib,deps);
});
//console.log('deps = ',deps);
toinstall = toinstall.concat(deps);
function installit(list) {
//console.log('list to install',list);
if(list.length <= 0) {
//console.log("done with the list");
cb(null);
return;
}
var lib = list.shift();
//console.log("installing",lib);
lib.install(function() {
console.log("installed lib");
installit(list);
})
}
installit(toinstall);
//cb(null);
}
exports.search = function(str,cb) {
str = str.toLowerCase();
var results = [];
libs.forEach(function(lib) {
if(lib.name.toLowerCase().indexOf(str)>=0) {
results.push(lib);
return;
}
for(var i=0; i<lib.tags.length; i++) {
if(lib.tags[i].toLowerCase().indexOf(str)>=0) {
results.push(lib);
return;
}
}
});
cb(results);
}
exports.getById = function(id) {
for(var i=0; i<libs.length; i++) {
if(libs[i].id == id.toLowerCase()) {
return libs[i];
}
}
return null;
}