Skip to content

Commit

Permalink
feat: Use new versioning strategy
Browse files Browse the repository at this point in the history
- Rename BuildHash to SCMVersion
- Expose the actual commit hash as a new `commit_hash.txt`
- Update GetGitHash() to actually return the git hash
  • Loading branch information
KazWolfe committed Jul 18, 2024
1 parent 1b92155 commit 897b256
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ jobs:
$newVersion = [System.IO.File]::ReadAllText("$(Get-Location)\scratch\TEMP_gitver.txt")
$revision = [System.IO.File]::ReadAllText("$(Get-Location)\scratch\revision.txt")
$commitHash = [System.IO.File]::ReadAllText("$(Get-Location)\scratch\commit_hash.txt")
Remove-Item -Force -Recurse .\scratch
if (Test-Path -Path $branchName) {
Expand All @@ -147,7 +148,7 @@ jobs:
} else {
Move-Item -Force ".\canary.zip" ".\${branchName}\latest.zip"
$versionData.AssemblyVersion = $newVersion
$versionData | add-member -Force -Name "GitSha" $newVersion -MemberType NoteProperty
$versionData | add-member -Force -Name "GitSha" $commitHash -MemberType NoteProperty
$versionData | add-member -Force -Name "Revision" $revision -MemberType NoteProperty
$versionData | ConvertTo-Json -Compress | Out-File ".\${branchName}\version"
}
Expand Down
70 changes: 29 additions & 41 deletions Dalamud/Dalamud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,57 +124,45 @@
<PropertyGroup>
<!-- Needed temporarily for CI -->
<TempVerFile>$(OutputPath)TEMP_gitver.txt</TempVerFile>
<CommitHashFile>$(OutputPath)commit_hash.txt</CommitHashFile>
<DalamudRevisionFile>$(OutputPath)revision.txt</DalamudRevisionFile>
</PropertyGroup>

<Target Name="GetGitCommitCount" BeforeTargets="WriteGitHash" Condition="'$(CommitCount)'==''">
<Target Name="GetVersionData" BeforeTargets="WriteVersionData" Condition="'$(SCMVersion)'=='' And '$(Configuration)'=='Release'">
<Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; rev-list --count HEAD" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="DalamudGitCommitCount" />
</Exec>

<!-- Set the BuildHash property to contain the GitVersion, if it wasn't already set.-->
<PropertyGroup>
<CommitCount>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitCommitCount), @"\t|\n|\r", ""))</CommitCount>
</PropertyGroup>

<Exec Command="echo|set /P =&quot;$(CommitCount)&quot; &gt; $(DalamudRevisionFile)" IgnoreExitCode="true" />
</Target>

<Target Name="GetGitHash" BeforeTargets="WriteGitHash" Condition="'$(BuildHash)'=='' And '$(Configuration)'=='Release'">
<!-- write the hash to the temp file.-->
<Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; describe --long --tags --always --dirty" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="DalamudGitDescribeOutput" />
<Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; describe --match=NeVeRmAtCh --always --abbrev=40 --dirty" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="DalamudGitCommitHash" />
</Exec>
<Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; rev-parse" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="DalamudFullGitCommitHash" />
<Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; describe --tags --always --dirty" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="DalamudGitDescribeOutput" />
</Exec>
<Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))\..\lib\FFXIVClientStructs&quot; describe --long --always --dirty" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="ClientStructsGitDescribeOutput" />
</Exec>

<!-- Set the BuildHash property to contain the GitVersion, if it wasn't already set.-->

<PropertyGroup>
<BuildHash>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitDescribeOutput), @"\t|\n|\r", ""))</BuildHash>
<BuildHashClientStructs>$([System.Text.RegularExpressions.Regex]::Replace($(ClientStructsGitDescribeOutput), @"\t|\n|\r", ""))</BuildHashClientStructs>
<CommitCount>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitCommitCount), @"\t|\n|\r", ""))</CommitCount>
<CommitHash>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitCommitHash), @"\t|\n|\r", ""))</CommitHash>
<SCMVersion>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitDescribeOutput), @"\t|\n|\r", ""))</SCMVersion>
<CommitHashClientStructs>$([System.Text.RegularExpressions.Regex]::Replace($(ClientStructsGitDescribeOutput), @"\t|\n|\r", ""))</CommitHashClientStructs>
</PropertyGroup>

