-
Notifications
You must be signed in to change notification settings - Fork 870
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
Use a script to proxy PNPM executable on Windows #1116
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
package com.github.eirslett.maven.plugins.frontend.lib; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.EOFException; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import org.apache.commons.io.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
|
||
public class PnpmInstaller { | ||
|
||
private static final String VERSION = "version"; | ||
|
@@ -69,7 +68,12 @@ public void install() throws InstallationException { | |
if (!pnpmIsAlreadyInstalled()) { | ||
installPnpm(); | ||
} | ||
copyPnpmScripts(); | ||
|
||
if (this.config.getPlatform().isWindows()) { | ||
linkExecutableWindows(); | ||
} else { | ||
linkExecutable(); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -169,42 +173,72 @@ private void installPnpm() throws InstallationException { | |
} | ||
} | ||
|
||
private void copyPnpmScripts() throws InstallationException{ | ||
File installDirectory = getNodeInstallDirectory(); | ||
|
||
File nodeModulesDirectory = new File(installDirectory, "node_modules"); | ||
File pnpmDirectory = new File(nodeModulesDirectory, "pnpm"); | ||
// create a copy of the pnpm scripts next to the node executable | ||
for (String script : Arrays.asList("pnpm", "pnpm.cmd")) { | ||
File scriptFile = new File(pnpmDirectory, "bin" + File.separator + script); | ||
if (scriptFile.exists()) { | ||
File copy = new File(installDirectory, script); | ||
if (!copy.exists()) { | ||
try | ||
{ | ||
FileUtils.copyFile(scriptFile, copy); | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new InstallationException("Could not copy pnpm", e); | ||
} | ||
copy.setExecutable(true); | ||
} | ||
} | ||
private void linkExecutable() throws InstallationException{ | ||
File nodeInstallDirectory = getNodeInstallDirectory(); | ||
File pnpmExecutable = new File(nodeInstallDirectory, "pnpm"); | ||
|
||
if (pnpmExecutable.exists()) { | ||
this.logger.info("Existing pnpm executable found, skipping linking."); | ||
return; | ||
} | ||
// On non-windows platforms, if no predefined executables exist, symlink the .cjs executable | ||
File pnpmExecutable = new File(installDirectory, "pnpm"); | ||
if (!pnpmExecutable.exists() && !this.config.getPlatform().isWindows()) { | ||
|
||
File pnpmJsExecutable = new File(pnpmDirectory, "bin" + File.separator + "pnpm.cjs"); | ||
if (pnpmJsExecutable.exists()) { | ||
this.logger.info("No pnpm executable found, creating symlink to {}", pnpmJsExecutable.toPath()); | ||
try { | ||
Files.createSymbolicLink(pnpmExecutable.toPath(), pnpmJsExecutable.toPath()); | ||
} catch (IOException e) { | ||
throw new InstallationException("Could not copy pnpm", e); | ||
} | ||
} | ||
|
||
NodeExecutorConfig executorConfig = new InstallNodeExecutorConfig(this.config); | ||
File pnpmJsExecutable = executorConfig.getPnpmCjsPath(); | ||
|
||
if (!pnpmJsExecutable.exists()) { | ||
throw new InstallationException("Could not link to pnpm executable, no pnpm installation found."); | ||
} | ||
|
||
this.logger.info("No pnpm executable found, creating symbolic link to {}.", pnpmJsExecutable.toPath()); | ||
|
||
try { | ||
Files.createSymbolicLink(pnpmExecutable.toPath(), pnpmJsExecutable.toPath()); | ||
} catch (IOException e) { | ||
throw new InstallationException("Could not create symbolic link for pnpm executable.", e); | ||
} | ||
} | ||
|
||
private void linkExecutableWindows() throws InstallationException{ | ||
File nodeInstallDirectory = getNodeInstallDirectory(); | ||
File pnpmExecutable = new File(nodeInstallDirectory, "pnpm.cmd"); | ||
|
||
if (pnpmExecutable.exists()) { | ||
this.logger.info("Existing pnpm executable found, skipping linking."); | ||
return; | ||
} | ||
|
||
NodeExecutorConfig executorConfig = new InstallNodeExecutorConfig(this.config); | ||
File pnpmJsExecutable = executorConfig.getPnpmCjsPath(); | ||
|
||
if (!pnpmJsExecutable.exists()) { | ||
throw new InstallationException("Could not link to pnpm executable, no pnpm installation found."); | ||
} | ||
|
||
this.logger.info("No pnpm executable found, creating proxy script to {}.", pnpmJsExecutable.toPath()); | ||
|
||
Path nodePath = executorConfig.getNodePath().toPath(); | ||
Path relativeNodePath = nodeInstallDirectory.toPath().relativize(nodePath); | ||
Path relativePnpmPath = nodeInstallDirectory.toPath().relativize(pnpmJsExecutable.toPath()); | ||
|
||
// Create a script that will proxy any commands passed into it to the pnpm executable. | ||
String scriptContents = new StringBuilder() | ||
.append(":: Created by frontend-maven-plugin, please don't edit manually.\r\n") | ||
.append("@ECHO OFF\r\n") | ||
.append("\r\n") | ||
.append("SETLOCAL\r\n") | ||
.append("\r\n") | ||
.append(String.format("SET \"NODE_EXE=%%~dp0\\%s\"\r\n", relativeNodePath)) | ||
.append(String.format("SET \"PNPM_CLI_JS=%%~dp0\\%s\"\r\n", relativePnpmPath)) | ||
.append("\r\n") | ||
.append("\"%NODE_EXE%\" \"%PNPM_CLI_JS%\" %*") | ||
.toString(); | ||
Comment on lines
+224
to
+234
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This script is partially taken from the NPM equivalent, but pruned down to the bare essentials needed for this use-case. |
||
|
||
try { | ||
BufferedWriter writer = new BufferedWriter(new FileWriter(pnpmExecutable)); | ||
writer.write(scriptContents); | ||
writer.close(); | ||
} catch (IOException e) { | ||
throw new InstallationException("Could not create proxy script for pnpm executable.", e); | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed this old code as it seems to have been copied from the
NpmInstaller
, but never actually functioned. I've manually checked every release of PNPM, and none of them include apnpm
orpnpm.cmd
file in theirbin
directory.