Skip to content

Commit

Permalink
Fixup native chrome lookup for installed tool scenario
Browse files Browse the repository at this point in the history
It is unfortunately not possible to conditionally reference native assets depending on the installed tool's runtime platform. This means we need to assume the dependency will be restored at the project level and look it up via the project.assets.json, rather than the runtime deps.

See
- How a tool project is installed/restored: https://github.com/dotnet/sdk/blob/main/src/Cli/dotnet/ToolPackage/ToolPackageInstaller.cs
- Native files not being automatically added to the runtime deps.json: dotnet/sdk#11373
- How to do that via custom targets, but this wouldn't be possible on the target machine when restoring the temp project created for global tools: https://github.com/ericstj/sample-code/blob/nativeLibSample/addNative/addNative.csproj
- How runtime.json might work but it's going away: dotnet/runtime#11404
- Really going away: dotnet/runtime#49137
- How it's still not solved for .net6: NuGet/Home#5862
  • Loading branch information
kzu committed Jun 3, 2022
1 parent fd2b77b commit 39df0de
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ jobs:
run: |
mkdir obj
curl https://download-chromium.appspot.com/dl/Win_x64?type=snapshots -L -o obj/chrome-win.zip
unzip obj/chrome-win.zip -d src/chromium-win-x64
unzip obj/chrome-win.zip -d src/chromium.win-x64
curl https://download-chromium.appspot.com/dl/Linux_x64?type=snapshots -L -o obj/chrome-linux.zip
unzip obj/chrome-linux.zip -d src/chromium-linux-x64
unzip obj/chrome-linux.zip -d src/chromium.linux-x64
- name: 🙏 build
run: dotnet build -m:1 -p:VersionLabel="$GITHUB_REF.$GITHUB_RUN_NUMBER"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ jobs:
run: |
mkdir obj
curl https://download-chromium.appspot.com/dl/Win_x64?type=snapshots -L -o obj/chrome-win.zip
unzip obj/chrome-win.zip -d src/chromium-win-x64
unzip obj/chrome-win.zip -d src/chromium.win-x64
curl https://download-chromium.appspot.com/dl/Linux_x64?type=snapshots -L -o obj/chrome-linux.zip
unzip obj/chrome-linux.zip -d src/chromium-linux-x64
unzip obj/chrome-linux.zip -d src/chromium.linux-x64
- name: 🙏 build
run: dotnet build -m:1 -p:version=${GITHUB_REF#refs/*/v}
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
src/chromium-linux-x64/chrome-linux
src/chromium-win-x64/chrome-win
src/chromium.linux-x64/chrome-linux
src/chromium.win-x64/chrome-win

bin
obj
Expand Down
16 changes: 8 additions & 8 deletions Chromium.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
readme.md = readme.md
EndProjectSection
EndProject
Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "chromium.linux-x64", "src\chromium-linux-x64\chromium.linux-x64.msbuildproj", "{64F0CBDB-18EE-4F83-B1AF-8BB49C524278}"
Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "chromium.linux-x64", "src\chromium.linux-x64\chromium.linux-x64.msbuildproj", "{64F0CBDB-18EE-4F83-B1AF-8BB49C524278}"
EndProject
Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "chromium.win-x64", "src\chromium-win-x64\chromium.win-x64.msbuildproj", "{B9C4882E-0A2D-4A49-B21E-625612313312}"
Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "chromium.win-x64", "src\chromium.win-x64\chromium.win-x64.msbuildproj", "{B9C4882E-0A2D-4A49-B21E-625612313312}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-chromium", "src\dotnet-chromium\dotnet-chromium.csproj", "{30A4C096-8FFB-467A-9411-A9049953E850}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "chromium", "src\chromium\chromium.csproj", "{B6479D22-0EC5-4B50-92FA-8A865F68B4D9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{97FAB98D-2FFC-41E4-B573-B8B9B47209F9}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build.yml = .github\workflows\build.yml
.github\workflows\publish.yml = .github\workflows\publish.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "chromium", "src\chromium\chromium.csproj", "{361F5BDA-1D20-4225-86DA-D23E98040882}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -48,10 +48,10 @@ Global
{30A4C096-8FFB-467A-9411-A9049953E850}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30A4C096-8FFB-467A-9411-A9049953E850}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30A4C096-8FFB-467A-9411-A9049953E850}.Release|Any CPU.Build.0 = Release|Any CPU
{B6479D22-0EC5-4B50-92FA-8A865F68B4D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6479D22-0EC5-4B50-92FA-8A865F68B4D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6479D22-0EC5-4B50-92FA-8A865F68B4D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6479D22-0EC5-4B50-92FA-8A865F68B4D9}.Release|Any CPU.Build.0 = Release|Any CPU
{361F5BDA-1D20-4225-86DA-D23E98040882}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{361F5BDA-1D20-4225-86DA-D23E98040882}.Debug|Any CPU.Build.0 = Debug|Any CPU
{361F5BDA-1D20-4225-86DA-D23E98040882}.Release|Any CPU.ActiveCfg = Release|Any CPU
{361F5BDA-1D20-4225-86DA-D23E98040882}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageVersion Include="NuGetizer" Version="0.8.0-beta" />
<PackageVersion Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
<PackageVersion Include="Microsoft.Playwright" Version="1.22.0" />
<PackageVersion Include="NuGet.ProjectModel" Version="6.2.0" />
</ItemGroup>

</Project>
55 changes: 30 additions & 25 deletions src/chromium/Chromium.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyModel;
using NuGet.ProjectModel;

