Skip to content

Commit 1d99b64

Browse files
authored
Fixed a generator bug. (#34)
Signed-off-by: AraHaan <seandhunt_7@yahoo.com>
1 parent 6e607f7 commit 1d99b64

File tree

5 files changed

+91
-35
lines changed

5 files changed

+91
-35
lines changed

src/Generator.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ internal class Generator
1515
public static string CreateAndGenerateCode(string optionsText, string gitInfoText)
1616
{
1717
var generator = Create(optionsText, gitInfoText);
18-
var splitted = generator.options!.AssemblyType!.Contains(".") ? generator.options!.AssemblyType.Split('.') : Array.Empty<string>();
19-
var splitted2 = splitted.AsSpan().Slice(0, splitted.Length - 1);
18+
var splitted = generator.options!.AssemblyType!.Contains(".") ? generator.options.AssemblyType.Split('.') : Array.Empty<string>();
19+
var splitted2 = splitted.AsSpan().Slice(0, splitted.Length > 0 ? splitted.Length - 1 : 0);
2020
return generator.GenerateCode(
2121
splitted2.ToArray(),
2222
"Elskom.Generic.Libs",
23-
splitted.Length > 0 ? splitted[splitted2.Length] : generator.options!.AssemblyType).ToFullString();
23+
splitted.Length > 0 ? splitted[splitted2.Length] : generator.options.AssemblyType).ToFullString();
2424
}
2525

2626
public CompilationUnitSyntax GenerateCode(string[] usings, string originalnamespace, string typeName)
2727
=> SyntaxFactory.CompilationUnit().WithUsings(
2828
SyntaxFactory.List(
29-
string.Equals(string.Join(".", usings), originalnamespace, StringComparison.Ordinal)
29+
string.Equals(string.Join(".", usings), originalnamespace, StringComparison.Ordinal) || usings.Length is 0
3030
? new[]
3131
{
3232
AddUsing(new[] {"Elskom", "Generic", "Libs"}, true)
@@ -76,11 +76,7 @@ private static Generator Create(string optionsText, string gitInfoText)
7676
options = JsonSerializer.Deserialize<GeneratorOptions>(optionsText, serializerOptions),
7777
gitInfo = JsonSerializer.Deserialize<GitInfo>(gitInfoText, serializerOptions),
7878
};
79-
if (string.IsNullOrEmpty(generator.options!.AssemblyType))
80-
{
81-
throw new InvalidOperationException("AssemblyType should not be null or an empty string.");
82-
}
83-
79+
generator.options!.Validate();
8480
return generator;
8581
}
8682

src/GeneratorOptions.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
namespace GitBuildInfo.SourceGenerator
22
{
3+
using System;
4+
using System.Runtime.CompilerServices;
35
using System.Text.Json.Serialization;
46

57
public record GeneratorOptions
68
{
7-
[JsonPropertyName("AssemblyType")]
8-
public string? AssemblyType { get; set; }
9+
[JsonPropertyName(nameof(AssemblyType))]
10+
public string? AssemblyType { get; init; }
911

10-
[JsonPropertyName("IsGeneric")]
11-
public bool IsGeneric { get; set; }
12+
[JsonPropertyName(nameof(IsGeneric))]
13+
public bool IsGeneric { get; init; }
14+
15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16+
public void Validate()
17+
{
18+
if (string.IsNullOrEmpty(this.AssemblyType))
19+
{
20+
throw new InvalidOperationException("AssemblyType should not be null or an empty string.");
21+
}
22+
}
1223
}
1324
}

src/GitBuildInfo.SourceGenerator.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<TargetFramework>netstandard2.0</TargetFramework>
66
<LangVersion>latest</LangVersion>
77
<Nullable>enable</Nullable>
8-
<Version>1.0.8</Version>
9-
<PackageReleaseNotes>Added missing assembly to package.</PackageReleaseNotes>
8+
<Version>1.0.9</Version>
9+
<PackageReleaseNotes>Fixed a generator bug.</PackageReleaseNotes>
1010
<Company>Els_kom org.</Company>
1111
<Authors>Els_kom org.</Authors>
1212
<Copyright>Copyright (c) 2021</Copyright>
@@ -42,6 +42,7 @@
4242
<ItemGroup>
4343
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="*-*" />
4444
<PackageReference Include="Nullable" Version="*-*" />
45+
<PackageReference Include="IsExternalInit" Version="*-*" />
4546
<PackageReference Include="System.Text.Json" Version="*-*" />
4647
</ItemGroup>
4748

src/GitInfo.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
public record GitInfo
66
{
7-
[JsonPropertyName("GitHead")]
8-
public string? GitHead { get; set; }
7+
[JsonPropertyName(nameof(GitHead))]
8+
public string? GitHead { get; init; }
99

10-
[JsonPropertyName("CommitHash")]
11-
public string? CommitHash { get; set; }
10+
[JsonPropertyName(nameof(CommitHash))]
11+
public string? CommitHash { get; init; }
1212

13-
[JsonPropertyName("GitBranch")]
14-
public string? GitBranch { get; set; }
13+
[JsonPropertyName(nameof(GitBranch))]
14+
public string? GitBranch { get; init; }
1515
}
1616
}

