-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (109 loc) · 4.15 KB
/
Program.cs
File metadata and controls
125 lines (109 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Playwright;
using NuGet.Versioning;
using Spectre.Console;
if (args.Any(x => x == "--debug"))
Debugger.Launch();
var chromium = Chromium.Path;
var arch = RuntimeInformation.OSArchitecture switch
{
Architecture.X64 => "x64",
Architecture.X86 => "x86",
// we don't ship arm binaries for now.
_ => null
};
var runtime = arch == null ? RuntimeInformation.RuntimeIdentifier :
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"win-{arch}" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? $"linux-{arch}" :
// We don't ship osx binaries for now.
//RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"osx-{arch}" :
RuntimeInformation.RuntimeIdentifier;
if (chromium == null)
{
var packageDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".nuget", "packages", $"chromium.{runtime}");
chromium = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".nuget", "packages",
$"chromium.{runtime}",
NuGetVersion.Parse(ThisAssembly.Project.NativeVersion).ToNormalizedString(),
"runtimes",
runtime,
"native",
Environment.OSVersion.Platform == PlatformID.Win32NT ? "chrome.exe" : "chrome");
// Attempt to use dotnet restore to download it
if (!File.Exists(chromium))
await AnsiConsole.Status().StartAsync($"Restoring chromium.{runtime} v{ThisAssembly.Project.NativeVersion}...",
async ctx => await DotNetMuxer.TryRestore());
var versionDir = Directory.EnumerateDirectories(packageDir)
.Select(x => new DirectoryInfo(x))
.OrderByDescending(x => NuGetVersion.Parse(x.Name))
.FirstOrDefault();
if (versionDir != null)
{
// We may have gotten a newer or slightly older/newer version, be flexible in our case.
chromium = Path.Combine(
versionDir.FullName,
"runtimes", runtime, "native",
Environment.OSVersion.Platform == PlatformID.Win32NT ? "chrome.exe" : "chrome");
}
// If it still doesn't exist after an attempted restore, then we can't continue.
if (!File.Exists(chromium))
{
AnsiConsole.MarkupLine($"[red]Current runtime {RuntimeInformation.RuntimeIdentifier} is not supported for v{NuGetVersion.Parse(ThisAssembly.Project.NativeVersion).ToNormalizedString()}.[/]");
if (DependencyContext.Default != null && args.Any(x => x == "--debug"))
{
AnsiConsole.MarkupLine($"[yellow]Compatible runtimes:[/]");
foreach (var rg in DependencyContext.Default.RuntimeGraph)
{
AnsiConsole.MarkupLine($"[dim]- {rg.Runtime}[/]");
}
}
return -1;
}
}
#if DEBUG
// format as ansiconsole link
AnsiConsole.MarkupLine($"Located compatible [lime][link={chromium}]{RuntimeInformation.RuntimeIdentifier} Chromium[/][/]");
#endif
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Args = args.Where(x => x.StartsWith("--") && x != "--debug"),
ExecutablePath = chromium,
Headless = args.Contains("--headless"),
});
var arg = args.FirstOrDefault(x => !x.StartsWith("--"));
var page = await browser.NewPageAsync();
// NOTE: showcases how to interact with the browser as it's navigating
page.FrameNavigated += async (sender, args) =>
{
try
{
await page.WaitForLoadStateAsync(LoadState.NetworkIdle, new PageWaitForLoadStateOptions
{
Timeout = 0,
});
// 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)
{
await page.GotoAsync(arg, new PageGotoOptions
{
Timeout = 0,
WaitUntil = WaitUntilState.NetworkIdle
});
var html = await page.InnerTextAsync("body");
AnsiConsole.MarkupLine($"[dim]{html}[/]");
}
return 0;