-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces flacky Maven Frontend Plugin download manager by Vertx to in…
…stall Node.js (#513) * 2.2.0.CR1 * Try use vertx webclient to install Node.js
- Loading branch information
Showing
3 changed files
with
169 additions
and
21 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...ain/java/com/github/eirslett/maven/plugins/frontend/lib/PackageManagerInstallFactory.java
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.github.eirslett.maven.plugins.frontend.lib; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
|
||
import io.vertx.core.Vertx; | ||
|
||
public class PackageManagerInstallFactory { | ||
|
||
private static final Platform defaultPlatform = Platform.guess(); | ||
private static final String DEFAULT_CACHE_PATH = "cache"; | ||
private final Vertx vertx; | ||
private final Path installDirectory; | ||
private final CacheResolver cacheResolver; | ||
private final VertxFileDownloader fileDownloader; | ||
|
||
public PackageManagerInstallFactory(Vertx vertx, Path installDirectory) { | ||
this.vertx = vertx; | ||
this.installDirectory = installDirectory; | ||
this.cacheResolver = getDefaultCacheResolver(installDirectory); | ||
fileDownloader = new VertxFileDownloader(vertx); | ||
} | ||
|
||
public NodeInstaller getNodeInstaller() { | ||
|
||
return new NodeInstaller(this.getInstallConfig(), new DefaultArchiveExtractor(), fileDownloader); | ||
} | ||
|
||
public NPMInstaller getNPMInstaller() { | ||
return new NPMInstaller(this.getInstallConfig(), new DefaultArchiveExtractor(), fileDownloader); | ||
} | ||
|
||
public PnpmInstaller getPnpmInstaller() { | ||
return new PnpmInstaller(this.getInstallConfig(), new DefaultArchiveExtractor(), fileDownloader); | ||
} | ||
|
||
public YarnInstaller getYarnInstaller() { | ||
return new YarnInstaller(this.getInstallConfig(), new DefaultArchiveExtractor(), fileDownloader); | ||
} | ||
|
||
private NodeExecutorConfig getExecutorConfig() { | ||
return new InstallNodeExecutorConfig(this.getInstallConfig()); | ||
} | ||
|
||
private InstallConfig getInstallConfig() { | ||
final File installDirFile = this.installDirectory.toFile(); | ||
return new DefaultInstallConfig(installDirFile, installDirFile, this.cacheResolver, defaultPlatform); | ||
} | ||
|
||
private static final CacheResolver getDefaultCacheResolver(Path root) { | ||
return new DirectoryCacheResolver(root.resolve("cache").toFile()); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...ent/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/VertxFileDownloader.java
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.eirslett.maven.plugins.frontend.lib; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.commons.io.FilenameUtils; | ||
import org.jboss.logging.Logger; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.ext.web.client.HttpRequest; | ||
import io.vertx.ext.web.client.HttpResponse; | ||
import io.vertx.ext.web.client.WebClient; | ||
import io.vertx.ext.web.client.WebClientOptions; | ||
|
||
public class VertxFileDownloader implements FileDownloader { | ||
private static final Logger LOG = Logger.getLogger(VertxFileDownloader.class); | ||
private final Vertx vertx; | ||
private WebClient webClient; | ||
|
||
public VertxFileDownloader(Vertx vertx) { | ||
this.vertx = vertx; | ||
webClient = WebClient.create(vertx, new WebClientOptions() | ||
.setSsl(true) | ||
.setFollowRedirects(true) | ||
.setTrustAll(true) | ||
.setKeepAlive(true)); | ||
} | ||
|
||
public void download(String downloadUrl, String destination, String userName, String password) throws DownloadException { | ||
System.setProperty("https.protocols", "TLSv1.2"); | ||
String fixedDownloadUrl = downloadUrl; | ||
|
||
try { | ||
fixedDownloadUrl = FilenameUtils.separatorsToUnix(fixedDownloadUrl); | ||
URI downloadURI = new URI(fixedDownloadUrl); | ||
final Path destinationPath = Path.of(destination); | ||
if ("file".equalsIgnoreCase(downloadURI.getScheme())) { | ||
Files.copy(Paths.get(downloadURI), destinationPath); | ||
} else { | ||
final HttpRequest<Buffer> request = webClient.getAbs(downloadUrl); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
Future<HttpResponse<Buffer>> future = request.send(); | ||
future.onComplete((r) -> latch.countDown()); | ||
latch.await(5, TimeUnit.MINUTES); | ||
if (future.succeeded()) { | ||
Files.write(destinationPath, future.result().body().getBytes()); | ||
} else { | ||
throw new DownloadException("Could not download " + downloadUrl, future.cause()); | ||
} | ||
} | ||
|
||
} catch (URISyntaxException | IOException e) { | ||
throw new DownloadException("Could not download " + downloadUrl, e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
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