Skip to content

Commit

Permalink
DCA11Y-1145: add node version manager support to node and npm installers
Browse files Browse the repository at this point in the history
  • Loading branch information
flipatlas committed Sep 16, 2024
1 parent d2f488e commit 0a771db
Show file tree
Hide file tree
Showing 22 changed files with 520 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,16 @@

<build>
<plugins>
<!-- <plugin>-->
<!-- <groupId>org.codehaus.mojo</groupId>-->
<!-- <artifactId>exec-maven-plugin</artifactId>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>Install nvm</id>-->
<!-- <phase>generate-sources</phase>-->
<!-- <goals>-->
<!-- <goal>exec</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <executable>${basedir}/install-nvm.sh</executable>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- NB! Set <version> to the latest released version of frontend-maven-plugin, like in README.md -->
<version>@project.version@</version>

<configuration>
<nodeVersion>v20.15.1</nodeVersion>
<nodeVersion>20.15.1</nodeVersion>
<workingDirectory>${basedir}</workingDirectory>
<useNodeVersionManager>true</useNodeVersionManager>
</configuration>

<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,16 @@

<build>
<plugins>
<!-- <plugin>-->
<!-- <groupId>org.codehaus.mojo</groupId>-->
<!-- <artifactId>exec-maven-plugin</artifactId>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>Install nvm</id>-->
<!-- <phase>generate-sources</phase>-->
<!-- <goals>-->
<!-- <goal>exec</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <executable>${basedir}/install-nvm.sh</executable>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- NB! Set <version> to the latest released version of frontend-maven-plugin, like in README.md -->
<version>@project.version@</version>

<configuration>
<nodeVersion>v20.15.1</nodeVersion>
<nodeVersion>20.15.1</nodeVersion>
<workingDirectory>${basedir}</workingDirectory>
<useNodeVersionManager>true</useNodeVersionManager>
</configuration>

