Skip to content

Commit

Permalink
Merge pull request #29 from UniToolsTeam/feature/improved-commandline
Browse files Browse the repository at this point in the history
Feature/improved commandline
  • Loading branch information
Rinal authored Mar 14, 2023
2 parents ed09579 + 565b3df commit 2bedb12
Show file tree
Hide file tree
Showing 51 changed files with 718 additions and 406 deletions.
16 changes: 11 additions & 5 deletions Editor/BatchMode/BatchModeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ public static async void Execute()
throw new Exception($"{nameof(BatchModeBuilder)}: can be run only from the BatchMode!");
}

BatchModeParameters parameters = CommandLineParser.Parse<BatchModeParameters>(Environment.CommandLine);
const string key = "--pipeline";
string pipeline = string.Empty;

string pipelineName = parameters.Pipeline;
if (!CommandLineParser.TryToParseValue(Environment.CommandLine, key, out object v))
{
throw new Exception($"The pipeline is not assigned from the CommandLine. Use {key} to define a pipeline for execution.");
}

pipeline = v.ToString();

if (Find(pipelineName) != null)
if (Find(pipeline) != null)
{
await RunBuildPipeline(pipelineName);
await RunBuildPipeline(pipeline);
}
else
{
throw new Exception($"Failed to find a pipeline with name {pipelineName}!");
throw new Exception($"Failed to find a pipeline with name {pipeline}!");
}
}

Expand Down
10 changes: 0 additions & 10 deletions Editor/BatchMode/BatchModeParameters.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/BatchMode/BatchModeParameters.cs.meta

This file was deleted.

9 changes: 0 additions & 9 deletions Editor/BatchMode/CommandLineParameterAttribute.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/BatchMode/CommandLineParameterAttribute.cs.meta

This file was deleted.

43 changes: 5 additions & 38 deletions Editor/BatchMode/CommandLineParser.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace UniTools.Build
{
public static class CommandLineParser
{
private const string KeyStart = "-";

public static T Parse<T>(string commandLine) where T : class, new()
public static bool TryToParseValue(string commandLine, string key, out object val)
{
T t = new T();

Dictionary<string, object> parameters = new Dictionary<string, object>();
string[] args = commandLine.Split(' ');
for (int i = 0; i < args.Length; i++)
{
if (IsKey(args[i]))
if (IsKey(args[i]) && args[i].Equals(key))
{
string key = args[i];
object val = null;
int next = i + 1;
if (next < args.Length && !IsKey(args[next]))
{
val = args[next];
}

key = key.Replace(KeyStart, string.Empty).ToLower();
if (parameters.ContainsKey(key))
{
Debug.LogWarning($"{nameof(CommandLineParser)}: duplication of the parameter {key}");
parameters[key] = val;
}
else
{
parameters.Add(key, val);
return true;
}
}
}

FieldInfo[] fields =
t.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(f => f.IsDefined(typeof(CommandLineParameterAttribute), false))
.ToArray()
;

foreach (FieldInfo info in fields)
{
string fieldName = info.Name.ToLower();
if (parameters.TryGetValue(fieldName, out object p))
{
info.SetValue(t, p ?? true);
}
}
val = null;

return t;
return false;
}

private static bool IsKey(string param)
Expand Down
60 changes: 0 additions & 60 deletions Editor/Core/CustomEditors/BuildPipelinePresenter.cs

This file was deleted.

41 changes: 0 additions & 41 deletions Editor/Core/CustomEditors/BuildPipelinesProjectSettingsProvider.cs

This file was deleted.

10 changes: 10 additions & 0 deletions Editor/Core/Parameters/BuildParameterPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace UniTools.Build
{
public abstract class BuildParameterPresenter
{

public abstract string CliKey { get; }

public abstract void Draw(bool duplicated);
}
}
3 changes: 3 additions & 0 deletions Editor/Core/Parameters/BuildParameterPresenter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Editor/Core/Parameters/Int.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Editor/Core/Parameters/Int/IntBuildParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;

namespace UniTools.Build
{
[CreateAssetMenu(
fileName = nameof(StringBuildParameter),
menuName = MenuPaths.Parameters + "Int"
)]
public sealed class IntBuildParameter : ScriptableBuildParameter<int>
{
protected override bool TryParseFromCommandLine(string commandLine, out int v)
{
if (CommandLineParser.TryToParseValue(commandLine, CliKey, out object obj))
{
if (int.TryParse(obj.ToString(), out v))
{
return true;
}
}

v = int.MinValue;

return false;
}
}
}
3 changes: 3 additions & 0 deletions Editor/Core/Parameters/Int/IntBuildParameter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions Editor/Core/Parameters/Int/IntBuildParameterEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace UniTools.Build
{
[CustomEditor(typeof(IntBuildParameter))]
public sealed class IntBuildParameterEditor : Editor
{
private IntBuildParameter m_target = default;
private SerializedProperty m_value = default;
private SerializedProperty m_options = default;

private void OnEnable()
{
m_value = serializedObject.FindProperty(nameof(m_value));
m_options = serializedObject.FindProperty(nameof(m_options));
m_target = target as IntBuildParameter;
}

public override void OnInspectorGUI()
{
Draw(m_target, serializedObject, m_value, m_options);
}

/// <summary>
/// This method is using to draw the same editor in different windows
/// </summary>
public static void Draw(IntBuildParameter target, SerializedObject serializedObject, SerializedProperty value, SerializedProperty options)
{
bool copyToClipboard = false;

if (target.Options == null || target.Options.Count <= 0)
{
EditorGUILayout.PropertyField(value);
}
else
{
int index = target.Options.IndexOf(value.intValue);
index = EditorGUILayout.Popup(index, target.Options.Select(o => o.ToString()).ToArray());

if (index >= 0 && index <= target.Options.Count)
{
value.intValue = target.Options[index];
}
}

EditorGUILayout.PropertyField(options);

EditorGUILayout.BeginHorizontal();
{
EditorGUILayout.LabelField($"CLI: {target.CliKey} {value.intValue}");
copyToClipboard = GUILayout.Button("Copy");
}
EditorGUILayout.EndHorizontal();

serializedObject.ApplyModifiedProperties();

if (copyToClipboard)
{
EditorGUIUtility.systemCopyBuffer = $"{target.CliKey} {value.intValue}";
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/Core/Parameters/Int/IntBuildParameterEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2bedb12

Please sign in to comment.