Skip to content

Commit f387011

Browse files
Merge pull request #391 from tannergooding/single-file-helper-types
Ensure generate-helper-types works with single-file mode
2 parents 8d55b99 + f05e35b commit f387011

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,22 @@ public void Close()
297297

298298
if (_config.GenerateHelperTypes && (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp))
299299
{
300-
_ = Directory.CreateDirectory(_config.OutputLocation);
300+
if (_config.GenerateMultipleFiles)
301+
{
302+
Debug.Assert(stream is null);
303+
Debug.Assert(leaveStreamOpen is false);
304+
}
305+
else
306+
{
307+
Debug.Assert(stream is not null);
308+
Debug.Assert(leaveStreamOpen is true);
309+
}
301310

302-
GenerateNativeInheritanceAttribute(this);
303-
GenerateNativeTypeNameAttribute(this);
304-
GenerateSetsLastSystemErrorAttribute(this);
305-
GenerateVtblIndexAttribute(this);
306-
GenerateTransparentStructs(this);
311+
GenerateNativeInheritanceAttribute(this, stream, leaveStreamOpen);
312+
GenerateNativeTypeNameAttribute(this, stream, leaveStreamOpen);
313+
GenerateSetsLastSystemErrorAttribute(this, stream, leaveStreamOpen);
314+
GenerateVtblIndexAttribute(this, stream, leaveStreamOpen);
315+
GenerateTransparentStructs(this, stream, leaveStreamOpen);
307316
}
308317

309318
if (leaveStreamOpen && _outputBuilderFactory.OutputBuilders.Any())
@@ -345,12 +354,17 @@ public void Close()
345354
_uuidsToGenerate.Clear();
346355
_visitedFiles.Clear();
347356

348-
static void GenerateNativeInheritanceAttribute(PInvokeGenerator generator)
357+
static void GenerateNativeInheritanceAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
349358
{
350359
var config = generator.Config;
351-
var outputPath = Path.Combine(config.OutputLocation, "NativeInheritanceAttribute.cs");
352360

353-
using var sw = new StreamWriter(outputPath);
361+
if (stream is null)
362+
{
363+
var outputPath = Path.Combine(config.OutputLocation, "NativeInheritanceAttribute.cs");
364+
stream = generator._outputStreamFactory(outputPath);
365+
}
366+
367+
using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
354368
sw.NewLine = "\n";
355369

356370
if (config.HeaderText != string.Empty)
@@ -418,12 +432,17 @@ static void GenerateNativeInheritanceAttribute(PInvokeGenerator generator)
418432
}
419433
}
420434

421-
static void GenerateNativeTypeNameAttribute(PInvokeGenerator generator)
435+
static void GenerateNativeTypeNameAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
422436
{
423437
var config = generator.Config;
424-
var outputPath = Path.Combine(config.OutputLocation, "NativeTypeNameAttribute.cs");
425438

426-
using var sw = new StreamWriter(outputPath);
439+
if (stream is null)
440+
{
441+
var outputPath = Path.Combine(config.OutputLocation, "NativeTypeNameAttribute.cs");
442+
stream = generator._outputStreamFactory(outputPath);
443+
}
444+
445+
using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
427446
sw.NewLine = "\n";
428447

429448
if (config.HeaderText != string.Empty)
@@ -491,12 +510,18 @@ static void GenerateNativeTypeNameAttribute(PInvokeGenerator generator)
491510
}
492511
}
493512

494-
static void GenerateSetsLastSystemErrorAttribute(PInvokeGenerator generator)
513+
static void GenerateSetsLastSystemErrorAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
495514
{
496515
var config = generator.Config;
497-
var outputPath = Path.Combine(config.OutputLocation, "SetsLastSystemErrorAttribute.cs");
498516

499-
using var sw = new StreamWriter(outputPath);
517+
if (stream is null)
518+
{
519+
Debug.Assert(stream is null);
520+
var outputPath = Path.Combine(config.OutputLocation, "SetsLastSystemErrorAttribute.cs");
521+
stream = generator._outputStreamFactory(outputPath);
522+
}
523+
524+
using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
500525
sw.NewLine = "\n";
501526

502527
if (config.HeaderText != string.Empty)
@@ -553,12 +578,18 @@ static void GenerateSetsLastSystemErrorAttribute(PInvokeGenerator generator)
553578
}
554579
}
555580

556-
static void GenerateVtblIndexAttribute(PInvokeGenerator generator)
581+
static void GenerateVtblIndexAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
557582
{
558583
var config = generator.Config;
559-
var outputPath = Path.Combine(config.OutputLocation, "VtblIndexAttribute.cs");
560584

561-
using var sw = new StreamWriter(outputPath);
585+
if (stream is null)
586+
{
587+
Debug.Assert(stream is null);
588+
var outputPath = Path.Combine(config.OutputLocation, "VtblIndexAttribute.cs");
589+
stream = generator._outputStreamFactory(outputPath);
590+
}
591+
592+
using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
562593
sw.NewLine = "\n";
563594

564595
if (config.HeaderText != string.Empty)
@@ -626,7 +657,7 @@ static void GenerateVtblIndexAttribute(PInvokeGenerator generator)
626657
}
627658
}
628659

629-
static void GenerateTransparentStructs(PInvokeGenerator generator)
660+
static void GenerateTransparentStructs(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
630661
{
631662
var config = generator.Config;
632663

@@ -637,9 +668,14 @@ static void GenerateTransparentStructs(PInvokeGenerator generator)
637668
var kind = transparentStruct.Value.Kind;
638669

639670
var isTypePointer = type.Contains('*');
640-
var outputPath = Path.Combine(config.OutputLocation, $"{name}.cs");
641671

642-
using var sw = new StreamWriter(outputPath);
672+
if (stream is null)
673+
{
674+
var outputPath = Path.Combine(config.OutputLocation, $"{name}.cs");
675+
stream = generator._outputStreamFactory(outputPath);
676+
}
677+
678+
using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
643679
sw.NewLine = "\n";
644680

645681
if (config.HeaderText != string.Empty)

0 commit comments

Comments
 (0)