-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
200 lines (163 loc) · 5.02 KB
/
install.js
File metadata and controls
200 lines (163 loc) · 5.02 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
191
192
193
194
195
196
197
198
199
200
var path = require('path'),
fs = require('fs'),
os = require('os'),
Q = null,
shelljs = null,
utils = null,
inquirer = null,
cordova = null,
semver = null;
const OPTIONS_OVERWIRTE = 0,
OPTIONS_RENAME = 1;
module.exports.run = function run(cli, targetPath, projectData) {
var task = new InstallTask(cli, targetPath, projectData);
return task.run();
};
class InstallTask {
constructor(cli, targetPath, projectData) {
this.cli = cli;
this.projectDir = targetPath;
this.projectData = projectData;
Q = cli.require('q');
shelljs = cli.require('shelljs');
inquirer = cli.require('inquirer');
utils = cli.utils;
cordova = cb_require('utils/cordova');
semver = cli.require('semver');
}
run() {
var self = this;
return Q()
.then(function() {
return self.checkForExistingFiles();
})
.then(function(action) {
var promise = null;
if (action === OPTIONS_RENAME) {
promise = self.renameSources();
}
else {
promise = self.cleanSources();
}
if (promise !== null) {
return promise;
}
})
.then(function() {
return self.runCordova();
})
.then(function() {
return self.copySources();
});
// .then(function() {
// var restoreTask = require('./restore');
// return restoreTask.run(self.cli, self.projectDir, self.projectData);
// });
}
cleanSources() {
var srcPath = path.join(this.projectDir, 'platforms', 'android');
if (fs.existsSync(srcPath)) {
shelljs.rm("-rf", srcPath);
}
return Q();
}
copySources() {
var androidVersion = cordova.findCordovaAndroidVersion(this.projectDir),
packagePath = path.join.apply(path, this.projectData.id.split('.')),
kitSrc = path.join(__dirname, "src"),
binPath = path.join(__dirname, "libs"),
assetsPath = path.join(__dirname, "assets"),
targetPath = path.join(this.projectDir, "platforms", "android"),
targetMainPath = path.join(targetPath, 'app', 'src', 'main'),
targetPackageDir = path.join(targetMainPath, "java", "com", "totvs", "appserver"),
targetMActivity = path.join(targetMainPath, "java", packagePath),
targetAssetsPath = path.join(targetMainPath, "assets"),
targetBinPath = path.join(targetMainPath, "jniLibs");
if (semver.lt(androidVersion, '7.0.0')) {
targetPackageDir = path.join(targetPath, "src", "com", "totvs", "appserver");
targetMActivity = path.join(targetPath, "src", packagePath);
targetAssetsPath = path.join(targetPath, "assets");
targetBinPath = path.join(targetPath, "libs");
}
// create directories
shelljs.mkdir('-p', targetPackageDir);
shelljs.mkdir('-p', targetMActivity);
shelljs.mkdir('-p', targetBinPath);
shelljs.mkdir('-p', targetAssetsPath);
console.log('directories created!');
//AppServer.java
shelljs.cp("-f", path.join(kitSrc, "AppServer.java"), targetPackageDir);
console.log('AppServer.java copied!');
var tempFolder = path.join(os.tmpdir(), 'cloudbridge-' + new Date().getTime());
shelljs.mkdir('-p', tempFolder);
shelljs.cp("-f", path.join(kitSrc, "MainActivity.java"), tempFolder);
utils.copyTemplate(tempFolder, targetMActivity, {
project: this.projectData
}, /\.(java)/);
console.log('MainActivity.java copied!');
shelljs.rm('-rf', tempFolder);
//Binario
var jnis = shelljs.ls("-d", path.join(binPath, "*"));
for (var i = 0; i < jnis.length; i++) {
shelljs.cp("-rf", jnis[i], targetBinPath);
}
console.log('libs copied!');
//Assets
var assets = shelljs.ls(path.join(assetsPath, "*"));
for (var j = 0; j < assets.length; j++) {
shelljs.cp("-rf", assets[j], targetAssetsPath);
}
console.log('assets copied!');
}
checkForExistingFiles() {
var deferred = Q.defer(),
targetPath = path.join(this.projectDir, 'platforms', 'android');
if (fs.existsSync(targetPath)) {
inquirer.prompt([{
type: 'list',
name: 'action',
message: ['The directory'.error.bold, targetPath, 'already exists.\n'.error.bold].join(' '),
choices: [
{
name: 'Clean',
value: OPTIONS_OVERWIRTE, //'overwrite',
short: '\nCleaning folder and files...'
},
{
name: 'Rename',
value: OPTIONS_RENAME,// 'rename',
short: '\nRenaming the existing directory and copying the new files...'
},
new inquirer.Separator(' ')
],
default: OPTIONS_RENAME //'rename'
}]).then(function(answers) {
deferred.resolve(answers.action);
});
}
else {
deferred.resolve({});
}
return deferred.promise;
}
renameSources() {
var srcPath = path.join(this.projectDir, 'platforms', 'android'),
targetPath = path.join(this.projectDir, 'platforms', 'android.old');
if (fs.existsSync(targetPath)) {
var count = 1;
while (fs.existsSync(targetPath + '.' + count)) {
count++;
}
targetPath += '.' + count;
}
shelljs.mv(srcPath, targetPath);
return Q();
}
runCordova() {
if (shelljs.exec("ionic cordova platform add android").code != 0) {
shelljs.rm('-rf', path.join(_this.projectDir, "platforms", "android"));
throw new Error("Make sure cordova is installed (npm install -g cordova).");
}
return Q();
}
}