From e690716723257721d6d483c402550f9d2f48396f Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Sun, 19 Nov 2023 09:24:37 -0800 Subject: [PATCH] Split the test output to its own stream in single-file mode --- .../PInvokeGenerator.cs | 68 ++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 4124b69c..b5013b87 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -210,6 +210,7 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func(); var methodClassTestOutputBuilders = new Dictionary(); @@ -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(StringComparer.Ordinal); var staticUsingDirectives = new SortedSet(StringComparer.Ordinal); var hasAnyContents = false; + var testHasAnyContents = false; foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders) { @@ -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(); } } @@ -316,6 +330,36 @@ 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(); + } + } + } + else + { + testStream = null; + } } foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders) @@ -359,11 +403,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); } } @@ -400,7 +451,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); @@ -415,6 +466,17 @@ public void Close() sw.WriteLine(" "); sw.WriteLine(""); } + + 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();