Skip to content

Commit 16fb618

Browse files
- fix `unity-cli run` input args validation, and ability to run without specifying a project path.
1 parent a9eccdc commit 16fb618

File tree

5 files changed

+82
-43
lines changed

5 files changed

+82
-43
lines changed

.github/workflows/unity-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ jobs:
116116
timeout-minutes: 60
117117
shell: bash
118118
run: |
119-
unity-cli run --log-name Validate -quit -nographics -batchmode -executeMethod Utilities.Editor.BuildPipeline.UnityPlayerBuildTools.ValidateProject -importTMProEssentialsAsset
120-
unity-cli run --log-name Build -quit -nographics -batchmode -buildTarget ${{ matrix.build-target }} -executeMethod Utilities.Editor.BuildPipeline.UnityPlayerBuildTools.StartCommandLineBuild -sceneList Assets/Scenes/SampleScene.unity ${{ matrix.build-args }}
119+
unity-cli run --log-name Validate -quit -executeMethod Utilities.Editor.BuildPipeline.UnityPlayerBuildTools.ValidateProject -importTMProEssentialsAsset
120+
unity-cli run --log-name Build -buildTarget ${{ matrix.build-target }} -quit -executeMethod Utilities.Editor.BuildPipeline.UnityPlayerBuildTools.StartCommandLineBuild -sceneList Assets/Scenes/SampleScene.unity ${{ matrix.build-args }}
121121
- name: Uninstall Editor
122122
shell: bash
123123
run: |

