-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
366 lines (313 loc) · 11.7 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// @ts-check
const fetch = require("node-fetch").default;
const { parseXml } = require("@rgrove/parse-xml");
const core = require("@actions/core");
const toolCache = require("@actions/tool-cache");
const child_process = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const ENV_FLEX_HOME = "FLEX_HOME";
const FLEX_TOOL_CACHE_NAME = "apache-flex";
const sdkConfigParseErrorText =
"Failed to parse Apache Flex SDK configuration file";
const sdkConfigURL =
"http://flex.apache.org/installer/sdk-installer-config-4.0.xml";
async function loadSDKConfig() {
const sdkConfigResponse = await fetch(sdkConfigURL);
if (!sdkConfigResponse.ok) {
throw new Error("Failed to load Apache Flex SDK configuration file");
}
const sdkConfigText = await sdkConfigResponse.text();
const sdkConfigXML = parseXml(sdkConfigText);
return sdkConfigXML;
}
async function getMirrorURLPrefix(sdkConfigXML) {
if (!sdkConfigXML || !("children" in sdkConfigXML)) {
throw new Error(sdkConfigParseErrorText);
}
const configXML = sdkConfigXML.children.find((child) => {
return child.type === "element" && child.name === "config";
});
if (!configXML || !("children" in configXML)) {
throw new Error(sdkConfigParseErrorText);
}
const mirrorXML = configXML.children.find((child) => {
return (
child.type === "element" &&
child.name === "mirror" &&
"attributes" in child &&
child.attributes.name === "MirrorURLCGI"
);
});
if (!mirrorXML || !("attributes" in mirrorXML)) {
throw new Error(sdkConfigParseErrorText);
}
const mirrorCGIFileName = mirrorXML.attributes.file;
const mirrorCGIURL = `https://flex.apache.org/${mirrorCGIFileName}`;
const mirrorResponse = await fetch(mirrorCGIURL);
if (!mirrorResponse.ok) {
throw new Error("Failed to load mirror for Apache Flex SDK");
}
return await mirrorResponse.text();
}
function getFlexSDKProducts(sdkConfigXML) {
if (!sdkConfigXML || !("children" in sdkConfigXML)) {
throw new Error();
}
const configXML = sdkConfigXML.children.find((child) => {
return child.type === "element" && child.name === "config";
});
if (!configXML || !("children" in configXML)) {
throw new Error(sdkConfigParseErrorText);
}
const productsXML = configXML.children.find((child) => {
return child.type === "element" && child.name === "products";
});
if (!productsXML || !("children" in productsXML)) {
throw new Error(sdkConfigParseErrorText);
}
const apacheFlexXML = productsXML.children.find((child) => {
return child.type === "element" && child.name === "ApacheFlexSDK";
});
if (!apacheFlexXML || !("attributes" in apacheFlexXML)) {
throw new Error(sdkConfigParseErrorText);
}
return apacheFlexXML;
}
function getFlexVersionLetterBestMatch(/** @type string */ expectedVersion, apacheFlexXML) {
if (!apacheFlexXML || !("children" in apacheFlexXML)) {
throw new Error(sdkConfigParseErrorText);
}
const versionsXML = apacheFlexXML.children.find((child) => {
return child.type === "element" && child.name === "versions";
});
if (!versionsXML || !("children" in versionsXML)) {
throw new Error(sdkConfigParseErrorText);
}
const versionLettersXML = versionsXML.children.filter((child) => {
return child.type === "element" && child.name.startsWith("version");
});
if (versionLettersXML.length === 0) {
throw new Error(sdkConfigParseErrorText);
}
// default order is from oldest to newest, so let's reverse that
versionLettersXML.reverse();
let bestMatch = null;
const requestedParts = expectedVersion.split(".");
for (let releaseXML of versionLettersXML) {
if (!("attributes" in releaseXML)) {
continue;
}
const releaseVersion = releaseXML.attributes.version;
const releaseParts = releaseVersion.split(".");
let matched = true;
for (let i = 0; i < requestedParts.length; i++) {
if (requestedParts[i] != releaseParts[i]) {
matched = false;
break;
}
}
if (matched) {
// this assumes that the releases are in order from newest to oldest
bestMatch = releaseXML;
break;
}
}
if (bestMatch == null) {
throw new Error(`Apache Flex SDK version '${expectedVersion}' not found`);
}
return bestMatch;
}
function getAIRVersionBestMatch(/** @type string */ airVersion, /** @type {any[]} */ releases) {
let bestMatch = null;
const requestedParts = airVersion.split(".");
for (let release of releases) {
const releaseName = release.name;
const releaseParts = releaseName.split(".");
let matched = true;
for (let i = 0; i < requestedParts.length; i++) {
if (requestedParts[i] != releaseParts[i]) {
matched = false;
break;
}
}
if (matched) {
// this assumes that the releases are in order from newest to oldest
bestMatch = releaseName;
break;
}
}
if (bestMatch == null) {
throw new Error(`Adobe AIR SDK (HARMAN) version '${airVersion}' not found`);
}
return bestMatch;
}
function getFlexVersionLetterURL(versionLetterXML, mirrorPrefix) {
let url = `${versionLetterXML.attributes.path}${versionLetterXML.attributes.file}`;
if (!/^https?:\/\//.test(url)) {
url = `${mirrorPrefix}/${url}`;
}
if (process.platform.startsWith("darwin") || process.platform.startsWith("linux")) {
url += ".tar.gz";
} else if (process.platform.startsWith("win")) {
url += ".zip";
} else {
throw new Error(
`Apache Flex SDK setup is not supported on platform: ${process.platform}`
);
}
return url;
}
async function setupApacheFlex() {
try {
const acceptAIRLicense = core.getInput("accept-air-license", { required: true });
if (!acceptAIRLicense) {
throw new Error(
"Parameter `accept-air-license` must be true to accept the Adobe AIR SDK License Agreement. Find it here: https://airsdk.harman.com/assets/pdfs/HARMAN%20AIR%20SDK%20License%20Agreement.pdf"
);
}
const licenseFile = core.getInput("air-license-base64", { required: false });
if (licenseFile) {
const licenseBuffer = Buffer.from(licenseFile, "base64");
const licensePath = path.join(os.homedir(), ".airsdk", "adt.lic");
fs.mkdirSync(path.dirname(licensePath), { recursive: true });
fs.writeFileSync(licensePath, licenseBuffer);
}
const flexVersion = core.getInput("flex-version", { required: true });
const sdkConfigXML = await loadSDKConfig();
const mirrorURLPrefix = await getMirrorURLPrefix(sdkConfigXML);
const apacheFlexXML = getFlexSDKProducts(sdkConfigXML);
const flexHome = await downloadFlexSDK(flexVersion, mirrorURLPrefix, apacheFlexXML);
const airVersion = core.getInput("air-version", { required: true });
const parsedMajorVersion = parseInt(airVersion.split(".")[0], 10);
if (parsedMajorVersion <= 32) {
// try to set up an old Adobe version of the AIR SDK
setupApacheFlexWithAdobeAIR(airVersion, flexHome);
return;
}
await setupApacheFlexWithHarmanAIR(airVersion, flexHome);
} catch (error) {
core.setFailed(error.message);
}
}
async function downloadFlexSDK(/** @type string */ flexVersion, /** @type string */ mirrorURLPrefix, /** @type any */ apacheFlexXML) {
const flexVersionLetterXML = getFlexVersionLetterBestMatch(flexVersion, apacheFlexXML);
core.info("Apache Flex SDK version: " + flexVersionLetterXML.attributes.version);
let cacheLocation = toolCache.find(FLEX_TOOL_CACHE_NAME, flexVersion);
if (cacheLocation) {
core.debug(`Resolved Apache Flex SDK ${flexVersion} from tool-cache at: ${cacheLocation}`);
} else {
core.debug(
`Apache Flex SDK ${flexVersion} was not found in tool-cache. Trying to download...`
);
const flexDownloadURL = getFlexVersionLetterURL(
flexVersionLetterXML,
mirrorURLPrefix
);
const flexDownloadFileName = path.basename(
new URL(flexDownloadURL).pathname
);
const downloadedPath = await toolCache.downloadTool(
flexDownloadURL,
flexDownloadFileName
);
const installLocation = process.platform.startsWith("win")
? "c:\\ApacheFlexSDK"
: "/usr/local/bin/ApacheFlexSDK";
fs.mkdirSync(installLocation);
if (process.platform.startsWith("darwin") || process.platform.startsWith("linux")) {
await toolCache.extractTar(downloadedPath, installLocation);
} else if (process.platform.startsWith("win")) {
await toolCache.extractZip(downloadedPath, installLocation);
}
let extractedLocation = installLocation;
if (process.platform.startsWith("darwin") || process.platform.startsWith("linux")) {
const baseFileName = flexDownloadFileName.substr(
0,
flexDownloadFileName.length - 7 //.tar.gz
);
extractedLocation = path.resolve(installLocation, baseFileName);
}
cacheLocation = await toolCache.cacheDir(
extractedLocation,
FLEX_TOOL_CACHE_NAME,
flexVersion
);
core.debug(
`Apache Flex SDK ${flexVersion} added to tool-cache at: ${cacheLocation}`
);
}
core.addPath(path.resolve(cacheLocation, "bin"));
core.exportVariable(ENV_FLEX_HOME, cacheLocation);
return cacheLocation;
}
async function setupApacheFlexWithHarmanAIR(/** @type string */ airVersion, /** @type string */ flexHome) {
const releasesResponse = await fetch(
"https://dcdu3ujoji.execute-api.us-east-1.amazonaws.com/production/releases"
);
const releases = await releasesResponse.json();
airVersion = getAIRVersionBestMatch(airVersion, releases.releases);
core.info(`Adobe AIR SDK (HARMAN) version: ${airVersion}`);
const urlsResponse = await fetch(
`https://dcdu3ujoji.execute-api.us-east-1.amazonaws.com/production/releases/${airVersion}/urls`
);
const urls = await urlsResponse.json();
var urlField = null;
if (process.platform.startsWith("darwin")) {
urlField = "AIR_Flex_Mac";
} else if (process.platform.startsWith("win")) {
urlField = "AIR_Flex_Win";
} else if (process.platform.startsWith("linux")) {
urlField = "AIR_Flex_Linux";
}
if (!urlField) {
// this probably shouldn't happen, but best to be safe
throw new Error(
`Adobe AIR SDK version '${airVersion}' not found for platform ${process.platform}`
);
}
core.debug(`Adobe AIR SDK type: ${urlField}`);
const urlPath = urls[urlField];
if (!urlPath) {
throw new Error(
`Adobe AIR SDK download URL for '${urlField}' version '${airVersion}' not found`
);
}
const archiveUrl = `https://airsdk.harman.com${urlPath}?license=accepted`;
const filename = path.basename(new URL(archiveUrl).pathname);
const downloadedPath = path.resolve(process.cwd(), await toolCache.downloadTool(archiveUrl, filename));
const foundMajorMinor = /\d+\.\d+/.exec(airVersion);
if (foundMajorMinor === null) {
throw new Error(
`Failed to parse Adobe AIR SDK version: ${airVersion}`
);
}
const airSDKMajorMinor = foundMajorMinor[0];
const antScriptPath = path.resolve(__dirname, "harman-installer.xml");
child_process.execSync(
`ant -f ${antScriptPath} -Dflexsdk=${flexHome} -Dairsdk.zip=${downloadedPath} -Dair.sdk.version=${airSDKMajorMinor}`,
{
cwd: flexHome,
stdio: "inherit",
}
);
}
function setupApacheFlexWithAdobeAIR(/** @type string */ airVersion, /** @type string */ flexHome) {
if (airVersion == "32") {
airVersion += ".0";
}
if (airVersion != "32.0") {
throw new Error(
`Expected Adobe AIR major version 32 or newer. Received version: ${airVersion}`
);
}
child_process.execSync(
"ant -f installer.xml -Dflash.sdk.version=32.0 -Dair.sdk.version=32.0 -Dinstaller=true -Ddo.flash.install=1 -Ddo.air.install=1 -Ddo.swfobject.install=1 -Ddo.fontswf.install=1 -Ddo.osmf.install=1 -Ddo.ofl.install=1",
{
cwd: flexHome,
stdio: "inherit",
}
);
}
setupApacheFlex();