Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Change IReportWriter.WriteStream to be async #743

Merged
merged 1 commit into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task<ServiceResponse<IEnumerable<ReportingResultWithFormat>>> SendA
{
using (var ms = new MemoryStream())
{
writer.WriteStream(ms, response);
await writer.WriteStreamAsync(ms, response);

result.Add(new ReportingResultWithFormat
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text;
using System.Xml.Linq;
using System.Threading.Tasks;

namespace Microsoft.Fx.Portability.Reports.DGML
{
Expand All @@ -26,7 +24,7 @@ public class DGMLOutputWriter : IReportWriter

private readonly DGMLManager dgml = new DGMLManager();

public void WriteStream(Stream stream, AnalyzeResponse response)
public Task WriteStreamAsync(Stream stream, AnalyzeResponse response)
{
ReferenceGraph rg = ReferenceGraph.CreateGraph(response);

Expand Down Expand Up @@ -95,7 +93,7 @@ public void WriteStream(Stream stream, AnalyzeResponse response)

dgml.Save(stream);

return;
return Task.CompletedTask;
}

private static string GenerateMissingTypes(string assembly, ReportingResult response, int i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.Fx.Portability.Reports
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public ExcelOpenXmlOutputWriter(
_catalogLastUpdated = catalogLastUpdated;
}

public void WriteTo(Stream outputStream)
public async Task WriteToAsync(Stream outputStream)
{
// Writing directly to the stream can cause problems if it is a BufferedStream (as seen when writing a multipart response)
// This will write the spreadsheet to a temporary stream, and then copy it to the expected stream afterward
Expand Down Expand Up @@ -91,7 +92,7 @@ public void WriteTo(Stream outputStream)
}

ms.Position = 0;
ms.CopyTo(outputStream);
await ms.CopyToAsync(outputStream);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
using Microsoft.Fx.Portability.ObjectModel;
using Microsoft.Fx.Portability.Reporting;
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.Fx.Portability.Reports
{
public class ExcelReportWriter : IReportWriter
{
private readonly ResultFormatInformation _formatInformation;
private readonly ITargetMapper _targetMapper;

public ExcelReportWriter(ITargetMapper targetMapper)
{
_targetMapper = targetMapper;

_formatInformation = new ResultFormatInformation
Format = new ResultFormatInformation
{
DisplayName = "Excel",
MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
FileExtension = ".xlsx"
};
}

public ResultFormatInformation Format { get { return _formatInformation; } }
public ResultFormatInformation Format { get; }

public void WriteStream(Stream stream, AnalyzeResponse response)
public Task WriteStreamAsync(Stream stream, AnalyzeResponse response)
{
var excelWriter = new ExcelOpenXmlOutputWriter(_targetMapper, response.ReportingResult, response.BreakingChanges, response.CatalogLastUpdated, description: null);

excelWriter.WriteTo(stream);
return excelWriter.WriteToAsync(stream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using static System.FormattableString;

Expand All @@ -36,7 +37,7 @@ public HtmlReportWriter(ITargetMapper targetMapper)

public ResultFormatInformation Format => _formatInformation;

public void WriteStream(Stream stream, AnalyzeResponse response)
public Task WriteStreamAsync(Stream stream, AnalyzeResponse response)
{
const string ReportTemplateName = "ReportTemplate";

Expand All @@ -48,6 +49,8 @@ public void WriteStream(Stream stream, AnalyzeResponse response)

writer.Write(razor);
}

return Task.CompletedTask;
}

private static IRazorEngineService CreateService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,25 @@
using Microsoft.Fx.Portability.Reporting;
using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.Fx.Portability.Reports
{
public class JsonReportWriter : IReportWriter
{
private readonly ResultFormatInformation _formatInformation;

public JsonReportWriter()
{
_formatInformation = new ResultFormatInformation
Format = new ResultFormatInformation
{
DisplayName = "Json",
MimeType = "application/json",
FileExtension = ".json"
};
}

public ResultFormatInformation Format
{
get { return _formatInformation; }
}
public ResultFormatInformation Format { get; }

public void WriteStream(Stream stream, AnalyzeResponse response)
public Task WriteStreamAsync(Stream stream, AnalyzeResponse response)
{
using (var streamWriter = new StreamWriter(stream))
{
Expand All @@ -36,6 +32,8 @@ public void WriteStream(Stream stream, AnalyzeResponse response)
DataExtensions.Serializer.Serialize(writer, response);
}
}

return Task.CompletedTask;
}
}
}
3 changes: 2 additions & 1 deletion src/lib/Microsoft.Fx.Portability/Reporting/IReportWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

using Microsoft.Fx.Portability.ObjectModel;
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.Fx.Portability.Reporting
{
public interface IReportWriter
{
ResultFormatInformation Format { get; }

void WriteStream(Stream stream, AnalyzeResponse response);
Task WriteStreamAsync(Stream stream, AnalyzeResponse response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Fx.Portability.Reports.Html.Tests
Expand Down Expand Up @@ -35,7 +36,7 @@ public static void CanFindTemplates(string templateName)
}

[Fact]
public static void CreatesHtmlReport()
public static async Task CreatesHtmlReport()
{
var mapper = Substitute.For<ITargetMapper>();
var writer = new HtmlReportWriter(mapper);
Expand All @@ -57,7 +58,7 @@ public static void CreatesHtmlReport()
{
using (var file = File.OpenWrite(tempFile))
{
writer.WriteStream(file, response);
await writer.WriteStreamAsync(file, response);
}

Assert.True(File.Exists(tempFile));
Expand Down