Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show git commit+branch for debug builds #885

Merged
merged 3 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,4 @@ ASALocalRun/
Test/Test - Backup.csproj
UndertaleModLib/UndertaleModLib - Backup.csproj
UndertaleModTests/UndertaleModTests - Backup.csproj
UndertaleModLib/gitversion.txt
13 changes: 13 additions & 0 deletions UndertaleModLib/UndertaleModLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>
<ItemGroup>
<None Remove="version.txt" />
</ItemGroup>
<ItemGroup>
<None Remove="gitversion.txt" />
<EmbeddedResource Include="gitversion.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<!-- puts the git commit name and branch name into gitversion.txt which is an embedded resource -->
<Exec Command="( git describe --always --dirty &amp;&amp; git rev-parse --abbrev-ref HEAD ) &gt; &quot;$(ProjectDir)/gitversion.txt&quot;" />
</Target>
</Project>
50 changes: 50 additions & 0 deletions UndertaleModLib/Util/GitVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;

namespace UndertaleModLib.Util;

/// <summary>
/// Includes miscellaneous git information about the project to compile.
/// <b>Only intended for Debug use!</b>
/// </summary>
public static class GitVersion
{
/// <summary>
/// Gets and returns the git commit and branch name.
/// </summary>
/// <returns>The git commit and branch name.</returns>
public static string GetGitVersion()
{
string gitOutput = "";

// try to access the embedded resource
try
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "UndertaleModLib.gitversion.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
// \r is getting nuked just in case Windows is weird.
gitOutput = reader.ReadToEnd().Trim().Replace("\r", "");
}

// gets formatted as "<commit> (<branch>)"
var outputAsArray = gitOutput.Split('\n');
gitOutput = $"{outputAsArray[0]} ({outputAsArray[1]})";
}
// If accessing it fails, give it a default output
catch
{
gitOutput = "unavailable";
}

// return combined commit + branch
if (String.IsNullOrWhiteSpace(gitOutput)) gitOutput = "unavailable";
return gitOutput;
}
}
22 changes: 17 additions & 5 deletions UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using UndertaleModLib.Models;
using UndertaleModLib.ModelsDebug;
using UndertaleModLib.Scripting;
using UndertaleModLib.Util;
using UndertaleModTool.Windows;
using System.IO.Pipes;
using Ookii.Dialogs.Wpf;
Expand Down Expand Up @@ -93,7 +94,7 @@ public static string GetTitleForObject(object obj)
else if (obj is UndertaleNamedResource namedRes)
{
string content = namedRes.Name?.Content;

string header = obj switch
{
UndertaleAudioGroup => "Audio Group",
Expand Down Expand Up @@ -301,7 +302,13 @@ public bool RoomRendererEnabled

// Version info
public static string Edition = "";

// On debug, build with git versions. Otherwise, use the provided release version.
#if DEBUG
public static string Version = GitVersion.GetGitVersion();
#else
public static string Version = Assembly.GetExecutingAssembly().GetName().Version.ToString() + (Edition != "" ? "-" + Edition : "");
#endif

public MainWindow()
{
Expand All @@ -313,7 +320,7 @@ public MainWindow()
SelectionHistory.Clear();
ClosedTabsHistory.Clear();

TitleMain = "UndertaleModTool by krzys_h v" + Version;
TitleMain = "UndertaleModTool by krzys_h v:" + Version;

CanSave = false;
CanSafelySave = false;
Expand Down Expand Up @@ -1732,7 +1739,7 @@ private void MenuItem_Delete_Click(object sender, RoutedEventArgs e)
if (Highlighted is UndertaleObject obj)
DeleteItem(obj);
}

private void MenuItem_Add_Click(object sender, RoutedEventArgs e)
{
object source = null;
Expand Down Expand Up @@ -2517,7 +2524,12 @@ public async void UpdateApp(SettingsWindow window)
httpClient = new();
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("UndertaleModTool", Version));

// remove the invalid characters (everything within square brackets) from the version string.
// Probably needs to be expanded later, these are just the ones I know of.
Regex invalidChars = new Regex(@"[ ()]");
string version = invalidChars.Replace(Version, "");
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("UndertaleModTool", version));

double bytesToMB = 1024 * 1024;

Expand Down Expand Up @@ -3231,7 +3243,7 @@ public void CloseTab(int tabIndex, bool addDefaultTab = true)
Tabs[i].TabIndex = i;

// if closing the currently open tab
if (currIndex == tabIndex)
if (currIndex == tabIndex)
{
// and if that tab is not the last
if (Tabs.Count > 1 && tabIndex < Tabs.Count - 1)
Expand Down