Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Added additional command line parameters for CI/CD (#829)
Browse files Browse the repository at this point in the history
* Added a way to set the application build identifier

* Added version and version code command line args support

* move lumin build version name to sync with other builds

* Sync WSA version

* Unity really likes to make things difficult

* Use Applicaton.version first, then bundle identifier

fixed WSA package assignment.

* updated project settings

* revert changes to BuildDeployWindow

* Fixed documentation
  • Loading branch information
StephenHodgson authored May 6, 2021
1 parent b875074 commit 20c135f
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 11 deletions.
11 changes: 10 additions & 1 deletion Editor/BuildAndDeploy/BuildInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public string OutputDirectory
/// <inheritdoc />
public string BuildPlatform { get; set; } = "x86";

/// <inheritdoc />
public string BundleIdentifier { get; set; }

/// <inheritdoc />
public Version Version { get; set; }

/// <inheritdoc />
public int? VersionCode { get; set; } = null;

/// <inheritdoc />
public string Configuration
{
Expand Down Expand Up @@ -88,4 +97,4 @@ public string Configuration
}
}
}
}
}
23 changes: 22 additions & 1 deletion Editor/BuildAndDeploy/IBuildInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public interface IBuildInfo
/// <summary>
/// Should the build auto increment the build version number?
/// </summary>
/// <remarks>
/// If <see cref="Version"/> is assigned then this flag is ignored.
/// </remarks>
bool AutoIncrement { get; set; }

/// <summary>
Expand All @@ -73,5 +76,23 @@ public interface IBuildInfo
/// The build platform (i.e. x86, x64)
/// </summary>
string BuildPlatform { get; set; }

/// <summary>
/// The build Bundle Identifier (i.e. 'com.xrtk.core')
/// </summary>
string BundleIdentifier { get; set; }

/// <summary>
/// The build version number
/// </summary>
/// <remarks>
/// If set will override <see cref="AutoIncrement"/>
/// </remarks>
Version Version { get; set; }

/// <summary>
/// The version code (usually a single integer for platforms like iOS, Android, and Magic Leap)
/// </summary>
int? VersionCode { get; set; }
}
}
}
95 changes: 94 additions & 1 deletion Editor/BuildAndDeploy/UnityPlayerBuildTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,39 @@ public static BuildReport BuildUnityPlayer(IBuildInfo buildInfo)
// Call the pre-build action, if any
buildInfo.PreBuildAction?.Invoke(buildInfo);

// use https://semver.org/
// major.minor.build
Version version = new Version(
(buildInfo.Version == null || buildInfo.AutoIncrement)
? string.IsNullOrWhiteSpace(Application.version)
? string.IsNullOrWhiteSpace(PlayerSettings.bundleVersion)
? "1.0.0"
: PlayerSettings.bundleVersion
: Application.version
: buildInfo.Version.ToString(3));

// Only auto incitement if the version wasn't specified in the build info.
if (buildInfo.Version == null &&
buildInfo.AutoIncrement)
{
version = new Version(version.Major, version.Minor, version.Build + 1);
}

// Updates the Application.version and syncs Android and iOS bundle version strings
PlayerSettings.bundleVersion = version.ToString(3);
// Update Lumin bc the Application.version isn't synced like Android & iOS
PlayerSettings.Lumin.versionName = PlayerSettings.bundleVersion;
// Update WSA bc the Application.version isn't synced line Android & iOS
PlayerSettings.WSA.packageVersion = new Version(version.Major, version.Minor, version.Build, 0);

var buildTargetGroup = buildInfo.BuildTarget.GetGroup();
var oldBuildIdentifier = PlayerSettings.GetApplicationIdentifier(buildTargetGroup);

if (!string.IsNullOrWhiteSpace(buildInfo.BundleIdentifier))
{
PlayerSettings.SetApplicationIdentifier(buildTargetGroup, buildInfo.BundleIdentifier);
}

var playerBuildSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);

if (!string.IsNullOrEmpty(playerBuildSymbols))
Expand Down Expand Up @@ -102,6 +134,23 @@ public static BuildReport BuildUnityPlayer(IBuildInfo buildInfo)
switch (buildInfo.BuildTarget)
{
case BuildTarget.Lumin:

if (buildInfo.VersionCode.HasValue)
{
PlayerSettings.Lumin.versionCode = buildInfo.VersionCode.Value;
}
else
{
// Usually version codes are unique and not tied to the usual semver versions
// see https://developer.android.com/studio/publish/versioning#appversioning
// versionCode - A positive integer used as an internal version number.
// This number is used only to determine whether one version is more recent than another,
// with higher numbers indicating more recent versions. The Android system uses the
// versionCode value to protect against downgrades by preventing users from installing
// an APK with a lower versionCode than the version currently installed on their device.
PlayerSettings.Lumin.versionCode++;
}

buildInfo.OutputDirectory += ".mpk";

if (Directory.Exists($"{Directory.GetParent(Application.dataPath)}\\Library\\Mabu"))
Expand All @@ -110,6 +159,22 @@ public static BuildReport BuildUnityPlayer(IBuildInfo buildInfo)
}
break;
case BuildTarget.Android:
if (buildInfo.VersionCode.HasValue)
{
PlayerSettings.Android.bundleVersionCode = buildInfo.VersionCode.Value;
}
else
{
// Usually version codes are unique and not tied to the usual semver versions
// see https://developer.android.com/studio/publish/versioning#appversioning
// versionCode - A positive integer used as an internal version number.
// This number is used only to determine whether one version is more recent than another,
// with higher numbers indicating more recent versions. The Android system uses the
// versionCode value to protect against downgrades by preventing users from installing
// an APK with a lower versionCode than the version currently installed on their device.
PlayerSettings.Android.bundleVersionCode++;
}

