Repository navigation
Expand file tree
/
Copy pathrelease.py
More file actions
1405 lines (1249 loc) · 64.3 KB
/
Copy pathrelease.py
File metadata and controls
1405 lines (1249 loc) · 64.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
release.py - one-shot release automation for a standalone Unity tool/module repo.
Run it from inside a module repo with a version number and it does the whole GitHub
release by itself:
1. Guards: refuses on a dirty working tree or an existing tag, and requires
module.json's "version" to equal the version you are releasing.
2. Resolves the dependency closure from module.json: downloads each REQUIRED
dependency's LATEST release .unitypackage, reads the module.json embedded in
it to discover transitive deps, and recurses. Optional deps are skipped.
3. If module.json declares a "sample", packs it into a separate
<name>.Samples.unitypackage (--sample separate, the default) or folds it into
the core package (--sample bundled, the default with --asset-store, whose
listing holds one package). A Samples~/ folder has the `~` stripped so it
installs as an importable Samples/; a plain folder ships as it is.
4. Verifies LOCALLY in a throwaway Unity project (skip with --skip-tests):
the module's committed source + the downloaded dependency packages + the
sample package (imported, so the sample is compiled too) + the UPM "packages".
Compiles, runs any EditMode tests, and builds a Windows player (of "buildScene"
if set, else an empty scene). ANY failure aborts before anything is pushed.
5. Packs the core <name>.unitypackage in pure Python (no Unity/license needed),
merging the required dependency packages in (deduplicated by GUID) so the core
release is self-contained.
6. Pushes the branch, creates + pushes the tag, creates the GitHub Release (with
auto-generated notes), and uploads the core package (+ the sample package).
7. Prints the permanent "releases/latest/download/..." URLs for your docs.
With --asset-store, steps 1-5 are the same and 6-7 are replaced: nothing is pushed or
tagged, and the core package is uploaded to the module's DRAFT on the Unity Asset
Store instead (see "ASSET STORE" below).
--------------------------------------------------------------------------------
USAGE
--------------------------------------------------------------------------------
From inside the module repo (most common):
python release.py 1.2.0
Point at another repo / override the derived name:
python release.py 1.2.0 --repo /path/to/module --name MyModule
Handy flags:
--skip-tests skip the local Unity verify (faster; when already confirmed)
--exclude A B extra path prefixes to keep OUT of the core package
(default: ReadmeResources; the "sample" path is auto-excluded
unless --sample bundled, and so is everything in module.json's
"exclude")
--sample separate|bundled
ship module.json's "sample" as its own package, or inside the
core (default: separate; bundled with --asset-store)
--draft/--prerelease make the GitHub release a draft / prerelease
--asset-store upload to the Asset Store draft instead of releasing on GitHub
--asset-store-list print the publisher's Asset Store packages and exit
--unity <path> explicit Unity.exe (else auto-detected from the Unity Hub for
the project's version, or set UNITY_PATH)
--test-platforms EditMode PlayMode
--keep-test-project keep the throwaway verify project on disk (to debug a failure)
REQUIREMENTS
Python 3.9+, git on PATH, a Unity install matching the project (for the verify),
and a GitHub token with Contents:write. The token is read from $GITHUB_TOKEN /
$GH_TOKEN, or from a gitignored `.release_token` file in the repo. The `gh` CLI is
NOT needed -- this talks to the GitHub REST API directly.
--------------------------------------------------------------------------------
module.json -- how to create one for a new module
--------------------------------------------------------------------------------
Put module.json at the repo ROOT and COMMIT it together with module.json.meta: it
ships inside the package so ModuleManager can read it in the buyer's project, and the
packer only packs files whose .meta is committed. Also gitignore the Unity-generated
metas for the tooling files: release.py.meta, dist.meta, __pycache__.meta (otherwise
release.py itself would get packed). Only "name" + "version" are required.
{
"name": "MyModule", // module id + default install folder
"version": "1.0.0", // MUST equal the released version (enforced)
"installPath": "Assets/MyModule", // where the package lands on import
"unity": "6000.1", // informational / ModuleManager
"packages": { // optional: UPM deps the CORE needs; added to
"com.unity.nuget.newtonsoft-json": "3.2.1" // the verify scaffold's manifest. NOT
}, // bundled -- buyers get them from the registry.
"defines": ["MODULE_MYMODULE_EXIST"], // optional; a define this module declares
// (see "optional dependencies" below)
"buildScene": "Samples/Demo/Demo.unity",// optional; the scene the verify's Windows
// build builds (post ~-strip path for a
// sample scene). Empty scene if omitted.
"sample": { // optional; a sample folder (Samples~/ or a plain
"path": "Samples~", // Samples/) shipped as a SEPARATE <name>.Samples
"packages": { // .unitypackage (a `~` is stripped). Excluded
"com.unity.ugui": "2.0.0", // from the core, and compile-tested by importing
"com.unity.inputsystem": "1.14.0"// it into the verify. "packages" = extra UPM
} // deps the SAMPLE needs. Shorthand: "sample":
}, // "Samples~" (no extra packages).
"exclude": ["docs"], // optional; path prefixes (repo-relative)
// kept OUT of the package and of the verify
// scaffold, e.g. a docs folder that is only
// read on GitHub. Added to --exclude.
"assetStore": { // optional; the Asset Store listing that
"package": "My Module" // --asset-store uploads to: its name or its
}, // package id. Shorthand: "assetStore": "My Module".
"dependencyRoot": "Assets/ArcaneOnyx/Modules", // optional; every BUNDLED module is
// relocated to <root>/<Name>, whatever its
// own installPath says (see "layout" below).
"dependencies": { // other MODULES this one needs
"OtherModule": {
"repo": "platinio/Unity-OtherModule", // fetched from its LATEST release and
"version": "1.0.0", // BUNDLED (deduped). "version" = the
"installPath": "Assets/Vendor/Other" // minimum needed (ModuleManager warnings).
}, // "installPath" = optional per-dependency
"Zenject": { // override of dependencyRoot.
"repo": "platinio/Unity-Zenject",
"version": "9.2.0",
"optional": true // NOT fetched/bundled; the module compiles and
} // works without it (see below).
}
}
A leaf module just omits "dependencies".
LAYOUT (where bundled dependencies land):
A module's own "installPath" is where it lands when imported on its own. A module
that BUNDLES it may put it somewhere else: "dependencyRoot" relocates every bundled
module (transitive ones included) to <root>/<Name>, and a dependency entry's own
"installPath" overrides that for one module. The relocation rewrites the pathnames
inside the downloaded package and the "installPath" in its embedded module.json,
and the verify scaffold uses the same layout, so what is tested is what ships. Two
routes to the same module (listed directly and nested inside another dependency)
collapse to one copy because the paths agree and the packer dedupes by GUID.
OPTIONAL DEPENDENCIES (make an integration optional):
1. Gate the integration code behind `#if MODULE_<DEP>_EXIST`.
2. Keep the asmdef reference to the dep -- a missing asmdef reference is a non-fatal
WARNING as long as the code using those types is #if'd out.
3. Have the DEPENDENCY module ship an [InitializeOnLoad] editor script that adds
MODULE_<DEP>_EXIST to the scripting define symbols when it is present (so the
define is set exactly when the module is there).
4. Mark it "optional": true here so it is not fetched/bundled.
ASSET STORE (--asset-store):
Packs and verifies exactly as a GitHub release does, then uploads the core package
to the listing named by module.json's "assetStore" (or --asset-store-package). It
does NOT create the listing, edit its metadata, or submit it for review: the
listing must already have a DRAFT version in the Publisher Portal, and Submit is
still pressed there. A listing holds one package, so the "sample" is folded into the
core by default (--sample bundled); with --sample separate it is packed but not uploaded.
The upload goes through Unity's own Asset Store Tools package, driven in a throwaway
batch-mode project. That needs:
* the com.unity.asset-store-tools package on disk -- found in the parent project's
Packages/ or Library/PackageCache, or pass --asset-store-tools <folder>;
* a publisher login on this machine. Nothing is typed or stored by this script:
it reuses the session Asset Store Tools cached the last time you logged in
through its Uploader window (any project), falling back to the Editor's own
signed-in account. If both fail, open Tools > Asset Store > Uploader and log in.
`python release.py --asset-store-list` prints every package the publisher has (name,
package id, status) -- use it to find what to put in "assetStore".
The .unitypackage format is a gzip-compressed tar: one folder per asset, named by the
asset's GUID (from its .meta), holding `asset`, `asset.meta`, `pathname`. That is why
packing needs no Unity install.
"""
import argparse
import atexit
import gzip
import io
import json
import os
import re
import shutil
import subprocess
import sys
import tarfile
import tempfile
import time
import urllib.error
import urllib.request
import xml.etree.ElementTree as ET
API = "https://api.github.com"
GUID_RE = re.compile(rb"^guid:\s*([0-9a-fA-F]+)", re.MULTILINE)
DEFAULT_UNITY_VERSION = "6000.1.15f1"
DEFAULT_TF_VERSION = "1.4.6" # com.unity.test-framework fallback
# --------------------------------------------------------------------------- #
# small helpers
# --------------------------------------------------------------------------- #
def fail(msg):
print(f"\n[release] ERROR: {msg}", file=sys.stderr)
sys.exit(1)
def info(msg):
print(f"[release] {msg}")
def git(repo, *args, check=True):
result = subprocess.run(
["git", "-C", repo, *args], capture_output=True, text=True
)
if check and result.returncode != 0:
fail(f"git {' '.join(args)} failed:\n{result.stderr or result.stdout}")
return (result.stdout or "").strip()
def token(repo):
"""GitHub token: env var first, then a gitignored .release_token file in the repo."""
tok = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
if not tok:
f = os.path.join(repo, ".release_token")
if os.path.isfile(f):
tok = open(f).read().strip()
if not tok:
fail(
"no GitHub token found. Set GITHUB_TOKEN (or GH_TOKEN), or put the token "
"in a gitignored .release_token file in the repo. Needs Contents: write."
)
return tok
def api(method, url, data=None, tok=None, headers=None, raw=False):
hdrs = {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "unity-tool-release-script",
}
if tok:
hdrs["Authorization"] = f"Bearer {tok}"
if headers:
hdrs.update(headers)
body = None
if data is not None and not raw:
body = json.dumps(data).encode()
hdrs["Content-Type"] = "application/json"
elif raw:
body = data
req = urllib.request.Request(url, data=body, method=method, headers=hdrs)
try:
with urllib.request.urlopen(req) as resp:
payload = resp.read()
return json.loads(payload) if payload else {}
except urllib.error.HTTPError as e:
fail(f"{method} {url} -> HTTP {e.code}\n{e.read().decode(errors='replace')}")
def owner_repo(repo):
url = git(repo, "remote", "get-url", "origin")
m = re.search(r"github\.com[:/]+([^/]+)/(.+?)(?:\.git)?/?$", url)
if not m:
fail(f"could not parse a github owner/repo from origin url: {url}")
return m.group(1), m.group(2)
# --------------------------------------------------------------------------- #
# module.json + dependency closure
# --------------------------------------------------------------------------- #
def load_manifest(repo):
"""The module's own module.json, or None if it doesn't have one."""
path = os.path.join(repo, "module.json")
if not os.path.isfile(path):
return None
manifest = json.load(open(path, encoding="utf-8-sig"))
if not git(repo, "ls-files", "module.json.meta"):
fail("module.json exists but module.json.meta is not tracked by git. The "
"manifest must ship inside the package (ModuleManager reads it), and "
"the packer only packs files whose .meta is committed.")
return manifest
class _DropAuthOnRedirect(urllib.request.HTTPRedirectHandler):
"""GitHub redirects asset downloads to its storage backend, which refuses a
request carrying both its signed URL and a forwarded Authorization header."""
def redirect_request(self, req, fp, code, msg, headers, newurl):
new = super().redirect_request(req, fp, code, msg, headers, newurl)
if new is not None:
new.remove_header("Authorization")
return new
def download_release_package(slug, tok):
"""Download the .unitypackage asset of `slug`'s latest GitHub release.
Returns (tag, raw_tar_bytes). The asset is matched by extension, not by
name, so older releases with versioned filenames still work. The download
goes through the API with the token so private repos work too.
"""
rel = api("GET", f"{API}/repos/{slug}/releases/latest", tok=tok)
packs = [a for a in rel.get("assets", []) if a["name"].endswith(".unitypackage")]
# a module release may also carry a separate <name>.Samples.unitypackage;
# always bundle the CORE package, never the sample one.
core = [a for a in packs if not a["name"].endswith(".Samples.unitypackage")]
asset = (core or packs)[0] if (core or packs) else None
if not asset:
fail(f"latest release of {slug} ({rel.get('tag_name')}) has no "
f".unitypackage asset. Release that module first.")
req = urllib.request.Request(asset["url"], headers={
"Accept": "application/octet-stream",
"Authorization": f"Bearer {tok}",
"User-Agent": "unity-tool-release-script",
"X-GitHub-Api-Version": "2022-11-28",
})
try:
with urllib.request.build_opener(_DropAuthOnRedirect()).open(req) as resp:
data = resp.read()
except urllib.error.HTTPError as e:
fail(f"downloading {asset['name']} from {slug} {rel.get('tag_name')} -> HTTP {e.code}")
return rel["tag_name"], gzip.decompress(data)
def package_groups(raw):
"""Group a raw unitypackage tar's members by asset GUID."""
tar = tarfile.open(fileobj=io.BytesIO(raw))
groups = {}
for member in tar.getmembers():
name = member.name[2:] if member.name.startswith("./") else member.name
guid, _, part = name.partition("/")
if guid and part and member.isfile():
groups.setdefault(guid, {})[part] = member
return tar, groups
def package_manifest(raw):
"""The module's own module.json inside a package (shallowest one), or None.
A bundled package can contain the manifests of its own bundled dependencies
too; the module's own manifest is the one closest to its install root.
"""
tar, groups = package_groups(raw)
best = None
for parts in groups.values():
if "pathname" not in parts or "asset" not in parts:
continue
pathname = tar.extractfile(parts["pathname"]).read().decode().splitlines()[0]
if os.path.basename(pathname) == "module.json":
if best is None or pathname.count("/") < best[0].count("/"):
best = (pathname, parts["asset"])
if best is None:
return None
return json.loads(tar.extractfile(best[1]).read().decode("utf-8-sig"))
def read_pathname(tar, member):
return tar.extractfile(member).read().decode().splitlines()[0]
def module_roots(tar, groups):
"""{install root: module name} for every module.json inside a package."""
roots = {}
for parts in groups.values():
if "pathname" not in parts or "asset" not in parts:
continue
pathname = read_pathname(tar, parts["pathname"])
if os.path.basename(pathname) != "module.json":
continue
root = os.path.dirname(pathname)
sub = json.loads(tar.extractfile(parts["asset"]).read().decode("utf-8-sig"))
roots[root] = sub.get("name") or os.path.basename(root)
return roots
def dependency_layout(manifest):
"""The bundler's layout rules: a root for every bundled module plus per-name overrides."""
manifest = manifest or {}
overrides = {}
for name, spec in manifest.get("dependencies", {}).items():
if isinstance(spec, dict) and spec.get("installPath"):
overrides[name] = spec["installPath"].rstrip("/")
root = (manifest.get("dependencyRoot") or "").rstrip("/") or None
if root is None and not overrides:
return None
return {"root": root, "overrides": overrides}
def relocate_package(raw, dep_name, layout):
"""Rewrite a dependency package so each module in it lands where the bundler wants.
Every module.json inside the package names a module and its current install
root (a bundled package carries its own dependencies' manifests too). Each root
moves to the override for that name, else <dependencyRoot>/<name>; the embedded
module.json is rewritten to say so. A package without a manifest is relocated
as one module rooted at the common folder of its assets.
"""
tar, groups = package_groups(raw)
roots = module_roots(tar, groups)
if not roots:
pathnames = [read_pathname(tar, p["pathname"]) for p in groups.values() if "pathname" in p]
common = os.path.commonpath(pathnames).replace("\\", "/") if pathnames else ""
if common.count("/") < 1:
info(f" ({dep_name} has no module.json and no single root folder; not relocated)")
return raw
roots[common] = dep_name
moves = {}
for old_root, name in roots.items():
new_root = layout["overrides"].get(name) or (f"{layout['root']}/{name}" if layout["root"] else None)
if new_root and new_root != old_root:
moves[old_root] = new_root
if not moves:
return raw
ordered = sorted(moves.items(), key=lambda kv: -len(kv[0])) # nested roots first
def moved(pathname):
for old, new in ordered:
if pathname == old or pathname.startswith(old + "/"):
return new + pathname[len(old):]
return pathname
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as out:
for guid, parts in groups.items():
pathname = read_pathname(tar, parts["pathname"]) if "pathname" in parts else None
for part, member in parts.items():
data = tar.extractfile(member).read()
if part == "pathname" and pathname is not None:
lines = data.decode().splitlines()
lines[0] = moved(pathname)
data = "\n".join(lines).encode()
elif part == "asset" and pathname is not None and os.path.dirname(pathname) in moves \
and os.path.basename(pathname) == "module.json":
sub = json.loads(data.decode("utf-8-sig"))
sub["installPath"] = moves[os.path.dirname(pathname)]
data = (json.dumps(sub, indent=4) + "\n").encode()
ti = tarfile.TarInfo(f"{guid}/{part}")
ti.size = len(data)
ti.mtime = member.mtime
ti.mode = 0o644
out.addfile(ti, io.BytesIO(data))
for old, new in ordered:
info(f" {roots[old]}: {old} -> {new}")
return buf.getvalue()
def write_package(tar, groups):
"""Serialise a {guid: {part: member}} selection back into a raw tar."""
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as out:
for guid, parts in groups.items():
for part, member in parts.items():
data = tar.extractfile(member).read()
ti = tarfile.TarInfo(f"{guid}/{part}")
ti.size = len(data)
ti.mtime = member.mtime
ti.mode = 0o644
out.addfile(ti, io.BytesIO(data))
return buf.getvalue()
def prune_nested_duplicates(dep_packages):
"""Drop, from each package, the modules that are also fetched directly.
A bundled release carries copies of its own dependencies, frozen at the
version it was built against. When this module fetches that dependency
itself it gets the LATEST release, and that copy must win: the packer keeps
the first GUID it sees and the scaffold keeps the last file written, so a
stale nested copy would leak into one or the other. Modules only reachable
through nesting are kept.
"""
direct = {dep["name"] for dep in dep_packages}
for dep in dep_packages:
tar, groups = package_groups(dep["raw"])
roots = module_roots(tar, groups)
foreign = {root: name for root, name in roots.items()
if name != dep["name"] and name in direct}
if not foreign:
continue
ordered = sorted(foreign, key=len, reverse=True)
kept = {}
for guid, parts in groups.items():
pathname = read_pathname(tar, parts["pathname"]) if "pathname" in parts else None
if pathname and any(pathname == r or pathname.startswith(r + "/") for r in ordered):
continue
kept[guid] = parts
dropped = len(groups) - len(kept)
dep["raw"] = write_package(tar, kept)
info(f" {dep['name']}: dropped its nested copy of "
f"{', '.join(sorted(set(foreign.values())))} ({dropped} assets; fetched directly)")
def resolve_dependency_closure(manifest, tok, layout=None):
"""BFS the dependency graph, downloading each REQUIRED dependency's latest release.
Returns a list of {name, repo, tag, raw} dicts, deduplicated by repo. Each
downloaded package's embedded module.json supplies its own dependencies;
releases that predate module.json are treated as leaves.
Dependencies marked "optional": true are NOT bundled -- they're integrations
(e.g. Zenject) whose code lives behind a define-constrained assembly, so the
module works without them. ModuleManager still sees them in module.json.
Each entry also carries the "packages" its manifest declares, so the verify
scaffold can install the UPM packages a bundled dependency compiles against.
With a layout (see dependency_layout) every downloaded package is relocated
right here, so the verify scaffold and the bundled release agree.
"""
packages, visited = [], set()
queue = list((manifest or {}).get("dependencies", {}).items())
while queue:
name, spec = queue.pop(0)
if isinstance(spec, dict) and spec.get("optional"):
info(f"skipping optional dependency {name} (not bundled)")
continue
slug = spec["repo"] if isinstance(spec, dict) else spec
if slug.lower() in visited:
continue
visited.add(slug.lower())
info(f"fetching dependency {name} ({slug}) ...")
tag, raw = download_release_package(slug, tok)
info(f" got {name} {tag}")
if layout:
raw = relocate_package(raw, name, layout)
sub = package_manifest(raw)
if sub:
queue.extend(sub.get("dependencies", {}).items())
else:
info(f" ({name} release has no module.json; treating as leaf)")
upm = (sub or {}).get("packages", {})
if upm:
info(f" {name} needs UPM packages: {', '.join(upm)}")
packages.append({"name": name, "repo": slug, "tag": tag, "raw": raw,
"packages": upm})
return packages
# --------------------------------------------------------------------------- #
# environment discovery (parent project -> Unity version / test-framework)
# --------------------------------------------------------------------------- #
def find_parent_project(start):
"""Walk up looking for a real Unity project (ProjectSettings/ProjectVersion.txt)."""
cur = os.path.abspath(start)
while True:
if os.path.isfile(os.path.join(cur, "ProjectSettings", "ProjectVersion.txt")):
return cur
parent = os.path.dirname(cur)
if parent == cur:
return None
cur = parent
def detect_unity_version(parent, override):
if override:
return override
if parent:
txt = open(os.path.join(parent, "ProjectSettings", "ProjectVersion.txt")).read()
m = re.search(r"m_EditorVersion:\s*(\S+)", txt)
if m:
return m.group(1)
return DEFAULT_UNITY_VERSION
def detect_tf_version(parent):
if parent:
mf = os.path.join(parent, "Packages", "manifest.json")
if os.path.isfile(mf):
deps = json.load(open(mf)).get("dependencies", {})
if "com.unity.test-framework" in deps:
return deps["com.unity.test-framework"]
return DEFAULT_TF_VERSION
# Built-in engine modules to fall back on when there's no parent project to copy
# them from (e.g. the tool repo cloned standalone in CI).
FALLBACK_MODULES = [
"ai", "androidjni", "animation", "assetbundle", "audio", "cloth",
"director", "imageconversion", "imgui", "jsonserialize", "particlesystem",
"physics", "physics2d", "screencapture", "terrain", "terrainphysics",
"tilemap", "ui", "uielements", "umbra", "unityanalytics", "unitywebrequest",
"unitywebrequestassetbundle", "unitywebrequestaudio", "unitywebrequesttexture",
"unitywebrequestwww", "vehicles", "video", "wind", "xr",
]
def scaffold_deps(parent, tf_version):
"""manifest dependencies for the throwaway project: engine modules + test framework."""
deps = {"com.unity.test-framework": tf_version}
if parent:
mf = os.path.join(parent, "Packages", "manifest.json")
if os.path.isfile(mf):
for k, v in json.load(open(mf)).get("dependencies", {}).items():
if k.startswith("com.unity.modules."):
deps[k] = v
if not any(k.startswith("com.unity.modules.") for k in deps):
for m in FALLBACK_MODULES:
deps[f"com.unity.modules.{m}"] = "1.0.0"
return deps
def find_unity(version, override):
"""Locate Unity.exe/binary for `version`. Returns path or None."""
if override:
return override if os.path.exists(override) else None
if os.environ.get("UNITY_PATH") and os.path.exists(os.environ["UNITY_PATH"]):
return os.environ["UNITY_PATH"]
candidates = []
if sys.platform.startswith("win"):
for base in (r"C:\Program Files\Unity\Hub\Editor",
r"C:\Program Files\Unity Hub\Editor"):
candidates.append(os.path.join(base, version, "Editor", "Unity.exe"))
elif sys.platform == "darwin":
candidates.append(f"/Applications/Unity/Hub/Editor/{version}/Unity.app/Contents/MacOS/Unity")
else:
candidates.append(os.path.expanduser(f"~/Unity/Hub/Editor/{version}/Editor/Unity"))
return next((c for c in candidates if os.path.exists(c)), None)
# --------------------------------------------------------------------------- #
# scaffold throwaway project + copy tool content
# --------------------------------------------------------------------------- #
CI_BUILD_CS = r"""
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
// Injected by release.py: builds a StandaloneWindows64 player so we know the
// tool's runtime code compiles for a real player, not just the editor. If
// release.py passes "-ciScene <path>" (from module.json's buildScene), that
// scene is built; otherwise a throwaway empty scene is used.
public static class CIBuild
{
private static string CiSceneArg()
{
var args = Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length - 1; i++)
if (args[i] == "-ciScene") return args[i + 1];
return null;
}
public static void BuildWindows()
{
try
{
string sceneArg = CiSceneArg();
string[] scenes;
if (!string.IsNullOrEmpty(sceneArg))
{
if (!File.Exists(sceneArg))
{
Debug.LogError($"[CIBuild] buildScene not found: {sceneArg}");
EditorApplication.Exit(1);
return;
}
scenes = new[] { sceneArg };
}
else
{
scenes = EditorBuildSettings.scenes.Where(s => s.enabled)
.Select(s => s.path).ToArray();
}
if (scenes.Length == 0)
{
var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene,
NewSceneMode.Single);
var p = "Assets/CI/_ci_empty.unity";
EditorSceneManager.SaveScene(scene, p);
scenes = new[] { p };
}
var outDir = Path.Combine(Path.GetTempPath(), "ci_build_" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(outDir);
var opts = new BuildPlayerOptions
{
scenes = scenes,
locationPathName = Path.Combine(outDir, "player.exe"),
target = BuildTarget.StandaloneWindows64,
targetGroup = BuildTargetGroup.Standalone,
options = BuildOptions.None,
};
var report = BuildPipeline.BuildPlayer(opts);
var s = report.summary;
Debug.Log($"[CIBuild] result={s.result} errors={s.totalErrors} size={s.totalSize}");
try { Directory.Delete(outDir, true); } catch {}
EditorApplication.Exit(s.result == BuildResult.Succeeded ? 0 : 1);
}
catch (Exception e)
{
Debug.LogError("[CIBuild] exception: " + e);
EditorApplication.Exit(1);
}
}
}
"""
# --------------------------------------------------------------------------- #
# Asset Store upload (through Unity's Asset Store Tools, in a throwaway project)
# --------------------------------------------------------------------------- #
ASSET_STORE_TOOLS_PACKAGE = "com.unity.asset-store-tools"
# Asset Store Tools keeps its API internal and grants access to an assembly with this
# name (its own tests). Compiling against it means an API change in a newer Asset
# Store Tools shows up as a compile error naming the member, not as a failed upload.
ASSET_STORE_UPLOAD_ASMDEF = {
"name": "AssetStoreTools.Tests",
"references": ["asset-store-tools-editor"],
"includePlatforms": ["Editor"],
"autoReferenced": False,
}
ASSET_STORE_UPLOAD_CS = r"""
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AssetStoreTools;
using AssetStoreTools.Api;
using AssetStoreTools.Api.Models;
using AssetStoreTools.Api.Responses;
using UnityEditor;
using UnityEngine;
// Injected by release.py. "-asMode list" prints the publisher's packages;
// "-asMode check -asPackage <name or id>" finds that package's draft;
// "-asMode upload -asPackage <name or id> -asFile <.unitypackage>" uploads to it.
public static class ReleaseAssetStoreUpload
{
private const string Tag = "[AssetStore] ";
private static string Arg(string name)
{
var args = Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length - 1; i++)
if (args[i] == name) return args[i + 1];
return null;
}
private static void Say(string message) => Debug.Log(Tag + message);
public static async void Run()
{
Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None);
int code = 1;
try
{
code = await RunAsync();
}
catch (Exception e)
{
Say("error: " + e);
}
EditorApplication.Exit(code);
}
private static async Task<int> RunAsync()
{
string mode = Arg("-asMode");
var api = new AssetStoreApi(new AssetStoreClient());
AuthenticationResponse auth = null;
string session = EditorPrefs.GetString(Constants.Cache.SessionTokenKey, string.Empty);
if (!string.IsNullOrEmpty(session))
auth = await api.Authenticate(new SessionAuthentication(session));
if (auth == null || !auth.Success)
{
string cloudToken = CloudProjectSettings.accessToken;
if (!string.IsNullOrEmpty(cloudToken))
auth = await api.Authenticate(new CloudTokenAuthentication(cloudToken));
}
if (auth == null || !auth.Success)
{
Say("error: not logged in" + (auth?.Exception != null ? " (" + auth.Exception.Message + ")" : ""));
return 3;
}
if (!auth.User.IsPublisher)
{
Say("error: account '" + auth.User.Username + "' is not an Asset Store publisher");
return 3;
}
Say("user=" + auth.User.Username);
var data = await api.GetPackages();
if (!data.Success)
{
Say("error: could not read the publisher's packages: " + data.Exception?.Message);
return 1;
}
List<Package> packages = data.Packages ?? new List<Package>();
foreach (var p in packages)
Say("package id=" + p.PackageId + " status=" + p.Status + " name=" + p.Name);
if (mode == "list")
return 0;
string key = Arg("-asPackage");
var matches = packages.Where(p => p.PackageId == key
|| string.Equals(p.Name, key, StringComparison.OrdinalIgnoreCase)).ToList();
if (matches.Count == 0)
{
Say("error: no package named or numbered '" + key + "'");
return 4;
}
var drafts = matches.Where(p => string.Equals(p.Status, "draft", StringComparison.OrdinalIgnoreCase)).ToList();
if (drafts.Count != 1)
{
Say("error: '" + key + "' has " + drafts.Count + " draft versions (status: "
+ string.Join(", ", matches.Select(p => p.Status)) + "); exactly one is needed");
return 4;
}
var draft = drafts[0];
Say("draft id=" + draft.PackageId + " version=" + draft.VersionId + " name=" + draft.Name);
if (mode == "check")
return 0;
string file = Arg("-asFile");
var uploader = new UnityPackageUploader(new UnityPackageUploadSettings
{
VersionId = draft.VersionId,
UnityPackagePath = file,
RootGuid = string.Empty,
RootPath = string.Empty,
ProjectPath = file,
});
var result = await api.UploadPackage(uploader, new ProgressLog());
Say("upload status=" + result.Status + (result.Exception != null ? " (" + result.Exception.Message + ")" : ""));
if (result.Status == UploadStatus.Success) return 0;
return result.Status == UploadStatus.ResponseTimeout ? 5 : 1;
}
private class ProgressLog : IProgress<float>
{
private int lastStep = -1;
public void Report(float percent)
{
int step = (int)(percent / 10f);
if (step <= lastStep) return;
lastStep = step;
Debug.Log(Tag + "uploaded " + step * 10 + "%");
}
}
}
"""
def asset_store_package_key(manifest, override):
"""The listing --asset-store targets: a package name or id, or None."""
if override:
return override
spec = (manifest or {}).get("assetStore")
if isinstance(spec, dict):
spec = spec.get("package")
if spec is None:
return None
if not isinstance(spec, (str, int)) or not str(spec).strip():
fail('module.json "assetStore" must be the listing\'s name or package id, '
'or {"package": <name or id>}.')
return str(spec).strip()
def find_asset_store_tools(parent, override):
"""Folder of the com.unity.asset-store-tools package, or None."""
candidates = [override] if override else []
if parent and not override:
candidates.append(os.path.join(parent, "Packages", ASSET_STORE_TOOLS_PACKAGE))
cache = os.path.join(parent, "Library", "PackageCache")
if os.path.isdir(cache):
candidates += [os.path.join(cache, d) for d in sorted(os.listdir(cache))
if d.startswith(ASSET_STORE_TOOLS_PACKAGE)]
for c in candidates:
if os.path.isfile(os.path.join(c, "package.json")):
return os.path.abspath(c)
return None
class AssetStoreSession:
"""A throwaway project holding Asset Store Tools and the injected uploader.
One project serves every call, so only the first pays for the import. Deleted
when the script exits, however it exits.
"""
def __init__(self, repo, args):
parent = find_parent_project(repo)
tools = find_asset_store_tools(parent, args.asset_store_tools)
if not tools:
fail(f"could not find the {ASSET_STORE_TOOLS_PACKAGE} package. Install Asset "
"Store Tools in the parent project, or pass --asset-store-tools <folder>.")
unity_version = detect_unity_version(parent, args.unity_version)
self.unity_exe = find_unity(unity_version, args.unity)
if not self.unity_exe:
fail(f"could not find Unity {unity_version}. Pass --unity <path> or set UNITY_PATH.")
self.keep = args.keep_test_project
deps = scaffold_deps(parent, DEFAULT_TF_VERSION)
del deps["com.unity.test-framework"]
deps.update(json.load(open(os.path.join(tools, "package.json"))).get("dependencies", {}))
deps[ASSET_STORE_TOOLS_PACKAGE] = "file:" + tools.replace("\\", "/")
self.proj = tempfile.mkdtemp(prefix="asset_store_upload_")
os.makedirs(os.path.join(self.proj, "ProjectSettings"))
os.makedirs(os.path.join(self.proj, "Packages"))
with open(os.path.join(self.proj, "ProjectSettings", "ProjectVersion.txt"), "w") as fh:
fh.write(f"m_EditorVersion: {unity_version}\n")
with open(os.path.join(self.proj, "Packages", "manifest.json"), "w") as fh:
json.dump({"dependencies": deps}, fh, indent=2)
code_dir = os.path.join(self.proj, "Assets", "AssetStoreUpload", "Editor")
os.makedirs(code_dir)
with open(os.path.join(code_dir, "AssetStoreTools.Tests.asmdef"), "w") as fh:
json.dump(ASSET_STORE_UPLOAD_ASMDEF, fh, indent=2)
with open(os.path.join(code_dir, "ReleaseAssetStoreUpload.cs"), "w") as fh:
fh.write(ASSET_STORE_UPLOAD_CS)
self.runs = 0
atexit.register(self.close)
def run(self, mode, package=None, file=None, timeout=1800):
"""Returns the uploader's own log lines, tag stripped. Fails on any error."""
extra = ["-executeMethod", "ReleaseAssetStoreUpload.Run", "-asMode", mode]
if package:
extra += ["-asPackage", package]
if file:
extra += ["-asFile", os.path.abspath(file)]
self.runs += 1
log = os.path.join(self.proj, f"asset-store-{self.runs}-{mode}.log")
rc, logtxt, cs = run_unity(self.unity_exe, self.proj, extra, log, timeout=timeout)
if cs:
fail("the Asset Store uploader did not compile against this Asset Store Tools "
"version:\n " + "\n ".join(cs[:25]))
tag = "[AssetStore] "
lines = [ln.split(tag, 1)[1].strip() for ln in logtxt.splitlines() if tag in ln]
errors = [ln for ln in lines if ln.startswith("error:")]
if rc == 3:
fail("Asset Store login failed (" + "; ".join(errors) + "). Open Tools > Asset "
"Store > Uploader in any Unity project, log in as the publisher, and retry.")
if rc == 5:
fail("the package was sent but the Asset Store did not answer in time. Check the "
"draft in the Publisher Portal before uploading again.")
if rc != 0:
if mode != "list" and any(ln.startswith("package ") for ln in lines):
errors += ["the publisher's packages:"] + \
[" " + ln[len("package "):] for ln in lines if ln.startswith("package ")]
fail(f"Asset Store {mode} failed (exit {rc}).\n "
+ ("\n ".join(errors) if errors
else "Log tail:\n" + "\n".join(logtxt.splitlines()[-25:])))
return lines
def close(self):
if not os.path.isdir(self.proj):
return
if self.keep:
info(f"keeping Asset Store upload project at: {self.proj}")
else:
shutil.rmtree(self.proj, ignore_errors=True)
def scaffold_project(repo, name, unity_version, deps, exclude_prefixes=(), install_path=None):
"""Create a temp Unity project containing the tracked tool content at its install path."""
proj = tempfile.mkdtemp(prefix=f"ci_{name}_")
os.makedirs(os.path.join(proj, "ProjectSettings"))
os.makedirs(os.path.join(proj, "Packages"))
with open(os.path.join(proj, "ProjectSettings", "ProjectVersion.txt"), "w") as fh:
fh.write(f"m_EditorVersion: {unity_version}\n")
with open(os.path.join(proj, "Packages", "manifest.json"), "w") as fh:
json.dump({"dependencies": deps}, fh, indent=2)
# copy every tracked file to the module's install path, preserving structure
dest_root = os.path.join(proj, *(install_path or f"Assets/{name}").split("/"))
for rel in git(repo, "ls-files").splitlines():
if any(rel == p or rel.startswith(p + "/") for p in exclude_prefixes):
continue
src = os.path.join(repo, rel)
if not os.path.isfile(src):
continue
dst = os.path.join(dest_root, rel)
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
# inject the CI build script
ci_dir = os.path.join(proj, "Assets", "CI", "Editor")
os.makedirs(ci_dir)
with open(os.path.join(ci_dir, "CIBuild.cs"), "w") as fh:
fh.write(CI_BUILD_CS)
return proj
def run_unity(unity_exe, project, extra_args, log_path, timeout, nographics=True):
"""nographics=False keeps the graphics device: EditMode tests that touch editor
GUI resources (EditorStyles, plugin icon sets) find them null without it."""
args = [unity_exe, "-batchmode", "-projectPath", project, "-logFile", log_path]
if nographics:
args.insert(2, "-nographics")
args += extra_args
proc = subprocess.run(args, timeout=timeout)
log = ""
if os.path.isfile(log_path):