mirror of
https://github.com/game-ci/unity-builder.git
synced 2025-07-04 12:25:19 -04:00
Refactor Builder
This commit is contained in:
parent
5ee9c59113
commit
6f1d03d8a8
@ -57,7 +57,10 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Assets\Editor\Builder.cs" />
|
||||
<Compile Include="Assets\Editor\Input\ArgumentsParser.cs" />
|
||||
<Compile Include="Assets\Editor\Reporting\StdOutReporter.cs" />
|
||||
<Compile Include="Assets\Editor\System\ProcessExtensions.cs" />
|
||||
<Compile Include="Assets\Editor\Versioning\VersionApplicator.cs" />
|
||||
<Compile Include="Assets\Editor\Versioning\Git.cs" />
|
||||
<Compile Include="Assets\Editor\Versioning\VersionGenerator.cs" />
|
||||
<Compile Include="Assets\Editor\Versioning\GitException.cs" />
|
||||
|
@ -1,87 +1,20 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityBuilderAction.Input;
|
||||
using UnityBuilderAction.Reporting;
|
||||
using UnityBuilderAction.Versioning;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Build.Reporting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityBuilderAction
|
||||
{
|
||||
static class Builder
|
||||
{
|
||||
private static string EOL = Environment.NewLine;
|
||||
|
||||
private static void ParseCommandLineArguments(out Dictionary<string, string> providedArguments)
|
||||
{
|
||||
providedArguments = new Dictionary<string, string>();
|
||||
string[] args = Environment.GetCommandLineArgs();
|
||||
|
||||
Console.WriteLine(
|
||||
$"{EOL}" +
|
||||
$"###########################{EOL}" +
|
||||
$"# Parsing settings #{EOL}" +
|
||||
$"###########################{EOL}" +
|
||||
$"{EOL}"
|
||||
);
|
||||
|
||||
// Extract flags with optional values
|
||||
for (int current = 0, next = 1; current < args.Length; current++, next++) {
|
||||
// Parse flag
|
||||
bool isFlag = args[current].StartsWith("-");
|
||||
if (!isFlag) continue;
|
||||
string flag = args[current].TrimStart('-');
|
||||
|
||||
// Parse optional value
|
||||
bool flagHasValue = next < args.Length && !args[next].StartsWith("-");
|
||||
string value = flagHasValue ? args[next].TrimStart('-') : "";
|
||||
|
||||
// Assign
|
||||
Console.WriteLine($"Found flag \"{flag}\" with value \"{value}\".");
|
||||
providedArguments.Add(flag, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> GetValidatedOptions()
|
||||
{
|
||||
ParseCommandLineArguments(out var validatedOptions);
|
||||
|
||||
if (!validatedOptions.TryGetValue("projectPath", out var projectPath)) {
|
||||
Console.WriteLine("Missing argument -projectPath");
|
||||
EditorApplication.Exit(110);
|
||||
}
|
||||
|
||||
if (!validatedOptions.TryGetValue("buildTarget", out var buildTarget)) {
|
||||
Console.WriteLine("Missing argument -buildTarget");
|
||||
EditorApplication.Exit(120);
|
||||
}
|
||||
|
||||
if (!Enum.IsDefined(typeof(BuildTarget), buildTarget)) {
|
||||
EditorApplication.Exit(121);
|
||||
}
|
||||
|
||||
if (!validatedOptions.TryGetValue("customBuildPath", out var customBuildPath)) {
|
||||
Console.WriteLine("Missing argument -customBuildPath");
|
||||
EditorApplication.Exit(130);
|
||||
}
|
||||
|
||||
string defaultCustomBuildName = "TestBuild";
|
||||
if (!validatedOptions.TryGetValue("customBuildName", out var customBuildName)) {
|
||||
Console.WriteLine($"Missing argument -customBuildName, defaulting to {defaultCustomBuildName}.");
|
||||
validatedOptions.Add("customBuildName", defaultCustomBuildName);
|
||||
}
|
||||
else if (customBuildName == "") {
|
||||
Console.WriteLine($"Invalid argument -customBuildName, defaulting to {defaultCustomBuildName}.");
|
||||
validatedOptions.Add("customBuildName", defaultCustomBuildName);
|
||||
}
|
||||
|
||||
return validatedOptions;
|
||||
}
|
||||
|
||||
public static void BuildProject()
|
||||
{
|
||||
// Gather values from args
|
||||
var options = GetValidatedOptions();
|
||||
var options = ArgumentsParser.GetValidatedOptions();
|
||||
|
||||
// Gather values from project
|
||||
var scenes = EditorBuildSettings.scenes.Where(scene => scene.enabled).Select(s => s.path).ToArray();
|
||||
@ -93,55 +26,19 @@ namespace UnityBuilderAction
|
||||
target = (BuildTarget) Enum.Parse(typeof(BuildTarget), options["buildTarget"]),
|
||||
};
|
||||
|
||||
// Set version for this build
|
||||
VersionApplicator.SetVersion(options["versioning"], options["version"]);
|
||||
|
||||
// Perform build
|
||||
BuildReport buildReport = BuildPipeline.BuildPlayer(buildOptions);
|
||||
|
||||
// Summary
|
||||
BuildSummary summary = buildReport.summary;
|
||||
ReportSummary(summary);
|
||||
StdOutReporter.ReportSummary(summary);
|
||||
|
||||
// Result
|
||||
BuildResult result = summary.result;
|
||||
ExitWithResult(result);
|
||||
}
|
||||
|
||||
private static void ReportSummary(BuildSummary summary)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"{EOL}" +
|
||||
$"###########################{EOL}" +
|
||||
$"# Build results #{EOL}" +
|
||||
$"###########################{EOL}" +
|
||||
$"{EOL}" +
|
||||
$"Duration: {summary.totalTime.ToString()}{EOL}" +
|
||||
$"Warnings: {summary.totalWarnings.ToString()}{EOL}" +
|
||||
$"Errors: {summary.totalErrors.ToString()}{EOL}" +
|
||||
$"Size: {summary.totalSize.ToString()} bytes{EOL}" +
|
||||
$"{EOL}"
|
||||
);
|
||||
}
|
||||
|
||||
private static void ExitWithResult(BuildResult result)
|
||||
{
|
||||
if (result == BuildResult.Succeeded) {
|
||||
Console.WriteLine("Build succeeded!");
|
||||
EditorApplication.Exit(0);
|
||||
}
|
||||
|
||||
if (result == BuildResult.Failed) {
|
||||
Console.WriteLine("Build failed!");
|
||||
EditorApplication.Exit(101);
|
||||
}
|
||||
|
||||
if (result == BuildResult.Cancelled) {
|
||||
Console.WriteLine("Build cancelled!");
|
||||
EditorApplication.Exit(102);
|
||||
}
|
||||
|
||||
if (result == BuildResult.Unknown) {
|
||||
Console.WriteLine("Build result is unknown!");
|
||||
EditorApplication.Exit(103);
|
||||
}
|
||||
StdOutReporter.ExitWithResult(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
3
action/default-build-script/Assets/Editor/Input.meta
Normal file
3
action/default-build-script/Assets/Editor/Input.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6e5ef18d769419d887b56665969442b
|
||||
timeCreated: 1587503329
|
3
action/default-build-script/Assets/Editor/Reporting.meta
Normal file
3
action/default-build-script/Assets/Editor/Reporting.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 166f919334c44e7a80ae916667974e7d
|
||||
timeCreated: 1587503566
|
@ -14,7 +14,7 @@ namespace UnityBuilderAction.Versioning
|
||||
SemanticClassic,
|
||||
}
|
||||
|
||||
public static void SetVersionForBuild(string strategy, [CanBeNull] string version)
|
||||
public static void SetVersion(string strategy, [CanBeNull] string version)
|
||||
{
|
||||
if (!Enum.TryParse<Strategy>(strategy, out Strategy validatedStrategy)) {
|
||||
throw new Exception($"Invalid versioning argument provided. {strategy} is not a valid strategy.");
|
||||
|
Loading…
Reference in New Issue
Block a user