tests/SourceGeneratorTests.cs

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Immutable;
55
using System.Linq;
66
using System.Threading;
7-
using GitBuildInfo.SourceGenerator;
87
using Microsoft.CodeAnalysis;
98
using Microsoft.CodeAnalysis.CSharp;
109
using Microsoft.CodeAnalysis.Text;
@@ -13,6 +12,28 @@
1312

1413
public class SourceGeneratorTests
1514
{
15+
[Fact]
16+
public void TestGeneratingDefaultNamespace()
17+
{
18+
var result = DoTest("Elskom.Generic.Libs.Test", false, TestGenerate);
19+
Assert.Equal(@"// <autogenerated/>
20+
using Elskom.Generic.Libs;
21+
22+
[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test))]
23+
", result);
24+
}
25+
26+
[Fact]
27+
public void TestGeneratingNoNamespace()
28+
{
29+
var result = DoTest("Test", false, TestGenerate);
30+
Assert.Equal(@"// <autogenerated/>
31+
using Elskom.Generic.Libs;
32+
33+
[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test))]
34+
", result);
35+
}
36+
1637
[Fact]
1738
public void TestGeneratingNonGeneric()
1839
{
@@ -55,7 +76,26 @@ public void TestVBGeneratingAbort()
5576
[Fact]
5677
public void TestGenerateWithOnlyOptions()
5778
{
58-
var result = DoTest("TestNamespace.Test", TestGenerateSingle);
79+
var result = DoTest(
80+
@"{
81+
""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"",
82+
""AssemblyType"": ""TestNamespace.Test"",
83+
""IsGeneric"": false,
84+
}",
85+
TestGenerateOptionsOnly);
86+
Assert.Equal(string.Empty, result);
87+
}
88+
89+
[Fact]
90+
public void TestGenerateWithOnlyGitInformation()
91+
{
92+
var result = DoTest(
93+
@"{
94+
""GitHead"": ""fbgtgretgtre"",
95+
""CommitHash"": ""vfdbttregter"",
96+
""GitBranch"": ""vsdfvfdsv"",
97+
}",
98+
TestGenerateGitInformationOnly);
5999
Assert.Equal(string.Empty, result);
60100
}
61101

@@ -74,25 +114,27 @@ private static string DoTest(string? assemblyType, bool generic, Func<string, st
74114
""GitBranch"": ""vsdfvfdsv"",
75115
}");
76116

77-
private static string DoTest(string? assemblyType, Func<string, string> func)
78-
=> func.Invoke($@"{{
79-
""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"",
80-
""AssemblyType"": ""{assemblyType}"",
81-
""IsGeneric"": false,
82-
}}");
117+
private static string DoTest(string text, Func<string, string> func)
118+
=> func.Invoke(text);
83119

84120
private static string TestGenerate(string optionsText, string gitInfoText)
85121
=> TestGenerateInternal(
86122
CreateCSharpCompilation(),
87123
ImmutableArray.Create<AdditionalText>(
88-
new CustomAdditionalText("GitBuildInfo.json", optionsText),
89-
new CustomAdditionalText("GitInfo.json", gitInfoText)));
124+
CreateGitBuildInfoText(optionsText),
125+
CreateGitInfoText(gitInfoText)));
90126

91-
private static string TestGenerateSingle(string optionsText)
127+
private static string TestGenerateOptionsOnly(string optionsText)
92128
=> TestGenerateInternal(
93129
CreateCSharpCompilation(),
94130
ImmutableArray.Create<AdditionalText>(
95-
new CustomAdditionalText("GitBuildInfo.json", optionsText)));
131+
CreateGitBuildInfoText(optionsText)));
132+
133+
private static string TestGenerateGitInformationOnly(string gitInfoText)
134+
=> TestGenerateInternal(
135+
CreateCSharpCompilation(),
136+
ImmutableArray.Create<AdditionalText>(
137+
CreateGitInfoText(gitInfoText)));
96138

97139
private static string TestGenerateVB(string optionsText, string gitInfoText)
98140
=> TestGenerateInternal(
@@ -103,8 +145,8 @@ private static string TestGenerateVB(string optionsText, string gitInfoText)
103145
new VisualBasicCompilationOptions(
104146
OutputKind.DynamicallyLinkedLibrary)),
105147
ImmutableArray.Create<AdditionalText>(
106-
new CustomAdditionalText("GitBuildInfo.json", optionsText),
107-
new CustomAdditionalText("GitInfo.json", gitInfoText)));
148+
CreateGitBuildInfoText(optionsText),
149+
CreateGitInfoText(gitInfoText)));
108150

109151
private static string TestGenerateInternal(Compilation compilation, ImmutableArray<AdditionalText> additionalTexts)
110152
{
@@ -134,6 +176,12 @@ private static Compilation CreateCSharpCompilation()
134176
new CSharpCompilationOptions(
135177
OutputKind.DynamicallyLinkedLibrary));
136178

179+
private static CustomAdditionalText CreateGitBuildInfoText(string text)
180+
=> new("GitBuildInfo.json", text);
181+
182+
private static CustomAdditionalText CreateGitInfoText(string text)
183+
=> new("GitInfo.json", text);
184+
137185
private class CustomAdditionalText : AdditionalText
138186
{
139187
private readonly string _text;

0 commit comments

Comments
 (0)