Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support corepack #1157

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support corepack.
This adds some basic support for using corepack to manage the tooling
via the 'packageManager' field in the package.json. Here we add the
ability to download a version of it along with a node version, along
with the ability to execute command via corepack such as 'pnpm install'
or 'yarn install' depending on the selected package manager.

Later we ought to work out how to use the version packaged with the node
runtime, but this was inspired by the pnpm download which is downloaded
separately, and so this is too.
  • Loading branch information
stevestorey committed Jun 12, 2024
commit 5b7e30e35903a485221f9d7136db68c0b183e047
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import java.io.File;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.sonatype.plexus.build.incremental.BuildContext;

import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.TaskRunnerException;

@Mojo(name="corepack", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public final class CorepackMojo extends AbstractFrontendMojo {

/**
* corepack arguments. Default is "enable".
*/
@Parameter(defaultValue = "enable", property = "frontend.corepack.arguments", required = false)
private String arguments;

@Parameter(property = "session", defaultValue = "${session}", readonly = true)
private MavenSession session;

@Component
private BuildContext buildContext;

@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

/**
* Skips execution of this mojo.
*/
@Parameter(property = "skip.corepack", defaultValue = "${skip.corepack}")
private boolean skip;

@Override
protected boolean skipExecution() {
return this.skip;
}

@Override
public synchronized void execute(FrontendPluginFactory factory) throws TaskRunnerException {
File packageJson = new File(workingDirectory, "package.json");
if (buildContext == null || buildContext.hasDelta(packageJson) || !buildContext.isIncremental()) {
factory.getCorepackRunner().execute(arguments, environmentVariables);
} else {
getLog().info("Skipping corepack install as package.json unchanged");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import com.github.eirslett.maven.plugins.frontend.lib.CorepackInstaller;
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.ProxyConfig;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.Server;

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

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

/**
* Where to download corepack binary from. Defaults to https://registry.npmjs.org/corepack/-/
*/
@Parameter(property = "corepackDownloadRoot", required = false, defaultValue = CorepackInstaller.DEFAULT_COREPACK_DOWNLOAD_ROOT)
private String corepackDownloadRoot;

/**
* The version of Node.js to install. IMPORTANT! Most Node.js version names start with 'v', for example 'v0.10.18'
*/
@Parameter(property="nodeVersion", required = true)
private String nodeVersion;

/**
* The version of corepack 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).
*/
@Parameter(property = "corepackVersion", required = true)
private String corepackVersion;

/**
* Server Id for download username and password
*/
@Parameter(property = "serverId", defaultValue = "")
private String serverId;

@Parameter(property = "session", defaultValue = "${session}", readonly = true)
private MavenSession session;

/**
* Skips execution of this mojo.
*/
@Parameter(property = "skip.installnodecorepack", defaultValue = "${skip.installnodecorepack}")
private boolean skip;

@Component(role = SettingsDecrypter.class)
private SettingsDecrypter decrypter;

@Override
protected boolean skipExecution() {
return this.skip;
}

@Override
public void execute(FrontendPluginFactory factory) throws InstallationException {
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(session, decrypter);
String resolvedNodeDownloadRoot = getNodeDownloadRoot();
String resolvedCorepackDownloadRoot = getCorepackDownloadRoot();
Server server = MojoUtils.decryptServer(serverId, session, decrypter);
if (null != server) {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
factory.getCorepackInstaller(proxyConfig)
.setCorepackVersion(corepackVersion)
.setCorepackDownloadRoot(resolvedCorepackDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
} else {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.install();
factory.getCorepackInstaller(proxyConfig)
.setCorepackVersion(this.corepackVersion)
.setCorepackDownloadRoot(resolvedCorepackDownloadRoot)
.install();
}
}

private String getNodeDownloadRoot() {
return nodeDownloadRoot;
}

private String getCorepackDownloadRoot() {
return corepackDownloadRoot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<pluginExecution>
<pluginExecutionFilter>
<goals>
<goal>install-node-and-corepack</goal>
<goal>install-node-and-npm</goal>
<goal>install-node-and-pnpm</goal>
<goal>install-node-and-yarn</goal>
Expand All @@ -20,6 +21,7 @@
<pluginExecution>
<pluginExecutionFilter>
<goals>
<goal>corepack</goal>
<goal>npm</goal>
<goal>npx</goal>
<goal>pnpm</goal>
Expand Down
Loading