-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: mermaid helpers for installation and doing file based genera…
…tion (#1814) * add a default factory for the playwright mermaid renderer * add file handler overload for getdiagram * add getdiagram text reader overload * Create InstallationHelper.cs * Create InstallationHelperTests.cs * logic to write diagram to file * more unit tests * Update PlaywrightRendererTests.cs * Update PlaywrightRendererTests.cs * add PlaywrightBrowserTypeAndChannel combination model * refactor to use PlaywrightBrowserTypeAndChannel arg
- Loading branch information
Showing
9 changed files
with
607 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Whipstaff.Mermaid/Playwright/GetDiagramResponseModelExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.IO.Abstractions; | ||
using System.Threading.Tasks; | ||
|
||
namespace Whipstaff.Mermaid.Playwright | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="GetDiagramResponseModel"/>. | ||
/// </summary> | ||
public static class GetDiagramResponseModelExtensions | ||
{ | ||
/// <summary> | ||
/// Write the diagram to a file. | ||
/// </summary> | ||
/// <param name="diagramResponseModel">Diagram response model to save.</param> | ||
/// <param name="targetFile">Target file to write to.</param> | ||
/// <returns>A task representing the asynchronous operation.</returns> | ||
public static Task ToFileAsync( | ||
this GetDiagramResponseModel diagramResponseModel, | ||
IFileInfo targetFile) | ||
{ | ||
ArgumentNullException.ThrowIfNull(diagramResponseModel); | ||
ArgumentNullException.ThrowIfNull(targetFile); | ||
if (targetFile.Exists) | ||
{ | ||
throw new ArgumentException("Target file already exists", nameof(targetFile)); | ||
} | ||
|
||
return diagramResponseModel.InternalToFileAsync( | ||
targetFile); | ||
} | ||
|
||
internal static async Task InternalToFileAsync( | ||
this GetDiagramResponseModel diagramResponseModel, | ||
IFileInfo targetFile) | ||
{ | ||
using (var textWriter = targetFile.CreateText()) | ||
{ | ||
await textWriter.WriteAsync(diagramResponseModel.Svg) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Microsoft.Playwright; | ||
|
||
namespace Whipstaff.Playwright | ||
{ | ||
/// <summary> | ||
/// Helper class for installing Playwright browser. The out-of-the-box experience is a bit clunky. | ||
/// | ||
/// This attempts to make it a bit easier. By allowing a first run execution to install the browser. | ||
/// | ||
/// See: | ||
/// https://github.com/microsoft/playwright-dotnet/issues/2239 | ||
/// https://github.com/microsoft/playwright-dotnet/issues/2286 | ||
/// | ||
/// This also removes the need to have msbuild run the task which has been causing CodeQL failures. | ||
/// </summary> | ||
public sealed class InstallationHelper | ||
{ | ||
private readonly Lazy<int> _lazyInstaller; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="InstallationHelper"/> class. | ||
/// </summary> | ||
/// <param name="playwrightBrowserType">The browser to use as part of the process.</param> | ||
public InstallationHelper(PlaywrightBrowserType playwrightBrowserType) | ||
{ | ||
var args = playwrightBrowserType switch | ||
{ | ||
PlaywrightBrowserType.None => new[] { "install" }, | ||
_ => new[] { "install", playwrightBrowserType.ToString().ToLowerInvariant() }, | ||
}; | ||
|
||
_lazyInstaller = new(() => Program.Main(args)); | ||
} | ||
|
||
/// <summary> | ||
/// Wrapper to carry out an installation a single time. | ||
/// </summary> | ||
/// <returns>result code from Playwright install.</returns> | ||
public int InstallPlaywright() | ||
{ | ||
return _lazyInstaller.Value; | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/Whipstaff.Playwright/PlaywrightBrowserTypeAndChannel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using Whipstaff.Runtime.Extensions; | ||
|
||
namespace Whipstaff.Playwright | ||
{ | ||
/// <summary> | ||
/// Represents a Playwright browser type and channel combination. | ||
/// </summary> | ||
public sealed class PlaywrightBrowserTypeAndChannel | ||
{ | ||
internal PlaywrightBrowserTypeAndChannel(PlaywrightBrowserType playwrightBrowserType, string? channel) | ||
{ | ||
PlaywrightBrowserType = playwrightBrowserType; | ||
Channel = channel; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Playwright browser type. | ||
/// </summary> | ||
public PlaywrightBrowserType PlaywrightBrowserType { get; } | ||
|
||
/// <summary> | ||
/// Gets the browser channel, if any. | ||
/// </summary> | ||
public string? Channel { get; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the default Playwright Chromium browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel ChromiumDefault() => new(PlaywrightBrowserType.Chromium, null); | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the Google Chrome Beta browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel Chrome() => new(PlaywrightBrowserType.Chromium, "chrome"); | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the Google Chrome browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel ChromeBeta() => new(PlaywrightBrowserType.Chromium, "chrome-beta"); | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for a custom Chromium browser. | ||
/// </summary> | ||
/// <param name="channel">The custom channel to use.</param> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel ChromiumCustom(string channel) | ||
{ | ||
channel.ThrowIfNullOrWhitespace(); | ||
return new PlaywrightBrowserTypeAndChannel(PlaywrightBrowserType.Chromium, channel); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the default Microsoft Edge browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel MsEdge() => new(PlaywrightBrowserType.Chromium, "msedge"); | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the Microsoft Edge Beta browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel MsEdgeBeta() => new(PlaywrightBrowserType.Chromium, "msedge-beta"); | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the Microsoft Edge Dev browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel MsEdgeDev() => new(PlaywrightBrowserType.Chromium, "msedge-dev"); | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the default Playwright Firefox browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel Firefox() => new(PlaywrightBrowserType.Firefox, null); | ||
|
||
/// <summary> | ||
/// Creates a new instance of the PlaywrightBrowserTypeAndChannel class for the default Playwright Webkit browser. | ||
/// </summary> | ||
/// <returns><see cref="PlaywrightBrowserTypeAndChannel"/> instance.</returns> | ||
public static PlaywrightBrowserTypeAndChannel Webkit() => new(PlaywrightBrowserType.WebKit, null); | ||
} | ||
} |
Oops, something went wrong.