Skip to content

Commit

Permalink
SIANXSVC-1203: Ensure library_version has correct value (#8)
Browse files Browse the repository at this point in the history
Several things had to get fixed in order for the library version to
appear in the metadata. First and foremost, the GitHub Actions workflow
was setting the PackageVersion (which is used for the NuGet package),
but not the AssemblyVersion. This is now fixed by passing the tag via
/p:Version.

Second, the current retrieved version included the commit hash, which
would have ended up to conflict with the database constraints of E5E
which limit the version to 20 characters. In order to circumvent this
problem, the hash is trimmed from the version.

Because I had the time to work on this part of the code, I took the
opportunity and made the E5ERuntimeMetadata sealed. Technically, this is
a breaking change, however I don't expect that anyone is affected by it,
so it's fine to put it in a patch release.

Closes SIANXSVC-1203
  • Loading branch information
nachtjasmin authored Feb 5, 2024
1 parent a63c6e9 commit 29f8675
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- run: dotnet restore

- run: dotnet pack --no-restore --include-symbols -o . /p:PackageVersion=${VERSION}
- run: dotnet pack --no-restore --include-symbols -o . /p:Version=${VERSION}

- name: Deploy to NuGet
run: dotnet nuget push *.nupkg --api-key $NUGET_AUTH_TOKEN --source https://api.nuget.org/v3/index.json
Expand Down
2 changes: 1 addition & 1 deletion src/Anexia.E5E.Tests/Integration/StartupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public StartupTests(ITestOutputHelper outputHelper) : base(outputHelper)
public async Task OutputMatches()
{
await Host.StartAsync();
var expected = JsonSerializer.Serialize(new E5ERuntimeMetadata(), E5EJsonSerializerOptions.Default);
var expected = JsonSerializer.Serialize(E5ERuntimeMetadata.Current, E5EJsonSerializerOptions.Default);
var stdout = await Host.GetStdoutAsync();
Assert.Equal(expected, stdout);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Anexia.E5E.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void ResponseSerializationRecognisesCorrectType()
Assert.Equal(E5EResponseType.Binary, E5EResponse.From("test"u8.ToArray()).Type);
Assert.Equal(E5EResponseType.Binary, E5EResponse.From("test"u8.ToArray().AsEnumerable()).Type);
Assert.Equal(E5EResponseType.Binary, E5EResponse.From(new E5EFileData("something"u8.ToArray())).Type);
Assert.Equal(E5EResponseType.StructuredObject, E5EResponse.From(new E5ERuntimeMetadata()).Type);
Assert.Equal(E5EResponseType.StructuredObject, E5EResponse.From(E5ERuntimeMetadata.Current).Type);
}

[Theory]
Expand All @@ -125,7 +125,7 @@ public void EnumsAreProperSerialized(object type, string expected)
[Fact]
public void MetadataIsProperSerialized()
{
var json = JsonSerializer.Serialize(new E5ERuntimeMetadata(), _options);
var json = JsonSerializer.Serialize(E5ERuntimeMetadata.Current, _options);
var deserialized = JsonSerializer.Deserialize<JsonElement>(json);
var sut = deserialized.EnumerateObject().ToDictionary(x => x.Name, x => x.Value);

Expand All @@ -135,7 +135,8 @@ public void MetadataIsProperSerialized()
() => Assert.Contains("runtime", sut),
() => Assert.Contains("features", sut),
() => Assert.Contains("library_version", sut),
() => Assert.Equal(1, sut["features"].GetArrayLength())
() => Assert.Equal(1, sut["features"].GetArrayLength()),
() => Assert.Equal("1.0.0", sut["library_version"].GetString())
);
}

Expand All @@ -158,7 +159,7 @@ private class SerializationTestsData : IEnumerable<object[]>
{
new E5EContext("generic", DateTimeOffset.FromUnixTimeSeconds(0), true),
new E5ERequestParameters(),
new E5ERuntimeMetadata(),
E5ERuntimeMetadata.Current,
new E5EFileData("data"u8.ToArray()),
};

Expand Down
1 change: 0 additions & 1 deletion src/Anexia.E5E/Anexia.E5E.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<!-- NuGet specific metadata, according to: https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/nuget#important-nuget-package-metadata -->
<PackageId>Anexia.E5E</PackageId>
<PackageVersion>1.0.0</PackageVersion> <!-- This is a placeholder, overridden by Nerdbank.GitVersioning -->
<Description>A helper library to help you build serverless functions on top of the Anexia Engine.</Description>
<Authors>anexia</Authors> <!-- needs to match the NuGet profiles -->
<Company>ANEXIA Internetdienstleistungs GmbH</Company>
Expand Down
4 changes: 2 additions & 2 deletions src/Anexia.E5E/Hosting/E5EHostWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
}

#if NET8_0_OR_GREATER
var metadata = JsonSerializer.Serialize(new E5ERuntimeMetadata(),
var metadata = JsonSerializer.Serialize(E5ERuntimeMetadata.Current,
E5ESerializationContext.Default.E5ERuntimeMetadata);
#else
var metadata = JsonSerializer.Serialize(new E5ERuntimeMetadata(), E5EJsonSerializerOptions.Default);
var metadata = JsonSerializer.Serialize(E5ERuntimeMetadata.Current, E5EJsonSerializerOptions.Default);
#endif

_console.Open();
Expand Down
17 changes: 13 additions & 4 deletions src/Anexia.E5E/Runtime/E5ERuntimeMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ namespace Anexia.E5E.Runtime;
/// </summary>
[SuppressMessage("ReSharper", "UnusedMember.Global")]
[SuppressMessage("Performance", "CA1822:Mark members as static")]
public record E5ERuntimeMetadata
public sealed record E5ERuntimeMetadata
{
private E5ERuntimeMetadata()
{
// We try to fetch the informational version from the generated AssemblyInfo. Because SourceLink appends the
// commit hash, we have to trim it afterwards.
var version = typeof(E5ERuntimeMetadata).Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion ?? "0.0.0+unknown";

LibraryVersion = version[..version.IndexOf('+')];
}

/// <summary>
/// The current instance of the metadata.
/// </summary>
Expand All @@ -18,9 +29,7 @@ public record E5ERuntimeMetadata
/// <summary>
/// The installed version of this NuGet library.
/// </summary>
public string LibraryVersion =>
typeof(E5ERuntimeMetadata).Assembly.GetCustomAttribute<AssemblyVersionAttribute>()?.Version ??
"0.0.0-unrecognized";
public string LibraryVersion { get; }

/// <summary>
/// The runtime this function is running in.
Expand Down

0 comments on commit 29f8675

Please sign in to comment.