Do not silently install cargo-about. (#377)

This commit is contained in:
Christian Authmann
2021-09-19 16:58:04 +02:00
committed by Keavon Chambers
parent 6e8f6d06d7
commit ce1adb71a0
4 changed files with 60 additions and 21 deletions
+3
View File
@@ -25,6 +25,9 @@ jobs:
- name: 🚧 Install Node dependencies - name: 🚧 Install Node dependencies
run: cd frontend && npm install run: cd frontend && npm install
- name: 📦 Install cargo-about
run: cargo install cargo-about
- name: 🌐 Build Graphite web code - name: 🌐 Build Graphite web code
run: cd frontend && npm run build run: cd frontend && npm run build
+6 -5
View File
@@ -1,7 +1,8 @@
// Be careful to prevent auto-formatting from breaking this file's indentation {{!
// Replace this file with JSON output once this is resolved: https://github.com/EmbarkStudios/cargo-about/issues/73 Be careful to prevent auto-formatting from breaking this file's indentation
Replace this file with JSON output once this is resolved: https://github.com/EmbarkStudios/cargo-about/issues/73
module.exports = [ }}
GENERATED_BY_CARGO_ABOUT: [
{{#each licenses}} {{#each licenses}}
{ {
licenseName: `{{name}}`, licenseName: `{{name}}`,
@@ -18,4 +19,4 @@ module.exports = [
], ],
}, },
{{/each}} {{/each}}
]; ]
+1 -1
View File
@@ -5,7 +5,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve || (npm install && vue-cli-service serve)", "serve": "vue-cli-service serve || (npm install && vue-cli-service serve)",
"build": "cd .. && cargo install cargo-about && cargo about generate about.hbs > frontend/rust-licenses.js && cd frontend && (vue-cli-service build || (npm install && vue-cli-service build))", "build": "vue-cli-service build || (npm install && vue-cli-service build)",
"lint": "vue-cli-service lint || (npm install && vue-cli-service lint)", "lint": "vue-cli-service lint || (npm install && vue-cli-service lint)",
"lint-no-fix": "vue-cli-service lint --no-fix || (echo 'There were lint errors. Please run `npm run lint` to fix auto-them. If the linter execution fails, try running `npm install` first.' && false)" "lint-no-fix": "vue-cli-service lint --no-fix || (echo 'There were lint errors. Please run `npm run lint` to fix auto-them. If the linter execution fails, try running `npm install` first.' && false)"
}, },
+50 -15
View File
@@ -1,18 +1,37 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires, no-console */
const path = require("path"); const path = require("path");
const { unlink } = require("fs"); const { spawnSync } = require("child_process");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin"); const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
const LicenseCheckerWebpackPlugin = require("license-checker-webpack-plugin"); const LicenseCheckerWebpackPlugin = require("license-checker-webpack-plugin");
let rustLicenses = []; function generateRustLicenses() {
let debugMode = false; console.info("Generating license information for rust code");
try { const { stdout, stderr, status } = spawnSync("cargo", ["about", "generate", "about.hbs"], {
// eslint-disable-next-line global-require, import/extensions, import/no-unresolved cwd: path.join(__dirname, ".."),
rustLicenses = require("./rust-licenses"); encoding: "utf8",
} catch (_) { timeout: 60000, // one minute
// Rust licenses are not generated by Cargo About except in release mode (`npm run build`) shell: true,
debugMode = true; windowsHide: true, // hide the DOS window on windows
});
if (status !== 0) {
if (status !== 101) {
// cargo returns 101 when the subcommand wasn't found
console.error("cargo-about failed", status, stderr);
}
return null;
}
// Make sure the output starts as expected, we don't want to eval an error message.
if (!stdout.trim().startsWith("GENERATED_BY_CARGO_ABOUT:")) {
console.error("Unexpected output from cargo-about", stdout);
return null;
}
// Security-wise, eval() isn't any worse than require(), but it doesn't need a temporary file.
// eslint-disable-next-line no-eval
return eval(stdout);
} }
module.exports = { module.exports = {
@@ -78,8 +97,27 @@ module.exports = {
}; };
function formatThirdPartyLicenses(jsLicenses) { function formatThirdPartyLicenses(jsLicenses) {
let rustLicenses = null;
if (process.env.NODE_ENV === "production") {
try {
rustLicenses = generateRustLicenses();
} catch (e) {
// Nothing to show. Error messages were printed above.
}
if (rustLicenses === null) {
// This is probably caused by cargo about not being installed
console.error(`
Could not run 'cargo about', which is required to generate license information.
To install cargo-about on your system, you can run:
cargo install cargo-about
License information is required on production builds. Aborting.`);
process.exit(1);
}
}
// Remove the HTML character encoding caused by Handlebars // Remove the HTML character encoding caused by Handlebars
let licenses = rustLicenses.map((rustLicense) => ({ let licenses = (rustLicenses || []).map((rustLicense) => ({
licenseName: htmlDecode(rustLicense.licenseName), licenseName: htmlDecode(rustLicense.licenseName),
licenseText: trimBlankLines(htmlDecode(rustLicense.licenseText)), licenseText: trimBlankLines(htmlDecode(rustLicense.licenseText)),
packages: rustLicense.packages.map((package) => ({ packages: rustLicense.packages.map((package) => ({
@@ -131,7 +169,7 @@ function formatThirdPartyLicenses(jsLicenses) {
// Generate the formatted text file // Generate the formatted text file
let formattedLicenseNotice = "GRAPHITE THIRD-PARTY SOFTWARE LICENSE NOTICES\n\n"; let formattedLicenseNotice = "GRAPHITE THIRD-PARTY SOFTWARE LICENSE NOTICES\n\n";
if (debugMode) formattedLicenseNotice += "WARNING: Licenses for Rust packages are excluded in debug mode to improve performance — do not release without their inclusion!\n\n"; if (!rustLicenses) formattedLicenseNotice += "WARNING: Licenses for Rust packages are excluded in debug mode to improve performance — do not release without their inclusion!\n\n";
licenses.forEach((license) => { licenses.forEach((license) => {
let packagesWithSameLicense = ""; let packagesWithSameLicense = "";
@@ -153,9 +191,6 @@ ${license.licenseText}
`; `;
}); });
// Clean up by deleting the `rust-licenses.js` Rust licenses data file generated by Cargo About
unlink("./rust-licenses.js", (_) => _);
return formattedLicenseNotice; return formattedLicenseNotice;
} }