Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktHensen committed Jan 14, 2021
2 parents 59268ed + 01b9efa commit a464961
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 48 deletions.
40 changes: 23 additions & 17 deletions Editor/Version Tool/GitVersionBuildStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,40 @@ private string ReplaceBranchPlaceholder(string versionString)
/// e.g. for UWP builds
/// The version is extracted from the version string
/// </summary>
/// <param name="versionString">The version string on which the version should be based</param>
/// <returns>Returns the version for the WSA packages</returns>
public Version WSAVersion(string versionString)
public Version WSAVersion
{
Regex rgx = new Regex("[^0-9.]");
versionString = rgx.Replace(versionString, "");
if (Version.TryParse(versionString, out Version result))
get
{
int major = Mathf.Max(0, result.Major);
int minor = Mathf.Max(0, result.Minor);
int build = Mathf.Max(0, result.Build);
return new Version(major, minor, build, 0);
}
else
{
return new Version(0, 0, 1, 0);
Regex rgx = new Regex("[^0-9.]");
gitVersion.TryGetVersion(out string versionString);
versionString = rgx.Replace(versionString, "");
if (Version.TryParse(versionString, out Version result))
{
int major = Mathf.Max(0, result.Major);
int minor = Mathf.Max(0, result.Minor);
int build = Mathf.Max(0, result.Build);
return new Version(major, minor, build, 0);
}
else
{
return new Version(0, 0, 1, 0);
}
}
}

/// <summary>
/// Calculates the version for Android installation packages
/// This value is based on the number of commits in git on this branch
/// </summary>
/// <returns>Returns an integer number that is increased with each git commit</returns>
public int AndroidVersion()
/// <returns>Returns an integer number that is increased with each git commit</returns>
public int AndroidVersion
{
gitVersion.TryGetTotalCommitsOnBranch(out int commitCount);
return commitCount;
get
{
gitVersion.TryGetTotalCommitsOnBranch(out int commitCount);
return commitCount;
}
}
}
}
14 changes: 14 additions & 0 deletions Editor/Version Tool/PostBuildCleanup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,31 @@