package-lock.json

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rage-against-the-pixel/unity-cli",
3-
"version": "1.4.3",
3+
"version": "1.5.0",
44
"description": "A command line utility for the Unity Game Engine.",
55
"author": "RageAgainstThePixel",
66
"license": "MIT",
@@ -60,7 +60,7 @@
6060
},
6161
"devDependencies": {
6262
"@types/jest": "^30.0.0",
63-
"@types/node": "^24.9.0",
63+
"@types/node": "^24.9.1",
6464
"@types/semver": "^7.7.1",
6565
"@types/update-notifier": "^6.0.8",
6666
"jest": "^30.2.0",

src/cli.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ program.commandsGroup('Unity Editor:');
351351
program.command('run')
352352
.description('Run command line args directly to the Unity Editor.')
353353
.option('--unity-editor <unityEditor>', 'The path to the Unity Editor executable. If unspecified, --unity-project or the UNITY_EDITOR_PATH environment variable must be set.')
354-
.option('--unity-project <unityProject>', 'The path to a Unity project. If unspecified, the UNITY_PROJECT_PATH environment variable or the current working directory will be used.')
354+
.option('--unity-project <unityProject>', 'The path to a Unity project. If unspecified, the UNITY_PROJECT_PATH environment variable will be used, otherwise no project will be specified.')
355355
.option('--log-name <logName>', 'The name of the log file.')
356356
.option('--verbose', 'Enable verbose logging.')
357357
.allowUnknownOption(true)
@@ -367,20 +367,38 @@ program.command('run')
367367
const editorPath = options.unityEditor?.toString()?.trim() || process.env.UNITY_EDITOR_PATH || undefined;
368368

369369
if (editorPath && editorPath.length > 0) {
370-
unityEditor = new UnityEditor(editorPath);
370+
try {
371+
unityEditor = new UnityEditor(editorPath);
372+
} catch {
373+
Logger.instance.error(`The specified Unity Editor path is invalid: ${editorPath}. Use --unity-editor or set the UNITY_EDITOR_PATH environment variable.`);
374+
process.exit(1);
375+
}
371376
}
372377

373-
const projectPath = options.unityProject?.toString()?.trim() || process.env.UNITY_PROJECT_PATH || process.cwd();
374-
const unityProject = await UnityProject.GetProject(projectPath);
378+
let unityProject: UnityProject | undefined;
379+
const projectPath = options.unityProject?.toString()?.trim() || process.env.UNITY_PROJECT_PATH || undefined;
375380

376-
if (!unityProject) {
377-
Logger.instance.error(`The specified path is not a valid Unity project: ${projectPath}`);
378-
process.exit(1);
379-
}
381+
if (projectPath && projectPath.length > 0) {
382+
try {
383+
unityProject = await UnityProject.GetProject(projectPath);
380384

381-
if (!unityEditor) {
382-
const unityHub = new UnityHub();
383-
unityEditor = await unityHub.GetEditor(unityProject.version);
385+
if (!unityProject) {
386+
throw Error('Invalid Unity project path.');
387+
}
388+
} catch (error) {
389+
Logger.instance.error(`The specified path is not a valid Unity project: ${projectPath}. Use --unity-project or set the UNITY_PROJECT_PATH environment variable.`);
390+
process.exit(1);
391+
}
392+
393+
if (!unityEditor) {
394+
const unityHub = new UnityHub();
395+
try {
396+
unityEditor = await unityHub.GetEditor(unityProject.version);
397+
} catch {
398+
Logger.instance.error(`Could not find Unity Editor version ${unityProject.version.version} installed for project at ${unityProject.projectPath}. Please specify the editor path with --unity-editor or set the UNITY_EDITOR_PATH environment variable.`);
399+
process.exit(1);
400+
}
401+
}
384402
}
385403

386404
if (!unityEditor) {
@@ -389,11 +407,12 @@ program.command('run')
389407
}
390408

391409
if (!args.includes('-logFile')) {
392-
const logPath = unityEditor.GenerateLogFilePath(unityProject.projectPath, options.logName);
410+
const logPath = unityEditor.GenerateLogFilePath(unityProject?.projectPath, options.logName);
393411
args.push('-logFile', logPath);
394412
}
395413

396414
await unityEditor.Run({
415+
projectPath: unityProject?.projectPath,
397416
args: [...args]
398417
});
399418
process.exit(0);

src/unity-editor.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919

2020
export interface EditorCommand {
2121
args: string[];
22-
projectPath?: string;
22+
projectPath?: string | undefined;
2323
}
2424

2525
export class UnityEditor {
@@ -212,22 +212,42 @@ export class UnityEditor {
212212
throw Error('No command arguments provided for Unity execution');
213213
}
214214

215-
if (!command.args.includes(`-automated`)) {
216-
command.args.push(`-automated`);
215+
if (this.autoAddNoGraphics &&
216+
!command.args.includes(`-nographics`) &&
217+
!command.args.includes(`-force-graphics`)) {
218+
command.args.unshift(`-nographics`);
217219
}
218220

219221
if (!command.args.includes(`-batchmode`)) {
220-
command.args.push(`-batchmode`);
222+
command.args.unshift(`-batchmode`);
221223
}
222224

223-
if (this.autoAddNoGraphics &&
224-
!command.args.includes(`-nographics`) &&
225-
!command.args.includes(`-force-graphics`)) {
226-
command.args.push(`-nographics`);
225+
if (!command.args.includes(`-automated`)) {
226+
command.args.unshift(`-automated`);
227227
}
228228

229229
if (!command.args.includes('-logFile')) {
230-
command.args.push('-logFile', this.GenerateLogFilePath(command.projectPath));
230+
command.args.unshift('-logFile', this.GenerateLogFilePath(command.projectPath));
231+
} else {
232+
const existingLogPath = GetArgumentValueAsString('-logFile', command.args);
233+
command.args.splice(command.args.indexOf(existingLogPath) - 1, 2);
234+
command.args.unshift('-logFile', existingLogPath);
235+
}
236+
237+
if (command.projectPath) {
238+
if (!command.args.includes('-projectPath')) {
239+
command.args.unshift('-projectPath', command.projectPath);
240+
} else {
241+
const existingPath = GetArgumentValueAsString('-projectPath', command.args);
242+
243+
if (existingPath !== command.projectPath) {
244+
throw Error(`Conflicting project paths provided. Argument: "${existingPath}", Command: "${command.projectPath}"`);
245+
}
246+
247+
// Ensure -projectPath is the first argument
248+
command.args.splice(command.args.indexOf(existingPath) - 1, 2);
249+
command.args.unshift('-projectPath', command.projectPath);
250+
}
231251
}
232252

233253
const logPath: string = GetArgumentValueAsString('-logFile', command.args);

0 commit comments

Comments
 (0)