Skip to content

Commit

Permalink
DCA11Y-1145: extract node install mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
flipatlas committed Sep 30, 2024
1 parent 02f0008 commit e1f5598
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.eirslett</groupId>
<artifactId>fnm-with-npm</artifactId>
<artifactId>fnm-with-no-version-manager</artifactId>
<version>0</version>
<packaging>pom</packaging>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.NodeVersionDetector;
import com.github.eirslett.maven.plugins.frontend.lib.NodeVersionHelper;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
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 abstract class AbstractInstallNodeMojo extends AbstractFrontendMojo {

protected static final Logger logger = LoggerFactory.getLogger(AbstractInstallNodeMojo.class);

/**
* Where to download Node.js binary from. Defaults to https://nodejs.org/dist/
*/
@Parameter(property = "nodeDownloadRoot", required = false)
protected String nodeDownloadRoot;

/**
* The version of Node.js to install. IMPORTANT! Most Node.js version names start with 'v', for example 'v0.10.18'
* If using with node version manager (enabled by default), nodeVersion parameter will be ignored
*/
@Parameter(property = "nodeVersion", defaultValue = "", required = false)
protected String nodeVersion;

/**
* The path to the file that contains the Node version to use
*/
@Parameter(property = "nodeVersionFile", defaultValue = "", required = false)
protected String nodeVersionFile;

@Override
protected void execute(FrontendPluginFactory factory) throws Exception {
verifyAndResolveNodeVersion(factory);
executeWithVerifiedNodeVersion(factory);
}

protected abstract void executeWithVerifiedNodeVersion(FrontendPluginFactory factory) throws Exception;

private void verifyAndResolveNodeVersion(FrontendPluginFactory factory) throws Exception {
if (factory.isVersionManagerAvailable()) {
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);
}
}

String nodeVersion = NodeVersionDetector.getNodeVersion(this.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);
logger.info("Resolved Node version: {}", validNodeVersion);

this.nodeVersion = validNodeVersion;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.InstallationException;
import com.github.eirslett.maven.plugins.frontend.lib.NPMInstaller;
import com.github.eirslett.maven.plugins.frontend.lib.NodeVersionDetector;
import com.github.eirslett.maven.plugins.frontend.lib.NodeVersionHelper;
Expand All @@ -18,13 +19,7 @@
import static java.util.Objects.isNull;

