-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuilder_android.ts
More file actions
201 lines (168 loc) · 6.71 KB
/
builder_android.ts
File metadata and controls
201 lines (168 loc) · 6.71 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
201
"use strict";
import * as async from "async";
import * as child_process from "child_process";
import * as fse from "fs-extra";
import * as klawSync from "klaw-sync";
import * as path from "path";
import { Builder, IBuilderCallback, IBuildOptions, IBuildOptionsOpts } from "./builder";
import { BuilderError } from "./builder_error";
import { IAndroidKey, ICompilation } from "./compilation";
import * as globals from "./globals";
export interface IAndroidBuildJSON {
android: {
release: {
keystore: string,
storePassword: string,
alias: string,
password: string,
keystoreType: string,
},
};
}
export class AndroidBuilder extends Builder {
protected get getJson(): IAndroidBuildJSON {
const androidKey = this.data.platform.key as IAndroidKey;
return {
android: {
release: {
alias: androidKey.alias,
keystore: path.join(globals.getCertsPath(this.env, this.data.code, this.data.starttime),
this.data.code + ".keystore"),
keystoreType: "",
password: androidKey.aliaspass,
storePassword: androidKey.keystorepass,
},
},
};
}
public constructor(env: globals.CocoonEnvironment, data: ICompilation, configPath: string, logLevel?: string) {
super(env, data, configPath, logLevel);
this.name = "AndroidBuilder";
}
/**
* Starts the build process.
* cb {Function} callback function
*/
protected build(cb: IBuilderCallback): void {
this.logger.debug("[build]");
child_process.execSync("yes | " + path.join(globals.SDKS_PATH(this.env), "android-sdks-linux", "tools", "bin", "sdkmanager") + " --licenses");
const tasks: Array<async.AsyncFunction<any, {} | Error>> = [];
// If there is a key set we get add signing tasks
if (this.data.platform.key) {
tasks.push(
this.createCertsFolder.bind(this),
this.keystore.bind(this),
this.buildJson.bind(this),
);
}
// We compile twice: in debug and release as in debug mode we need to provide
// both debug and release-unsigned APKs to the user
if (!this.data.platform.key) {
tasks.push(async.apply(this.compile.bind(this), this.options(false)));
tasks.push(async.apply(this.compile.bind(this), this.options(true)));
} else {
tasks.push(async.apply(this.compile.bind(this), this.options(true)));
}
// Start building tasks sequence execution
async.waterfall(tasks, (err: BuilderError) => {
if (err) {
cb(err);
return;
}
cb();
});
}
/**
* Zips the compilation result
* cb {Function} callback function
*/
protected pack(cb: IBuilderCallback): void {
this.logger.debug("[pack]");
const buildPath: string = globals.getAndroidResultsPath(this.env, this.data.code, this.data.starttime, this.data.libVersion);
const filterFn = (item: {path: string, stats: object}) => {
if (this.data.platform.key) {
return /\/(android|app)(-armv7|-x86)?-release\.apk$/.test(item.path);
} else {
return /\/(android|app)(-armv7|-x86)?-(debug|release)(-unsigned)?\.apk$/.test(item.path);
}
};
const files = klawSync(buildPath, {nodir: true}).filter(filterFn);
if (files.length === 0) {
cb(new BuilderError(
"No matching APKs found for packaging",
"Internal compiler error (code 0200)"));
}
this.createOutputZip(files.map((file: any) => file.path), cb);
}
protected cleanUp(): void {
this.stopGradleProcesses();
super.cleanUp();
}
protected removeTmpFiles(): void {
this.gradleCleanUp();
super.removeTmpFiles();
}
/** Download the keystore and save it to the certs directory as code.keystore
* cb {Function} callback function
*/
private keystore(cb: IBuilderCallback): void {
this.logger.debug("[build] keystore");
const key: IAndroidKey = this.data.platform.key as IAndroidKey;
const keystore = path.join(globals.getCertsPath(this.env, this.data.code, this.data.starttime),
this.data.code + ".keystore");
this.getBackendFile(key.keystore, keystore, (error: Error) => {
if (error) {
cb(new BuilderError(
"Error downloading the keystore: " + error.message,
"Internal compiler error (code 0201)",
));
return;
}
cb();
});
}
private stopGradleProcesses(): void {
try {
this.logger.debug("Starting gradle daemon stopping processes");
const pathString: string = path.join(globals.getHome(), ".gradle");
const filterFn = (item: {path: string, stats: object}) => {
return /([\\\/])gradle$/.test(item.path);
};
const files = klawSync(pathString, {nodir: true, filter: filterFn});
for (const file of files) {
const gradle = file.path;
try {
this.logger.debug("File: " + gradle + " stdout: " + child_process.execSync(gradle + " --stop"));
} catch (e) {
this.logger.warn("There was an error stopping the gradle daemon: " + e.message
+ "\n from this file " + gradle);
}
}
} catch (e) {
this.logger.error("There was an error finding the gradle executables");
}
}
private gradleCleanUp(): void {
try {
const gradlePath: string = path.join(globals.getHome(), ".gradle", "caches");
this.logger.debug("Gradle cache cleanup: " + gradlePath);
fse.removeSync(gradlePath);
} catch (e) {
this.logger.warn("There was an error removing the Gradle cache folder: " + e.message);
}
}
private options(release: boolean): IBuildOptions {
const opts: IBuildOptionsOpts = {};
opts.release = release;
opts.device = true;
if (this.data.platform.key) {
opts.buildConfig = path.join(globals.getCordovaProjectPath(this.env, this.data.code, this.data.starttime),
"build.json");
}
const options: IBuildOptions = {};
options.verbose = true;
options.platforms = [this.data.platform.name];
options.options = opts;
return options;
}
}