Skip to content

Commit

Permalink
Merge pull request #25 from UniToolsTeam/feature/generic-steps
Browse files Browse the repository at this point in the history
Feature/generic steps
  • Loading branch information
Rinal authored Mar 2, 2023
2 parents f03d1a9 + 7a121cc commit fd0f5de
Show file tree
Hide file tree
Showing 46 changed files with 212 additions and 366 deletions.
64 changes: 13 additions & 51 deletions Editor/BatchMode/BatchModeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace UniTools.Build
Expand All @@ -13,21 +11,14 @@ public static async void Execute()
{
if (!Application.isBatchMode)
{
//TODO Implement BatchMode exception
throw new Exception($"{nameof(BatchModeBuilder)}: can be run only from the BatchMode!");
}

BatchModeParameters parameters = CommandLineParser.Parse<BatchModeParameters>(Environment.CommandLine);

string pipelineName = parameters.Pipeline;

//check if the selected pipeline is composite
if (Find<CompositeBuildPipeline>(pipelineName) != null)
{
await RunCompositeBuildPipeline(pipelineName);
}
//check if the selected pipeline is simple
else if (Find<BuildPipeline>(pipelineName) != null)
if (Find(pipelineName) != null)
{
await RunBuildPipeline(pipelineName);
}
Expand All @@ -37,14 +28,14 @@ public static async void Execute()
}
}