<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public abstract class AbstractFrontendMojo extends AbstractMojo {
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
private RepositorySystemSession repositorySystemSession;

@Parameter(property = "useNodeVersionManager", readonly = true, defaultValue = "false")
private boolean useNodeVersionManager;

/**
* Determines if this execution should be skipped.
*/
Expand Down Expand Up @@ -92,7 +95,7 @@ public void execute() throws MojoFailureException {
}
try {
execute(new FrontendPluginFactory(workingDirectory, installDirectory,
new RepositoryCacheResolver(repositorySystemSession)));
new RepositoryCacheResolver(repositorySystemSession), useNodeVersionManager));
} catch (TaskRunnerException e) {
if (testFailureIgnore && isTestingPhase()) {
getLog().error("There are test failures.\nFailed to run task: " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public final class InstallNodeAndNpmMojo extends AbstractFrontendMojo {

/**
* The version of Node.js to install. IMPORTANT! Most Node.js version names start with 'v', for example 'v0.10.18'
* If set to `version_manager`, node version manager will be used to run installation commands
*/
@Parameter(property="nodeVersion", required = true)
@Parameter(property="nodeVersion", required = false)
private String nodeVersion;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public synchronized void execute(FrontendPluginFactory factory) throws TaskRunne
File packageJson = new File(workingDirectory, "package.json");
if (buildContext == null || buildContext.hasDelta(packageJson) || !buildContext.isIncremental()) {
ProxyConfig proxyConfig = getProxyConfig();
factory.getVersionManagerRunner().populateCache();
factory.getNpmRunner(proxyConfig, getRegistryUrl()).execute(arguments, environmentVariables);
} else {
getLog().info("Skipping npm install as package.json unchanged");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.github.eirslett.maven.plugins.frontend.lib;

import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerCache;
import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerRunner;
import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerType;
import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerLocator;

import java.io.File;

public final class FrontendPluginFactory {
Expand All @@ -10,26 +15,45 @@ public final class FrontendPluginFactory {
private final File workingDirectory;
private final File installDirectory;
private final CacheResolver cacheResolver;
private final boolean useNodeVersionManager;

public FrontendPluginFactory(File workingDirectory, File installDirectory){
this(workingDirectory, installDirectory, getDefaultCacheResolver(installDirectory));
}

public FrontendPluginFactory(File workingDirectory, File installDirectory, CacheResolver cacheResolver){
this(workingDirectory, installDirectory, cacheResolver, false);
}

public FrontendPluginFactory(File workingDirectory, File installDirectory, CacheResolver cacheResolver, boolean useNodeVersionManager){
this.workingDirectory = workingDirectory;
this.installDirectory = installDirectory;
this.cacheResolver = cacheResolver;
this.useNodeVersionManager = useNodeVersionManager;

initializeGlobalCache();
}

private void initializeGlobalCache() {
InstallConfig installConfig = new DefaultInstallConfig(installDirectory, workingDirectory, cacheResolver, defaultPlatform, useNodeVersionManager);
GlobalCache.setInstallConfig(installConfig);

VersionManagerLocator versionManagerLocator = new VersionManagerLocator(installConfig);
VersionManagerType versionManagerType = versionManagerLocator.findAvailable();
GlobalCache.setVersionManagerCache(
new VersionManagerCache(versionManagerType)
);
}

public BunInstaller getBunInstaller(ProxyConfig proxy) {
return new BunInstaller(getInstallConfig(), new DefaultArchiveExtractor(), new DefaultFileDownloader(proxy));
}
public NodeInstaller getNodeInstaller(ProxyConfig proxy) {
return new NodeInstaller(getInstallConfig(), new DefaultArchiveExtractor(), new DefaultFileDownloader(proxy));
return new NodeInstaller(getInstallConfig(), getVersionManagerCache(), new DefaultArchiveExtractor(), new DefaultFileDownloader(proxy));
}

public NPMInstaller getNPMInstaller(ProxyConfig proxy) {
return new NPMInstaller(getInstallConfig(), new DefaultArchiveExtractor(), new DefaultFileDownloader(proxy));
return new NPMInstaller(getInstallConfig(), getVersionManagerCache(), new DefaultArchiveExtractor(), new DefaultFileDownloader(proxy));
}

public PnpmInstaller getPnpmInstaller(ProxyConfig proxy) {
Expand Down Expand Up @@ -88,12 +112,20 @@ public WebpackRunner getWebpackRunner(){
return new DefaultWebpackRunner(getExecutorConfig());
}

public VersionManagerRunner getVersionManagerRunner() {
return new VersionManagerRunner(getInstallConfig(), getVersionManagerCache());
}

private NodeExecutorConfig getExecutorConfig() {
return new InstallNodeExecutorConfig(getInstallConfig());
return new InstallNodeExecutorConfig(getInstallConfig(), getVersionManagerCache());
}

private InstallConfig getInstallConfig() {
return new DefaultInstallConfig(installDirectory, workingDirectory, cacheResolver, defaultPlatform);
return GlobalCache.getInstallConfig();
}

private VersionManagerCache getVersionManagerCache() {
return GlobalCache.getVersionManagerCache();
}

private static final CacheResolver getDefaultCacheResolver(File root) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.eirslett.maven.plugins.frontend.lib;

import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerCache;
import com.github.eirslett.maven.plugins.frontend.lib.version.manager.client.VersionManagerClient;

// TODO add builder pattern
public class GlobalCache {

private static InstallConfig installConfig;

private static VersionManagerCache versionManagerCache;


public static InstallConfig getInstallConfig() {
assert installConfig != null;
return installConfig;
}

public static void setInstallConfig(InstallConfig installConfig) {
GlobalCache.installConfig = installConfig;
}

public static VersionManagerCache getVersionManagerCache() {
assert versionManagerCache != null;
return versionManagerCache;
}

public static void setVersionManagerCache(VersionManagerCache versionManagerCache) {
GlobalCache.versionManagerCache = versionManagerCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,43 @@

public interface InstallConfig {
File getInstallDirectory();
void setInstallDirectory(File installDirectory);
File getWorkingDirectory();
CacheResolver getCacheResolver();
Platform getPlatform();
boolean isUseNodeVersionManager();
}

final class DefaultInstallConfig implements InstallConfig {

private final File installDirectory;
private File installDirectory;
private final File workingDirectory;
private final CacheResolver cacheResolver;
private final Platform platform;
private final boolean useNodeVersionManager;

public DefaultInstallConfig(File installDirectory,
File workingDirectory,
CacheResolver cacheResolver,
Platform platform) {
Platform platform,
boolean useNodeVersionManager) {
this.installDirectory = installDirectory;
this.workingDirectory = workingDirectory;
this.cacheResolver = cacheResolver;
this.platform = platform;
this.useNodeVersionManager = useNodeVersionManager;
}

@Override
public File getInstallDirectory() {
return this.installDirectory;
}

@Override
public void setInstallDirectory(File installDirectory) {
this.installDirectory = installDirectory;
}

@Override
public File getWorkingDirectory() {
return this.workingDirectory;
Expand All @@ -45,4 +55,7 @@ public Platform getPlatform() {
return this.platform;
}

public boolean isUseNodeVersionManager() {
return useNodeVersionManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;

import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerCache;
import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerRunner;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,11 +31,14 @@ public class NPMInstaller {

private final FileDownloader fileDownloader;

NPMInstaller(InstallConfig config, ArchiveExtractor archiveExtractor, FileDownloader fileDownloader) {
private final VersionManagerCache versionManagerCache;

NPMInstaller(InstallConfig config, VersionManagerCache versionManagerCache, ArchiveExtractor archiveExtractor, FileDownloader fileDownloader) {
this.logger = LoggerFactory.getLogger(getClass());
this.config = config;
this.archiveExtractor = archiveExtractor;
this.fileDownloader = fileDownloader;
this.versionManagerCache = versionManagerCache;
}

public NPMInstaller setNodeVersion(String nodeVersion) {
Expand Down Expand Up @@ -78,6 +84,11 @@ public void install() throws InstallationException {
if (this.npmDownloadRoot == null || this.npmDownloadRoot.isEmpty()) {
this.npmDownloadRoot = DEFAULT_NPM_DOWNLOAD_ROOT;
}

if (versionManagerCache.isVersionManagerInstalled()){
// TODO maybe handle custom npm installation in the future
return;
}
if (!npmProvided() && !npmIsAlreadyInstalled()) {
installNpm();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.eirslett.maven.plugins.frontend.lib;

import com.github.eirslett.maven.plugins.frontend.lib.version.manager.VersionManagerCache;

import java.io.File;

public interface NodeExecutorConfig {
Expand All @@ -25,18 +27,29 @@ final class InstallNodeExecutorConfig implements NodeExecutorConfig {

private final InstallConfig installConfig;

private final VersionManagerCache versionManagerCache;

public InstallNodeExecutorConfig(InstallConfig installConfig) {
this(installConfig, null);
}

public InstallNodeExecutorConfig(InstallConfig installConfig, VersionManagerCache versionManagerCache) {
this.installConfig = installConfig;
this.versionManagerCache = versionManagerCache;
}

@Override
public File getNodePath() {
if (versionManagerCache != null && versionManagerCache.isVersionManagerInstalled()) return versionManagerCache.getNodeExecutable();

String nodeExecutable = getPlatform().isWindows() ? NODE_WINDOWS : NODE_DEFAULT;
return new File(installConfig.getInstallDirectory() + nodeExecutable);
}

@Override
public File getNpmPath() {
if (versionManagerCache != null && versionManagerCache.isVersionManagerInstalled()) return versionManagerCache.getNpmExecutable();

return new File(installConfig.getInstallDirectory() + Utils.normalize(NPM));
}

Expand Down
Loading

0 comments on commit 0a771db

Please sign in to comment.