Skip to content

Commit 9302035

Browse files
committed
Resolve Unity default packages dynamically
1 parent 7f1c5e2 commit 9302035

2 files changed

Lines changed: 139 additions & 109 deletions

File tree

.github/workflows/unity-beta-test.yml

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -160,56 +160,86 @@ jobs:
160160
-logFile -
161161
echo "UNITY_TEST_PROJECT=$test_project" >> "$GITHUB_ENV"
162162
163-
- name: Read Unity package defaults
163+
- name: Resolve Unity package defaults
164164
shell: bash
165165
run: |
166-
built_in_packages="$(dirname "$UNITY_EDITOR_PATH")/Data/Resources/PackageManager/BuiltInPackages"
167-
test_framework_manifest="$(
168-
find "$built_in_packages" -type f \
169-
-path '*/com.unity.test-framework/package.json' \
170-
-print -quit 2>/dev/null || true
171-
)"
172-
ugui_manifest="$(
173-
find "$built_in_packages" -type f \
174-
-path '*/com.unity.ugui/package.json' \
175-
-print -quit 2>/dev/null || true
176-
)"
166+
resolver_dir="$UNITY_TEST_PROJECT/Assets/Editor"
167+
resolver_file="$resolver_dir/ResolveDefaultPackages.cs"
168+
mkdir -p "$resolver_dir"
169+
cat > "$resolver_file" <<'CSHARP'
170+
using System;
171+
using System.Threading;
172+
using UnityEditor.PackageManager;
173+
using UnityEditor.PackageManager.Requests;
174+
using UnityEngine;
177175
178-
if [[ -z "$test_framework_manifest" || -z "$ugui_manifest" ]]; then
179-
echo "::error::Cannot find Unity's built-in test-framework and ugui metadata."
180-
exit 1
181-
fi
176+
public static class ResolveDefaultPackages
177+
{
178+
public static void Run()
179+
{
180+
AddLatestCompatible("com.unity.test-framework");
181+
AddLatestCompatible("com.unity.ugui");
182+
}
183+
184+
private static void AddLatestCompatible(string packageName)
185+
{
186+
AddRequest request = Client.Add(packageName);
187+
DateTime deadline = DateTime.UtcNow.AddMinutes(10);
188+
189+
while (!request.IsCompleted)
190+
{
191+
if (DateTime.UtcNow >= deadline)
192+
{
193+
throw new TimeoutException(
194+
"Timed out resolving " + packageName + ".");
195+
}
196+
197+
Thread.Sleep(100);
198+
}
199+
200+
if (request.Status != StatusCode.Success)
201+
{
202+
throw new InvalidOperationException(
203+
"Cannot resolve " + packageName + ": "
204+
+ request.Error.message);
205+
}
206+
207+
Debug.Log("Resolved Unity default package: "
208+
+ request.Result.packageId);
209+
}
210+
}
211+
CSHARP
212+
213+
"$UNITY_EDITOR_PATH" \
214+
-projectPath "$UNITY_TEST_PROJECT" \
215+
-batchmode \
216+
-nographics \
217+
-quit \
218+
-executeMethod ResolveDefaultPackages.Run \
219+
-logFile -
182220
183-
jq -n \
184-
--arg testFrameworkVersion \
185-
"$(jq -er '.version' "$test_framework_manifest")" \
186-
--arg uguiVersion \
187-
"$(jq -er '.version' "$ugui_manifest")" \
188-
'{
189-
dependencies: {
190-
"com.unity.test-framework": $testFrameworkVersion,
191-
"com.unity.ugui": $uguiVersion
221+
rm -f "$resolver_file" "$resolver_file.meta"
222+
jq -e '
223+
.dependencies["com.unity.test-framework"] as $testFramework
224+
| .dependencies["com.unity.ugui"] as $ugui
225+
| select($testFramework != null and $ugui != null)
226+
| {
227+
"com.unity.test-framework": $testFramework,
228+
"com.unity.ugui": $ugui
192229
}
193-
}' > /tmp/unity-default-packages.json
194-
cat /tmp/unity-default-packages.json
230+
' "$UNITY_TEST_PROJECT/Packages/manifest.json"
195231
196232
- name: Prepare test project
197233
shell: bash
198234
run: |
235+
package_name="$(jq -er '.name' MyPlugin/package.json)"
199236
jq \
200-
--slurpfile unityDefaults /tmp/unity-default-packages.json \
201-
--arg packageName "com.litefeel.aligntools" \
237+
--arg packageName "$package_name" \
202238
--arg pluginPath "file:$GITHUB_WORKSPACE/MyPlugin" \
203-
'{
204-
dependencies: {
205-
"com.unity.test-framework":
206-
$unityDefaults[0].dependencies["com.unity.test-framework"],
207-
"com.unity.ugui":
208-
$unityDefaults[0].dependencies["com.unity.ugui"],
209-
($packageName): $pluginPath
210-
},
211-
testables: [$packageName]
212-
}' \
239+
'.dependencies[$packageName] = $pluginPath
240+
| .testables = (
241+
((.testables // []) + [$packageName]) | unique
242+
)' \
213243
"$UNITY_TEST_PROJECT/Packages/manifest.json" \
214244
> "$UNITY_TEST_PROJECT/Packages/manifest.json.tmp"
215245
mv "$UNITY_TEST_PROJECT/Packages/manifest.json.tmp" \

.github/workflows/unity-test.yml

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -113,86 +113,86 @@ jobs:
113113
-logFile -
114114
echo "UNITY_TEST_PROJECT=$test_project" >> "$GITHUB_ENV"
115115
116-
- name: Read Unity package defaults
116+
- name: Resolve Unity package defaults
117117
shell: bash
118118
run: |
119-
built_in_packages="$(dirname "$UNITY_EDITOR_PATH")/Data/Resources/PackageManager/BuiltInPackages"
120-
test_framework_manifest="$(
121-
find "$built_in_packages" -type f \
122-
-path '*/com.unity.test-framework/package.json' \
123-
-print -quit 2>/dev/null || true
124-
)"
125-
ugui_manifest="$(
126-
find "$built_in_packages" -type f \
127-
-path '*/com.unity.ugui/package.json' \
128-
-print -quit 2>/dev/null || true
129-
)"
130-
131-
if [[ -n "$test_framework_manifest" && -n "$ugui_manifest" ]]; then
132-
jq -n \
133-
--arg testFrameworkVersion \
134-
"$(jq -er '.version' "$test_framework_manifest")" \
135-
--arg uguiVersion \
136-
"$(jq -er '.version' "$ugui_manifest")" \
137-
'{
138-
dependencies: {
139-
"com.unity.test-framework": $testFrameworkVersion,
140-
"com.unity.ugui": $uguiVersion
141-
}
142-
}' > /tmp/unity-default-packages.json
143-
else
144-
template_dir="$(dirname "$UNITY_EDITOR_PATH")/Data/Resources/PackageManager/ProjectTemplates"
145-
template_archive="$(
146-
find "$template_dir" -type f \
147-
-name 'com.unity.template.3d-*.tgz' -print -quit \
148-
2>/dev/null || true
149-
)"
150-
if [[ -z "$template_archive" ]]; then
151-
echo "::error::Cannot find Unity's built-in package metadata or 3D template."
152-
exit 1
153-
fi
119+
resolver_dir="$UNITY_TEST_PROJECT/Assets/Editor"
120+
resolver_file="$resolver_dir/ResolveDefaultPackages.cs"
121+
mkdir -p "$resolver_dir"
122+
cat > "$resolver_file" <<'CSHARP'
123+
using System;
124+
using System.Threading;
125+
using UnityEditor.PackageManager;
126+
using UnityEditor.PackageManager.Requests;
127+
using UnityEngine;
128+
129+
public static class ResolveDefaultPackages
130+
{
131+
public static void Run()
132+
{
133+
AddLatestCompatible("com.unity.test-framework");
134+
AddLatestCompatible("com.unity.ugui");
135+
}
136+
137+
private static void AddLatestCompatible(string packageName)
138+
{
139+
AddRequest request = Client.Add(packageName);
140+
DateTime deadline = DateTime.UtcNow.AddMinutes(10);
141+
142+
while (!request.IsCompleted)
143+
{
144+
if (DateTime.UtcNow >= deadline)
145+
{
146+
throw new TimeoutException(
147+
"Timed out resolving " + packageName + ".");
148+
}
149+
150+
Thread.Sleep(100);
151+
}
152+
153+
if (request.Status != StatusCode.Success)
154+
{
155+
throw new InvalidOperationException(
156+
"Cannot resolve " + packageName + ": "
157+
+ request.Error.message);
158+
}
159+
160+
Debug.Log("Resolved Unity default package: "
161+
+ request.Result.packageId);
162+
}
163+
}
164+
CSHARP
154165
155-
manifest_entry="$(
156-
tar -tzf "$template_archive" \
157-
| grep '/Packages/manifest.json$' \
158-
| head -n 1
159-
)"
160-
if [[ -z "$manifest_entry" ]]; then
161-
echo "::error::Cannot find Packages/manifest.json in $template_archive."
162-
exit 1
163-
fi
164-
165-
tar -xOzf "$template_archive" "$manifest_entry" \
166-
> /tmp/unity-default-packages.json
167-
fi
166+
"$UNITY_EDITOR_PATH" \
167+
-projectPath "$UNITY_TEST_PROJECT" \
168+
-batchmode \
169+
-nographics \
170+
-quit \
171+
-executeMethod ResolveDefaultPackages.Run \
172+
-logFile -
168173
169-
if ! jq -e '
170-
.dependencies["com.unity.test-framework"] != null
171-
and .dependencies["com.unity.ugui"] != null
172-
' /tmp/unity-default-packages.json >/dev/null
173-
then
174-
echo "::error::Unity defaults do not define test-framework and ugui."
175-
exit 1
176-
fi
177-
cat /tmp/unity-default-packages.json
174+
rm -f "$resolver_file" "$resolver_file.meta"
175+
jq -e '
176+
.dependencies["com.unity.test-framework"] as $testFramework
177+
| .dependencies["com.unity.ugui"] as $ugui
178+
| select($testFramework != null and $ugui != null)
179+
| {
180+
"com.unity.test-framework": $testFramework,
181+
"com.unity.ugui": $ugui
182+
}
183+
' "$UNITY_TEST_PROJECT/Packages/manifest.json"
178184
179185
- name: Prepare test project
180186
shell: bash
181187
run: |
188+
package_name="$(jq -er '.name' MyPlugin/package.json)"
182189
jq \
183-
--slurpfile unityDefaults /tmp/unity-default-packages.json \
184-
--arg packageName "com.litefeel.aligntools" \
190+
--arg packageName "$package_name" \
185191
--arg pluginPath "file:$GITHUB_WORKSPACE/MyPlugin" \
186-
'{
187-
dependencies: {
188-
"com.unity.test-framework":
189-
$unityDefaults[0].dependencies["com.unity.test-framework"],
190-
"com.unity.ugui":
191-
$unityDefaults[0].dependencies["com.unity.ugui"],
192-
($packageName): $pluginPath
193-
},
194-
testables: [$packageName]
195-
}' \
192+
'.dependencies[$packageName] = $pluginPath
193+
| .testables = (
194+
((.testables // []) + [$packageName]) | unique
195+
)' \
196196
"$UNITY_TEST_PROJECT/Packages/manifest.json" \
197197
> "$UNITY_TEST_PROJECT/Packages/manifest.json.tmp"
198198
mv "$UNITY_TEST_PROJECT/Packages/manifest.json.tmp" \

0 commit comments

Comments
 (0)