mirror of
https://github.com/game-ci/unity-builder.git
synced 2025-07-04 12:25:19 -04:00
Add semantic versioning logic
This commit is contained in:
parent
328b0d8ac0
commit
a513e5b640
@ -57,6 +57,10 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Assets\Editor\Builder.cs" />
|
||||
<Compile Include="Assets\Editor\System\ProcessExtensions.cs" />
|
||||
<Compile Include="Assets\Editor\Versioning\Git.cs" />
|
||||
<Compile Include="Assets\Editor\Versioning\VersionGenerator.cs" />
|
||||
<Compile Include="Assets\Editor\Versioning\GitException.cs" />
|
||||
<Reference Include="UnityEditor.TestRunner">
|
||||
<HintPath>C:/Repositories/unity-builder/builder/default-build-script/Library/ScriptAssemblies/UnityEditor.TestRunner.dll</HintPath>
|
||||
</Reference>
|
||||
@ -688,6 +692,9 @@
|
||||
<HintPath>C:/Program Files/Unity/2019.2.11f1/Editor/Data/MonoBleedingEdge/lib/mono/unityscript/Boo.Lang.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include=".editorconfig" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c3bddf6d8984cde9208e3f0fe584879
|
||||
timeCreated: 1587490700
|
94
action/default-build-script/Assets/Editor/Versioning/Git.cs
Normal file
94
action/default-build-script/Assets/Editor/Versioning/Git.cs
Normal file
@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityBuilderAction.Versioning
|
||||
{
|
||||
public static class Git
|
||||
{
|
||||
const string application = @"git";
|
||||
|
||||
/// <summary>
|
||||
/// Generate a version based on the latest tag and the amount of commits.
|
||||
/// Format: 0.1.2 (where 2 is the amount of commits).
|
||||
///
|
||||
/// If no tag is present in the repository then v0.0 is assumed.
|
||||
/// This would result in 0.0.# where # is the amount of commits.
|
||||
/// </summary>
|
||||
public static string GenerateSemanticCommitVersion()
|
||||
{
|
||||
string version;
|
||||
if (HasAnyVersionTags()) {
|
||||
version = GetSemanticCommitVersion();
|
||||
Console.WriteLine("Repository has a valid version tag.");
|
||||
} else {
|
||||
version = $"0.0.{GetTotalNumberOfCommits()}";
|
||||
Console.WriteLine("Repository does not have tags to base the version on.");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Version is {version}");
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the total number of commits.
|
||||
/// </summary>
|
||||
static int GetTotalNumberOfCommits()
|
||||
{
|
||||
string numberOfCommitsAsString = Run(@"git rev-list --count HEAD");
|
||||
|
||||
return int.Parse(numberOfCommitsAsString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not the repository has any version tags yet.
|
||||
/// </summary>
|
||||
static bool HasAnyVersionTags()
|
||||
{
|
||||
return "0" != Run(@"tag --list --merged HEAD | grep v[0-9]* | wc -l");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the build version from git based on the most recent matching tag and
|
||||
/// commit history. This returns the version as: {major.minor.build} where 'build'
|
||||
/// represents the nth commit after the tagged commit.
|
||||
/// Note: The initial 'v' and the commit hash are removed.
|
||||
/// </summary>
|
||||
static string GetSemanticCommitVersion()
|
||||
{
|
||||
// v0.1-2-g12345678
|
||||
string version = GetVersionString();
|
||||
// 0.1-2
|
||||
version = version.Substring(1, version.LastIndexOf('-') - 1);
|
||||
// 0.1.2
|
||||
version = version.Replace('-', '.');
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get version string.
|
||||
///
|
||||
/// Format: `v0.1-2-g12345678` (where 2 is the amount of commits since the last tag)
|
||||
/// </summary>
|
||||
static string GetVersionString()
|
||||
{
|
||||
return Run(@"describe --tags --long --match ""v[0-9]*""");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs git binary with any given arguments and returns the output.
|
||||
/// </summary>
|
||||
static string Run(string arguments)
|
||||
{
|
||||
using (var process = new System.Diagnostics.Process()) {
|
||||
string workingDirectory = Application.dataPath;
|
||||
|
||||
int exitCode = process.Run(application, arguments, workingDirectory, out string output, out string errors);
|
||||
if (exitCode != 0) { throw new GitException(exitCode, errors); }
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cdec7fa0f5bb44958fdf74d4658a4601
|
||||
timeCreated: 1587495075
|
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace UnityBuilderAction.Versioning
|
||||
{
|
||||
public class GitException : InvalidOperationException
|
||||
{
|
||||
public readonly int code;
|
||||
|
||||
public GitException(int code, string errors) : base(errors)
|
||||
{
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d375e209fd14fc5bc2f3dc3c78ac574
|
||||
timeCreated: 1587490750
|
@ -0,0 +1,10 @@
|
||||
namespace UnityBuilderAction.Versioning
|
||||
{
|
||||
public static class VersionGenerator
|
||||
{
|
||||
public static string Generate()
|
||||
{
|
||||
return Git.GenerateSemanticCommitVersion();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9892e03ae8314b7eacd793c8002de007
|
||||
timeCreated: 1587490842
|
@ -0,0 +1,3 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Untracked/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Versioning/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
Loading…
Reference in New Issue
Block a user