Skip to content

Commit

Permalink
Merge pull request #299 from achimmihca/master
Browse files Browse the repository at this point in the history
fix BuildInfoGenerator: wrong version number and missing commit hash
  • Loading branch information
achimmihca authored May 11, 2022
2 parents 16a6378 + 027d8bb commit 0b25653
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-companion-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
workflow_dispatch: {}

env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
Expand Down Expand Up @@ -31,7 +32,6 @@ jobs:
with:
fetch-depth: 0
lfs: true
submodules: recursive
- uses: actions/cache@v2
with:
path: ${{ env.PROJECT_PATH }}/Library
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-main-game.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
workflow_dispatch: {}

env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
Expand Down Expand Up @@ -31,7 +32,6 @@ jobs:
with:
fetch-depth: 0
lfs: true
submodules: recursive
- uses: actions/cache@v2
with:
path: ${{ env.PROJECT_PATH }}/Library
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-companion-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
pull_request:
branches:
- master
workflow_dispatch: {}

env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
Expand All @@ -30,7 +31,6 @@ jobs:
with:
fetch-depth: 0
lfs: true
submodules: recursive
- uses: actions/cache@v2
with:
path: ${{ env.PROJECT_PATH }}/Library
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-main-game.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
pull_request:
branches:
- master
workflow_dispatch: {}

env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
Expand All @@ -30,7 +31,6 @@ jobs:
with:
fetch-depth: 0
lfs: true
submodules: recursive
- uses: actions/cache@v2
with:
path: ${{ env.PROJECT_PATH }}/Library
Expand Down
58 changes: 39 additions & 19 deletions UltraStar Play/Packages/playshared/Editor/BuildInfoGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

// Fills a file with build information before Unity performs the actual build.
public class BuildInfoGenerator : IPreprocessBuildWithReport
{
public static readonly string versionPropertyName = "release";
public static readonly string timeStampPropertyName = "build_timestamp";
public static readonly string commitShortHashPropertyName = "commit_hash";
public static readonly string versionFile = "Assets/VERSION.txt";
private static readonly string versionPropertyName = "release";
private static readonly string timeStampPropertyName = "build_timestamp";
private static readonly string commitShortHashPropertyName = "commit_hash";
private static readonly string versionFile = "Assets/VERSION.txt";

public int callbackOrder
{
Expand All @@ -26,32 +29,49 @@ public void OnPreprocessBuild(BuildReport report)
UpdateVersionFile();
}

private void UpdateVersionFile()
private static void UpdateVersionFile()
{
string bundleVersion = GetPlayerSettingsFileBundleVersion();
string timeStamp = DateTime.Now.ToString("yyMMddHHmm", CultureInfo.InvariantCulture);
string commitShortHash = GitUtils.GetCurrentCommitShortHash();


Dictionary<string, string> propertyNameToValueMap = new();
propertyNameToValueMap.Add(versionPropertyName, bundleVersion);
propertyNameToValueMap.Add(timeStampPropertyName, timeStamp);
propertyNameToValueMap.Add(commitShortHashPropertyName, commitShortHash);

Debug.Log($"Updating {versionFile} with {JsonConverter.ToJson(propertyNameToValueMap)}");

string[] versionFileLines = File.ReadAllLines(versionFile);
for (int i = 0; i < versionFileLines.Length; i++)
{
string line = versionFileLines[i];
if (line.StartsWith(versionPropertyName, true, CultureInfo.InvariantCulture))
{
versionFileLines[i] = $"{versionPropertyName} = {PlayerSettings.bundleVersion}";
}

if (line.StartsWith(timeStampPropertyName, true, CultureInfo.InvariantCulture))
{
versionFileLines[i] = $"{timeStampPropertyName} = {timeStamp}";
}

if (line.StartsWith(commitShortHashPropertyName, true, CultureInfo.InvariantCulture))
propertyNameToValueMap.ForEach(entry =>
{
versionFileLines[i] = $"{commitShortHashPropertyName} = {commitShortHash}";
}
string propertyName = entry.Key;
string propertyValue = entry.Value;
if (line.StartsWith(entry.Key, true, CultureInfo.InvariantCulture))
{
versionFileLines[i] = $"{propertyName} = {propertyValue}";
}
});
}
File.WriteAllLines(versionFile, versionFileLines);

Debug.Log($"New contents of {versionFile}:\n{versionFileLines.ToCsv(", ")}");

// Unity needs a hint that this asset has changed.
AssetDatabase.ImportAsset(versionFile);
}

