Skip to content

Commit

Permalink
DCA11Y-1145: move version setting to node installer
Browse files Browse the repository at this point in the history
  • Loading branch information
flipatlas committed Sep 30, 2024
1 parent 5c58f3b commit 02f0008
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 59 deletions.
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.8
Original file line number Diff line number Diff line change
Expand Up @@ -90,42 +90,34 @@ public void execute(FrontendPluginFactory factory) throws Exception {
String npmDownloadRoot = getNpmDownloadRoot();
Server server = MojoUtils.decryptServer(serverId, session, decrypter);

String nodeVersion = NodeVersionDetector.getNodeVersion(workingDirectory, this.nodeVersion, this.nodeVersionFile);

if (isNull(nodeVersion)) {
throw new LifecycleExecutionException("Node version could not be detected from a file and was not set");
}

if (!NodeVersionHelper.validateVersion(nodeVersion)) {
throw new LifecycleExecutionException("Node version (" + nodeVersion + ") is not valid. If you think it actually is, raise an issue");
}

String validNodeVersion = getDownloadableVersion(nodeVersion);

if (null != server) {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(validNodeVersion)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(validNodeVersion)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNpmVersion(npmVersion)
.setNpmDownloadRoot(npmDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
} else {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(validNodeVersion)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(validNodeVersion)
.setNpmVersion(this.npmVersion)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNpmVersion(npmVersion)
.setNpmDownloadRoot(npmDownloadRoot)
.install();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,10 @@ public void execute(FrontendPluginFactory factory) throws Exception {
String resolvedPnpmDownloadRoot = getPnpmDownloadRoot();
Server server = MojoUtils.decryptServer(serverId, session, decrypter);

String nodeVersion = NodeVersionDetector.getNodeVersion(workingDirectory, this.nodeVersion, this.nodeVersionFile);

if (isNull(nodeVersion)) {
throw new LifecycleExecutionException("Node version could not be detected from a file and was not set");
}

if (!NodeVersionHelper.validateVersion(nodeVersion)) {
throw new LifecycleExecutionException("Node version (" + nodeVersion + ") is not valid. If you think it actually is, raise an issue");
}

String validNodeVersion = getDownloadableVersion(nodeVersion);

if (null != server) {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(validNodeVersion)
.setNodeVersion(nodeVersion)
.setNodeVersionFile(nodeVersionFile)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
Expand All @@ -123,7 +112,8 @@ public void execute(FrontendPluginFactory factory) throws Exception {
.install();
} else {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(validNodeVersion)
.setNodeVersion(nodeVersion)
.setNodeVersionFile(nodeVersionFile)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.install();
factory.getPnpmInstaller(proxyConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,25 @@ public void execute(FrontendPluginFactory factory) throws Exception {
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(this.session, this.decrypter);
Server server = MojoUtils.decryptServer(this.serverId, this.session, this.decrypter);

String nodeVersion = NodeVersionDetector.getNodeVersion(workingDirectory, this.nodeVersion, this.nodeVersionFile);

if (isNull(nodeVersion)) {
throw new LifecycleExecutionException("Node version could not be detected from a file and was not set");
}

if (!NodeVersionHelper.validateVersion(nodeVersion)) {
throw new LifecycleExecutionException("Node version (" + nodeVersion + ") is not valid. If you think it actually is, raise an issue");
}

String validNodeVersion = getDownloadableVersion(nodeVersion);

boolean isYarnYamlFilePresent = isYarnrcYamlFilePresent(this.session, this.workingDirectory);

if (null != server) {
factory.getNodeInstaller(proxyConfig).setNodeDownloadRoot(this.nodeDownloadRoot)
.setNodeVersion(validNodeVersion).setPassword(server.getPassword())
.setUserName(server.getUsername()).install();
factory.getNodeInstaller(proxyConfig)
.setNodeDownloadRoot(this.nodeDownloadRoot)
.setNodeVersion(nodeVersion)
.setNodeVersionFile(nodeVersionFile)
.setPassword(server.getPassword())
.setUserName(server.getUsername())
.install();
factory.getYarnInstaller(proxyConfig).setYarnDownloadRoot(this.yarnDownloadRoot)
.setYarnVersion(this.yarnVersion).setUserName(server.getUsername())
.setPassword(server.getPassword()).setIsYarnBerry(isYarnYamlFilePresent).install();
} else {
factory.getNodeInstaller(proxyConfig).setNodeDownloadRoot(this.nodeDownloadRoot)
.setNodeVersion(validNodeVersion).install();
factory.getNodeInstaller(proxyConfig)
.setNodeDownloadRoot(this.nodeDownloadRoot)
.setNodeVersion(nodeVersion)
.setNodeVersionFile(nodeVersionFile)
.install();
factory.getYarnInstaller(proxyConfig).setYarnDownloadRoot(this.yarnDownloadRoot)
.setYarnVersion(this.yarnVersion).setIsYarnBerry(isYarnYamlFilePresent).install();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@

import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerCache;
import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerRunner;
import jdk.internal.joptsimple.internal.Strings;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.github.eirslett.maven.plugins.frontend.lib.NodeVersionHelper.getDownloadableVersion;
import static java.util.Objects.isNull;

public class NodeInstaller {

public static final String INSTALL_PATH = "/node";

private static final Object LOCK = new Object();

private String npmVersion, nodeVersion, nodeDownloadRoot, userName, password;
private String npmVersion, nodeVersion, nodeVersionFile, nodeDownloadRoot, userName, password;

private final Logger logger;

Expand All @@ -46,6 +48,11 @@ public NodeInstaller setNodeVersion(String nodeVersion) {
return this;
}

public NodeInstaller setNodeVersionFile(String nodeVersionFile) {
this.nodeVersionFile = nodeVersionFile;
return this;
}

public NodeInstaller setNodeDownloadRoot(String nodeDownloadRoot) {
this.nodeDownloadRoot = nodeDownloadRoot;
return this;
Expand Down Expand Up @@ -86,7 +93,7 @@ public void install() throws InstallationException {
if (this.nodeDownloadRoot == null || this.nodeDownloadRoot.isEmpty()) {
this.nodeDownloadRoot = this.installConfig.getPlatform().getNodeDownloadRoot();
}
verifyNodeVersion();
verifyAndResolveNodeVersion();

// try to install with node version manager
if (this.versionManagerCache.isVersionManagerAvailable()) {
Expand Down Expand Up @@ -117,17 +124,33 @@ public void install() throws InstallationException {
}
}

private void verifyNodeVersion() throws InstallationException {
private void verifyAndResolveNodeVersion() throws InstallationException {
if (this.versionManagerCache.isVersionManagerAvailable()) {
if (!Strings.isNullOrEmpty(this.nodeVersion)) {
logger.warn("`nodeVersion` has been configured to {} but will be ignored." +
if (this.nodeVersion != null && !this.nodeVersion.isEmpty()) {
logger.warn("`nodeVersion` has been configured to {} but will be ignored when installing with node version manager." +
" Version Manager will load the version from their version file (e.g. .nvmrc, .tool-versions)", this.nodeVersion);
}
} else {
if (Strings.isNullOrEmpty(this.nodeVersion)) {
throw new InstallationException("`nodeVersion` needs to be provided when running node installation without node version manager.");
}
}

String nodeVersion;
try {
nodeVersion = NodeVersionDetector.getNodeVersion(this.installConfig.getWorkingDirectory(), this.nodeVersion, this.nodeVersionFile);
} catch (Exception e) {
throw new InstallationException(e.getMessage());
}

if (isNull(nodeVersion)) {
throw new InstallationException("Node version could not be detected from a file and was not set");
}

if (!NodeVersionHelper.validateVersion(nodeVersion)) {
throw new InstallationException("Node version (" + nodeVersion + ") is not valid. If you think it actually is, raise an issue");
}

String validNodeVersion = getDownloadableVersion(nodeVersion);
logger.info("Resolved Node version: {}", validNodeVersion);

this.nodeVersion = validNodeVersion;
}

private boolean nodeIsAlreadyInstalled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.List;
import java.util.Map;

import jdk.jfr.internal.LogLevel;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
Expand Down

0 comments on commit 02f0008

Please sign in to comment.