buildInfo.OutputDirectory += ".apk";
cacheIl2Cpp = false;
break;
Expand Down Expand Up @@ -165,6 +230,11 @@ public static BuildReport BuildUnityPlayer(IBuildInfo buildInfo)
EditorUserBuildSettings.SwitchActiveBuildTarget(oldBuildTargetGroup, oldBuildTarget);
}

if (PlayerSettings.GetApplicationIdentifier(oldBuildTargetGroup) != oldBuildIdentifier)
{
PlayerSettings.SetApplicationIdentifier(oldBuildTargetGroup, oldBuildIdentifier);
}

// Call the post-build action, if any
buildInfo.PostBuildAction?.Invoke(buildInfo, buildReport);

Expand Down Expand Up @@ -306,6 +376,26 @@ public static void ParseBuildCommandLine(ref IBuildInfo buildInfo)
case "-autoIncrement":
buildInfo.AutoIncrement = true;
break;
case "-version":
if (Version.TryParse(arguments[++i], out var version))
{
buildInfo.Version = version;
}
else
{
Debug.LogError($"Failed to parse -version \"{arguments[i]}\"");
}
break;
case "-versionCode":
if (int.TryParse(arguments[++i], out var versionCode))
{
buildInfo.VersionCode = versionCode;
}
else
{
Debug.LogError($"Failed to parse -versionCode \"{arguments[i]}\"");
}
break;
case "-sceneList":
buildInfo.Scenes = buildInfo.Scenes.Union(SplitSceneList(arguments[++i]));
break;
Expand All @@ -329,6 +419,9 @@ public static void ParseBuildCommandLine(ref IBuildInfo buildInfo)
case "-release":
buildInfo.Configuration = arguments[i].Substring(1).ToLower();
break;
case "-bundleIdentifier":
buildInfo.BundleIdentifier = arguments[++i];
break;
}
}
}
Expand Down Expand Up @@ -357,4 +450,4 @@ public static async Task<bool> RestoreNugetPackagesAsync(string nugetPath, strin
return File.Exists($"{storePath}\\project.lock.json");
}
}
}
}
8 changes: 2 additions & 6 deletions Editor/BuildAndDeploy/UwpAppxBuildTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,7 @@ private static bool UpdateAppxManifest(IBuildInfo buildInfo)
// According to https://msdn.microsoft.com/en-us/library/windows/apps/br211441.aspx
// Package versions are always of the form Major.Minor.Build.Revision.
// Note: Revision number reserved for Windows Store, and a value other than 0 will fail WACK.
var version = PlayerSettings.WSA.packageVersion;
var newVersion = new Version(version.Major, version.Minor, buildInfo.AutoIncrement ? version.Build + 1 : version.Build, version.Revision);

PlayerSettings.WSA.packageVersion = newVersion;
versionAttr.Value = newVersion.ToString();
versionAttr.Value = PlayerSettings.WSA.packageVersion.ToString();
rootNode.Save(manifests[0]);
return true;
}
Expand Down Expand Up @@ -368,4 +364,4 @@ private static void UpdateDependenciesElement(XElement dependencies, XNamespace
}
}
}
}
}
4 changes: 2 additions & 2 deletions Editor/BuildAndDeploy/UwpPlayerBuildTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static async Task<bool> BuildPlayer(string buildDirectory, bool showDialo
OutputDirectory = buildDirectory,
Scenes = EditorBuildSettings.scenes.Where(scene => !string.IsNullOrWhiteSpace(scene.path)).Where(scene => scene.enabled),
BuildAppx = !showDialog,

Version = new Version(PlayerSettings.WSA.packageVersion.ToString(3)),
// Configure a post build action that will compile the generated solution
PostBuildAction = PostBuildAction
};
Expand Down Expand Up @@ -111,4 +111,4 @@ public static async Task<BuildReport> BuildPlayer(UwpBuildInfo buildInfo, Cancel
return buildReport;
}
}
}
}

0 comments on commit 20c135f

Please sign in to comment.