diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fb7c726..4bbf402 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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" diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 72f7d20..6ed0166 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -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} diff --git a/.gitignore b/.gitignore index d9ec34e..bbf0ad2 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Chromium.sln b/Chromium.sln index a0f2f68..34b2ccc 100644 --- a/Chromium.sln +++ b/Chromium.sln @@ -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 @@ -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 diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 7131be6..8ce1973 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -4,6 +4,7 @@ + \ No newline at end of file diff --git a/src/chromium-linux-x64/chromium.linux-x64.msbuildproj b/src/chromium.linux-x64/chromium.linux-x64.msbuildproj similarity index 100% rename from src/chromium-linux-x64/chromium.linux-x64.msbuildproj rename to src/chromium.linux-x64/chromium.linux-x64.msbuildproj diff --git a/src/chromium-win-x64/chromium.win-x64.msbuildproj b/src/chromium.win-x64/chromium.win-x64.msbuildproj similarity index 100% rename from src/chromium-win-x64/chromium.win-x64.msbuildproj rename to src/chromium.win-x64/chromium.win-x64.msbuildproj diff --git a/src/chromium/Chromium.cs b/src/chromium/Chromium.cs index 8c0a04e..cea8502 100644 --- a/src/chromium/Chromium.cs +++ b/src/chromium/Chromium.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyModel; +using NuGet.ProjectModel; /// /// Provides access to the Chromium location, @@ -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) { @@ -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(); } } } \ No newline at end of file diff --git a/src/chromium/chromium.csproj b/src/chromium/chromium.csproj index 4932022..24b1c38 100644 --- a/src/chromium/chromium.csproj +++ b/src/chromium/chromium.csproj @@ -4,16 +4,19 @@ net6.0 chromium Allows referencing a portable version of Chromium for Windows and Linux and locating it for launching. + false + true + - - + + diff --git a/src/chromium/readme.md b/src/chromium/readme.md new file mode 100644 index 0000000..602d70b --- /dev/null +++ b/src/chromium/readme.md @@ -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}"); +``` \ No newline at end of file diff --git a/src/dotnet-chromium/Program.cs b/src/dotnet-chromium/Program.cs index 3782ffe..bed0bfd 100644 --- a/src/dotnet-chromium/Program.cs +++ b/src/dotnet-chromium/Program.cs @@ -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) { @@ -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"), }); @@ -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) diff --git a/src/dotnet-chromium/readme.md b/src/dotnet-chromium/readme.md new file mode 100644 index 0000000..e34bd33 --- /dev/null +++ b/src/dotnet-chromium/readme.md @@ -0,0 +1,11 @@ +## Usage + +``` +> chromium [url] [switches] +``` + +Example: + +``` +> chromium https://clarius.org --headless +``` \ No newline at end of file