<!-- Looks like this is the only way to write a file without a carriage return in msbuild... -->
<Exec Command="echo|set /P =&quot;$(BuildHash)&quot; &gt; $(TempVerFile)" IgnoreExitCode="true" />
<Exec Command="echo|set /P =&quot;$(CommitCount)&quot; &gt; $(DalamudRevisionFile)" IgnoreExitCode="true" />
<Exec Command="echo|set /P =&quot;$(CommitHash)&quot; &gt; $(CommitHashFile)" IgnoreExitCode="true" />
<Exec Command="echo|set /P =&quot;$(SCMVersion)&quot; &gt; $(TempVerFile)" IgnoreExitCode="true" />
</Target>

<Target Name="GetGitHashStub" BeforeTargets="WriteGitHash" Condition="'$(BuildHash)'=='' And '$(Configuration)'=='Debug'">
<!-- Set the BuildHash property to contain some placeholder, if it wasn't already set.-->
<Target Name="GenerateStubVersionData" BeforeTargets="WriteVersionData" Condition="'$(SCMVersion)'=='' And '$(Configuration)'!='Release'">
<!-- stub out version since it takes a while. -->
<PropertyGroup>
<LocalBuildText>Local build at $([System.DateTime]::Now.ToString(yyyy-MM-dd HH:mm:ss))</LocalBuildText>
<BuildHash>$(LocalBuildText)</BuildHash>
<BuildHashClientStructs>???</BuildHashClientStructs>
<SCMVersion>Local build at $([System.DateTime]::Now.ToString(yyyy-MM-dd HH:mm:ss))</SCMVersion>
<CommitHashClientStructs>???</CommitHashClientStructs>
</PropertyGroup>

<!-- Looks like this is the only way to write a file without a carriage return in msbuild... -->
<Exec Command="echo|set /P =&quot;$(BuildHash)&quot; &gt; $(TempVerFile)" IgnoreExitCode="true" />
</Target>

