Folder Select

This commit is contained in:
PaulaBras
2022-09-21 18:25:29 +02:00
parent f2ae18fba8
commit 0dc02629e6
10 changed files with 613 additions and 30 deletions

View File

@ -0,0 +1,61 @@
name: Electron Builder Action
author: Samuel Meuli
description: GitHub Action for building and releasing Electron apps
inputs:
github_token:
description: GitHub authentication token
required: true
mac_certs:
description: Base64-encoded code signing certificate for macOS
required: false
mac_certs_password:
description: Password for decrypting `mac_certs`
required: false
release:
description: Whether the app should be released after a successful build
required: false
default: false
windows_certs:
description: Base64-encoded code signing certificate for Windows
required: false
windows_certs_password:
description: Password for decrypting `windows_certs`
required: false
package_root:
description: Directory where NPM/Yarn commands should be run
required: false
default: "."
build_script_name:
description: Name of the optional NPM build script which is executed before `electron-builder`
required: false
default: build
skip_build:
description: Whether the action should execute the NPM build script before running `electron-builder`
required: false
default: false
use_vue_cli:
description: Whether to run `electron-builder` using the Vue CLI plugin instead of calling the command directly
required: false
default: false
args:
description: Other arguments to pass to the `electron-builder` command, e.g. configuration overrides
required: false
default: ""
max_attempts:
description: Maximum number of attempts for completing the build and release step
required: false
default: "1"
# Deprecated
app_root:
description: Directory where `electron-builder` commands should be run
required: false
runs:
using: node12
main: index.js
branding:
icon: upload-cloud
color: blue

View File

@ -0,0 +1,156 @@
const { execSync } = require("child_process");
const { existsSync, readFileSync } = require("fs");
const { join } = require("path");
/**
* Logs to the console
*/
const log = (msg) => console.log(`\n${msg}`); // eslint-disable-line no-console
/**
* Exits the current process with an error code and message
*/
const exit = (msg) => {
console.error(msg);
process.exit(1);
};
/**
* Executes the provided shell command and redirects stdout/stderr to the console
*/
const run = (cmd, cwd) => execSync(cmd, { encoding: "utf8", stdio: "inherit", cwd });
/**
* Determines the current operating system (one of ["mac", "windows", "linux"])
*/
const getPlatform = () => {
switch (process.platform) {
case "darwin":
return "mac";
case "win32":
return "windows";
default:
return "linux";
}
};
/**
* Returns the value for an environment variable (or `null` if it's not defined)
*/
const getEnv = (name) => process.env[name.toUpperCase()] || null;
/**
* Sets the specified env variable if the value isn't empty
*/
const setEnv = (name, value) => {
if (value) {
process.env[name.toUpperCase()] = value.toString();
}
};
/**
* Returns the value for an input variable (or `null` if it's not defined). If the variable is
* required and doesn't have a value, abort the action
*/
const getInput = (name, required) => {
const value = getEnv(`INPUT_${name}`);
if (required && !value) {
exit(`"${name}" input variable is not defined`);
}
return value;
};
/**
* Installs NPM dependencies and builds/releases the Electron app
*/
const runAction = () => {
const platform = getPlatform();
const release = getInput("release", true) === "true";
const pkgRoot = getInput("package_root", true);
const buildScriptName = getInput("build_script_name", true);
const skipBuild = getInput("skip_build") === "true";
const useVueCli = getInput("use_vue_cli") === "true";
const args = getInput("args") || "";
const maxAttempts = Number(getInput("max_attempts") || "1");
// TODO: Deprecated option, remove in v2.0. `electron-builder` always requires a `package.json` in
// the same directory as the Electron app, so the `package_root` option should be used instead
const appRoot = getInput("app_root") || pkgRoot;
const pkgJsonPath = join(pkgRoot, "package.json");
const pkgLockPath = join(pkgRoot, "package-lock.json");
// Determine whether NPM should be used to run commands (instead of Yarn, which is the default)
const useNpm = existsSync(pkgLockPath);
log(`Will run ${useNpm ? "NPM" : "Yarn"} commands in directory "${pkgRoot}"`);
// Make sure `package.json` file exists
if (!existsSync(pkgJsonPath)) {
exit(`\`package.json\` file not found at path "${pkgJsonPath}"`);
}
// Copy "github_token" input variable to "GH_TOKEN" env variable (required by `electron-builder`)
setEnv("GH_TOKEN", getInput("github_token", true));
// Require code signing certificate and password if building for macOS. Export them to environment
// variables (required by `electron-builder`)
if (platform === "mac") {
const mac_certs = getInput("mac_certs");
const mac_certs_password = getInput("mac_certs_password");
if (mac_certs && mac_certs_password) {
setEnv("CSC_LINK", mac_certs);
setEnv("CSC_KEY_PASSWORD", mac_certs_password);
} else {
setEnv("CSC_IDENTITY_AUTO_DISCOVERY", "false");
}
} else if (platform === "windows") {
setEnv("CSC_LINK", getInput("windows_certs"));
setEnv("CSC_KEY_PASSWORD", getInput("windows_certs_password"));
}
// Disable console advertisements during install phase
setEnv("ADBLOCK", true);
// log(`Installing dependencies using ${useNpm ? "NPM" : "Yarn"}…`);
// run(useNpm ? "npm install" : "yarn", pkgRoot);
// Run NPM build script if it exists
if (skipBuild) {
log("Skipping build script because `skip_build` option is set");
} else {
log("Running the build script…");
if (useNpm) {
run(`npm run ${buildScriptName} --if-present`, pkgRoot);
} else {
// TODO: Use `yarn run ${buildScriptName} --if-present` once supported
// https://github.com/yarnpkg/yarn/issues/6894
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, "utf8"));
if (pkgJson.scripts && pkgJson.scripts[buildScriptName]) {
run(`yarn run ${buildScriptName}`, pkgRoot);
}
}
}
log(`Building${release ? " and releasing" : ""} the Electron app…`);
const cmd = useVueCli ? "vue-cli-service electron:build" : `electron-builder`;
for (let i = 0; i < maxAttempts; i += 1) {
try {
run(
`${useNpm ? "npx --no-install" : "yarn run"} ${cmd} --${platform} ${
release ? "--publish always" : ""
} ${args}`,
appRoot,
);
break;
} catch (err) {
if (i < maxAttempts - 1) {
log(`Attempt ${i + 1} failed:`);
log(err);
} else {
throw err;
}
}
}
};
runAction();