build: allow overriding electron version (#39974)

This commit is contained in:
Cheng Zhao 2023-09-27 14:49:10 +09:00 committed by GitHub
parent 624ae024e2
commit e613595982
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 27 deletions

View file

@ -1,22 +1,30 @@
const { spawnSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const { ELECTRON_DIR, getOutDir } = require('./utils');
// Print the value of electron_version set in gn config.
module.exports.getElectronVersion = () => {
// Find the nearest tag to the current HEAD
// This is equivilant to our old logic of "use a value in package.json" for the following reasons
//
// 1. Whenever we updated the package.json we ALSO pushed a tag with the same version
// 2. Whenever we _reverted_ a bump all we actually did was push a commit that deleted the tag and changed the version number back
//
// The only difference in the "git describe" technique is that technically a commit can "change" it's version
// number if a tag is created / removed retroactively. i.e. the first time a commit is pushed it will be 1.2.3
// and after the tag is made rebuilding the same commit will result in it being 1.2.4
const output = spawnSync('git', ['describe', '--tags', '--abbrev=0'], {
cwd: path.resolve(__dirname, '..', '..')
});
if (output.status !== 0) {
console.error(output.stderr);
throw new Error('Failed to get current electron version');
// Read the override_electron_version from args.gn file.
try {
const outDir = path.resolve(ELECTRON_DIR, '..', 'out', getOutDir());
const content = fs.readFileSync(path.join(outDir, 'args.gn'));
const regex = /override_electron_version\s*=\s*["']([^"']+)["']/;
const match = content.toString().match(regex);
if (match) {
return match[1];
}
} catch (error) {
// Error may happen when trying to get version before running gn, which is a
// valid case and error will be ignored.
}
return output.stdout.toString().trim().replace(/^v/g, '');
// Most win32 machines have python.exe but no python3.exe.
const python = process.platform === 'win32' ? 'python.exe' : 'python3';
// Get the version from git tag if it is not defined in gn args.
const output = spawnSync(python, [path.join(ELECTRON_DIR, 'script', 'get-git-version.py')]);
if (output.status !== 0) {
throw new Error(`Failed to get git tag, script quit with ${output.status}: ${output.stdout}`);
}
return output.stdout.toString().trim();
};