Skip to content

Commit

Permalink
fix: get rid of driver wrapper (#2786)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Dec 5, 2023
1 parent 7d37063 commit 9da82ec
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 148 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

| | Linux | macOS | Windows |
| :--- | :---: | :---: | :---: |
| Chromium <!-- GEN:chromium-version -->120.0.6099.28<!-- GEN:stop --> ||||
| Chromium <!-- GEN:chromium-version -->120.0.6099.56<!-- GEN:stop --> ||||
| WebKit <!-- GEN:webkit-version -->17.4<!-- GEN:stop --> ||||
| Firefox <!-- GEN:firefox-version -->119.0<!-- GEN:stop --> ||||

Expand Down
3 changes: 0 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ function update_assets() {
}

function roll_driver() {
cat "${upstream_repo_path}/utils/build/run-driver-posix.sh" | sed "s|package|../../package|" > "src/Playwright/Scripts/playwright.sh"
cat "${upstream_repo_path}/utils/build/run-driver-win.cmd" | sed "s|package|..\\\..\\\package|" > "src/Playwright/Scripts/playwright.cmd"

new_driver_version="$1"
upstream_package_version=$(node -e "console.log(require('${upstream_repo_path}/package.json').version)")
echo "Rolling .NET driver to driver ${new_driver_version} and upstream version ${upstream_package_version}..."
Expand Down
3 changes: 2 additions & 1 deletion src/Playwright.Tests/BrowserTypeConnectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ internal RemoteServer(string browserName)
{
try
{
var startInfo = new ProcessStartInfo(Driver.GetExecutablePath(), $"launch-server --browser {browserName}")
var (executablePath, getArgs) = Driver.GetExecutablePath();
var startInfo = new ProcessStartInfo(executablePath, getArgs($"launch-server --browser {browserName}"))
{
UseShellExecute = false,
RedirectStandardOutput = true,
Expand Down
34 changes: 21 additions & 13 deletions src/Playwright/Helpers/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static class Driver
["PW_CLI_DISPLAY_VERSION"] = typeof(Driver).Assembly.GetName().Version.ToString(3),
};

internal static string GetExecutablePath()
internal static (string ExecutablePath, Func<string, string> GetArgs) GetExecutablePath()
{
DirectoryInfo assemblyDirectory = null;
if (!string.IsNullOrEmpty(AppContext.BaseDirectory))
Expand All @@ -64,60 +64,68 @@ internal static string GetExecutablePath()
}

string executableFile;
Func<string, string> getArgs;

// When loading the Assembly via the memory we don't have any references where the driver might be located.
// To workaround this we pass this env from the .ps1 wrapper over to the Assembly.
var driverSearchPath = Environment.GetEnvironmentVariable("PLAYWRIGHT_DRIVER_SEARCH_PATH");
if (!string.IsNullOrEmpty(driverSearchPath))
{
executableFile = GetPath(driverSearchPath);
(executableFile, getArgs) = GetPath(driverSearchPath);
if (!File.Exists(executableFile))
{
throw new PlaywrightException("Couldn't find driver in \"PLAYWRIGHT_DRIVER_SEARCH_PATH\"");
}
return executableFile;
return (executableFile, getArgs);
}

executableFile = GetPath(assemblyDirectory.FullName);
(executableFile, getArgs) = GetPath(assemblyDirectory.FullName);
if (File.Exists(executableFile))
{
return executableFile;
return (executableFile, getArgs);
}

// if the above fails, we can assume we're in the nuget registry
executableFile = GetPath(assemblyDirectory.Parent.Parent.FullName);
(executableFile, getArgs) = GetPath(assemblyDirectory.Parent.Parent.FullName);
if (File.Exists(executableFile))
{
return executableFile;
return (executableFile, getArgs);
}

throw new PlaywrightException($"Driver not found: {executableFile}");
}

private static string GetPath(string driversPath)
private static (string ExecutablePath, Func<string, string> GetArgs) GetPath(string driversPath)
{
string platformId;
string runnerName;
string nodeExecutable;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
platformId = "win32_x64";
runnerName = "playwright.cmd";
nodeExecutable = "node.exe";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
runnerName = "playwright.sh";
nodeExecutable = "node";
platformId = RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ? "darwin-arm64" : "darwin-x64";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
runnerName = "playwright.sh";
nodeExecutable = "node";
platformId = RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ? "linux-arm64" : "linux-x64";
}
else
{
throw new PlaywrightException("Unknown platform");
}

return Path.Combine(driversPath, ".playwright", "node", platformId, runnerName);
var cliEntrypoint = Path.Combine(driversPath, ".playwright", "package", "lib", "cli", "cli.js");
string getArgs(string args)
{
return !string.IsNullOrEmpty(args) ? $"\"{cliEntrypoint}\" {args}" : cliEntrypoint;
}
return (
Environment.GetEnvironmentVariable("PLAYWRIGHT_NODEJS_PATH") ?? Path.GetFullPath(Path.Combine(driversPath, ".playwright", "node", platformId, nodeExecutable)),
getArgs);
}
}
5 changes: 0 additions & 5 deletions src/Playwright/Playwright.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
<None Include=".drivers\mac\node" Link=".playwright\node\darwin-x64\node" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\darwin-x64" />
<None Include=".drivers\mac-arm64\node" Link=".playwright\node\darwin-arm64\node" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\darwin-arm64" />
<None Include=".drivers\win32_x64\node.exe" Link=".playwright\node\win32_x64\node.exe" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\win32_x64" />
<None Include="Scripts\playwright.sh" Link=".playwright\node\linux-x64\playwright.sh" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\linux-x64" />
<None Include="Scripts\playwright.sh" Link=".playwright\node\linux-arm64\playwright.sh" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\linux-arm64" />
<None Include="Scripts\playwright.sh" Link=".playwright\node\darwin-x64\playwright.sh" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\darwin-x64" />
<None Include="Scripts\playwright.sh" Link=".playwright\node\darwin-arm64\playwright.sh" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\darwin-arm64" />
<None Include="Scripts\playwright.cmd" Link=".playwright\node\win32_x64\playwright.cmd" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath=".playwright\node\win32_x64" />
<None Include="build\playwright.ps1" Link="playwright.ps1" CopyToOutputDirectory="PreserveNewest" />
<None Include="build\**" Pack="true" PackagePath="buildTransitive" />
<None Include="build\**" Pack="true" PackagePath="build" />
Expand Down
8 changes: 4 additions & 4 deletions src/Playwright/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ public static int Main(string[] args)

public int Run(string[] args)
{
string pwPath = null;
Func<string, string> getArgs;
string executablePath;
try
{
pwPath = Driver.GetExecutablePath();
(executablePath, getArgs) = Driver.GetExecutablePath();
}
catch
{
return PrintError("Microsoft.Playwright assembly was found, but is missing required assets. Please ensure to build your project before running Playwright tool.");
}

string allArgs = args != null && args.Length > 0 ? "\"" + string.Join("\" \"", args) + "\"" : string.Empty;
var playwrightStartInfo = new ProcessStartInfo(pwPath, allArgs)
var playwrightStartInfo = new ProcessStartInfo(executablePath, getArgs(args?.Length > 0 ? "\"" + string.Join("\" \"", args) + "\"" : null))
{
UseShellExecute = false,
// This works after net8.0-preview-4
Expand Down
4 changes: 0 additions & 4 deletions src/Playwright/Scripts/playwright.cmd

This file was deleted.

6 changes: 0 additions & 6 deletions src/Playwright/Scripts/playwright.sh

This file was deleted.

8 changes: 4 additions & 4 deletions src/Playwright/Transport/StdIOTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ internal class StdIOTransport : IDisposable

internal StdIOTransport()
{
_process = GetProcess();
_process.StartInfo.Arguments = "run-driver";
_process = GetProcess("run-driver");
StartProcessWithUTF8IOEncoding(_process);
_process.Exited += (_, _) => Close(new TargetClosedException("Process exited"));
_process.ErrorDataReceived += (_, error) =>
Expand Down Expand Up @@ -112,9 +111,10 @@ public async Task SendAsync(byte[] message)
}
}

private static Process GetProcess()
private static Process GetProcess(string driverArgs)
{
var startInfo = new ProcessStartInfo(Driver.GetExecutablePath())
var (executablePath, getArgs) = Driver.GetExecutablePath();
var startInfo = new ProcessStartInfo(executablePath, getArgs(driverArgs))
{
UseShellExecute = false,
RedirectStandardOutput = true,
Expand Down
6 changes: 3 additions & 3 deletions src/Playwright/build/Microsoft.Playwright.targets
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
</Content>
</ItemGroup>
</Target>
<Target Name="PlaywrightChmodExecutables" AfterTargets="CopyFilesToOutputDirectory;CopyFilesToPublishDirectory" Condition="!$([MSBuild]::IsOSPlatform('Windows'))">
<Target Name="PlaywrightChmodExecutables" AfterTargets="CopyFilesToOutputDirectory;CopyFilesToPublishDirectory" Condition="!$([MSBuild]::IsOSPlatform('Windows'))">
<ItemGroup>
<_PlaywrightChmodItems Include="$(OutputPath).playwright\node\*\playwright.sh" />
<_PlaywrightChmodItems Include="$(PublishDir).playwright\node\*\playwright.sh" />
<_PlaywrightChmodItems Include="$(OutputPath).playwright\node\*\node" />
<_PlaywrightChmodItems Include="$(PublishDir).playwright\node\*\node" />
</ItemGroup>
<Exec Command="chmod +x &quot;%(_PlaywrightChmodItems.FullPath)&quot;" />
</Target>
Expand Down
17 changes: 0 additions & 17 deletions src/tools/Playwright.Tooling/DriverDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using DriverDownloader.Linux;
using Playwright.Tooling.Options;

namespace Playwright.Tooling;
Expand Down Expand Up @@ -137,20 +134,6 @@ private async Task DownloadDriverAsync(DirectoryInfo destinationDirectory, strin

new ZipArchive(await response.Content.ReadAsStreamAsync().ConfigureAwait(false)).ExtractToDirectory(directory.FullName);

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var executables = new[] { "playwright.sh", "node" }
.Select(f => Path.Combine(directory.FullName, f));

foreach (string executable in executables)
{
if (new FileInfo(executable).Exists && LinuxSysCall.Chmod(executable, LinuxSysCall.ExecutableFilePermissions) != 0)
{
throw new($"Unable to chmod {executable} ({Marshal.GetLastWin32Error()})");
}
}
}

Console.WriteLine($"Driver for {platform} downloaded");
}
catch (Exception ex)
Expand Down
44 changes: 0 additions & 44 deletions src/tools/Playwright.Tooling/Linux/FileAccessPermissions.cs

This file was deleted.

43 changes: 0 additions & 43 deletions src/tools/Playwright.Tooling/Linux/LinuxSysCall.cs

This file was deleted.

0 comments on commit 9da82ec

Please sign in to comment.