From bf877cd3f42d5df28b2cd5c0b9b60067d66128d3 Mon Sep 17 00:00:00 2001 From: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com> Date: Wed, 25 Oct 2023 11:27:16 +0200 Subject: [PATCH] Include copying of Node.js runtimes to the plugin in `build-fast` script (#4315) --- package.json | 4 +- tools/fetch-node/scripts/copy-to-plugin.mjs | 22 +++++------ tools/fetch-node/scripts/directories.mjs | 29 +++++++++++++- tools/fetch-node/scripts/wrapper.mjs | 43 +++++++++++++++++++++ 4 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 tools/fetch-node/scripts/wrapper.mjs diff --git a/package.json b/package.json index 0143b390428..d15c733da27 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "format": "prettier --write .", "build": "mvn clean && npm run _:plugin:pre-build && npm run plugin:build-no-bridge", - "build:fast": "npm run bridge:build:fast && npm run _:plugin:prepare-bridge && npm run plugin:build:fast", + "build:fast": "npm run bridge:build:fast && npm run _:plugin:prepare-bridge && npm run _:plugin-fetch-node && npm run plugin:build:fast", "bf": "npm run build:fast", "new-rule": "ts-node tools/newRule.ts", "bridge:compile": "tsc -b packages profiling", @@ -22,7 +22,7 @@ "precommit": "pretty-quick --staged", "_:bridge:clear": "rimraf lib/*", "_:plugin:prepare-bridge": "npm pack && node tools/check-distribution-filepath-length.js && npm run _:plugin:copy-bridge", - "_:plugin-fetch-node": "node tools/fetch-node/scripts/fetch-node.mjs && mvn -f tools/fetch-node/pom.xml package exec:java && node tools/fetch-node/scripts/copy-to-plugin.mjs", + "_:plugin-fetch-node": "node tools/fetch-node/scripts/wrapper.mjs", "_:plugin:pre-build": "npm run bridge:build && npm run _:plugin:prepare-bridge && npm run _:plugin-fetch-node", "_:plugin:copy-bridge": "cpy sonarjs-1.0.0.tgz sonar-plugin/sonar-javascript-plugin/target/classes" }, diff --git a/tools/fetch-node/scripts/copy-to-plugin.mjs b/tools/fetch-node/scripts/copy-to-plugin.mjs index f055d83fb20..acd21e18039 100644 --- a/tools/fetch-node/scripts/copy-to-plugin.mjs +++ b/tools/fetch-node/scripts/copy-to-plugin.mjs @@ -20,7 +20,7 @@ import fse from 'fs-extra'; import * as path from 'node:path'; import * as fs from 'node:fs'; -import { RUNTIMES_DIR, TARGET_DIR } from './directories.mjs'; +import { RUNTIMES_DIR, TARGET_DIR, getRuntimePaths } from './directories.mjs'; import { DISTROS, NODE_VERSION, VERSION_FILENAME } from '../node-distros.mjs'; /** @@ -32,15 +32,11 @@ import { DISTROS, NODE_VERSION, VERSION_FILENAME } from '../node-distros.mjs'; * sonar-plugin/sonar-javascript-plugin/target/node/{distro.id}/version.txt files */ -for (const distro of DISTROS) { - const sourceDir = path.join(RUNTIMES_DIR, distro.id); - // we know that there is a single compressed archive per distro - const filename = fs.readdirSync(sourceDir).filter(filename => filename.endsWith('.xz'))[0]; - const targetDir = path.join(TARGET_DIR, distro.id); - fse.mkdirpSync(targetDir); - const sourceFilename = path.join(sourceDir, filename); - const targetFilename = path.join(targetDir, filename); - console.log(`Copying ${sourceFilename} to ${targetFilename}`); - fse.copySync(sourceFilename, targetFilename); - fs.writeFileSync(path.join(targetDir, VERSION_FILENAME), NODE_VERSION); -} +const runtimePaths = getRuntimePaths(); + +runtimePaths.forEach(p => { + fse.mkdirpSync(p.targetDir); + console.log(`Copying ${p.sourceFilename} to ${p.targetFilename}`); + fse.copySync(p.sourceFilename, p.targetFilename); + fs.writeFileSync(path.join(p.targetDir, VERSION_FILENAME), NODE_VERSION); +}); diff --git a/tools/fetch-node/scripts/directories.mjs b/tools/fetch-node/scripts/directories.mjs index 353ab581c0a..82e70b8782d 100644 --- a/tools/fetch-node/scripts/directories.mjs +++ b/tools/fetch-node/scripts/directories.mjs @@ -17,8 +17,10 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import * as url from 'url'; -import * as path from 'path'; +import * as url from 'node:url'; +import * as path from 'node:path'; +import * as fs from 'node:fs'; +import { DISTROS } from '../node-distros.mjs'; // replace __dirname in module const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); @@ -45,3 +47,26 @@ export const TARGET_DIR = path.join( 'target', 'node', ); + +/** + * Builds the paths for the Node.js runtimes + * + * @returns + */ +export function getRuntimePaths() { + const runtimeDirectories = []; + for (const distro of DISTROS) { + const sourceDir = path.join(RUNTIMES_DIR, distro.id); + // we know that there is a single compressed archive per distro + const filename = fs.readdirSync(sourceDir).filter(filename => filename.endsWith('.xz'))[0]; + const sourceFilename = path.join(sourceDir, filename); + const targetDir = path.join(TARGET_DIR, distro.id); + const targetFilename = path.join(targetDir, filename); + runtimeDirectories.push({ + targetDir, + targetFilename, + sourceFilename, + }); + } + return runtimeDirectories; +} diff --git a/tools/fetch-node/scripts/wrapper.mjs b/tools/fetch-node/scripts/wrapper.mjs new file mode 100644 index 00000000000..fcf2f3fcdd1 --- /dev/null +++ b/tools/fetch-node/scripts/wrapper.mjs @@ -0,0 +1,43 @@ +/* + * SonarQube JavaScript Plugin + * Copyright (C) 2011-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as fs from 'node:fs'; +import * as url from 'node:url'; +import * as path from 'node:path'; +import { execSync } from 'node:child_process'; +import { getRuntimePaths, TARGET_DIR } from './directories.mjs'; +// replace __dirname in module +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); + +/** + * Skips running the fetch node scripts if Node.js runtimes are already present + */ +const runtimePaths = getRuntimePaths(); + +if (runtimePaths.every(p => fs.existsSync(p.targetFilename))) { + console.log(`Skipping. All Node.js runtimes are already present in the plugin: ${TARGET_DIR}`); + console.log('If the runtimes are outdated, delete the folder and run the script again.'); + process.exit(0); +} + +execSync(`node ${path.join(__dirname, 'fetch-node.mjs')}`, { stdio: 'inherit' }); + +execSync(`mvn -f ${path.join(__dirname, '..', 'pom.xml')} package exec:java`, { stdio: 'inherit' }); + +execSync(`node ${path.join(__dirname, 'copy-to-plugin.mjs')}`, { stdio: 'inherit' });