This repository was archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
Move HTML report generator to a razor library #742
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
src/lib/Microsoft.Fx.Portability.Reports.Html/HtmlHelper.cs
This file contains hidden or 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,52 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.AspNetCore.Html; | ||
| using Microsoft.AspNetCore.Mvc.Rendering; | ||
| using Microsoft.Fx.Portability.Reports.Html; | ||
| using Microsoft.Fx.Portability.Reports.Html.Resources; | ||
|
|
||
| using static System.FormattableString; | ||
|
|
||
| namespace Microsoft.Fx.Portability.Reports | ||
| { | ||
| public static class HtmlHelper | ||
| { | ||
| public static IHtmlContent ConvertMarkdownToHtml<T>(this IHtmlHelper<T> helper, string markdown) | ||
| { | ||
| return helper.Raw(CommonMark.CommonMarkConverter.Convert(markdown)); | ||
| } | ||
|
|
||
| public static IHtmlContent Raw<T>(this IHtmlHelper<T> helper, string rawString) | ||
| { | ||
| return new HtmlString(rawString); | ||
| } | ||
|
|
||
| public static IHtmlContent TargetSupportCell<T>(this IHtmlHelper<T> helper, TargetSupportedIn supportStatus) | ||
| { | ||
| var supported = supportStatus.SupportedIn != null | ||
| && supportStatus.Target.Version >= supportStatus.SupportedIn; | ||
|
|
||
| var imageId = supported ? "icon-supported" : "icon-unsupported"; | ||
| var title = supported ? LocalizedStrings.Supported : LocalizedStrings.NotSupported; | ||
| var icon = $"<svg class=\"support-icon\"><use href=#{imageId}></use></svg>"; | ||
|
|
||
| return helper.Raw(Invariant($"<td class=\"textCentered\" title=\"{title}\">{icon}</td>")); | ||
| } | ||
|
|
||
| public static IHtmlContent BreakingChangeCountCell<T>(this IHtmlHelper<T> helper, int breaks, int warningThreshold, int errorThreshold) | ||
| { | ||
| var className = string.Empty; | ||
| if (breaks <= warningThreshold) | ||
| { | ||
| className = "NoBreakingChanges"; | ||
| } | ||
| else | ||
| { | ||
| className = breaks <= errorThreshold ? "FewBreakingChanges" : "ManyBreakingChanges"; | ||
| } | ||
|
|
||
| return helper.Raw(Invariant($"<td class=\"textCentered {className}\">{breaks}</td>")); | ||
| } | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
src/lib/Microsoft.Fx.Portability.Reports.Html/HtmlRazorReportWriter.cs
This file contains hidden or 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,84 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Hosting.Internal; | ||
| using Microsoft.AspNetCore.Mvc.Razor; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.FileProviders; | ||
| using Microsoft.Extensions.ObjectPool; | ||
| using Microsoft.Fx.Portability.ObjectModel; | ||
| using Microsoft.Fx.Portability.Reporting; | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Reflection; | ||
|
|
||
| namespace Microsoft.Fx.Portability.Reports | ||
| { | ||
| public sealed class HtmlRazorReportWriter : IReportWriter, IDisposable | ||
| { | ||
| private static readonly ResultFormatInformation _formatInformation = new ResultFormatInformation | ||
| { | ||
| DisplayName = "HTML", | ||
| MimeType = "text/html", | ||
| FileExtension = ".html" | ||
| }; | ||
|
|
||
| private readonly ITargetMapper _targetMapper; | ||
| private readonly ServiceProvider _serviceProvider; | ||
| private readonly IServiceScopeFactory _factory; | ||
|
|
||
| public HtmlRazorReportWriter(ITargetMapper targetMapper) | ||
| { | ||
| _targetMapper = targetMapper; | ||
| _serviceProvider = BuildProvider(); | ||
| _factory = _serviceProvider.GetRequiredService<IServiceScopeFactory>(); | ||
| } | ||
|
|
||
| public ResultFormatInformation Format => _formatInformation; | ||
|
|
||
| public void WriteStream(Stream stream, AnalyzeResponse response) | ||
| { | ||
| using (var scope = _factory.CreateScope()) | ||
| { | ||
| var model = new RazorHtmlObject(response, _targetMapper); | ||
| var helper = scope.ServiceProvider.GetRequiredService<RazorViewToStringRenderer>(); | ||
|
|
||
| helper.RenderViewAsync("Views/ReportTemplate.cshtml", model, stream).GetAwaiter().GetResult(); | ||
| } | ||
| } | ||
|
|
||
| private static ServiceProvider BuildProvider() | ||
| { | ||
| var services = new ServiceCollection(); | ||
| var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); | ||
|
|
||
| services.AddSingleton<IHostingEnvironment>(new HostingEnvironment | ||
| { | ||
| ApplicationName = Assembly.GetEntryAssembly().GetName().Name, | ||
| WebRootFileProvider = fileProvider | ||
| }); | ||
|
|
||
| services.Configure<RazorViewEngineOptions>(options => | ||
| { | ||
| options.FileProviders.Clear(); | ||
| options.FileProviders.Add(fileProvider); | ||
| }); | ||
|
|
||
| services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>(); | ||
| services.AddSingleton<DiagnosticSource>(new DiagnosticListener("Microsoft.AspNetCore")); | ||
| services.AddLogging(); | ||
| services.AddMvc(); | ||
| services.AddMvc(); | ||
| services.AddTransient<RazorViewToStringRenderer>(); | ||
|
|
||
| return services.BuildServiceProvider(); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| _serviceProvider.Dispose(); | ||
| } | ||
| } | ||
| } | ||
151 changes: 0 additions & 151 deletions
151
src/lib/Microsoft.Fx.Portability.Reports.Html/HtmlReportWriter.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant
AddMvcThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed