-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathinstall.js
More file actions
156 lines (151 loc) · 4.67 KB
/
install.js
File metadata and controls
156 lines (151 loc) · 4.67 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
var http = require('http');
var fs = require('fs');
var os = require('os');
var exec = require('child_process').exec;
var path = require('path');
var AdmZip = require('adm-zip');
var aaptExe = 'aapt';
var doChmod = true;
var targetDir = __dirname + '/tools/';
var dlLink;
var ProgressBar = require('progress');
try {
fs.statSync(targetDir);
} catch (e) {
fs.mkdirSync(targetDir);
}
var platform = null;
var osType = os.type();
switch(osType){
case('Darwin'):
dlLink = 'http://dl.google.com/android/adt/22.6.2/adt-bundle-mac-x86_64-20140321.zip';
platform = 'macosx';
break;
case('Linux'):
if(os.arch() === 'x64'){
dlLink = 'http://dl.google.com/android/adt/22.6.2/adt-bundle-linux-x86_64-20140321.zip';
}else{
dlLink = 'http://dl.google.com/android/adt/22.6.2/adt-bundle-linux-x86-20140321.zip';
}
platform = 'linux';
break;
case('Windows_NT'):
if(os.arch() === 'x64'){
dlLink = 'http://dl.google.com/android/adt/22.6.2/adt-bundle-windows-x86_64-20140321.zip';
}else{
dlLink = 'http://dl.google.com/android/adt/22.6.2/adt-bundle-windows-x86-20140321.zip';
}
platform = 'windows';
aaptExe = 'aapt.exe';
doChmod = false;
break;
default:
throw new Error('Unknown OS "'+osType+'"!');
}
var mkDir = function(fpath, callback){
var dirs = fpath.split(/[\\\/]/);
var root = './';
var mk = function(){
var dir = dirs.shift();
if(dir === ''){
root = path.sep;
}
fs.exists(root+dir, function(exists){
if(!exists){
fs.mkdir(root+dir, function(err){
if(err){
return callback(err);
}
root += dir + path.sep;
if(dirs.length){
return mk();
}else if(callback){
return callback(null, root);
}
});
}else{
root += dir + path.sep;
if(dirs.length){
return mk();
}else if(callback){
return callback(null, root);
}
}
});
};
mk();
};
function attemptDownload(attemptsLeft) {
// If you want the latest and greatest versions then comment out the platform-tools_r16- line and uncomment the next line
// know though that that will download a 500+ MB file and it takes forever
//var url = dlLink;
var url = "http://dl-ssl.google.com/android/repository/platform-tools_r16-" + platform + ".zip";
var tempFile = "./tmp/platform-tools-" + (new Date().getTime()) + ".zip";
if(!attemptsLeft){
throw new Error('Coudln\'t download platform tools in 3 tries... Failing...');
}
attemptsLeft--;
mkDir('tmp', function(err){
if(err){
throw err;
}
var file = fs.createWriteStream(tempFile);
var request = http.get(url, function(response) {
var len = parseInt(response.headers['content-length'], 10);
var bar = new ProgressBar('Downloading Android ADT Bundle [:bar] :percent :current of :total', {
complete: '=',
incomplete: ' ',
width: 20,
total: len
});
response.pipe(file);
response.on('data', function(chunk){
bar.tick(chunk.length);
});
response.on('end', function () {
console.log('\n');
var zip = new AdmZip(tempFile);
mkDir('tools', function(err){
if(err){
throw err;
}
try{
var zipEntries = zip.getEntries();
var entryName = false;
zipEntries.forEach(function(zipEntry) {
if (zipEntry.entryName.match(/aapt/)) {
entryName = zipEntry.entryName;
}
});
if(!entryName){
throw new Error('Couldn\'t locate aapt application.');
}
zip.extractEntryTo(entryName, 'tools', false, true);
if(doChmod){
fs.chmodSync('tools/'+aaptExe, '755');
}
try{
fs.unlinkSync(tempFile);
fs.unlinkSync('tmp');
}catch(e){
console.log('Failed to remove temp file: '+tempFile);
console.log('You might want to delete that to save disk space');
}
}catch(e){
try{
fs.unlinkSync(file);
}catch(e){}
console.error(e);
if (attemptsLeft === 0) {
throw err;
} else {
attemptDownload(attemptsLeft);
return;
}
}
});
});
});
});
}
attemptDownload(3);