Skip to content

Commit

Permalink
Fixed a generator bug. (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: AraHaan <[email protected]>
  • Loading branch information
AraHaan authored May 18, 2021
1 parent 6e607f7 commit 1d99b64
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 35 deletions.
14 changes: 5 additions & 9 deletions src/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
var splitted2 = splitted.AsSpan().Slice(0, splitted.Length - 1);
var splitted = generator.options!.AssemblyType!.Contains(".") ? generator.options.AssemblyType.Split('.') : Array.Empty<string>();
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)
Expand Down Expand Up @@ -76,11 +76,7 @@ private static Generator Create(string optionsText, string gitInfoText)
options = JsonSerializer.Deserialize<GeneratorOptions>(optionsText, serializerOptions),
gitInfo = JsonSerializer.Deserialize<GitInfo>(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;
}

Expand Down
19 changes: 15 additions & 4 deletions src/GeneratorOptions.cs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
}
5 changes: 3 additions & 2 deletions src/GitBuildInfo.SourceGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<Version>1.0.8</Version>
<PackageReleaseNotes>Added missing assembly to package.</PackageReleaseNotes>
<Version>1.0.9</Version>
<PackageReleaseNotes>Fixed a generator bug.</PackageReleaseNotes>
<Company>Els_kom org.</Company>
<Authors>Els_kom org.</Authors>
<Copyright>Copyright (c) 2021</Copyright>
Expand Down Expand Up @@ -42,6 +42,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="*-*" />
<PackageReference Include="Nullable" Version="*-*" />
<PackageReference Include="IsExternalInit" Version="*-*" />
<PackageReference Include="System.Text.Json" Version="*-*" />
</ItemGroup>

Expand Down
12 changes: 6 additions & 6 deletions src/GitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
76 changes: 62 additions & 14 deletions tests/SourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +12,28 @@

public class SourceGeneratorTests
{
[Fact]
public void TestGeneratingDefaultNamespace()
{
var result = DoTest("Elskom.Generic.Libs.Test", false, TestGenerate);
Assert.Equal(@"// <autogenerated/>
using Elskom.Generic.Libs;
[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test))]
", result);
}

[Fact]
public void TestGeneratingNoNamespace()
{
var result = DoTest("Test", false, TestGenerate);
Assert.Equal(@"// <autogenerated/>
using Elskom.Generic.Libs;
[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test))]
", result);
}

[Fact]
public void TestGeneratingNonGeneric()
{
Expand Down Expand Up @@ -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);
}

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

private static string DoTest(string? assemblyType, Func<string, string> 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<string, string> func)
=> func.Invoke(text);

private static string TestGenerate(string optionsText, string gitInfoText)
=> TestGenerateInternal(
CreateCSharpCompilation(),
ImmutableArray.Create<AdditionalText>(
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<AdditionalText>(
new CustomAdditionalText("GitBuildInfo.json", optionsText)));
CreateGitBuildInfoText(optionsText)));

private static string TestGenerateGitInformationOnly(string gitInfoText)
=> TestGenerateInternal(
CreateCSharpCompilation(),
ImmutableArray.Create<AdditionalText>(
CreateGitInfoText(gitInfoText)));

private static string TestGenerateVB(string optionsText, string gitInfoText)
=> TestGenerateInternal(
Expand All @@ -103,8 +145,8 @@ private static string TestGenerateVB(string optionsText, string gitInfoText)
new VisualBasicCompilationOptions(
OutputKind.DynamicallyLinkedLibrary)),
ImmutableArray.Create<AdditionalText>(
new CustomAdditionalText("GitBuildInfo.json", optionsText),
new CustomAdditionalText("GitInfo.json", gitInfoText)));
CreateGitBuildInfoText(optionsText),
CreateGitInfoText(gitInfoText)));

private static string TestGenerateInternal(Compilation compilation, ImmutableArray<AdditionalText> additionalTexts)
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1d99b64

Please sign in to comment.