This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
720 lines (647 loc) · 18.9 KB
/
index.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
const core = require("@actions/core");
const exec = require("@actions/exec");
const io = require("@actions/io");
const fetch = require("node-fetch");
const fs = require("fs");
const semver = require("semver");
const os = require("os");
const path = require("path");
const temp = os.tmpdir();
const SCOPE_DSN = "SCOPE_DSN";
const scopeDir = temp + "/.scope_dir";
const derivedDataPath = scopeDir + "/derived";
const xctestDir = derivedDataPath + "/Build/Products/";
const testrunJson = scopeDir + "/testrun.json";
const scope_ios_path = "/scopeAgent/ios";
const scope_macos_path = "/scopeAgent/mac";
const scope_tvos_path = "/scopeAgent/tvos";
let envVars = Object.assign({}, process.env);
async function run() {
try {
let dsn = core.getInput("dsn") || process.env[SCOPE_DSN];
while (dsn.charAt(dsn.length - 1) == "/") {
dsn = dsn.substring(0, dsn.length - 1);
}
if (dsn) {
envVars[SCOPE_DSN] = dsn;
}
let platform = core.getInput("platform") || "ios";
platform = platform.toLowerCase();
const scopeFrameworkPath = getPathForPlatform(platform);
const scopeFrameworkToolsPath = getToolsPathForPlatform(platform);
const sdk = core.getInput("sdk") || getSDKForPlatform(platform);
const destination =
core.getInput("destination") || getDestinationForPlatform(platform);
const configuration = core.getInput("configuration") || "Debug";
const agentVersion = core.getInput("agentVersion");
const codePathEnabled = core.getInput("codePath") === "true";
const extraParameters = core.getInput("extraParameters") || "";
if (codePathEnabled) {
//If project uses testplan force use of code coverage
let file_list = recFindByExt(".", "xctestplan");
for (let testPlanFile of file_list) {
await deleteLinesContaining(testPlanFile, "codeCoverage");
}
}
//Create folder to store files
if (!fs.existsSync(scopeDir)) {
fs.mkdirSync(scopeDir);
}
//Read project
const workspace = await getWorkspace();
let xcodeproj = await getXCodeProj();
var projectParameter;
if (workspace) {
console.log(`Workspace selected: ${workspace}`);
projectParameter = "-workspace " + `"${workspace}"`;
} else if (xcodeproj) {
console.log(`Project selected: ${xcodeproj}`);
projectParameter = "-project " + `"${xcodeproj}"`;
} else if (fs.existsSync("Package.swift")) {
if (core.getInput("forceSPM") === "true") {
await swiftPackageRun(extraParameters, codePathEnabled, agentVersion);
return;
} else {
xcodeproj = await generateProjectFromSPM();
projectParameter = "-project " + `"${xcodeproj}"`;
}
} else {
core.setFailed(
"Unable to find workspace, project or Swift package file. Please set with workspace or xcodeproj"
);
}
const scheme = await getScheme(workspace, xcodeproj);
console.log(`Scheme selected: ${scheme}`);
//copy configfile
const configfileName = "scopeConfig.xcconfig";
const configFilePath = scopeDir + "/" + configfileName;
createXCConfigFile(configFilePath, scopeFrameworkPath);
//download scope
await downloadLatestScope(agentVersion);
let codeCoverParam = "";
if (codePathEnabled) {
codeCoverParam = "-enableCodeCoverage YES";
}
//build for testing
let buildCommand =
"xcodebuild build-for-testing " +
codeCoverParam +
" -xcconfig " +
configFilePath +
" " +
projectParameter +
" -configuration " +
configuration +
" -scheme " +
`"${scheme}"` +
" -sdk " +
sdk +
" -derivedDataPath " +
derivedDataPath +
" -destination " +
`"${destination}" ` +
extraParameters;
const result = await exec.exec(buildCommand, null, null);
uploadSymbols(projectParameter, dsn, scopeFrameworkToolsPath);
//Fol all testruns that are configured
let testRuns = await getXCTestRuns();
let testError;
for (const testRun of testRuns) {
//modify xctestrun with Scope variables
let plutilExportCommand =
"plutil -convert json -o " + testrunJson + ` "${testRun}"`;
await exec.exec(plutilExportCommand, null, null);
let jsonString = fs.readFileSync(testrunJson, "utf8");
const testTargets = JSON.parse(jsonString);
for (const target of Object.keys(testTargets)) {
if (target.charAt(0) !== "_") {
if (testTargets[target].TestingEnvironmentVariables) {
await insertEnvVariables(testRun, target);
} else if (target === "TestConfigurations") {
let configurationNumber = 0;
for (const configuration of testTargets["TestConfigurations"]) {
let testNumber = 0;
for (const test of configuration["TestTargets"]) {
await insertEnvVariables(
testRun,
target +
"." +
configurationNumber +
"." +
"TestTargets" +
"." +
testNumber,
dsn
);
}
}
}
}
}
//run tests
let testCommand =
"xcodebuild test-without-building " +
codeCoverParam +
" -xctestrun " +
`"${testRun}"` +
' -destination "' +
destination +
'"' +
extraParameters;
try {
await exec.exec(testCommand, null, null);
} catch (error) {
testError = error.message;
}
}
if (codePathEnabled) {
//build command settings
let buildCommandSettings =
"xcodebuild -showBuildSettings -json -configuration " +
configuration +
" build-for-testing -xcconfig " +
configFilePath +
" " +
projectParameter +
" -scheme " +
`"${scheme}"` +
" -sdk " +
sdk +
" -derivedDataPath " +
derivedDataPath +
" -destination " +
`"${destination}" ` +
extraParameters;
let auxOutput = "";
const options = {};
options.listeners = {
stdout: data => {
auxOutput += data.toString();
}
};
await exec.exec(buildCommandSettings, null, options);
const settingsArray = JSON.parse(auxOutput);
for (const settings of settingsArray) {
if (
settings.buildSettings["PACKAGE_TYPE"] !==
"com.apple.package-type.bundle.unit-test"
) {
await runScopeCoverageWithSettings(
settings.buildSettings,
false,
scopeFrameworkToolsPath
);
}
}
}
if (!dsn) {
core.warning(
"SCOPE_DSN not found in secrets, results wont be uploaded to Scope app"
);
}
if (testError) {
core.setFailed(testError.message);
}
} catch (error) {
core.setFailed(error.message);
}
//Clean up
fs.rmdirSync(scopeDir, { recursive: true });
}
async function swiftPackageRun(extraParameters, codePathEnabled, agentVersion) {
//download scope
await downloadLatestScope(agentVersion);
let codeCoverParam = "";
if (codePathEnabled) {
codeCoverParam = " --enable-code-coverage ";
}
const scopeMacFrameworkPath = scopeDir + scope_macos_path;
const scopeMacFrameworkToolsPath =
scope_macos_path + "/ScopeAgent.framework/Resources/";
//build and test
let buildTestCommand =
"swift test " +
codeCoverParam +
" -Xswiftc " +
"-F" +
scopeMacFrameworkPath +
" " +
" -Xswiftc -framework -Xswiftc ScopeAgent -Xlinker -rpath -Xlinker " +
scopeMacFrameworkPath +
" " +
extraParameters;
let testError;
try {
await exec.exec(buildTestCommand, null, {
env: {
...envVars,
SCOPE_COMMIT_SHA: envVars["GITHUB_SHA"],
SCOPE_SOURCE_ROOT: envVars["GITHUB_WORKSPACE"]
}
});
} catch (error) {
testError = error.message;
}
if (testError) {
core.setFailed(testError.message);
}
// Upload symbols
let runScriptCommand =
"sh -c " + scopeDir + scopeMacFrameworkToolsPath + "upload_symbols";
exec.exec(runScriptCommand, null, {
env: {
...envVars,
TARGET_BUILD_DIR:
process.env["GITHUB_WORKSPACE"] + "/.build/x86_64-apple-macosx/debug",
CONFIGURATION_BUILD_DIR:
process.env["GITHUB_WORKSPACE"] + "/.build/x86_64-apple-macosx/debug"
},
ignoreReturnCode: true
});
if (codePathEnabled) {
await runScopeCoverageWithSettings(null, true, scopeMacFrameworkToolsPath);
}
}
function getPathForPlatform(platform) {
switch (platform) {
case "macos":
case "mac":
return scope_macos_path;
case "tvos":
return scope_tvos_path;
default:
return scope_ios_path;
}
}
function getToolsPathForPlatform(platform) {
switch (platform) {
case "macos":
case "mac":
return scope_macos_path + "/ScopeAgent.framework/Resources/";
case "tvos":
return scope_tvos_path + "/ScopeAgent.framework/";
default:
return scope_ios_path + "/ScopeAgent.framework/";
}
}
function getSDKForPlatform(platform) {
switch (platform) {
case "macos":
case "mac":
return "macosx";
case "tvos":
return "appletvsimulator";
default:
return "iphonesimulator";
}
}
function getDestinationForPlatform(platform) {
switch (platform) {
case "macos":
case "mac":
return "platform=macOS,arch=x86_64";
case "tvos":
return "platform=tvOS Simulator,name=Apple TV 4K";
default:
return "platform=iOS Simulator,name=iPhone 11";
}
}
async function getWorkspace() {
let workspace = core.getInput("workspace");
if (!workspace) {
let myOutput = "";
const options = {};
options.listeners = {
stdout: data => {
myOutput += data.toString();
workspace = myOutput.split("\n").find(function(file) {
return file.match(/\.xcworkspace$/);
});
}
};
await exec.exec("ls", null, options);
}
return workspace;
}
async function getXCodeProj() {
let xcodeproj = core.getInput("xcodeproj");
if (!xcodeproj) {
let myOutput = "";
const options = {};
options.listeners = {
stdout: data => {
myOutput += data.toString();
xcodeproj = myOutput.split("\n").find(function(file) {
return file.match(/\.xcodeproj/);
});
}
};
await exec.exec("ls", null, options);
}
return xcodeproj;
}
async function generateProjectFromSPM() {
let generateProjectCommand = "swift package generate-xcodeproj";
const result = await exec.exec(generateProjectCommand, null, null);
console.log(`SPM package`);
xcodeproj = await getXCodeProj();
return xcodeproj;
}
async function getScheme(workspace, xcodeproj) {
let scheme = core.getInput("scheme");
if (!scheme) {
let command;
if (workspace) {
command = "xcodebuild -workspace " + workspace + " -list -json";
} else {
command = "xcodebuild -project " + xcodeproj + " -list -json";
}
let myOutput = "";
const options = {};
options.listeners = {
stdout: data => {
myOutput += data.toString();
}
};
try {
await exec.exec(command, null, options);
} catch (error) {
core.setFailed(
"Unable to automatically select a Scheme. Please set with .scheme parameter"
);
throw error;
}
const info = JSON.parse(myOutput);
const aux = info.workspace || info.project;
const schemes = aux.schemes;
console.log("Available schemes: " + JSON.stringify(schemes));
scheme = intelligentSelectScheme(schemes, aux);
}
return scheme;
}
function intelligentSelectScheme(schemes, workspacePath) {
if (schemes.length < 1) {
return null;
}
const workspaceName = workspacePath.name;
if (schemes.includes(workspaceName)) {
return workspaceName;
}
var el = schemes.find(a => a.includes(workspaceName));
return el || schemes[0];
}
function createXCConfigFile(path, scopeFrameworkPath) {
let configText =
`
// Configuration settings file format documentation can be found at:
// https://help.apple.com/xcode/#/dev745c5c974
DEBUG_INFORMATION_FORMAT = dwarf-with-dsym
` +
"FRAMEWORK_SEARCH_PATHS = $(inherited) " +
scopeDir +
scopeFrameworkPath +
"\n" +
"OTHER_LDFLAGS = $(inherited) -ObjC -framework ScopeAgent\n" +
"LD_RUNPATH_SEARCH_PATHS = $(inherited) " +
scopeDir +
scopeFrameworkPath +
"\n";
fs.writeFileSync(path, configText, null);
}
async function downloadLatestScope(agentVersion) {
const versionsUrl =
"https://releases.undefinedlabs.com/scope/agents/ios/ScopeAgent.json";
const jsonResponse = await fetch(versionsUrl);
const versions = await jsonResponse.json();
let currentVersion = "0.0.1";
Object.keys(versions).forEach(function(name) {
if (semver.gt(name, currentVersion) && !semver.prerelease(name)) {
currentVersion = name;
}
});
const scopeURL = versions[agentVersion] || versions[currentVersion];
const scopePath = scopeDir + "/scopeAgent.zip";
console.log(`Scope agent downloading: ${scopeURL}`);
await downloadFile(scopeURL, scopePath);
const extractCommand =
"ditto -x -k " + scopePath + " " + scopeDir + "/scopeAgent";
await exec.exec(extractCommand, null, null);
}
const downloadFile = async (url, path) => {
const res = await fetch(url);
const fileStream = fs.createWriteStream(path);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on("error", err => {
reject(err);
});
fileStream.on("finish", function() {
resolve();
});
});
};
function uploadSymbols(projectParameter, dsn, scopeFrameworkToolsPath) {
let runScriptCommand =
"sh -c " + scopeDir + scopeFrameworkToolsPath + "upload_symbols";
exec.exec(runScriptCommand, null, {
env: {
...envVars,
TARGET_BUILD_DIR: xctestDir,
CONFIGURATION_BUILD_DIR: xctestDir
},
ignoreReturnCode: true
});
}
async function getXCTestRuns() {
let myOutput = "";
let testRuns = [""];
const options = {};
options.listeners = {
stdout: data => {
myOutput += data.toString();
testRuns = myOutput.split("\n").filter(function(file) {
return file.match(/\.xctestrun$/);
});
}
};
await exec.exec("ls " + xctestDir, null, options);
testRuns.forEach(function(part, index, theArray) {
theArray[index] = xctestDir + part;
});
return testRuns;
}
async function runScopeCoverageWithSettings(
buildSettings,
isSPM,
scopeFrameworkToolsPath
) {
let runScriptCommand =
"sh -c " + scopeDir + scopeFrameworkToolsPath + "scope-coverage";
await exec.exec(runScriptCommand, null, {
env: {
...buildSettings,
SCOPE_DSN: envVars[SCOPE_DSN],
TMPDIR: isSPM
? process.env["GITHUB_WORKSPACE"] + "/.build/x86_64-apple-macosx/debug"
: os.tmpdir() + "/",
PRODUCT_BUNDLE_IDENTIFIER: isSPM
? ""
: buildSettings.PRODUCT_BUNDLE_IDENTIFIER
},
ignoreReturnCode: true
});
}
async function insertEnvVariables(file, target) {
await insertEnvVariable(SCOPE_DSN, envVars[SCOPE_DSN], file, target);
await insertEnvVariable(
"SCOPE_COMMIT_SHA",
envVars["GITHUB_SHA"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_SOURCE_ROOT",
envVars["GITHUB_WORKSPACE"] || "",
file,
target
);
await insertEnvVariable(
"GITHUB_REPOSITORY",
envVars["GITHUB_REPOSITORY"] || "",
file,
target
);
await insertEnvVariable(
"GITHUB_RUN_ID",
envVars["GITHUB_RUN_ID"] || "",
file,
target
);
await insertEnvVariable(
"GITHUB_RUN_NUMBER",
envVars["GITHUB_RUN_NUMBER"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_AUTO_INSTRUMENT",
envVars["SCOPE_AUTO_INSTRUMENT"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_SERVICE",
envVars["SCOPE_SERVICE"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_SET_GLOBAL_TRACER",
envVars["SCOPE_SET_GLOBAL_TRACER"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_TESTING_MODE",
envVars["SCOPE_TESTING_MODE"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_METADATA",
envVars["SCOPE_METADATA"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_CONFIGURATION",
envVars["SCOPE_CONFIGURATION"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_INSTRUMENTATION_HTTP_PAYLOADS",
envVars["SCOPE_INSTRUMENTATION_HTTP_PAYLOADS"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_INSTRUMENTATION_HTTP_CLIENT",
envVars["SCOPE_INSTRUMENTATION_HTTP_CLIENT"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_INSTRUMENTATION_HTTP_HEADERS",
envVars["SCOPE_INSTRUMENTATION_HTTP_HEADERS"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_RUNNER_ENABLED",
envVars["SCOPE_RUNNER_ENABLED"] || "",
file,
target
);
await insertEnvVariable(
"SCOPE_RUNNER_EXCLUDE_BRANCHES",
envVars["SCOPE_RUNNER_EXCLUDE_BRANCHES"] || "",
file,
target
);
}
async function insertEnvVariable(name, value, file, target) {
if (value !== "") {
let insertCommand =
'plutil -replace "' +
target +
".EnvironmentVariables." +
name +
'" -string ' +
value +
` "${file}"`;
await exec.exec(insertCommand, null, null);
}
}
function recFindByExt(base, ext, files, result) {
files = files || fs.readdirSync(base);
result = result || [];
files.forEach(function(file) {
var newbase = path.join(base, file);
if (fs.statSync(newbase).isDirectory()) {
result = recFindByExt(newbase, ext, fs.readdirSync(newbase), result);
} else {
if (file.substr(-1 * (ext.length + 1)) === "." + ext) {
result.push(newbase);
}
}
});
return result;
}
async function deleteLinesContaining(file, match) {
let newName = file + "_old";
await io.mv(file, newName);
fs.readFile(newName, { encoding: "utf-8" }, function(err, data) {
if (err) throw error;
let dataArray = data.split("\n"); // convert file data in an array
const searchKeyword = match; // we are looking for a line, contains, key word 'user1' in the file
let lastIndex = -1; // let say, we have not found the keyword
for (let index = 0; index < dataArray.length; index++) {
if (dataArray[index].includes(searchKeyword)) {
// check if a line contains the 'user1' keyword
lastIndex = index; // found a line includes a 'user1' keyword
break;
}
}
dataArray.splice(lastIndex, 1); // remove the keyword 'user1' from the data Array
// UPDATE FILE WITH NEW DATA
// IN CASE YOU WANT TO UPDATE THE CONTENT IN YOUR FILE
// THIS WILL REMOVE THE LINE CONTAINS 'user1' IN YOUR shuffle.txt FILE
const updatedData = dataArray.join("\n");
fs.writeFile(file, updatedData, err => {
if (err) throw err;
console.log("Successfully updated the file data");
});
});
}
run();