Update BuildGeneratorEditorWindow.cs

This commit is contained in:
Syntriax 2025-01-11 17:58:31 +03:00
parent 883472fd3d
commit 07b34535fb

View File

@ -1,13 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
public class BuildGeneratorEditorWindow : EditorWindow public class BuildGeneratorEditorWindow : EditorWindow
{ {
private VersionDefinition versionDefinition = default; private VersionDefinition editorVersion = default;
private VersionDefinition gitVersion = default;
[MenuItem("Window/Build Generator")] [MenuItem("Window/Build Generator")]
private static void ShowWindow() private static void ShowWindow()
@ -15,45 +17,80 @@ public class BuildGeneratorEditorWindow : EditorWindow
BuildGeneratorEditorWindow window = GetWindow<BuildGeneratorEditorWindow>(); BuildGeneratorEditorWindow window = GetWindow<BuildGeneratorEditorWindow>();
window.titleContent = new GUIContent("Build Generator"); window.titleContent = new GUIContent("Build Generator");
window.versionDefinition = new(PlayerSettings.bundleVersion); window.editorVersion = new(PlayerSettings.bundleVersion);
try { window.gitVersion = GitProcess.GetLatestBuildVersion(); } catch { }
window.Show(); window.Show();
} }
private void OnGUI() private void OnGUI()
{ {
EditorGUILayout.LabelField($"Current Version: {versionDefinition.Version}"); if (GUILayout.Button($"Re-evaluate versions"))
EditorGUILayout.LabelField($"Build Number: {versionDefinition.BuildNumber}"); {
editorVersion = new(PlayerSettings.bundleVersion);
gitVersion = GitProcess.GetLatestBuildVersion();
}
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
if (GUILayout.Button($"Create Release")) CommitVersion(GitProcess.GetUpcomingReleaseVersion());
if (GUILayout.Button($"Create Release Candidate")) CommitVersion(GitProcess.GetUpcomingReleaseCandidateVersion());
EditorGUILayout.EndVertical();
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField($"Editor Version: {editorVersion} ({editorVersion.BuildNumber})");
EditorGUILayout.LabelField($"Git Version: {editorVersion} ({editorVersion.BuildNumber})");
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
if (GUILayout.Button($"Push All")) { GitProcess.Push(); GitProcess.PushTags(); }
// EditorGUILayout.BeginHorizontal(); // EditorGUILayout.BeginHorizontal();
// if (IncrementButton("Major", versionDefinition.IncreaseMajor())) // if (IncrementButton("Major", versionDefinition.IncreaseMajor()))
// ApplyVersion(versionDefinition.IncreaseMajor()); // CommitVersion(versionDefinition.IncreaseMajor());
// if (IncrementButton("Minor", versionDefinition.IncreaseMinor())) // if (IncrementButton("Minor", versionDefinition.IncreaseMinor()))
// ApplyVersion(versionDefinition.IncreaseMinor()); // CommitVersion(versionDefinition.IncreaseMinor());
// if (IncrementButton("Patch", versionDefinition.IncreasePatch())) // if (IncrementButton("Patch", versionDefinition.IncreasePatch()))
// ApplyVersion(versionDefinition.IncreasePatch()); // CommitVersion(versionDefinition.IncreasePatch());
// if (IncrementButton("Release Candidate", versionDefinition.IncreaseReleaseCandidate())) // if (IncrementButton("Release Candidate", versionDefinition.IncreaseReleaseCandidate()))
// ApplyVersion(versionDefinition.IncreaseReleaseCandidate()); // CommitVersion(versionDefinition.IncreaseReleaseCandidate());
// EditorGUILayout.EndHorizontal(); // EditorGUILayout.EndHorizontal();
if (GUILayout.Button($"Evaluate Version from git")) versionDefinition = GitProcess.GetUpcomingReleaseCandidateVersion();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button($"Commit Release Candidate")) CommitVersion(GitProcess.GetUpcomingReleaseCandidateVersion());
if (GUILayout.Button($"Commit Release")) CommitVersion(GitProcess.GetUpcomingReleaseVersion());
EditorGUILayout.EndHorizontal();
} }
private void CommitVersion(VersionDefinition versionDefinition) private void CommitVersion(VersionDefinition versionDefinition)
{ {
if (this.editorVersion >= versionDefinition)
{
EditorUtility.DisplayDialog(
"Release Conflict",
$"You can't create a new version because the old version({this.editorVersion}) is either higher or the same as the new version({versionDefinition}). Please make new commits to create new releases.",
"Return");
return;
}
if (EditorUtility.DisplayDialog(
"Release Confirmation",
$"Are you sure to commit a new incremental version of {versionDefinition}?",
"Yes",
"Cancel")
)
return;
ApplyVersion(versionDefinition); ApplyVersion(versionDefinition);
GitProcess.Add("ProjectSettings\\ProjectSettings.asset"); GitProcess.Add("ProjectSettings\\ProjectSettings.asset");
GitProcess.Add("Assets\\Settings\\Build Profiles\\**"); GitProcess.Add("Assets\\Settings\\Build Profiles\\**");
GitProcess.Commit($"chore: Bump Version to {versionDefinition}"); GitProcess.Commit($"chore: Bump Version to {versionDefinition}");
GitProcess.CreateVersionTag(versionDefinition); GitProcess.CreateTag(versionDefinition);
GitProcess.PushTag(versionDefinition);
} }
private bool IncrementButton(string fieldName, VersionDefinition resultDefinition) private bool IncrementButton(string fieldName, VersionDefinition resultDefinition)
@ -61,7 +98,7 @@ public class BuildGeneratorEditorWindow : EditorWindow
EditorGUILayout.BeginVertical(); EditorGUILayout.BeginVertical();
bool isButtonPressed = GUILayout.Button($"Add Incremental {fieldName}"); bool isButtonPressed = GUILayout.Button($"Add Incremental {fieldName}");
EditorGUILayout.LabelField($"{versionDefinition} -> {resultDefinition}", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter }); EditorGUILayout.LabelField($"{editorVersion} -> {resultDefinition}", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter });
EditorGUILayout.EndVertical(); EditorGUILayout.EndVertical();
@ -70,7 +107,7 @@ public class BuildGeneratorEditorWindow : EditorWindow
private void ApplyVersion(VersionDefinition versionDefinition) private void ApplyVersion(VersionDefinition versionDefinition)
{ {
this.versionDefinition = this.versionDefinition >= versionDefinition ? this.versionDefinition : versionDefinition; this.editorVersion = versionDefinition;
PlayerSettings.Android.bundleVersionCode = versionDefinition.BuildNumber; PlayerSettings.Android.bundleVersionCode = versionDefinition.BuildNumber;
PlayerSettings.iOS.buildNumber = versionDefinition.BuildNumber.ToString(); PlayerSettings.iOS.buildNumber = versionDefinition.BuildNumber.ToString();
PlayerSettings.bundleVersion = versionDefinition.ToString(); PlayerSettings.bundleVersion = versionDefinition.ToString();
@ -79,27 +116,28 @@ public class BuildGeneratorEditorWindow : EditorWindow
private struct VersionDefinition private struct VersionDefinition
{ {
public const uint MAX_VALUE = 99; public const int MAX_VALUE = 99;
public const string BUILD_NUMBER_FORMAT = "00";
public const uint DEFAULT_MAJOR = 0; public const int DEFAULT_MAJOR = 0;
public const uint DEFAULT_MINOR = 0; public const int DEFAULT_MINOR = 0;
public const uint DEFAULT_PATCH = 0; public const int DEFAULT_PATCH = 0;
public const uint DEFAULT_RELEASE_CANDIDATE = 1; public const int DEFAULT_RELEASE_CANDIDATE = 1;
public const uint RELEASE_RC_VALUE = 99; public const int RELEASE_RC_VALUE = 99;
private readonly uint _major; private readonly int _major;
private readonly uint _minor; private readonly int _minor;
private readonly uint _patch; private readonly int _patch;
private readonly uint _releaseCandidate; private readonly int _releaseCandidate;
public readonly uint Major => _major > MAX_VALUE ? MAX_VALUE : _major; public readonly int Major => _major > MAX_VALUE ? MAX_VALUE : _major;
public readonly uint Minor => _minor > MAX_VALUE ? MAX_VALUE : _minor; public readonly int Minor => _minor > MAX_VALUE ? MAX_VALUE : _minor;
public readonly uint Patch => _patch > MAX_VALUE ? MAX_VALUE : _patch; public readonly int Patch => _patch > MAX_VALUE ? MAX_VALUE : _patch;
public readonly uint ReleaseCandidate => _releaseCandidate > MAX_VALUE ? MAX_VALUE : _releaseCandidate; public readonly int ReleaseCandidate => _releaseCandidate > MAX_VALUE ? MAX_VALUE : _releaseCandidate;
public readonly bool IsRelease => ReleaseCandidate == RELEASE_RC_VALUE; public readonly bool IsRelease => ReleaseCandidate == RELEASE_RC_VALUE;
public VersionDefinition(uint? major = null, uint? minor = null, uint? patch = null, uint? releaseCandidate = null) public VersionDefinition(int? major = null, int? minor = null, int? patch = null, int? releaseCandidate = null)
{ {
_major = major ?? DEFAULT_MAJOR; _major = major ?? DEFAULT_MAJOR;
_minor = minor ?? DEFAULT_MINOR; _minor = minor ?? DEFAULT_MINOR;
@ -107,7 +145,7 @@ public class BuildGeneratorEditorWindow : EditorWindow
_releaseCandidate = releaseCandidate ?? DEFAULT_RELEASE_CANDIDATE; _releaseCandidate = releaseCandidate ?? DEFAULT_RELEASE_CANDIDATE;
if (_major == 0 && _minor == 0) if (_major == 0 && _minor == 0)
_minor = 1; _minor = _releaseCandidate = 1;
} }
public VersionDefinition(string versionString) public VersionDefinition(string versionString)
@ -123,13 +161,13 @@ public class BuildGeneratorEditorWindow : EditorWindow
string[] releaseCandidateVersionStrings = versionString.Split("-rc"); string[] releaseCandidateVersionStrings = versionString.Split("-rc");
string[] versionNumbers = releaseCandidateVersionStrings[0].Split('.'); string[] versionNumbers = releaseCandidateVersionStrings[0].Split('.');
if (versionNumbers.Length > 0 && uint.TryParse(versionNumbers[0], out uint major)) _major = major; if (versionNumbers.Length > 0 && int.TryParse(versionNumbers[0], out int major)) _major = major;
if (versionNumbers.Length > 1 && uint.TryParse(versionNumbers[1], out uint minor)) _minor = minor; if (versionNumbers.Length > 1 && int.TryParse(versionNumbers[1], out int minor)) _minor = minor;
if (versionNumbers.Length > 2 && uint.TryParse(versionNumbers[2], out uint patch)) _patch = patch; if (versionNumbers.Length > 2 && int.TryParse(versionNumbers[2], out int patch)) _patch = patch;
if (releaseCandidateVersionStrings.Length > 1 && uint.TryParse(releaseCandidateVersionStrings[1], out uint releaseCandidate)) _releaseCandidate = releaseCandidate; if (releaseCandidateVersionStrings.Length > 1 && int.TryParse(releaseCandidateVersionStrings[1], out int releaseCandidate)) _releaseCandidate = releaseCandidate;
if (Major == 0 && Minor == 0) if (_major == 0 && _minor == 0)
Minor = ReleaseCandidate = 1; _minor = _releaseCandidate = 1;
} }
public readonly VersionDefinition IncreaseMajor() => new(Major + 1, DEFAULT_MINOR, DEFAULT_PATCH, DEFAULT_RELEASE_CANDIDATE); public readonly VersionDefinition IncreaseMajor() => new(Major + 1, DEFAULT_MINOR, DEFAULT_PATCH, DEFAULT_RELEASE_CANDIDATE);
@ -138,7 +176,7 @@ public class BuildGeneratorEditorWindow : EditorWindow
public readonly VersionDefinition IncreaseReleaseCandidate() => new(Major, Minor, Patch, ReleaseCandidate + 1); public readonly VersionDefinition IncreaseReleaseCandidate() => new(Major, Minor, Patch, ReleaseCandidate + 1);
public readonly VersionDefinition ToReleaseVersion() => new(Major, Minor, Patch, RELEASE_RC_VALUE); public readonly VersionDefinition ToReleaseVersion() => new(Major, Minor, Patch, RELEASE_RC_VALUE);
public readonly uint BuildNumber => uint.Parse($"{Major:00}{Minor:00}{Patch:00}{ReleaseCandidate:00}"); public readonly int BuildNumber => int.Parse($"{Major.ToString(BUILD_NUMBER_FORMAT)}{Minor.ToString(BUILD_NUMBER_FORMAT)}{Patch.ToString(BUILD_NUMBER_FORMAT)}{ReleaseCandidate.ToString(BUILD_NUMBER_FORMAT)}");
public override readonly string ToString() public override readonly string ToString()
{ {
@ -153,6 +191,12 @@ public class BuildGeneratorEditorWindow : EditorWindow
public static bool operator <=(VersionDefinition left, VersionDefinition right) => left.BuildNumber <= right.BuildNumber; public static bool operator <=(VersionDefinition left, VersionDefinition right) => left.BuildNumber <= right.BuildNumber;
public static bool operator ==(VersionDefinition left, VersionDefinition right) => left.BuildNumber == right.BuildNumber; public static bool operator ==(VersionDefinition left, VersionDefinition right) => left.BuildNumber == right.BuildNumber;
public static bool operator !=(VersionDefinition left, VersionDefinition right) => left.BuildNumber != right.BuildNumber; public static bool operator !=(VersionDefinition left, VersionDefinition right) => left.BuildNumber != right.BuildNumber;
public override readonly bool Equals(object obj)
=> obj is VersionDefinition versionDefinition && BuildNumber == versionDefinition.BuildNumber;
public override readonly int GetHashCode()
=> HashCode.Combine(Major, Minor, Patch, ReleaseCandidate);
} }
private static class GitProcess private static class GitProcess
@ -219,8 +263,8 @@ public class BuildGeneratorEditorWindow : EditorWindow
public static string[] GetCommitsSinceLastTag() public static string[] GetCommitsSinceLastTag()
=> RunGitCommand($"log {GetLatestBuildVersionString()}..HEAD --oneline").Replace("\r", "").Split('\n'); => RunGitCommand($"log {GetLatestBuildVersionString()}..HEAD --oneline").Replace("\r", "").Split('\n');
public static string CreateVersionTag(VersionDefinition versionDefinition) public static string CreateTag(VersionDefinition versionDefinition)
=> CreateTag($"v{versionDefinition.Version}", $"{(versionDefinition.IsRelease ? "Release" : "Build")} {versionDefinition.Version} with build number: {versionDefinition.BuildNumber}\""); => CreateTag($"v{versionDefinition}", $"{(versionDefinition.IsRelease ? "Release" : "Build")} {versionDefinition} with build number: {versionDefinition.BuildNumber}\"");
public static string Push() public static string Push()
=> RunGitCommand($"push"); => RunGitCommand($"push");
@ -235,10 +279,10 @@ public class BuildGeneratorEditorWindow : EditorWindow
=> RunGitCommand($"commit -m \"{message}\""); => RunGitCommand($"commit -m \"{message}\"");
public static string PushTags() public static string PushTags()
=> RunGitCommand($"tag --push"); => RunGitCommand($"push --tags");
public static string PushVersionTag(VersionDefinition versionDefinition) public static string PushTag(VersionDefinition versionDefinition, string remote = "origin")
=> RunGitCommand($"push origin tag v{versionDefinition.Version}"); => RunGitCommand($"push {remote} tag v{versionDefinition}");
public static string CreateTag(string title, string message) public static string CreateTag(string title, string message)
=> RunGitCommand($"tag -a {title} -m \"{message}"); => RunGitCommand($"tag -a {title} -m \"{message}");