diff --git a/lib/PuppeteerSharp/BrowserData/Cache.cs b/lib/PuppeteerSharp/BrowserData/Cache.cs index 8324379d3..f168e7b64 100644 --- a/lib/PuppeteerSharp/BrowserData/Cache.cs +++ b/lib/PuppeteerSharp/BrowserData/Cache.cs @@ -9,7 +9,7 @@ internal class Cache { private readonly string _rootDir; - public Cache() => _rootDir = BrowserFetcher.GetExecutablePath(); + public Cache() => _rootDir = BrowserFetcher.GetBrowsersLocation(); public Cache(string rootDir) => _rootDir = rootDir; @@ -27,8 +27,8 @@ public IEnumerable GetInstalledBrowsers() return Array.Empty(); } - var browerNames = Enum.GetNames(typeof(SupportedBrowser)).Select(browser => browser.ToUpperInvariant()); - var browsers = rootInfo.GetDirectories().Where(browser => browerNames.Contains(browser.Name.ToUpperInvariant())); + var browserNames = Enum.GetNames(typeof(SupportedBrowser)).Select(browser => browser.ToUpperInvariant()); + var browsers = rootInfo.GetDirectories().Where(browser => browserNames.Contains(browser.Name.ToUpperInvariant())); return browsers.SelectMany(browser => { diff --git a/lib/PuppeteerSharp/BrowserFetcher.cs b/lib/PuppeteerSharp/BrowserFetcher.cs index 18ab4aac5..310fdc715 100644 --- a/lib/PuppeteerSharp/BrowserFetcher.cs +++ b/lib/PuppeteerSharp/BrowserFetcher.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; @@ -35,7 +34,7 @@ public class BrowserFetcher : IBrowserFetcher /// public BrowserFetcher() { - CacheDir = GetExecutablePath(); + CacheDir = GetBrowsersLocation(); Platform = GetCurrentPlatform(); Browser = SupportedBrowser.Chrome; _customFileDownload = _webClient.DownloadFileTaskAsync; @@ -55,7 +54,7 @@ public BrowserFetcher(BrowserFetcherOptions options) } Browser = options.Browser; - CacheDir = string.IsNullOrEmpty(options.Path) ? GetExecutablePath() : options.Path; + CacheDir = string.IsNullOrEmpty(options.Path) ? GetBrowsersLocation() : options.Path; Platform = options.Platform ?? GetCurrentPlatform(); _customFileDownload = options.CustomFileDownload ?? _webClient.DownloadFileTaskAsync; } @@ -92,10 +91,8 @@ public async Task CanDownloadAsync(string revision) var client = WebRequest.Create(url); client.Proxy = _webClient.Proxy; client.Method = "HEAD"; - using (var response = (HttpWebResponse)await client.GetResponseAsync().ConfigureAwait(false)) - { - return response.StatusCode == HttpStatusCode.OK; - } + using var response = (HttpWebResponse)await client.GetResponseAsync().ConfigureAwait(false); + return response.StatusCode == HttpStatusCode.OK; } catch (WebException) { @@ -170,6 +167,14 @@ public async Task DownloadAsync(string buildId) return new InstalledBrowser(cache, Browser, buildId, Platform); } + /// + public string GetExecutablePath(string buildId) + => new InstalledBrowser( + new Cache(CacheDir), + Browser, + buildId, + Platform).GetExecutablePath(); + /// public void Dispose() { @@ -197,7 +202,7 @@ internal static Platform GetCurrentPlatform() return Platform.Unknown; } - internal static string GetExecutablePath() + internal static string GetBrowsersLocation() { var assembly = typeof(Puppeteer).Assembly; var assemblyName = assembly.GetName().Name + ".dll"; @@ -276,11 +281,8 @@ private Task InstallDMGAsync(string dmgPath, string folderPath) var mountAndCopyTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using var process = new Process - { - EnableRaisingEvents = true, - }; - + using var process = new Process(); + process.EnableRaisingEvents = true; process.StartInfo.FileName = "hdiutil"; process.StartInfo.Arguments = $"attach -nobrowse -noautoopen \"{dmgPath}\""; process.StartInfo.RedirectStandardOutput = true; diff --git a/lib/PuppeteerSharp/IBrowserFetcher.cs b/lib/PuppeteerSharp/IBrowserFetcher.cs index 5762eb346..8f8823fed 100644 --- a/lib/PuppeteerSharp/IBrowserFetcher.cs +++ b/lib/PuppeteerSharp/IBrowserFetcher.cs @@ -8,13 +8,12 @@ namespace PuppeteerSharp { /// /// BrowserFetcher can download and manage different versions of Chromium. - /// BrowserFetcher operates on revision strings that specify a precise version of Chromium, e.g. 533271. Revision strings can be obtained from omahaproxy.appspot.com. /// /// /// Example on how to use BrowserFetcher to download a specific version of Chromium and run Puppeteer against it: /// /// var browserFetcher = Puppeteer.CreateBrowserFetcher(); - /// var revisionInfo = await browserFetcher.DownloadAsync("533271"); + /// var revisionInfo = await browserFetcher.DownloadAsync(BrowserData.Chrome.DefaultBuildId); /// var browser = await await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = revisionInfo.ExecutablePath}); /// /// @@ -88,5 +87,12 @@ public interface IBrowserFetcher : IDisposable /// /// Browser to remove. void Uninstall(string buildId); + + /// + /// Gets the executable path. + /// + /// Browser buildId. + /// The executable path. + string GetExecutablePath(string buildId); } } diff --git a/lib/PuppeteerSharp/PuppeteerSharp.csproj b/lib/PuppeteerSharp/PuppeteerSharp.csproj index 3bc282c62..ea482084c 100644 --- a/lib/PuppeteerSharp/PuppeteerSharp.csproj +++ b/lib/PuppeteerSharp/PuppeteerSharp.csproj @@ -12,10 +12,10 @@ Headless Browser .NET API PuppeteerSharp - 13.0.0 - 13.0.0 - 13.0.0 - 13.0.0 + 13.0.1 + 13.0.1 + 13.0.1 + 13.0.1 false false embedded