From 1d99b64aff9207d22eac71e01ba61f01f336ce3e Mon Sep 17 00:00:00 2001 From: AraHaan Date: Tue, 18 May 2021 12:23:11 -0400 Subject: [PATCH] Fixed a generator bug. (#34) Signed-off-by: AraHaan --- src/Generator.cs | 14 ++--- src/GeneratorOptions.cs | 19 +++++-- src/GitBuildInfo.SourceGenerator.csproj | 5 +- src/GitInfo.cs | 12 ++-- tests/SourceGeneratorTests.cs | 76 ++++++++++++++++++++----- 5 files changed, 91 insertions(+), 35 deletions(-) diff --git a/src/Generator.cs b/src/Generator.cs index a984c65..76a67ff 100644 --- a/src/Generator.cs +++ b/src/Generator.cs @@ -15,18 +15,18 @@ internal class Generator public static string CreateAndGenerateCode(string optionsText, string gitInfoText) { var generator = Create(optionsText, gitInfoText); - var splitted = generator.options!.AssemblyType!.Contains(".") ? generator.options!.AssemblyType.Split('.') : Array.Empty(); - var splitted2 = splitted.AsSpan().Slice(0, splitted.Length - 1); + var splitted = generator.options!.AssemblyType!.Contains(".") ? generator.options.AssemblyType.Split('.') : Array.Empty(); + var splitted2 = splitted.AsSpan().Slice(0, splitted.Length > 0 ? splitted.Length - 1 : 0); return generator.GenerateCode( splitted2.ToArray(), "Elskom.Generic.Libs", - splitted.Length > 0 ? splitted[splitted2.Length] : generator.options!.AssemblyType).ToFullString(); + splitted.Length > 0 ? splitted[splitted2.Length] : generator.options.AssemblyType).ToFullString(); } public CompilationUnitSyntax GenerateCode(string[] usings, string originalnamespace, string typeName) => SyntaxFactory.CompilationUnit().WithUsings( SyntaxFactory.List( - string.Equals(string.Join(".", usings), originalnamespace, StringComparison.Ordinal) + string.Equals(string.Join(".", usings), originalnamespace, StringComparison.Ordinal) || usings.Length is 0 ? new[] { AddUsing(new[] {"Elskom", "Generic", "Libs"}, true) @@ -76,11 +76,7 @@ private static Generator Create(string optionsText, string gitInfoText) options = JsonSerializer.Deserialize(optionsText, serializerOptions), gitInfo = JsonSerializer.Deserialize(gitInfoText, serializerOptions), }; - if (string.IsNullOrEmpty(generator.options!.AssemblyType)) - { - throw new InvalidOperationException("AssemblyType should not be null or an empty string."); - } - + generator.options!.Validate(); return generator; } diff --git a/src/GeneratorOptions.cs b/src/GeneratorOptions.cs index 53bfb9f..629229a 100644 --- a/src/GeneratorOptions.cs +++ b/src/GeneratorOptions.cs @@ -1,13 +1,24 @@ namespace GitBuildInfo.SourceGenerator { + using System; + using System.Runtime.CompilerServices; using System.Text.Json.Serialization; public record GeneratorOptions { - [JsonPropertyName("AssemblyType")] - public string? AssemblyType { get; set; } + [JsonPropertyName(nameof(AssemblyType))] + public string? AssemblyType { get; init; } - [JsonPropertyName("IsGeneric")] - public bool IsGeneric { get; set; } + [JsonPropertyName(nameof(IsGeneric))] + public bool IsGeneric { get; init; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Validate() + { + if (string.IsNullOrEmpty(this.AssemblyType)) + { + throw new InvalidOperationException("AssemblyType should not be null or an empty string."); + } + } } } diff --git a/src/GitBuildInfo.SourceGenerator.csproj b/src/GitBuildInfo.SourceGenerator.csproj index ad630a2..6c61cbc 100644 --- a/src/GitBuildInfo.SourceGenerator.csproj +++ b/src/GitBuildInfo.SourceGenerator.csproj @@ -5,8 +5,8 @@ netstandard2.0 latest enable - 1.0.8 - Added missing assembly to package. + 1.0.9 + Fixed a generator bug. Els_kom org. Els_kom org. Copyright (c) 2021 @@ -42,6 +42,7 @@ + diff --git a/src/GitInfo.cs b/src/GitInfo.cs index e1decad..4efdf84 100644 --- a/src/GitInfo.cs +++ b/src/GitInfo.cs @@ -4,13 +4,13 @@ public record GitInfo { - [JsonPropertyName("GitHead")] - public string? GitHead { get; set; } + [JsonPropertyName(nameof(GitHead))] + public string? GitHead { get; init; } - [JsonPropertyName("CommitHash")] - public string? CommitHash { get; set; } + [JsonPropertyName(nameof(CommitHash))] + public string? CommitHash { get; init; } - [JsonPropertyName("GitBranch")] - public string? GitBranch { get; set; } + [JsonPropertyName(nameof(GitBranch))] + public string? GitBranch { get; init; } } } diff --git a/tests/SourceGeneratorTests.cs b/tests/SourceGeneratorTests.cs index 3f7348c..24502ff 100644 --- a/tests/SourceGeneratorTests.cs +++ b/tests/SourceGeneratorTests.cs @@ -4,7 +4,6 @@ using System.Collections.Immutable; using System.Linq; using System.Threading; - using GitBuildInfo.SourceGenerator; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Text; @@ -13,6 +12,28 @@ public class SourceGeneratorTests { + [Fact] + public void TestGeneratingDefaultNamespace() + { + var result = DoTest("Elskom.Generic.Libs.Test", false, TestGenerate); + Assert.Equal(@"// +using Elskom.Generic.Libs; + +[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test))] +", result); + } + + [Fact] + public void TestGeneratingNoNamespace() + { + var result = DoTest("Test", false, TestGenerate); + Assert.Equal(@"// +using Elskom.Generic.Libs; + +[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test))] +", result); + } + [Fact] public void TestGeneratingNonGeneric() { @@ -55,7 +76,26 @@ public void TestVBGeneratingAbort() [Fact] public void TestGenerateWithOnlyOptions() { - var result = DoTest("TestNamespace.Test", TestGenerateSingle); + var result = DoTest( + @"{ + ""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"", + ""AssemblyType"": ""TestNamespace.Test"", + ""IsGeneric"": false, +}", + TestGenerateOptionsOnly); + Assert.Equal(string.Empty, result); + } + + [Fact] + public void TestGenerateWithOnlyGitInformation() + { + var result = DoTest( + @"{ + ""GitHead"": ""fbgtgretgtre"", + ""CommitHash"": ""vfdbttregter"", + ""GitBranch"": ""vsdfvfdsv"", +}", + TestGenerateGitInformationOnly); Assert.Equal(string.Empty, result); } @@ -74,25 +114,27 @@ private static string DoTest(string? assemblyType, bool generic, Func func) - => func.Invoke($@"{{ - ""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"", - ""AssemblyType"": ""{assemblyType}"", - ""IsGeneric"": false, -}}"); + private static string DoTest(string text, Func func) + => func.Invoke(text); private static string TestGenerate(string optionsText, string gitInfoText) => TestGenerateInternal( CreateCSharpCompilation(), ImmutableArray.Create( - new CustomAdditionalText("GitBuildInfo.json", optionsText), - new CustomAdditionalText("GitInfo.json", gitInfoText))); + CreateGitBuildInfoText(optionsText), + CreateGitInfoText(gitInfoText))); - private static string TestGenerateSingle(string optionsText) + private static string TestGenerateOptionsOnly(string optionsText) => TestGenerateInternal( CreateCSharpCompilation(), ImmutableArray.Create( - new CustomAdditionalText("GitBuildInfo.json", optionsText))); + CreateGitBuildInfoText(optionsText))); + + private static string TestGenerateGitInformationOnly(string gitInfoText) + => TestGenerateInternal( + CreateCSharpCompilation(), + ImmutableArray.Create( + CreateGitInfoText(gitInfoText))); private static string TestGenerateVB(string optionsText, string gitInfoText) => TestGenerateInternal( @@ -103,8 +145,8 @@ private static string TestGenerateVB(string optionsText, string gitInfoText) new VisualBasicCompilationOptions( OutputKind.DynamicallyLinkedLibrary)), ImmutableArray.Create( - new CustomAdditionalText("GitBuildInfo.json", optionsText), - new CustomAdditionalText("GitInfo.json", gitInfoText))); + CreateGitBuildInfoText(optionsText), + CreateGitInfoText(gitInfoText))); private static string TestGenerateInternal(Compilation compilation, ImmutableArray additionalTexts) { @@ -134,6 +176,12 @@ private static Compilation CreateCSharpCompilation() new CSharpCompilationOptions( OutputKind.DynamicallyLinkedLibrary)); + private static CustomAdditionalText CreateGitBuildInfoText(string text) + => new("GitBuildInfo.json", text); + + private static CustomAdditionalText CreateGitInfoText(string text) + => new("GitInfo.json", text); + private class CustomAdditionalText : AdditionalText { private readonly string _text;