Skip to content

Commit

Permalink
JS-370 Add Node.js amd64 alpine into the multi plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiez committed Nov 20, 2024
1 parent ce80653 commit b013f39
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import com.sonar.orchestrator.locator.FileLocation;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
Expand Down Expand Up @@ -74,11 +76,18 @@ private static String classifier() {
var os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
var arch = System.getProperty("os.arch");
if (os.contains("linux") && arch.contains("64")) {
if (isAlpine()) {
return "-linux-x64-musl";
}
return "-linux-x64";
} else if (os.contains("windows")) {
return "-win-x64";
} else {
return "-multi";
}
}

private static boolean isAlpine() {
return Files.exists(Path.of("/etc/alpine-release"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,21 @@ enum Platform {
WIN_X64,
LINUX_ARM64,
LINUX_X64,
LINUX_X64_MUSL,
DARWIN_ARM64,
DARWIN_X64,
UNSUPPORTED;

private String pathInJar() {
switch (this) {
case WIN_X64:
return "/win-x64/";
case LINUX_ARM64:
return "/linux-arm64/";
case LINUX_X64:
return "/linux-x64/";
case DARWIN_ARM64:
return "/darwin-arm64/";
case DARWIN_X64:
return "/darwin-x64/";
default:
return "";
}
return switch (this) {
case WIN_X64 -> "/win-x64/";
case LINUX_ARM64 -> "/linux-arm64/";
case LINUX_X64 -> "/linux-x64/";
case LINUX_X64_MUSL -> "/linux-x64-musl/";
case DARWIN_ARM64 -> "/darwin-arm64/";
case DARWIN_X64 -> "/darwin-x64/";
default -> "";
};
}

/**
Expand Down Expand Up @@ -120,8 +116,10 @@ static Platform detect(Environment env) {
return WIN_X64;
} else if (lowerCaseOsName.contains("linux") && isARM64(env) ) {
return LINUX_ARM64;
} else if (lowerCaseOsName.contains("linux") && isX64(env) && !env.isAlpine()) {
// alpine linux is using musl libc, which is not compatible with linux-x64
} else if (lowerCaseOsName.contains("linux") && isX64(env)) {
if (env.isAlpine()) {
return LINUX_X64_MUSL;
}
return LINUX_X64;
} else if (lowerCaseOsName.contains("mac os") && isARM64(env)) {
return DARWIN_ARM64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.DARWIN_X64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.LINUX_ARM64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.LINUX_X64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.LINUX_X64_MUSL;
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 @@ -124,14 +125,18 @@ void should_detect_platform_for_linux_arm64_environment() {

@Test
void should_detect_platform_for_linux_x64_environment() {
var linux = mock(Environment.class);
when(linux.getOsName()).thenReturn("linux");
when(linux.getOsArch()).thenReturn("amd64");
var platform = Platform.detect(linux);
var platform = Platform.detect(createLinuxX64Environment());
assertThat(platform).isEqualTo(LINUX_X64);
assertThat(platform.archivePathInJar()).isEqualTo("/linux-x64/node.xz");
}

@Test
void should_detect_platform_for_linux_x64_musl_environment() {
var platform = Platform.detect(createLinuxX64MuslEnvironment());
assertThat(platform).isEqualTo(LINUX_X64_MUSL);
assertThat(platform.archivePathInJar()).isEqualTo("/linux-x64-musl/node.xz");
}

@Test
void should_return_unsupported_for_unknown_environment() {
var platform = Platform.detect(createUnsupportedEnvironment());
Expand Down Expand Up @@ -205,6 +210,22 @@ private Environment createLinuxArm64Environment() {
return mockEnvironment;
}

private Environment createLinuxX64Environment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("linux");
when(mockEnvironment.getOsArch()).thenReturn("amd64");
when(mockEnvironment.isAlpine()).thenReturn(false);
return mockEnvironment;
}

private Environment createLinuxX64MuslEnvironment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("linux");
when(mockEnvironment.getOsArch()).thenReturn("amd64");
when(mockEnvironment.isAlpine()).thenReturn(true);
return mockEnvironment;
}

private Environment createMacOSArm64Environment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("mac os x");
Expand Down
39 changes: 38 additions & 1 deletion sonar-plugin/sonar-javascript-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@
</configuration>
</execution>

<execution>
<id>linux-x64-musl</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>linux-x64-musl</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>linux-x64-musl/node.xz</resource>
<file>${project.build.directory}/node/linux-x64-musl/node.xz</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>linux-x64-musl/version.txt</resource>
<file>${project.build.directory}/node/linux-x64-musl/version.txt</file>
</transformer>
</transformers>
</configuration>
</execution>

<execution>
<id>darwin-arm64</id>
<phase>package</phase>
Expand Down Expand Up @@ -316,6 +344,14 @@
<resource>linux-x64/version.txt</resource>
<file>${project.build.directory}/node/linux-x64/version.txt</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>linux-x64-musl/node.xz</resource>
<file>${project.build.directory}/node/linux-x64-musl/node.xz</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>linux-x64-musl/version.txt</resource>
<file>${project.build.directory}/node/linux-x64-musl/version.txt</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>darwin-arm64/node.xz</resource>
<file>${project.build.directory}/node/darwin-arm64/node.xz</file>
Expand Down Expand Up @@ -363,7 +399,7 @@
<rules>
<requireFilesSize>
<minsize>70000000</minsize>
<maxsize>90000000</maxsize>
<maxsize>110000000</maxsize>
<files>
<file>${project.build.directory}/${project.build.finalName}-multi.jar</file>
</files>
Expand All @@ -373,6 +409,7 @@
<maxsize>55000000</maxsize>
<files>
<file>${project.build.directory}/${project.build.finalName}-linux-x64.jar</file>
<file>${project.build.directory}/${project.build.finalName}-linux-x64-musl.jar</file>
<file>${project.build.directory}/${project.build.finalName}-win-x64.jar</file>
<file>${project.build.directory}/${project.build.finalName}-darwin-arm64.jar</file>
</files>
Expand Down
10 changes: 10 additions & 0 deletions tools/fetch-node/node-distros.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export const NODE_VERSION = 'v22.11.0';
const NODE_ORG_URL = `https://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}`;
const NODE_ARTIFACTORY_URL = `https://repox.jfrog.io/artifactory/nodejs-dist/${NODE_VERSION}/node-${NODE_VERSION}`;

// alpine builds are not officially supported. this is the URL the official docker alpine uses
// https://github.com/nodejs/docker-node/blob/15cd6b44e0284c459de7763b45e3b16972c0716e/22/alpine3.20/Dockerfile#L23
const UNOFFICIAL_NODE_ORG_URL = `https://unofficial-builds.nodejs.org/download/release/${NODE_VERSION}/node-${NODE_VERSION}`;
/**
* Node.js runtimes distributions
*
Expand Down Expand Up @@ -47,6 +50,13 @@ export const DISTROS = [
sha: '4f862bab52039835efbe613b532238b6e4dde98d139a34e6923193e073438b13',
binPath: 'bin/node',
},
{
id: 'linux-x64-musl',
url: `${UNOFFICIAL_NODE_ORG_URL}-linux-x64-musl.tar.gz`,
artifactoryUrl: `${NODE_ARTIFACTORY_URL}-linux-x64-musl.tar.gz`,
sha: 'c9b4eba63f6569547e3a3423b446613a5a56dffb571b10f556bac2ae45fdc1fb',
binPath: 'bin/node',
},
];

export const VERSION_FILENAME = 'version.txt';
1 change: 1 addition & 0 deletions tools/fetch-node/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<argument>${project.basedir}/downloads/runtimes/darwin-x64/node</argument>
<argument>${project.basedir}/downloads/runtimes/linux-arm64/node</argument>
<argument>${project.basedir}/downloads/runtimes/linux-x64/node</argument>
<argument>${project.basedir}/downloads/runtimes/linux-x64-musl/node</argument>
<argument>${project.basedir}/downloads/runtimes/win-x64/node.exe</argument>
</arguments>
</configuration>
Expand Down

0 comments on commit b013f39

Please sign in to comment.