<Target Name="WriteGitHash" BeforeTargets="CoreCompile">
<Target Name="WriteVersionData" BeforeTargets="CoreCompile">
<!-- names the obj/.../CustomAssemblyInfo.cs file -->
<PropertyGroup>
<CustomAssemblyInfoFile>$(IntermediateOutputPath)CustomAssemblyInfo.cs</CustomAssemblyInfoFile>
Expand All @@ -185,21 +173,21 @@
</ItemGroup>
<!-- defines the AssemblyMetadata attribute that will be written -->
<ItemGroup>
<AssemblyAttributes Include="AssemblyMetadata">
<_Parameter1>GitHash</_Parameter1>
<_Parameter2>$(BuildHash)</_Parameter2>
<AssemblyAttributes Include="AssemblyMetadata" Condition="'$(SCMVersion)' != ''">
<_Parameter1>SCMVersion</_Parameter1>
<_Parameter2>$(SCMVersion)</_Parameter2>
</AssemblyAttributes>
<AssemblyAttributes Include="AssemblyMetadata">
<AssemblyAttributes Include="AssemblyMetadata" Condition="'$(CommitCount)' != ''">
<_Parameter1>GitCommitCount</_Parameter1>
<_Parameter2>$(CommitCount)</_Parameter2>
</AssemblyAttributes>
<AssemblyAttributes Include="AssemblyMetadata">
<AssemblyAttributes Include="AssemblyMetadata" Condition="'$(CommitHashClientStructs)' != ''">
<_Parameter1>GitHashClientStructs</_Parameter1>
<_Parameter2>$(BuildHashClientStructs)</_Parameter2>
<_Parameter2>$(CommitHashClientStructs)</_Parameter2>
</AssemblyAttributes>
<AssemblyAttributes Include="AssemblyMetadata" Condition="'$(DalamudFullGitCommitHash)' != ''">
<_Parameter1>FullGitHash</_Parameter1>
<_Parameter2>$(DalamudFullGitCommitHash)</_Parameter2>
<AssemblyAttributes Include="AssemblyMetadata" Condition="'$(CommitHash)' != ''">
<_Parameter1>GitHash</_Parameter1>
<_Parameter2>$(CommitHash)</_Parameter2>
</AssemblyAttributes>
</ItemGroup>
<!-- writes the attribute to the customAssemblyInfo file -->
Expand Down
2 changes: 1 addition & 1 deletion Dalamud/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private static void RunThread(DalamudStartInfo info, IntPtr mainThreadContinueEv

var dalamud = new Dalamud(info, fs, configuration, mainThreadContinueEvent);
Log.Information("This is Dalamud - Core: {GitHash}, CS: {CsGitHash} [{CsVersion}]",
Util.GetGitHash(),
Util.GetScmVersion(),
Util.GetGitHashClientStructs(),
FFXIVClientStructs.ThisAssembly.Git.Commits);

Expand Down
2 changes: 1 addition & 1 deletion Dalamud/Interface/Internal/DalamudCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private void OnVersionInfoCommand(string command, string arguments)

chatGui.Print(new SeStringBuilder()
.AddItalics("Dalamud:")
.AddText($" D{Util.AssemblyVersion}({Util.GetGitHash()}")
.AddText($" D{Util.AssemblyVersion}({Util.GetScmVersion()}")
.Build());

chatGui.Print(new SeStringBuilder()
Expand Down
4 changes: 2 additions & 2 deletions Dalamud/Interface/Internal/DalamudInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ private void DrawDevMenu()

ImGui.MenuItem(Util.AssemblyVersion, false);
ImGui.MenuItem(this.dalamud.StartInfo.GameVersion?.ToString() ?? "Unknown version", false);
ImGui.MenuItem($"D: {Util.GetGitHash()}[{Util.GetGitCommitCount()}] CS: {Util.GetGitHashClientStructs()}[{FFXIVClientStructs.ThisAssembly.Git.Commits}]", false);
ImGui.MenuItem($"D: {Util.GetScmVersion()} CS: {Util.GetGitHashClientStructs()}[{FFXIVClientStructs.ThisAssembly.Git.Commits}]", false);
ImGui.MenuItem($"CLR: {Environment.Version}", false);

ImGui.EndMenu();
Expand Down Expand Up @@ -1020,7 +1020,7 @@ private void DrawDevMenu()
{
ImGui.PushFont(InterfaceManager.MonoFont);

ImGui.BeginMenu($"{Util.GetGitHash()}({Util.GetGitCommitCount()})", false);
ImGui.BeginMenu(Util.GetScmVersion(), false);
ImGui.BeginMenu(this.FrameCount.ToString("000000"), false);
ImGui.BeginMenu(ImGui.GetIO().Framerate.ToString("000"), false);
ImGui.BeginMenu($"W:{Util.FormatBytes(GC.GetTotalMemory(false))}", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public override void OnOpen()
return;

var versionInfo = t.Result;
if (versionInfo.AssemblyVersion != Util.GetGitHash() && versionInfo.Track != "release")
if (versionInfo.AssemblyVersion != Util.GetScmVersion() && versionInfo.Track != "release")
this.staleDalamudNewVersion = versionInfo.AssemblyVersion;
});
}
Expand Down Expand Up @@ -1538,7 +1538,7 @@ void DrawLinesCentered(string text)
DrawWarningIcon();
DrawLinesCentered("A new version of Dalamud is available.\n" +
"Please restart the game to ensure compatibility with updated plugins.\n" +
$"old: {Util.GetGitHash()} new: {this.staleDalamudNewVersion}");
$"old: {Util.GetScmVersion()} new: {this.staleDalamudNewVersion}");

ImGuiHelpers.ScaledDummy(10);
}
Expand Down
2 changes: 1 addition & 1 deletion Dalamud/Support/BugBait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task SendFeedback(IPluginManifest plugin, bool isTesting, st
Reporter = reporter,
Name = plugin.InternalName,
Version = isTesting ? plugin.TestingAssemblyVersion?.ToString() : plugin.AssemblyVersion.ToString(),
DalamudHash = Util.GetGitHash(),
DalamudHash = Util.GetScmVersion(),
};