/// <summary>
/// Provides access to the Chromium <see cref="Path"/> location,
Expand All @@ -12,6 +13,9 @@ static Chromium()
{
// Locate proper runtime binaries
var chromeDir = default(string);
var chromeFile = "chrome";
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
chromeFile += ".exe";

foreach (var runtime in DependencyContext.Default.RuntimeGraph)
{
Expand All @@ -21,35 +25,36 @@ static Chromium()
chromeDir = candidate;
}

if (chromeDir == null)
if (chromeDir != null)
{
// In the installed tool scenario, we need to go up to the tool project restore root
// since it migtht be a trimmed tool package to avoid going over the nuget.org limit.
// Just like we do in dotnet-chromium.
var rootDir = AppContext.BaseDirectory;
while (rootDir != null && !File.Exists(System.IO.Path.Combine(rootDir, "project.assets.json")))
rootDir = new DirectoryInfo(rootDir).Parent?.FullName;

if (rootDir != null && File.Exists(System.IO.Path.Combine(rootDir, "project.assets.json")))
{
// Search again but starting from each runtime dependency path, where the runtime matches the
// current dependency grap
var dirs = from runtime in DependencyContext.Default.RuntimeGraph
from lib in DependencyContext.Default.RuntimeLibraries
where lib.NativeLibraryGroups.Any(g => g.Runtime == runtime.Runtime)
let candidate = System.IO.Path.Combine(rootDir, lib.Path, "runtimes", runtime.Runtime, "native")
where Directory.Exists(candidate)
select candidate;

chromeDir = dirs.FirstOrDefault();
}
Path = System.IO.Path.Combine(chromeDir, chromeFile);
return;
}

if (chromeDir != null)
// In the installed tool scenario, we need to go up to the tool project restore root
// since it migtht be a trimmed tool package to avoid going over the nuget.org limit.
// Just like we do in dotnet-chromium.
var rootDir = AppContext.BaseDirectory;
while (rootDir != null && !File.Exists(System.IO.Path.Combine(rootDir, "project.assets.json")))
rootDir = new DirectoryInfo(rootDir).Parent?.FullName;

if (rootDir != null && File.Exists(System.IO.Path.Combine(rootDir, "project.assets.json")))
{
Path = System.IO.Path.Combine(chromeDir, "chrome");
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
Path += ".exe";
var lockFile = new LockFileFormat().Read(System.IO.Path.Combine(rootDir, "project.assets.json"));

// Search again but starting from each runtime dependency path, where the runtime matches the
// current dependency grap
var nativeFiles = from runtime in DependencyContext.Default.RuntimeGraph
from target in lockFile.Targets
from lib in target.Libraries
from native in lib.RuntimeTargets
where native.Runtime == runtime.Runtime &&
System.IO.Path.GetFileName(native.Path) == chromeFile
let file = new FileInfo(System.IO.Path.Combine(rootDir, lib.Name, lib.Version.ToString(), native.Path))
where file.Exists
select file.FullName;

Path = nativeFiles.FirstOrDefault();
}
}
}
7 changes: 5 additions & 2 deletions src/chromium/chromium.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
<TargetFramework>net6.0</TargetFramework>
<PackageId>chromium</PackageId>
<Description>Allows referencing a portable version of Chromium for Windows and Linux and locating it for launching.</Description>
<PackBuildOutput>false</PackBuildOutput>
<PackCompile>true</PackCompile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NuGetizer" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" />
<PackageReference Include="NuGet.ProjectModel" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\chromium-linux-x64\chromium.linux-x64.msbuildproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\chromium-win-x64\chromium.win-x64.msbuildproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\chromium.linux-x64\chromium.linux-x64.msbuildproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\chromium.win-x64\chromium.win-x64.msbuildproj" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/chromium/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Usage

```csharp
if (Chromium.Path == null)
{
Console.WriteLine($"Current runtime {System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier} is not supported.");
return;
}

Console.WriteLine($"Located compatible Chromium at {Chromium.Path}");
```
30 changes: 21 additions & 9 deletions src/dotnet-chromium/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Microsoft.Playwright;
using System.Diagnostics;
using Microsoft.Playwright;

if (args.Any(x => x == "--debug"))
Debugger.Launch();

if (Chromium.Path == null)
{
Expand All @@ -11,7 +15,7 @@
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Args = args.Where(x => x.StartsWith("--")),
Args = args.Where(x => x.StartsWith("--") && x != "--debug"),
ExecutablePath = Chromium.Path,
Headless = args.Contains("--headless"),
});
Expand All @@ -22,14 +26,22 @@
// NOTE: showcases how to interact with the browser as it's navigating
page.FrameNavigated += async (sender, args) =>
{
await page.WaitForNavigationAsync(new PageWaitForNavigationOptions
try
{
Timeout = 0,
WaitUntil = WaitUntilState.NetworkIdle
});

// Example of how to get the page body HTML
Console.WriteLine(await page.InnerHTMLAsync("body"));
await page.WaitForNavigationAsync(new PageWaitForNavigationOptions
{
Timeout = 0,
WaitUntil = WaitUntilState.NetworkIdle
});

// Example of how to get the page body HTML
Console.WriteLine(await page.InnerHTMLAsync("body"));
}
catch (PlaywrightException)
{
// Will be thrown when the app is shutting down, so ignore
return;
}
};

if (arg != null)
Expand Down
11 changes: 11 additions & 0 deletions src/dotnet-chromium/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Usage

```
> chromium [url] [switches]
```

Example:

```
> chromium https://clarius.org --headless
```

0 comments on commit 39df0de

Please sign in to comment.