Skip to content

Commit 2f7a995

Browse files
committed
Add "run <project> --upgrade <version>" to upgrade a project to a specific Unity version
1 parent b7b8b56 commit 2f7a995

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

Command/Program.cs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public class InstallUnityCLI
103103
/// </summary>
104104
public AllowNewer allowNewer;
105105
/// <summary>
106+
/// Version to upgrade project to when running.
107+
/// </summary>
108+
public string upgradeVersion;
109+
/// <summary>
106110
/// Arguments to launch Unity with.
107111
/// </summary>
108112
public List<string> unityArguments = new List<string>();
@@ -170,6 +174,7 @@ public override string ToString()
170174

171175
if (child) cmd += " --child";
172176
if (allowNewer != AllowNewer.None) cmd += " --allow-newer " + allowNewer.ToString().ToLower();
177+
if (upgradeVersion != null) cmd += " --upgrade " + upgradeVersion;
173178
if (unityArguments.Count > 0) cmd += " -- " + string.Join(" ", unityArguments);
174179

175180
return cmd;
@@ -270,6 +275,9 @@ public static Arguments<InstallUnityCLI> ArgumentsDefinition {
270275
.Option((InstallUnityCLI t, AllowNewer v) => t.allowNewer = v, "a", "allow-newer", "allownewer")
271276
.ArgumentName("none|hash|build|patch|minor|all")
272277
.Description("Allow newer versions of Unity to open a project")
278+
.Option((InstallUnityCLI t, string v) => t.upgradeVersion = v, "upgrade")
279+
.ArgumentName("<version>")
280+
.Description("Run the project with the highest installed Unity version matching the pattern")
273281

274282
.Action("create", (t, a) => t.action = a)
275283
.Description("Create a new empty Unity project")
@@ -1331,23 +1339,46 @@ public async Task Run()
13311339
throw new Exception("Could not parse version from ProjectVersion.txt: " + versionPath);
13321340
}
13331341

1334-
var allowedVersion = version;
1335-
if (allowNewer >= AllowNewer.Hash) allowedVersion.hash = null;
1336-
if (allowNewer >= AllowNewer.Build) allowedVersion.build = -1;
1337-
if (allowNewer >= AllowNewer.Patch) { allowedVersion.patch = -1; allowedVersion.type = UnityVersion.Type.Undefined; }
1338-
if (allowNewer >= AllowNewer.Minor) allowedVersion.minor = -1;
1339-
if (allowNewer >= AllowNewer.All) allowedVersion.major = -1;
1340-
foreach (var install in installs.OrderByDescending(i => i.version)) {
1341-
// Prevent downgrading project
1342-
if (version > install.version)
1343-
break;
1344-
// Exact match trumps fuzzy
1345-
if (version.FuzzyMatches(install.version)) {
1346-
installation = install;
1342+
if (upgradeVersion != null && allowNewer != AllowNewer.None) {
1343+
throw new Exception("--upgrade and --allow-newer options cannot be used together");
1344+
}
1345+
1346+
if (upgradeVersion != null) {
1347+
var uprgadeToVersion = new UnityVersion(upgradeVersion);
1348+
if (!uprgadeToVersion.IsValid) {
1349+
throw new Exception("Not a valid --upgrade Unity version pattern: " + upgradeVersion);
13471350
}
1348-
// Fuzzy match only newest version
1349-
if (installation == null && allowedVersion.FuzzyMatches(install.version, allowTypeUpgrade: false)) {
1350-
installation = install;
1351+
1352+
// Use version explicitly set with --upgrade
1353+
foreach (var install in installs.OrderByDescending(i => i.version)) {
1354+
// Prevent downgrading project
1355+
if (version > install.version)
1356+
break;
1357+
1358+
if (uprgadeToVersion.FuzzyMatches(install.version)) {
1359+
installation = install;
1360+
}
1361+
}
1362+
} else {
1363+
// Check if an installed version matches the allowed upgrade path
1364+
var allowedVersion = version;
1365+
if (allowNewer >= AllowNewer.Hash) allowedVersion.hash = null;
1366+
if (allowNewer >= AllowNewer.Build) allowedVersion.build = -1;
1367+
if (allowNewer >= AllowNewer.Patch) { allowedVersion.patch = -1; allowedVersion.type = UnityVersion.Type.Undefined; }
1368+
if (allowNewer >= AllowNewer.Minor) allowedVersion.minor = -1;
1369+
if (allowNewer >= AllowNewer.All) allowedVersion.major = -1;
1370+
foreach (var install in installs.OrderByDescending(i => i.version)) {
1371+
// Prevent downgrading project
1372+
if (version > install.version)
1373+
break;
1374+
// Exact match trumps fuzzy
1375+
if (version.FuzzyMatches(install.version)) {
1376+
installation = install;
1377+
}
1378+
// Fuzzy match only newest version
1379+
if (installation == null && allowedVersion.FuzzyMatches(install.version, allowTypeUpgrade: false)) {
1380+
installation = install;
1381+
}
13511382
}
13521383
}
13531384

@@ -1368,7 +1399,7 @@ public async Task Run()
13681399
unityArguments.Add(projectPath);
13691400
}
13701401

1371-
if (allowNewer != AllowNewer.None) {
1402+
if (allowNewer != AllowNewer.None || upgradeVersion != null) {
13721403
unityArguments.Add("-skipUpgradeDialogs");
13731404
}
13741405
}
@@ -1377,9 +1408,9 @@ public async Task Run()
13771408
var projectName = Path.GetFileName(projectPath);
13781409
if (installation == null) {
13791410
Logger.LogError($"Could not run project '{projectName}', Unity {version} not installed");
1380-
1411+
13811412
var next = installs.Where(i => i.version > version).OrderBy(i => i.version).FirstOrDefault();
1382-
if (!next.version.IsValid) {
1413+
if (next == null || !next.version.IsValid) {
13831414
Environment.Exit(1);
13841415
} else {
13851416
var allowUpgrade = false;

0 commit comments

Comments
 (0)