Skip to content

Commit 19f783f

Browse files
committed
refactor: improve package exporter design and structure
1 parent ed9889a commit 19f783f

27 files changed

+481
-189
lines changed

.github/workflows/reusable-build-package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ on:
3535
description: 'Path where the builds should be stored.'
3636
required: false
3737
type: string
38-
default: 'PackageExporter.Export'
38+
default: 'PackageExporter.Editor.Builder.Build'
3939
custom-parameters:
4040
description: 'Custom parameters to configure the build. https://game.ci/docs/github/builder#customparameters'
4141
required: false

Assets/Editor/ArgumentsParser.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

Assets/Editor/PackageExporter.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: adb346ada09d4f62892e782d664fc210, type: 3}
13+
m_Name: PackageExporterSetting
14+
m_EditorClassIdentifier:
15+
_folderPath: Assets/Plugins/PackageExporter/

Assets/Editor/PackageExporterSetting.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// https://github.com/game-ci/documentation/blob/main/example/BuildScript.cs
2+
using System;
3+
using System.Collections.Generic;
4+
using UnityEditor;
5+
6+
namespace PackageExporter.Editor
7+
{
8+
internal class ArgumentsParser
9+
{
10+
static readonly string s_eol = Environment.NewLine;
11+
12+
public static Dictionary<string, string> GetValidatedOptions()
13+
{
14+
ParseCommandLineArguments(out var validatedOptions);
15+
16+
if (!validatedOptions.TryGetValue("projectPath", out string _))
17+
{
18+
Console.WriteLine("Missing argument -projectPath");
19+
EditorApplication.Exit(110);
20+
}
21+
if (!validatedOptions.TryGetValue("buildTarget", out string buildTarget))
22+
{
23+
Console.WriteLine("Missing argument -buildTarget");
24+
EditorApplication.Exit(120);
25+
}
26+
if (!Enum.IsDefined(typeof(BuildTarget), buildTarget ?? string.Empty))
27+
{
28+
Console.WriteLine($"{buildTarget} is not a defined {nameof(BuildTarget)}");
29+
EditorApplication.Exit(121);
30+
}
31+
if (!validatedOptions.TryGetValue("customBuildPath", out string _))
32+
{
33+
Console.WriteLine("Missing argument -customBuildPath");
34+
EditorApplication.Exit(130);
35+
}
36+
if (!validatedOptions.TryGetValue("customBuildName", out string _))
37+
{
38+
Console.WriteLine("Missing argument -customBuildName");
39+
EditorApplication.Exit(131);
40+
}
41+
return validatedOptions;
42+
}
43+
44+
static void ParseCommandLineArguments(out Dictionary<string, string> providedArguments)
45+
{
46+
providedArguments = new Dictionary<string, string>();
47+
var args = Environment.GetCommandLineArgs();
48+
49+
Console.WriteLine(
50+
$"{s_eol}" +
51+
$"###########################{s_eol}" +
52+
$"# Parsing settings #{s_eol}" +
53+
$"###########################{s_eol}" +
54+
$"{s_eol}"
55+
);
56+
57+
for (int current = 0, next = 1; current < args.Length; current++, next++)
58+
{
59+
var isFlag = args[current].StartsWith("-");
60+
if (!isFlag)
61+
{
62+
continue;
63+
}
64+
var flag = args[current].TrimStart('-');
65+
var flagHasValue = next < args.Length && !args[next].StartsWith("-");
66+
var value = flagHasValue ? args[next].TrimStart('-') : "";
67+
var displayValue = "\"" + value + "\"";
68+
Console.WriteLine($"Found flag \"{flag}\" with value {displayValue}.");
69+
providedArguments.Add(flag, value);
70+
}
71+
}
72+
}
73+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
using System.IO;
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace PackageExporter.Editor
7+
{
8+
internal sealed class AssetDatabaseSupport
9+
{
10+
public static T CreateOrLoad<T>(string path) where T : ScriptableObject
11+
{
12+
var asset = LoadAsset<T>(path);
13+
return asset != default
14+
? asset
15+
: CreateAsset<T>(path);
16+
}
17+
18+
public static T CreateAsset<T>(string path) where T : ScriptableObject
19+
{
20+
CreateDirectories(path);
21+
var asset = ScriptableObject.CreateInstance<T>();
22+
AssetDatabase.CreateAsset(asset, path);
23+
AssetDatabase.SaveAssets();
24+
return asset;
25+
}
26+
27+
public static T LoadAsset<T>(string path) where T : ScriptableObject
28+
{
29+
var asset = AssetDatabase.LoadAssetAtPath<T>(path);
30+
if (asset != default)
31+
{
32+
return asset;
33+
}
34+
35+
var dir = Path.GetDirectoryName(path);
36+
dir = dir?.Replace("\\", "/");
37+
if (!AssetDatabase.IsValidFolder(dir))
38+
{
39+
return default;
40+
}
41+
var currentGuids = AssetDatabase.FindAssets("t:" + typeof(T), new[] { dir });
42+
if (currentGuids is {Length: > 0})
43+
{
44+
path = AssetDatabase.GUIDToAssetPath(currentGuids[0]);
45+
asset = AssetDatabase.LoadAssetAtPath<T>(path);
46+
}
47+
return asset;
48+
}
49+
50+
public static void CreateDirectories(string path)
51+
{
52+
if (!path.StartsWith("Assets"))
53+
{
54+
Debug.LogError("The path must start with 'Assets' ");
55+
return;
56+
}
57+
58+
path = Path.HasExtension(path)
59+
? Path.GetDirectoryName(path)
60+
: path;
61+
path = path?.Replace("\\", "/");
62+
63+
if (string.IsNullOrEmpty(path)
64+
|| AssetDatabase.IsValidFolder(path))
65+
{
66+
return;
67+
}
68+
69+
var folders = path.Split('/');
70+
var parentFolder = folders[0];
71+
72+
for (var i = 1; i < folders.Length; i++)
73+
{
74+
var newFolder = parentFolder + "/" + folders[i];
75+
if (!AssetDatabase.IsValidFolder(newFolder))
76+
{
77+
AssetDatabase.CreateFolder(parentFolder, folders[i]);
78+
}
79+
parentFolder = newFolder;
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)