2020-12-01 17:38:32 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.microsoft.playwright.impl;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2020-12-14 12:10:25 -08:00
|
|
|
import java.net.URI;
|
2020-12-01 17:38:32 -08:00
|
|
|
import java.net.URISyntaxException;
|
2020-12-14 12:10:25 -08:00
|
|
|
import java.nio.file.*;
|
|
|
|
import java.util.Collections;
|
2020-12-01 17:38:32 -08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2020-12-10 22:08:20 -08:00
|
|
|
public class DriverJar extends Driver {
|
2020-12-01 17:38:32 -08:00
|
|
|
private final Path driverTempDir;
|
|
|
|
|
|
|
|
DriverJar() throws IOException, URISyntaxException, InterruptedException {
|
|
|
|
driverTempDir = Files.createTempDirectory("playwright-java-");
|
|
|
|
driverTempDir.toFile().deleteOnExit();
|
2020-12-14 12:10:25 -08:00
|
|
|
System.err.println("extracting driver to " + driverTempDir);
|
|
|
|
extractDriverToTempDir();
|
|
|
|
installBrowsers();
|
|
|
|
}
|
2020-12-01 17:38:32 -08:00
|
|
|
|
2020-12-14 12:10:25 -08:00
|
|
|
private void installBrowsers() throws IOException, InterruptedException {
|
2020-12-01 17:38:32 -08:00
|
|
|
Path driver = driverTempDir.resolve("playwright-cli");
|
|
|
|
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "install");
|
|
|
|
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
|
|
|
|
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
|
|
|
Process p = pb.start();
|
|
|
|
boolean result = p.waitFor(10, TimeUnit.MINUTES);
|
|
|
|
if (!result) {
|
|
|
|
System.err.println("Timed out waiting for browsers to install");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:10:25 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:54:59 -08:00
|
|
|
private static String platformDir() {
|
|
|
|
String name = System.getProperty("os.name").toLowerCase();
|
|
|
|
if (name.contains("windows")) {
|
2020-12-15 00:45:50 +05:30
|
|
|
return System.getProperty("os.arch").equals("amd64") ? "win32_x64" : "win32";
|
2020-12-11 13:54:59 -08:00
|
|
|
}
|
|
|
|
if (name.contains("linux")) {
|
|
|
|
return "linux";
|
|
|
|
}
|
|
|
|
if (name.contains("mac os x")) {
|
|
|
|
return "mac";
|
|
|
|
}
|
|
|
|
throw new RuntimeException("Unexpected os.name value: " + name);
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:38:32 -08:00
|
|
|
private static Path extractResource(Path from, Path toDir) throws IOException {
|
2020-12-14 12:10:25 -08:00
|
|
|
Path path = toDir.resolve(from.getFileName().toString());
|
2020-12-01 17:38:32 -08:00
|
|
|
Files.copy(from, path);
|
|
|
|
path.toFile().setExecutable(true);
|
|
|
|
path.toFile().deleteOnExit();
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
Path driverDir() {
|
|
|
|
return driverTempDir;
|
|
|
|
}
|
|
|
|
}
|