private static string GetPlayerSettingsFileBundleVersion()
{
// Return the value from the file because the C# API (PlayerSettings.bundleVersion) returns an older value
// during the GitHub Actions build for whatever reason.
string[] projectSettingsAssetLines = File.ReadAllLines("ProjectSettings/ProjectSettings.asset");
string bundleVersionLine = projectSettingsAssetLines.FirstOrDefault(line => line.Contains("bundleVersion:"));
string bundleVersion = bundleVersionLine.Replace("bundleVersion:", "").Trim();
Debug.Log($"bundleVersion from ProjectSettings/ProjectSettings.asset: {bundleVersion}");
return bundleVersion;
}
}
57 changes: 49 additions & 8 deletions UltraStar Play/Packages/playshared/Editor/GitUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,57 @@ public static string RunGitCommand(string gitCommand, bool throwOnFailure = true

public static string GetCurrentCommitShortHash()
{
string result = RunGitCommand("rev-parse --short --verify HEAD", false);
if (result.IsNullOrEmpty())
return GetMasterBranchCommitShortHash();

// TODO: running Git command to get the commit hash of the current branch fails with "fatal: unsafe repository ('/github/workspace' is owned by someone else)"
// string result = RunGitCommand("rev-parse --short --verify HEAD");

// Clean up whitespace around hash. (seems to just be the way this command returns :/ )
// commitShortHash = string.Join("", commitShortHash.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
// Debug.Log("Current commit short hash: " + commitShortHash);
// return commitShortHash;
}

private static string GetMasterBranchCommitShortHash()
{
string gitFolderPath = GetGitFolder();
if (gitFolderPath.IsNullOrEmpty())
{
// Failed to get commit hash from Git.
return "???";
throw new BuildFailedException("No .git folder found");
}

// Clean up whitespace around hash. (seems to just be the way this command returns :/ )
result = string.Join("", result.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
Debug.Log("Current commit short hash: " + result);
return result;
// Get commit hash from .git/refs/heads/master
string commitHashFile = gitFolderPath + "/refs/heads/master";
if (!File.Exists(commitHashFile))
{
throw new BuildFailedException($"No file with commit hash found inside .git folder (tried file path: {commitHashFile})");
}

Debug.Log($"commitHashFile: {commitHashFile}");
string commitHash = File.ReadAllText(commitHashFile);
Debug.Log($"commitHash: {commitHash}");
string commitShortHash = commitHash[..8];
Debug.Log($"commitShortHash: {commitShortHash}");
return commitShortHash;
}

private static string GetGitFolder()
{
Debug.Log("Searching .git folder");
string currentDirectory = Directory.GetCurrentDirectory();
do
{
Debug.Log($"currentDirectory: {currentDirectory}");
if (Directory.Exists(currentDirectory + "/.git"))
{
string gitFolder = currentDirectory + "/.git";
Debug.Log($"Found .git folder: {gitFolder}");
return gitFolder;
}
DirectoryInfo parentDirectory = Directory.GetParent(currentDirectory);
currentDirectory = parentDirectory?.FullName;
} while (!currentDirectory.IsNullOrEmpty() && Directory.Exists(currentDirectory));

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
}
}

public static string ToCsv<T>(this IEnumerable<T> enumerable, string separator = ",", string prefix = "[", string suffix = "]")
public static string JoinWith<T>(this IEnumerable<T> enumerable, string separator, string prefix = "", string suffix = "")
{
return prefix + string.Join(separator, enumerable) + suffix;
}

public static string ToCsv<T>(this IEnumerable<T> enumerable, string separator = ",", string prefix = "[", string suffix = "]")
{
return enumerable.JoinWith(separator, prefix, suffix);
}

public static void AddIfNotContains<T>(this ICollection<T> collection, T item)
{
if (!collection.Contains(item))
Expand Down

0 comments on commit 0b25653

Please sign in to comment.