2020-03-20 20:28:31 +00:00
|
|
|
const { GitProcess } = require('dugite');
|
|
|
|
const fs = require('fs');
|
2022-06-22 10:23:11 +00:00
|
|
|
const os = require('os');
|
2020-03-20 20:28:31 +00:00
|
|
|
const path = require('path');
|
2018-09-13 16:57:39 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
|
|
|
|
const SRC_DIR = path.resolve(ELECTRON_DIR, '..');
|
2019-06-05 23:30:39 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const RELEASE_BRANCH_PATTERN = /(\d)+-(?:(?:[0-9]+-x$)|(?:x+-y$))/;
|
2021-05-28 00:32:16 +00:00
|
|
|
// TODO(main-migration): Simplify once main branch is renamed
|
|
|
|
const MAIN_BRANCH_PATTERN = /^(main|master)$/;
|
|
|
|
const ORIGIN_MAIN_BRANCH_PATTERN = /^origin\/(main|master)$/;
|
2019-12-18 22:57:46 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
require('colors');
|
|
|
|
const pass = '✓'.green;
|
|
|
|
const fail = '✗'.red;
|
2018-12-03 21:28:10 +00:00
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
function getElectronExec () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const OUT_DIR = getOutDir();
|
2018-09-13 16:57:39 +00:00
|
|
|
switch (process.platform) {
|
|
|
|
case 'darwin':
|
2020-03-20 20:28:31 +00:00
|
|
|
return `out/${OUT_DIR}/Electron.app/Contents/MacOS/Electron`;
|
2018-09-13 16:57:39 +00:00
|
|
|
case 'win32':
|
2020-03-20 20:28:31 +00:00
|
|
|
return `out/${OUT_DIR}/electron.exe`;
|
2018-09-13 16:57:39 +00:00
|
|
|
case 'linux':
|
2020-03-20 20:28:31 +00:00
|
|
|
return `out/${OUT_DIR}/electron`;
|
2018-09-13 16:57:39 +00:00
|
|
|
default:
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Unknown platform');
|
2018-09-13 16:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:00:42 +00:00
|
|
|
function getOutDir (options = {}) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const shouldLog = options.shouldLog || false;
|
|
|
|
const presetDirs = ['Testing', 'Release', 'Default', 'Debug'];
|
2019-12-17 22:00:42 +00:00
|
|
|
|
|
|
|
if (options.outDir || process.env.ELECTRON_OUT_DIR) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const outDir = options.outDir || process.env.ELECTRON_OUT_DIR;
|
|
|
|
const outPath = path.resolve(SRC_DIR, 'out', outDir);
|
2019-12-17 22:00:42 +00:00
|
|
|
|
|
|
|
// Check that user-set variable is a valid/existing directory
|
|
|
|
if (fs.existsSync(outPath)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (shouldLog) console.log(`OUT_DIR is: ${outDir}`);
|
|
|
|
return outDir;
|
2019-12-17 22:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Throw error if user passed/set nonexistent directory.
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`${outDir} directory not configured on your machine.`);
|
2019-08-21 17:41:35 +00:00
|
|
|
} else {
|
2020-02-27 19:32:09 +00:00
|
|
|
for (const buildType of presetDirs) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const outPath = path.resolve(SRC_DIR, 'out', buildType);
|
2019-08-21 17:41:35 +00:00
|
|
|
if (fs.existsSync(outPath)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (shouldLog) console.log(`OUT_DIR is: ${buildType}`);
|
|
|
|
return buildType;
|
2019-08-21 17:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 19:32:09 +00:00
|
|
|
|
|
|
|
// If we got here, it means process.env.ELECTRON_OUT_DIR was not
|
|
|
|
// set and none of the preset options could be found in /out, so throw
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`No valid out directory found; use one of ${presetDirs.join(',')} or set process.env.ELECTRON_OUT_DIR`);
|
2019-08-21 17:41:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
function getAbsoluteElectronExec () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return path.resolve(SRC_DIR, getElectronExec());
|
2018-09-13 16:57:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 03:56:43 +00:00
|
|
|
async function handleGitCall (args, gitDir) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const details = await GitProcess.exec(args, gitDir);
|
2019-06-17 03:56:43 +00:00
|
|
|
if (details.exitCode === 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return details.stdout.replace(/^\*|\s+|\s+$/, '');
|
2018-12-03 21:28:10 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
const error = GitProcess.parseError(details.stderr);
|
|
|
|
console.log(`${fail} couldn't parse git process call: `, error);
|
|
|
|
process.exit(1);
|
2018-12-03 21:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 03:56:43 +00:00
|
|
|
async function getCurrentBranch (gitDir) {
|
2020-03-20 20:28:31 +00:00
|
|
|
let branch = await handleGitCall(['rev-parse', '--abbrev-ref', 'HEAD'], gitDir);
|
2021-05-28 00:32:16 +00:00
|
|
|
if (!MAIN_BRANCH_PATTERN.test(branch) && !RELEASE_BRANCH_PATTERN.test(branch)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const lastCommit = await handleGitCall(['rev-parse', 'HEAD'], gitDir);
|
2019-06-21 20:59:00 +00:00
|
|
|
const branches = (await handleGitCall([
|
|
|
|
'branch',
|
|
|
|
'--contains',
|
|
|
|
lastCommit,
|
|
|
|
'--remote'
|
2020-03-20 20:28:31 +00:00
|
|
|
], gitDir)).split('\n');
|
2019-06-21 20:59:00 +00:00
|
|
|
|
2021-05-28 00:32:16 +00:00
|
|
|
branch = branches.find(b => MAIN_BRANCH_PATTERN.test(b.trim()) || ORIGIN_MAIN_BRANCH_PATTERN.test(b.trim()) || RELEASE_BRANCH_PATTERN.test(b.trim()));
|
2019-06-18 14:54:32 +00:00
|
|
|
if (!branch) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} no release branch exists for this ref`);
|
|
|
|
process.exit(1);
|
2019-06-18 14:54:32 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
if (branch.startsWith('origin/')) branch = branch.substr('origin/'.length);
|
2019-06-17 03:56:43 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return branch.trim();
|
2019-06-17 03:56:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:23:11 +00:00
|
|
|
function chunkFilenames (filenames, offset = 0) {
|
|
|
|
// Windows has a max command line length of 2047 characters, so we can't
|
|
|
|
// provide too many filenames without going over that. To work around that,
|
|
|
|
// chunk up a list of filenames such that it won't go over that limit when
|
|
|
|
// used as args. Other platforms may have higher limits, but 4095 might be
|
|
|
|
// the limit on Linux systems according to `termios(3)`, so cap it there.
|
|
|
|
const MAX_FILENAME_ARGS_LENGTH =
|
|
|
|
(os.platform() === 'win32' ? 2047 : 4095) - offset;
|
|
|
|
|
|
|
|
return filenames.reduce(
|
|
|
|
(chunkedFilenames, filename) => {
|
|
|
|
const currChunk = chunkedFilenames[chunkedFilenames.length - 1];
|
|
|
|
const currChunkLength = currChunk.reduce(
|
|
|
|
(totalLength, _filename) => totalLength + _filename.length + 1,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
if (currChunkLength + filename.length + 1 > MAX_FILENAME_ARGS_LENGTH) {
|
|
|
|
chunkedFilenames.push([filename]);
|
|
|
|
} else {
|
|
|
|
currChunk.push(filename);
|
|
|
|
}
|
|
|
|
return chunkedFilenames;
|
|
|
|
},
|
|
|
|
[[]]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
module.exports = {
|
2022-06-22 10:23:11 +00:00
|
|
|
chunkFilenames,
|
2018-12-03 21:28:10 +00:00
|
|
|
getCurrentBranch,
|
2018-09-13 16:57:39 +00:00
|
|
|
getElectronExec,
|
2019-08-21 17:41:35 +00:00
|
|
|
getOutDir,
|
2018-09-13 16:57:39 +00:00
|
|
|
getAbsoluteElectronExec,
|
2019-06-24 17:18:04 +00:00
|
|
|
ELECTRON_DIR,
|
|
|
|
SRC_DIR
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|