private static TPipeline Find<TPipeline>(string pipelineName) where TPipeline : ScriptableBuildPipeline
private static BuildPipeline Find(string pipelineName)
{
string[] guids = AssetDatabase.FindAssets($"t:{nameof(ScriptableBuildPipeline)}");
string[] guids = AssetDatabase.FindAssets($"t:{nameof(BuildPipeline)}");

foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
TPipeline pipeline = AssetDatabase.LoadAssetAtPath<TPipeline>(path);
BuildPipeline pipeline = AssetDatabase.LoadAssetAtPath<BuildPipeline>(path);
if (pipeline == null)
{
continue;
Expand All @@ -59,53 +50,24 @@ private static TPipeline Find<TPipeline>(string pipelineName) where TPipeline :
return null;
}

private static async Task RunCompositeBuildPipeline(string name)
{
CompositeBuildPipeline pipeline = Find<CompositeBuildPipeline>(name);
if (pipeline == null)
{
throw new Exception($"Failed to find a pipeline with name {name}!");
}

string[] childNames = pipeline.Pipelines.Select(p => p.name).ToArray();

foreach (string s in childNames)
{
Debug.Log($"{nameof(RunCompositeBuildPipeline)}: {s}");
}

await pipeline.PreBuild();

foreach (string childName in childNames)
{
await RunBuildPipeline(childName);
}
}

private static async Task RunBuildPipeline(string name)
{
BuildPipeline pipeline = Find<BuildPipeline>(name);
BuildPipeline pipeline = Find(name);
if (pipeline == null)
{
throw new Exception($"Failed to find a pipeline with name {name}!");
}

await pipeline.PreBuild();
BuildReport report = await pipeline.Build();
BuildSummary summary = report.summary;
if (summary.result == BuildResult.Failed)
int stepsCount = pipeline.Count;
for (int i = 0; i < stepsCount; i++)
{
throw new Exception($"{nameof(BatchModeBuilder)}: {name} failed!");
}

//after the build we have to find a pipeline again due to unity clean all serialize fields and previous reference is invalid
pipeline = Find<BuildPipeline>(name);
if (pipeline == null)
{
throw new Exception($"Failed to find a pipeline with name {name}!");
await pipeline.RunStep(i);
pipeline = Find(name);
if (pipeline == null)
{
throw new Exception($"Failed to find a pipeline with name {name}!");
}
}

await pipeline.PostBuild();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace UniTools.Build
{
public sealed class ScriptableBuildPipelinePresenter
public sealed class BuildPipelinePresenter
{
private readonly ScriptableBuildPipeline m_buildPipeline = default;
private readonly BuildPipeline m_buildPipeline = default;
private bool m_foldout = false;

public ScriptableBuildPipelinePresenter(ScriptableBuildPipeline buildPipeline)
public BuildPipelinePresenter(BuildPipeline buildPipeline)
{
m_buildPipeline = buildPipeline;
}
Expand Down Expand Up @@ -38,7 +38,7 @@ public void Draw()

if (run)
{
m_buildPipeline.Run();
m_buildPipeline.Run().GetAwaiter().OnCompleted(() => { Debug.Log($"Pipeline {m_buildPipeline} is completed!"); });
}

if (modify)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class BuildPipelinesProjectSettingsProvider : SettingsProvider
public static SettingsProvider Register() =>
new BuildPipelinesProjectSettingsProvider($"Project/{nameof(UniTools)}/Build");

private readonly List<ScriptableBuildPipelinePresenter> m_presenters = new List<ScriptableBuildPipelinePresenter>();
private readonly List<BuildPipelinePresenter> m_presenters = new List<BuildPipelinePresenter>();

private BuildPipelinesProjectSettingsProvider(string path)
: base(path, SettingsScope.Project)
Expand All @@ -21,18 +21,18 @@ public override void OnActivate(string searchContext, VisualElement rootElement)
{
m_presenters.Clear();

string[] guids = AssetDatabase.FindAssets($"t:{nameof(ScriptableBuildPipeline)}");
string[] guids = AssetDatabase.FindAssets($"t:{nameof(BuildPipeline)}");
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
ScriptableBuildPipeline pipline = AssetDatabase.LoadAssetAtPath<ScriptableBuildPipeline>(path);
m_presenters.Add(new ScriptableBuildPipelinePresenter(pipline));
BuildPipeline pipline = AssetDatabase.LoadAssetAtPath<BuildPipeline>(path);
m_presenters.Add(new BuildPipelinePresenter(pipline));
}
}

public override void OnGUI(string searchContext)
{
foreach (ScriptableBuildPipelinePresenter presenter in m_presenters)
foreach (BuildPipelinePresenter presenter in m_presenters)
{
presenter.Draw();
}
Expand Down
88 changes: 77 additions & 11 deletions Editor/Core/Pipelines/BuildPipeline.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Threading.Tasks;
using UnityEditor.Build.Reporting;
using UnityEditor;
using UnityEngine;

namespace UniTools.Build
Expand All @@ -9,33 +9,99 @@ namespace UniTools.Build
fileName = nameof(BuildPipeline),
menuName = MenuPaths.Pipelines + "Pipeline"
)]
public sealed class BuildPipeline : ScriptablePostBuildPipeline
public sealed class BuildPipeline : ScriptableObject
{
[Serializable]
private sealed class PipelineStep
{
public string Name = string.Empty;
public BuildStep Step = default;
public bool Skip = false;
}

[SerializeField] private PipelineStep[] m_steps = default;

public int Count => m_steps.Length;

private void OnValidate()
{
foreach (PipelineStep step in m_steps)
{
if (step == null) continue;

if (step.Skip)
{
step.Name = $"{step.Step.name}(Skipped)";
}
else
{
step.Name = step.Step.name;
}
}
}

public async Task RunStep(int index)
{
if (index < 0 || index >= Count)
{
throw new Exception($"{nameof(BuildPipeline)}: Invalid step index {index}!");
}

try
{
EditorUtility.ClearProgressBar();
if (m_steps[index] == null || m_steps[index].Step == null)
{
Debug.LogWarning($"{nameof(BuildPipeline)}: The step at index {index} is null!");

return;
}

EditorUtility.DisplayProgressBar($"Execute {m_steps[index].Step.name} ({index + 1}/{m_steps.Length})...", $"{m_steps[index].Step.name} ", index / (float)Count);

if (m_steps[index].Skip)
{
Debug.LogWarning($"{nameof(BuildPipeline)}: The {m_steps[index].Step.name} was skipped!");

return;
}

await m_steps[index].Step.Execute();
EditorUtility.ClearProgressBar();
}
finally
{
EditorUtility.ClearProgressBar();
}
}

[ContextMenu("Run")]
public override async Task Run()
public async Task Run()
{
if (Application.isBatchMode)
{
throw new Exception($"{nameof(BuildPipeline)}: can not be run from the BatchMode!");
}

if (m_steps == null || m_steps.Length == 0)
{
Debug.Log($"{nameof(BuildPipeline)}: no any post build steps");

return;
}

try
{
await PreBuild();
BuildReport report = await Build();
BuildSummary summary = report.summary;
if (summary.result == BuildResult.Failed)
for (int i = 0; i < Count; i++)
{
throw new Exception($"{nameof(BuildPipeline)}: {name} Build failed!");
await RunStep(i);
}

await PostBuild();
}
catch (Exception e)
{
Debug.LogException(e);

throw;
throw e;
}
}
}
Expand Down
44 changes: 0 additions & 44 deletions Editor/Core/Pipelines/CompositeBuildPipeline.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/Core/Pipelines/CompositeBuildPipeline.cs.meta

This file was deleted.

10 changes: 0 additions & 10 deletions Editor/Core/Pipelines/ScriptableBuildPipeline.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/Core/Pipelines/ScriptableBuildPipeline.cs.meta

This file was deleted.

Loading

0 comments on commit fd0f5de

Please sign in to comment.