From 57c618b2ca511a99394dc99556eb4fa3129aefaf Mon Sep 17 00:00:00 2001 From: Monica S Date: Sat, 10 Aug 2019 15:36:24 +0100 Subject: [PATCH] Always write headers and check against empty paths (#80) * Always write headers and check against empty paths This fixes #78 and fixes #79. Header files are written whenever a valid header file is passed, and null/empty paths are now ignored. * Skip generating the header in the method class It was already written earlier, so it is unnecessary. * Make suggested changes --- .../PInvokeGenerator.cs | 28 +++++++++++-------- .../PInvokeGeneratorConfiguration.cs | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index b341488a..b988e813 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -268,19 +268,25 @@ private void CloseOutputBuilder(Stream stream, OutputBuilder outputBuilder, bool using var sw = new StreamWriter(stream, defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen); sw.NewLine = "\n"; - if (outputBuilder.UsingDirectives.Any() && _config.GenerateMultipleFiles) + if (_config.GenerateMultipleFiles) { - sw.Write(_config.HeaderText); - - foreach (var usingDirective in outputBuilder.UsingDirectives) + if (_config.HeaderText != string.Empty) { - sw.Write("using"); - sw.Write(' '); - sw.Write(usingDirective); - sw.WriteLine(';'); + sw.WriteLine(_config.HeaderText); } - sw.WriteLine(); + if (outputBuilder.UsingDirectives.Any()) + { + foreach (var usingDirective in outputBuilder.UsingDirectives) + { + sw.Write("using"); + sw.Write(' '); + sw.Write(usingDirective); + sw.WriteLine(';'); + } + + sw.WriteLine(); + } } var indentationString = outputBuilder.IndentationString; @@ -1399,12 +1405,12 @@ private void VisitParmVarDecl(ParmVarDecl parmVarDecl, TypedefDecl typedefDecl) var parameters = typedefDecl.CursorChildren.Where((cursor) => cursor is ParmVarDecl).Cast().ToList(); var index = parameters.IndexOf(parmVarDecl); var lastIndex = parameters.Count - 1; - + if (name.Equals("param")) { _outputBuilder.Write(index); } - + if (index != lastIndex) { _outputBuilder.Write(','); diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs index bcc2e35d..ca8cfd2a 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs @@ -51,7 +51,7 @@ public PInvokeGeneratorConfiguration(string libraryPath, string namespaceName, s _options = options; ExcludedNames = excludedNames; - HeaderText = headerFile is object ? File.ReadAllText(headerFile) : string.Empty; + HeaderText = string.IsNullOrWhiteSpace(headerFile) ? string.Empty : File.ReadAllText(headerFile); LibraryPath = libraryPath; MethodClassName = methodClassName; MethodPrefixToStrip = methodPrefixToStrip;