if (includeException)
Expand Down
4 changes: 2 additions & 2 deletions Dalamud/Support/Troubleshooting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ internal static void LogTroubleshooting()
LoadedPlugins = pluginManager?.InstalledPlugins?.Select(x => x.Manifest as LocalPluginManifest)?.OrderByDescending(x => x.InternalName).ToArray(),
PluginStates = pluginManager?.InstalledPlugins?.Where(x => !x.IsDev).ToDictionary(x => x.Manifest.InternalName, x => x.IsBanned ? "Banned" : x.State.ToString()),
EverStartedLoadingPlugins = pluginManager?.InstalledPlugins.Where(x => x.HasEverStartedLoad).Select(x => x.InternalName).ToList(),
DalamudVersion = Util.AssemblyVersion,
DalamudGitHash = Util.GetGitHash(),
DalamudVersion = Util.GetScmVersion(),
DalamudGitHash = Util.GetGitHash() ?? "Unknown",
GameVersion = startInfo.GameVersion?.ToString() ?? "Unknown",
Language = startInfo.Language.ToString(),
BetaKey = configuration.DalamudBetaKey,
Expand Down
31 changes: 24 additions & 7 deletions Dalamud/Utility/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Dalamud.Utility;
public static class Util
{
private static readonly Type GenericSpanType = typeof(Span<>);
private static string? scmVersionInternal;
private static string? gitHashInternal;
private static int? gitCommitCountInternal;
private static string? gitHashClientStructsInternal;
Expand Down Expand Up @@ -105,27 +106,43 @@ public static unsafe bool FastByteArrayCompare(byte[]? a1, byte[]? a2)
}

/// <summary>
/// Gets the git hash value from the assembly
/// or null if it cannot be found.
/// Gets the SCM Version from the assembly, or null if it cannot be found. This method will generally return
/// the <c>git describe</c> output for this build, which will be a raw version if this is a stable build or an
/// appropriately-annotated version if this is *not* stable. Local builds will return a `Local Build` text string.
/// </summary>
/// <returns>The SCM version of the assembly.</returns>
public static string GetScmVersion()
{
if (scmVersionInternal != null) return scmVersionInternal;

var asm = typeof(Util).Assembly;
var attrs = asm.GetCustomAttributes<AssemblyMetadataAttribute>();

return scmVersionInternal = attrs.First(a => a.Key == "SCMVersion").Value
?? asm.GetName().Version!.ToString();
}

/// <summary>
/// Gets the git commit hash value from the assembly or null if it cannot be found. Will be null for Debug builds,
/// and will be suffixed with `-dirty` if in release with pending changes.
/// </summary>
/// <returns>The git hash of the assembly.</returns>
public static string GetGitHash()
public static string? GetGitHash()
{
if (gitHashInternal != null)
return gitHashInternal;

var asm = typeof(Util).Assembly;
var attrs = asm.GetCustomAttributes<AssemblyMetadataAttribute>();

gitHashInternal = attrs.First(a => a.Key == "GitHash").Value;

return gitHashInternal;
return gitHashInternal = attrs.First(a => a.Key == "GitHash").Value;
}

/// <summary>
/// Gets the amount of commits in the current branch, or null if undetermined.
/// </summary>
/// <returns>The amount of commits in the current branch.</returns>
[Obsolete($"Planned for removal in API 11. Use {nameof(GetScmVersion)} for version tracking.")]
public static int? GetGitCommitCount()
{
if (gitCommitCountInternal != null)
Expand All @@ -147,7 +164,7 @@ public static string GetGitHash()
/// or null if it cannot be found.
/// </summary>
/// <returns>The git hash of the assembly.</returns>
public static string GetGitHashClientStructs()
public static string? GetGitHashClientStructs()
{
if (gitHashClientStructsInternal != null)
return gitHashClientStructsInternal;
Expand Down

0 comments on commit 897b256

Please sign in to comment.