Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ApiPort/ApiPort/ApiPort.Offline.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ProjectReference Include="..\..\lib\Microsoft.Fx.Portability.Offline\Microsoft.Fx.Portability.Offline.csproj" />
<ProjectReference Include="..\..\lib\Microsoft.Fx.Portability.Reports.DGML\Microsoft.Fx.Portability.Reports.DGML.csproj" />
<ProjectReference Include="..\..\lib\Microsoft.Fx.Portability.Reports.Excel\Microsoft.Fx.Portability.Reports.Excel.csproj" />
<ProjectReference Include="..\..\lib\Microsoft.Fx.Portability.Reports.Html\Microsoft.Fx.Portability.Reports.Html.csproj" Condition=" '$(TargetFramework)' == 'net461' " />
<ProjectReference Include="..\..\lib\Microsoft.Fx.Portability.Reports.Html\Microsoft.Fx.Portability.Reports.Html.csproj" Condition=" '$(TargetFramework)' != 'net461' " />
<ProjectReference Include="..\..\lib\Microsoft.Fx.Portability.Reports.Json\Microsoft.Fx.Portability.Reports.Json.csproj" />
</ItemGroup>

Expand Down
52 changes: 52 additions & 0 deletions src/lib/Microsoft.Fx.Portability.Reports.Html/HtmlHelper.cs
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>"));
}
}
}
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant AddMvc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

services.AddTransient<RazorViewToStringRenderer>();

return services.BuildServiceProvider();
}

public void Dispose()
{
_serviceProvider.Dispose();
}
}
}
151 changes: 0 additions & 151 deletions src/lib/Microsoft.Fx.Portability.Reports.Html/HtmlReportWriter.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>An HTML formatter for ApiPort reports</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommonMark.NET" Version="0.15.1" />
<PackageReference Include="RazorEngine" Version="4.5.1-alpha002" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand All @@ -17,11 +18,13 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\*.cshtml" />
<ProjectReference Include="..\Microsoft.Fx.Portability\Microsoft.Fx.Portability.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.Fx.Portability\Microsoft.Fx.Portability.csproj" />
<Content Update="Views\*.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
Loading