Skip to content

Commit

Permalink
Split the test output to its own stream in single-file mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Nov 19, 2023
1 parent 124535b commit 1918c78
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
public void Close()
{
Stream? stream = null;
Stream? testStream = null;

var methodClassOutputBuilders = new Dictionary<string, IOutputBuilder>();
var methodClassTestOutputBuilders = new Dictionary<string, IOutputBuilder>();
Expand Down Expand Up @@ -237,11 +238,16 @@ public void Close()
{
var outputPath = _config.OutputLocation;
stream = _outputStreamFactory(outputPath);

var testOutputPath = _config.TestOutputLocation;
testStream = _outputStreamFactory(testOutputPath);

leaveStreamOpen = true;

var usingDirectives = new SortedSet<string>(StringComparer.Ordinal);
var staticUsingDirectives = new SortedSet<string>(StringComparer.Ordinal);
var hasAnyContents = false;
var testHasAnyContents = false;

foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders)
{
Expand All @@ -257,11 +263,19 @@ public void Close()
_ = staticUsingDirectives.Add(staticUsingDirective);
}

hasAnyContents = csharpOutputBuilder.Contents.Any();
if (csharpOutputBuilder.IsTestOutput)
{
testHasAnyContents |= csharpOutputBuilder.Contents.Any();
}
else
{
hasAnyContents |= csharpOutputBuilder.Contents.Any();
}
}
else if (outputBuilder is XmlOutputBuilder xmlOutputBuilder)
{
hasAnyContents = xmlOutputBuilder.Contents.Any();
Debug.Assert(!xmlOutputBuilder.IsTestOutput);
hasAnyContents |= xmlOutputBuilder.Contents.Any();
}
}

Expand Down Expand Up @@ -316,6 +330,32 @@ public void Close()
}
}
}

if (testHasAnyContents)
{
using var sw = new StreamWriter(testStream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
{
if (!string.IsNullOrEmpty(_config.HeaderText))
{
sw.WriteLine(_config.HeaderText);
}

if (usingDirectives.Count != 0)
{
foreach (var usingDirective in usingDirectives)
{
sw.Write("using ");
sw.Write(usingDirective);
sw.WriteLine(';');
}

sw.WriteLine();
}
}
}
}

foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders)
Expand Down Expand Up @@ -359,11 +399,18 @@ public void Close()

Debug.Assert(stream is not null);
CloseOutputBuilder(stream, outputBuilder, isMethodClass, leaveStreamOpen, emitNamespaceDeclaration);

if (testStream is not null)
{
CloseOutputBuilder(testStream, outputBuilder, isMethodClass, leaveStreamOpen, emitNamespaceDeclaration);
}

emitNamespaceDeclaration = false;

if (_config.GenerateMultipleFiles)
{
stream = null;
Debug.Assert(testStream is null);
}
}

Expand Down Expand Up @@ -400,7 +447,7 @@ public void Close()

foreach (var entry in methodClassTestOutputBuilders)
{
CloseOutputBuilder(stream, entry.Value, isMethodClass: true, leaveStreamOpen, emitNamespaceDeclaration);
CloseOutputBuilder(testStream ?? stream, entry.Value, isMethodClass: true, leaveStreamOpen, emitNamespaceDeclaration);
}

using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
Expand All @@ -415,6 +462,17 @@ public void Close()
sw.WriteLine(" </namespace>");
sw.WriteLine("</bindings>");
}

if (testStream is not null)
{
using var tsw = new StreamWriter(testStream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
tsw.NewLine = "\n";

if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
{
tsw.WriteLine('}');
}
}
}

_context.Clear();
Expand Down

0 comments on commit 1918c78

Please sign in to comment.