fix(diver): create ZipFileSystem instance to extrac cli files (#119)

This commit is contained in:
Yury Semikhatsky 2020-12-14 12:10:25 -08:00 committed by GitHub
parent 4b33bd6704
commit b1cc4d6b43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 15 deletions

View File

@ -17,10 +17,10 @@
package com.microsoft.playwright.impl;
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.nio.file.*;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
public class DriverJar extends Driver {
@ -29,16 +29,12 @@ public class DriverJar extends Driver {
DriverJar() throws IOException, URISyntaxException, InterruptedException {
driverTempDir = Files.createTempDirectory("playwright-java-");
driverTempDir.toFile().deleteOnExit();
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
Path path = Paths.get(classloader.getResource("driver/" + platformDir()).toURI());
Files.list(path).forEach(filePath -> {
try {
extractResource(filePath, driverTempDir);
} catch (IOException e) {
throw new RuntimeException("Failed to extract driver from " + path, e);
}
});
System.err.println("extracting driver to " + driverTempDir);
extractDriverToTempDir();
installBrowsers();
}
private void installBrowsers() throws IOException, InterruptedException {
Path driver = driverTempDir.resolve("playwright-cli");
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "install");
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
@ -50,6 +46,22 @@ public class DriverJar extends Driver {
}
}
private void extractDriverToTempDir() throws URISyntaxException, IOException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
URI uri = classloader.getResource("driver/" + platformDir()).toURI();
System.out.println(uri);
// Create zip filesystem if loading from jar.
try (FileSystem fileSystem = "jar".equals(uri.getScheme()) ? FileSystems.newFileSystem(uri, Collections.emptyMap()) : null) {
Files.list(Paths.get(uri)).forEach(filePath -> {
try {
extractResource(filePath, driverTempDir);
} catch (IOException e) {
throw new RuntimeException("Failed to extract driver from " + uri, e);
}
});
}
}
private static String platformDir() {
String name = System.getProperty("os.name").toLowerCase();
if (name.contains("windows")) {
@ -65,12 +77,10 @@ public class DriverJar extends Driver {
}
private static Path extractResource(Path from, Path toDir) throws IOException {
Path path = toDir.resolve(from.getFileName());
Path path = toDir.resolve(from.getFileName().toString());
Files.copy(from, path);
path.toFile().setExecutable(true);
path.toFile().deleteOnExit();
// System.out.println("extracting: " + from.toString() + " to " +
// path.toString());
return path;
}

View File

@ -50,6 +50,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>