Skip to content

Commit

Permalink
Bundled Node.js runtime: add MacOS x64 support (#4194)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-kebets-sonarsource authored Sep 22, 2023
1 parent c4e8d6e commit 9781086
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
28 changes: 28 additions & 0 deletions sonar-plugin/sonar-javascript-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,34 @@
</configuration>
</execution>

<execution>
<id>darwin-x64</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>darwin-x64</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<!-- This is used to get plugin version at runtime using getPackage().getImplementationVersion() -->
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>darwin-x64/node.xz</resource>
<file>${project.build.directory}/node/darwin-x64/node.xz</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>darwin-x64/version.txt</resource>
<file>${project.build.directory}/node/darwin-x64/version.txt</file>
</transformer>
</transformers>
</configuration>
</execution>

<execution>
<id>multi</id>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ enum Platform {
WIN_X64,
LINUX_X64,
DARWIN_ARM64,
DARWIN_X64,
UNSUPPORTED;

private String pathInJar() {
Expand All @@ -71,6 +72,8 @@ private String pathInJar() {
return "/linux-x64/";
case DARWIN_ARM64:
return "/darwin-arm64/";
case DARWIN_X64:
return "/darwin-x64/";
default:
return "";
}
Expand Down Expand Up @@ -113,6 +116,8 @@ static Platform detect(Environment env) {
return LINUX_X64;
} else if (lowerCaseOsName.contains("mac os") && isARM64(env)) {
return DARWIN_ARM64;
} else if (lowerCaseOsName.contains("mac os") && isX64(env)) {
return DARWIN_X64;
}
return UNSUPPORTED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.DARWIN_ARM64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.DARWIN_X64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.LINUX_X64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.UNSUPPORTED;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.WIN_X64;
Expand Down Expand Up @@ -70,12 +71,19 @@ void should_detect_platform_for_windows_environment() {
}

@Test
void should_detect_platform_for_mac_os_environment() {
var platform = Platform.detect(createMacOSEnvironment());
void should_detect_platform_for_mac_os_arm64_environment() {
var platform = Platform.detect(createMacOSArm64Environment());
assertThat(platform).isEqualTo(DARWIN_ARM64);
assertThat(platform.archivePathInJar()).isEqualTo("/darwin-arm64/node.xz");
}

@Test
void should_detect_platform_for_mac_os_x64_environment() {
var platform = Platform.detect(createMacOSX64Environment());
assertThat(platform).isEqualTo(DARWIN_X64);
assertThat(platform.archivePathInJar()).isEqualTo("/darwin-x64/node.xz");
}

@Test
void should_detect_platform_for_linux_environment() {
var linux = mock(Environment.class);
Expand Down Expand Up @@ -123,13 +131,20 @@ private Environment createTestEnvironment() {
return mockEnvironment;
}

private Environment createMacOSEnvironment() {
private Environment createMacOSArm64Environment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("mac os x");
when(mockEnvironment.getOsArch()).thenReturn("aarch64");
return mockEnvironment;
}

private Environment createMacOSX64Environment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("mac os x");
when(mockEnvironment.getOsArch()).thenReturn("amd64");
return mockEnvironment;
}

private Environment createWindowsEnvironment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("Windows 99");
Expand Down
6 changes: 6 additions & 0 deletions tools/fetch-node/node-distros.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export const DISTROS = [
artifactoryUrl: `${NODE_ARTIFACTORY_URL}-darwin-arm64.tar.gz`,
sha: '9cc794517788aee103dfcffa04d0b90ac33854b0d10eb11a26ba4be38403f731',
},
{
id: 'darwin-x64',
url: `${NODE_ORG_URL}-darwin-x64.tar.gz`,
artifactoryUrl: `${NODE_ARTIFACTORY_URL}-darwin-x64.tar.gz`,
sha: '7a451dd07551a14ce64afdcc5bf8a71df12558edc19a2a9446e37cefe1e572fb',
},
{
id: 'linux-x64',
url: `${NODE_ORG_URL}-linux-x64.tar.gz`,
Expand Down
1 change: 1 addition & 0 deletions tools/fetch-node/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<mainClass>org.sonarsource.javascript.XZ</mainClass>
<arguments>
<argument>${project.basedir}/downloads/runtimes/darwin-arm64/node</argument>
<argument>${project.basedir}/downloads/runtimes/darwin-x64/node</argument>
<argument>${project.basedir}/downloads/runtimes/linux-x64/node</argument>
<argument>${project.basedir}/downloads/runtimes/win-x64/node.exe</argument>
</arguments>
Expand Down
9 changes: 7 additions & 2 deletions tools/fetch-node/scripts/fetch-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ function getFilenameFromUrl(url) {
function copyRuntime(distroName, distroId, nodeDir, targetDir) {
console.log(`Copying runtime for ${distroName} from ${nodeDir} to ${targetDir}`);
let nodeBin;
if (distroName.includes('win-x64')) {
// the starting dash is necessary, otherwise it captures "darwin-x64" as well
if (distroName.includes('-win-x64')) {
nodeBin = 'node.exe';
} else if (distroName.includes('darwin-arm64') || distroName.includes('linux-x64')) {
} else if (
distroName.includes('darwin-arm64') ||
distroName.includes('linux-x64') ||
distroName.includes('darwin-x64')
) {
nodeBin = path.join('bin', 'node');
} else {
throw new Error(
Expand Down

0 comments on commit 9781086

Please sign in to comment.