Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include copying of Node.js runtimes to the plugin in build-fast script #4315

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
22 changes: 9 additions & 13 deletions tools/fetch-node/scripts/copy-to-plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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);
});
29 changes: 27 additions & 2 deletions tools/fetch-node/scripts/directories.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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;
}
43 changes: 43 additions & 0 deletions tools/fetch-node/scripts/wrapper.mjs
Original file line number Diff line number Diff line change
@@ -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' });