@Mojo(name="install-node-and-npm", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class InstallNodeAndNpmMojo extends AbstractFrontendMojo {

/**
* Where to download Node.js binary from. Defaults to https://nodejs.org/dist/
*/
@Parameter(property = "nodeDownloadRoot", required = false)
private String nodeDownloadRoot;
public final class InstallNodeAndNpmMojo extends AbstractInstallNodeMojo {

/**
* Where to download NPM binary from. Defaults to https://registry.npmjs.org/npm/-/
Expand All @@ -35,25 +30,12 @@ public final class InstallNodeAndNpmMojo extends AbstractFrontendMojo {
/**
* Where to download Node.js and NPM binaries from.
*
* @deprecated use {@link #nodeDownloadRoot} and {@link #npmDownloadRoot} instead, this configuration will be used only when no {@link #nodeDownloadRoot} or {@link #npmDownloadRoot} is specified.
* @deprecated use {@link AbstractInstallNodeMojo#nodeDownloadRoot} and {@link #npmDownloadRoot} instead, this configuration will be used only when no {@link AbstractInstallNodeMojo#nodeDownloadRoot} or {@link #npmDownloadRoot} is specified.
*/
@Parameter(property = "downloadRoot", required = false, defaultValue = "")
@Deprecated
private String downloadRoot;

/**
* The version of Node.js to install. IMPORTANT! Most Node.js version names start with 'v', for example 'v0.10.18'
* If using with node version manager (enabled by default), nodeVersion parameter will be ignored
*/
@Parameter(property = "nodeVersion", defaultValue = "", required = false)
private String nodeVersion;

/**
* The path to the file that contains the Node version to use
*/
@Parameter(property = "nodeVersionFile", defaultValue = "", required = false)
private String nodeVersionFile;

/**
* The version of NPM to install.
*/
Expand Down Expand Up @@ -84,7 +66,7 @@ protected boolean skipExecution() {
}

@Override
public void execute(FrontendPluginFactory factory) throws Exception {
public void executeWithVerifiedNodeVersion(FrontendPluginFactory factory) throws Exception {
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(session, decrypter);
String nodeDownloadRoot = getNodeDownloadRoot();
String npmDownloadRoot = getNpmDownloadRoot();
Expand All @@ -93,15 +75,13 @@ public void execute(FrontendPluginFactory factory) throws Exception {
if (null != server) {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNpmVersion(npmVersion)
.setNpmDownloadRoot(npmDownloadRoot)
.setUserName(server.getUsername())
Expand All @@ -110,13 +90,11 @@ public void execute(FrontendPluginFactory factory) throws Exception {
} else {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeVersion(nodeVersionFile)
.setNpmVersion(npmVersion)
.setNpmDownloadRoot(npmDownloadRoot)
.install();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@
import static java.util.Objects.isNull;

@Mojo(name="install-node-and-pnpm", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class InstallNodeAndPnpmMojo extends AbstractFrontendMojo {

/**
* Where to download Node.js binary from. Defaults to https://nodejs.org/dist/
*/
@Parameter(property = "nodeDownloadRoot", required = false)
private String nodeDownloadRoot;

public final class InstallNodeAndPnpmMojo extends AbstractInstallNodeMojo {
/**
* Where to download pnpm binary from. Defaults to https://registry.npmjs.org/pnpm/-/
*/
Expand All @@ -41,19 +34,6 @@ public final class InstallNodeAndPnpmMojo extends AbstractFrontendMojo {
@Deprecated
private String downloadRoot;

/**
* The version of Node.js to install. IMPORTANT! Most Node.js version names start with 'v', for example 'v0.10.18'
* If using with node version manager (enabled by default), nodeVersion parameter will be ignored
*/
@Parameter(property = "nodeVersion", defaultValue = "", required = false)
private String nodeVersion;

/**
* The path to the file that contains the Node version to use
*/
@Parameter(property = "nodeVersionFile", defaultValue = "", required = false)
private String nodeVersionFile;

/**
* The version of pnpm to install. Note that the version string can optionally be prefixed with
* 'v' (i.e., both 'v1.2.3' and '1.2.3' are valid).
Expand Down Expand Up @@ -85,7 +65,7 @@ protected boolean skipExecution() {
}

@Override
public void execute(FrontendPluginFactory factory) throws Exception {
public void executeWithVerifiedNodeVersion(FrontendPluginFactory factory) throws Exception {
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(session, decrypter);
// Use different names to avoid confusion with fields `nodeDownloadRoot` and
// `pnpmDownloadRoot`.
Expand All @@ -99,7 +79,6 @@ public void execute(FrontendPluginFactory factory) throws Exception {
if (null != server) {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeVersionFile(nodeVersionFile)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
Expand All @@ -113,7 +92,6 @@ public void execute(FrontendPluginFactory factory) throws Exception {
} else {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeVersionFile(nodeVersionFile)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.install();
factory.getPnpmInstaller(proxyConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,17 @@
import static java.util.Objects.isNull;

@Mojo(name = "install-node-and-yarn", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class InstallNodeAndYarnMojo extends AbstractFrontendMojo {
public final class InstallNodeAndYarnMojo extends AbstractInstallNodeMojo {

private static final String YARNRC_YAML_FILE_NAME = ".yarnrc.yml";

/**
* Where to download Node.js binary from. Defaults to https://nodejs.org/dist/
*/
@Parameter(property = "nodeDownloadRoot", required = false)
private String nodeDownloadRoot;

/**
* Where to download Yarn binary from. Defaults to https://github.com/yarnpkg/yarn/releases/download/...
*/
@Parameter(property = "yarnDownloadRoot", required = false,
defaultValue = YarnInstaller.DEFAULT_YARN_DOWNLOAD_ROOT)
private String yarnDownloadRoot;

/**
* The version of Node.js to install. IMPORTANT! Most Node.js version names start with 'v', for example
* 'v0.10.18'
* If using with node version manager (enabled by default), nodeVersion parameter will be ignored
*/
@Parameter(property = "nodeVersion", defaultValue = "", required = false)
private String nodeVersion;

/**
* The path to the file that contains the Node version to use
*/
@Parameter(property = "nodeVersionFile", defaultValue = "", required = false)
private String nodeVersionFile;

/**
* The version of Yarn to install. IMPORTANT! Most Yarn names start with 'v', for example 'v0.15.0'.
*/
Expand Down Expand Up @@ -80,7 +60,7 @@ protected boolean skipExecution() {
}

@Override
public void execute(FrontendPluginFactory factory) throws Exception {
public void executeWithVerifiedNodeVersion(FrontendPluginFactory factory) throws Exception {
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(this.session, this.decrypter);
Server server = MojoUtils.decryptServer(this.serverId, this.session, this.decrypter);

Expand All @@ -90,7 +70,6 @@ public void execute(FrontendPluginFactory factory) throws Exception {
factory.getNodeInstaller(proxyConfig)
.setNodeDownloadRoot(this.nodeDownloadRoot)
.setNodeVersion(nodeVersion)
.setNodeVersionFile(nodeVersionFile)
.setPassword(server.getPassword())
.setUserName(server.getUsername())
.install();
Expand All @@ -101,7 +80,6 @@ public void execute(FrontendPluginFactory factory) throws Exception {
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 @@ -158,4 +158,9 @@ public void loadVersionManager() {
}
}
}

public boolean isVersionManagerAvailable() {
if (!getInstallConfig().isUseNodeVersionManager()) return false;
return getVersionManagerCache().isVersionManagerAvailable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
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, nodeVersionFile, nodeDownloadRoot, userName, password;
private String npmVersion, nodeVersion, nodeDownloadRoot, userName, password;

private final Logger logger;

Expand All @@ -48,11 +45,6 @@ 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 @@ -93,7 +85,6 @@ public void install() throws InstallationException {
if (this.nodeDownloadRoot == null || this.nodeDownloadRoot.isEmpty()) {
this.nodeDownloadRoot = this.installConfig.getPlatform().getNodeDownloadRoot();
}
verifyAndResolveNodeVersion();

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

private void verifyAndResolveNodeVersion() throws InstallationException {
if (this.versionManagerCache.isVersionManagerAvailable()) {
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);
}
}

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() {
try {
NodeExecutorConfig executorConfig = new InstallNodeExecutorConfig(this.installConfig, versionManagerCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Objects.isNull;
import static java.util.Optional.empty;
import static java.util.stream.Collectors.toSet;

Expand Down

0 comments on commit e1f5598

Please sign in to comment.