namespace i5.Toolkit.Core.VersionTool
{
/// <summary>
/// Clean up step that is executed after the build
/// Resets version values which have been altered by the version tool
/// This happens so that version control does not detect changes after the build
/// </summary>
public class PostBuildVersionCleanup : IPostprocessBuildWithReport
{
/// <summary>
/// The position in the execution order in which this script is executed after the build
/// </summary>
public int callbackOrder => 0;

/// <summary>
/// Called once the build has finished
/// Restores version project settings that have been altered by the version tool
/// </summary>
/// <param name="report">A report about the build</param>
public void OnPostprocessBuild(BuildReport report)
{
RestoreVersions();
Debug.Log($"[{GitVersionBuildStep.toolName}] Removing temporary cache");
VersionCache.Remove();
}

// restores the versions
private void RestoreVersions()
{
VersionCache cache = VersionCache.Load();
Expand Down
17 changes: 14 additions & 3 deletions Editor/Version Tool/PreBuildVersioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,27 @@ public void OnPreprocessBuild(BuildReport report)
{
versionString = buildStep.ReplacePlaceholders(versionString);

PlayerSettings.bundleVersion = versionString;
PlayerSettings.WSA.packageVersion = buildStep.WSAVersion(versionString);
PlayerSettings.Android.bundleVersionCode = buildStep.AndroidVersion();
switch (report.summary.platformGroup)
{
case BuildTargetGroup.Standalone:
PlayerSettings.bundleVersion = versionString;
break;
case BuildTargetGroup.WSA:
PlayerSettings.WSA.packageVersion = buildStep.WSAVersion;
break;
case BuildTargetGroup.Android:
PlayerSettings.Android.bundleVersionCode = buildStep.AndroidVersion;
break;
}
}
else
{
Debug.Log($"[{GitVersionBuildStep.toolName}] Version placeholders not found. To use automatic semantic versioning with Git, add a placeholder to the application's version string");
}
}

// caches the project's original version configuration
// so that it can be restored after the build
private void CacheVersionConfig()
{
Debug.Log($"[{GitVersionBuildStep.toolName}] Caching version config:\n{PlayerSettings.bundleVersion}\n{PlayerSettings.WSA.packageVersion}\n{PlayerSettings.Android.bundleVersionCode}");
Expand Down
26 changes: 26 additions & 0 deletions Editor/Version Tool/VersionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,47 @@

namespace i5.Toolkit.Core.VersionTool
{
/// <summary>
/// Cache for the version settings so that they can be restored after the build
/// </summary>
[Serializable]
public class VersionCache
{
/// <summary>
/// Version string which is used on many platforms, e.g. Standalone
/// </summary>
public string appVersion;
/// <summary>
/// Version for WSA apps
/// </summary>
public Version wsaVersion;
/// <summary>
/// Android version number
/// </summary>
public int androidVersion;

// Unity cannot serialize the Version object,
// so it is first converted to a string that is serialized
[SerializeField] private string strWsaVersion;

// path where the cache is stored
private const string savePath = "Temp/VersionCache.tmp";

/// <summary>
/// Saves the cache as a temporary file in the project's Temp folder
/// </summary>
public void Save()
{
strWsaVersion = wsaVersion.ToString();
string json = JsonUtility.ToJson(this);
File.WriteAllText(savePath, json);
}

/// <summary>
/// Loads the cache if it exists
/// If no cache file exists, default values are loaded
/// </summary>
/// <returns>Returns the loaded version cache object</returns>
public static VersionCache Load()
{
if (!File.Exists(savePath))
Expand All @@ -34,6 +57,9 @@ public static VersionCache Load()
return versionCache;
}

/// <summary>
/// Clears the cache by deleting the temporary file
/// </summary>
public static void Remove()
{
File.Delete(savePath);
Expand Down
34 changes: 6 additions & 28 deletions Tests/Editor/Version Tool/GitVersionBuildStepTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,41 +138,19 @@ public void ReplacePlaceholders_ContainsVersionAndBranch_ReplacesBoth()
public void WSAVersion_IsolatedVersionGiven_ReturnsVersion()
{
GitVersionBuildStep buildStep = CreateGitVersionBuildStep(out IGitVersionCalculator versionCalculator);
string ignored = null;
A.CallTo(() => versionCalculator.TryGetVersion(out ignored))
.Returns(true)
.AssignsOutAndRefParameters("1.2.3");

Version result = buildStep.WSAVersion("1.2.3");

Assert.AreEqual(1, result.Major);
Assert.AreEqual(2, result.Minor);
Assert.AreEqual(3, result.Build);
Assert.AreEqual(0, result.Revision);
}

[Test]
public void WSAVersion_InfixVersionGiven_ReturnsVersion()
{
GitVersionBuildStep buildStep = CreateGitVersionBuildStep(out IGitVersionCalculator versionCalculator);

Version result = buildStep.WSAVersion("prefix1.2.3postfix");
Version result = buildStep.WSAVersion;

Assert.AreEqual(1, result.Major);
Assert.AreEqual(2, result.Minor);
Assert.AreEqual(3, result.Build);
Assert.AreEqual(0, result.Revision);
}

[Test]
public void WSAVersion_InvalidString_ReturnsDefault()
{
GitVersionBuildStep buildStep = CreateGitVersionBuildStep(out IGitVersionCalculator versionCalculator);

Version result = buildStep.WSAVersion("no-version");

Assert.AreEqual(0, result.Major);
Assert.AreEqual(0, result.Minor);
Assert.AreEqual(1, result.Build);
Assert.AreEqual(0, result.Revision);
}

[Test]
public void AndroidVersion_UsesCommitCount()
{
Expand All @@ -184,7 +162,7 @@ public void AndroidVersion_UsesCommitCount()
.Returns(true)
.AssignsOutAndRefParameters(123);

int result = buildStep.AndroidVersion();
int result = buildStep.AndroidVersion;

Assert.AreEqual(123, result);
}
Expand Down

0 comments on commit a464961

Please sign in to comment.