Skip to content

Commit bd4e782

Browse files
unity-cli@v1.0.5 (#6)
- fixed legacy unity installer - change return type of `UnityHub.GetEditor` from `string` to `UnityEditor`
1 parent 774e3ca commit bd4e782

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ unity-cli [command] [options]
3131
- `unity-cli hub-install`: Install Unity Hub
3232
- `unity-cli hub-version`: Print Unity Hub version
3333
- `unity-cli hub-path`: Print Unity Hub executable path
34-
- `unity-cli hub [args...]`: Run Unity Hub commands directly
35-
- `unity-cli activate-license`: Activate a Unity license
36-
- `unity-cli return-license`: Return a Unity license
34+
- `unity-cli hub [options] <args...>`: Run [Unity Hub command line arguments](https://docs.unity3d.com/hub/manual/HubCLI.html)
35+
- `unity-cli activate-license [options]`: Activate a Unity license
36+
- `unity-cli return-license [options]`: Return a Unity license
3737
- `unity-cli license-version`: Print Unity License Client version
38-
- `unity-cli setup-unity`: Find or install Unity Editor for a project/version
39-
- `unity-cli create-project`: Create a new Unity project from a template
40-
- `unity-cli run [args...]`: Run commands in [Unity Editor Command Line Arguments](https://docs.unity3d.com/Manual/EditorCommandLineArguments.html)
38+
- `unity-cli setup-unity [options]`: Find or install Unity Editor for a project/version
39+
- `unity-cli create-project [options]`: Create a new Unity project from a [template](https://docs.unity3d.com/hub/manual/Templates.html)
40+
- `unity-cli run [options] <args...>`: Run [Unity Editor Command Line Arguments](https://docs.unity3d.com/Manual/EditorCommandLineArguments.html)
4141

4242
#### Install Unity Hub and Editor
4343

4444
```bash
4545
unity-cli hub-install
46-
unity-cli setup-unity --unity-version 2022.3.x --modules android,ios --json
46+
unity-cli setup-unity --unity-version 2022.3.x --modules android,ios
4747
```
4848

4949
#### Activate a Unity License
@@ -61,5 +61,5 @@ unity-cli create-project --name "MyGame" --template com.unity.template.3d --unit
6161
#### Build a Project
6262

6363
```bash
64-
unity-cli run --unity-editor <path-to-editor> --unity-project <path-to-project> -quit -batchmode -executeMethod Utilities.Editor.BuildPipeline.UnityPlayerBuildTools.StartCommandLineBuild
64+
unity-cli run --unity-editor <path-to-editor> --unity-project <path-to-project> -quit -batchmode -executeMethod StartCommandLineBuild
6565
```

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rage-against-the-pixel/unity-cli",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "A command line utility for the Unity Game Engine.",
55
"author": "RageAgainstThePixel",
66
"license": "MIT",

src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,17 @@ program.command('setup-unity')
214214
}
215215

216216
const unityHub = new UnityHub();
217-
const editorPath = await unityHub.GetEditor(unityVersion, modules);
217+
const unityEditor = await unityHub.GetEditor(unityVersion, modules);
218218
const output: { [key: string]: string } = {
219219
'UNITY_HUB_PATH': unityHub.executable,
220-
'UNITY_EDITOR': editorPath
220+
'UNITY_EDITOR': unityEditor.editorPath
221221
};
222222

223223
if (unityProject) {
224224
output['UNITY_PROJECT_PATH'] = unityProject.projectPath;
225225

226226
if (modules.includes('android')) {
227-
await CheckAndroidSdkInstalled(editorPath, unityProject.projectPath);
227+
await CheckAndroidSdkInstalled(unityEditor.editorPath, unityProject.projectPath);
228228
}
229229
}
230230

src/unity-editor.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
spawn,
1313
ChildProcessByStdio,
1414
} from 'child_process';
15+
import { UnityVersion } from './unity-version';
1516

1617
export interface EditorCommand {
1718
args: string[];
@@ -31,27 +32,36 @@ export class UnityEditor {
3132
* @param editorPath The path to the Unity Editor installation.
3233
* @throws Will throw an error if the editor path is invalid or not executable.
3334
*/
34-
constructor(public readonly editorPath: string) {
35+
constructor(
36+
public readonly editorPath: string,
37+
public readonly version: UnityVersion | undefined = undefined
38+
) {
3539
if (!fs.existsSync(editorPath)) {
3640
throw new Error(`The Unity Editor path does not exist: ${editorPath}`);
3741
}
3842

3943
fs.accessSync(editorPath, fs.constants.X_OK);
4044
this.editorRootPath = UnityEditor.GetEditorRootPath(editorPath);
4145

42-
const match = editorPath.match(/(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/);
46+
if (!version) {
47+
const match = editorPath.match(/(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\w+)/);
4348

44-
if (!match) {
45-
throw Error(`Invalid Unity Editor Path: ${editorPath}`);
46-
}
49+
if (!match || !match.groups) {
50+
throw Error(`Invalid Unity Editor Path: ${editorPath}`);
51+
}
4752

48-
const unityMajorVersion = match.groups?.major;
53+
const unityMajorVersion = match.groups!.major;
4954

50-
if (!unityMajorVersion) {
51-
throw Error(`Invalid Unity Major Version: ${editorPath}`);
55+
if (!unityMajorVersion) {
56+
throw Error(`Invalid Unity Major Version: ${editorPath}`);
57+
}
58+
59+
this.version = new UnityVersion(`${match.groups!.major}.${match.groups!.minor}.${match.groups!.patch}`);
60+
} else {
61+
this.version = version;
5262
}
5363

54-
this.autoAddNoGraphics = parseInt(unityMajorVersion, 10) > 2018;
64+
this.autoAddNoGraphics = this.version.satisfies('>2018.0.0');
5565
}
5666

5767
/**

src/unity-hub.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ chmod -R 777 "$hubPath"`]);
402402
* @param modules The modules to install alongside the editor.
403403
* @returns The path to the Unity Editor executable.
404404
*/
405-
public async GetEditor(unityVersion: UnityVersion, modules: string[]): Promise<string> {
405+
public async GetEditor(unityVersion: UnityVersion, modules: string[]): Promise<UnityEditor> {
406406
const retryErrorMessages = [
407407
'Editor already installed in this location',
408408
'failed to download. Error given: Request timeout'
@@ -442,6 +442,7 @@ chmod -R 777 "$hubPath"`]);
442442
if (installPath) {
443443
await DeleteDirectory(installPath);
444444
}
445+
445446
installPath = await this.installUnity(unityVersion, modules);
446447
} else {
447448
throw error;
@@ -459,7 +460,7 @@ chmod -R 777 "$hubPath"`]);
459460
await this.patchBeeBackend(editorPath);
460461

461462
if (unityVersion.isLegacy() || modules.length === 0) {
462-
return editorPath;
463+
return new UnityEditor(path.normalize(editorPath), unityVersion);
463464
}
464465

465466
try {
@@ -487,7 +488,7 @@ chmod -R 777 "$hubPath"`]);
487488
}
488489
}
489490

490-
return path.normalize(editorPath);
491+
return new UnityEditor(path.normalize(editorPath), unityVersion);
491492
}
492493

493494
/**
@@ -503,6 +504,7 @@ chmod -R 777 "$hubPath"`]);
503504

504505
private async checkInstalledEditors(unityVersion: UnityVersion, failOnEmpty: boolean, installPath: string | undefined = undefined): Promise<string | undefined> {
505506
let editorPath = undefined;
507+
506508
if (!installPath) {
507509
const paths: string[] = await this.ListInstalledEditors();
508510

@@ -578,7 +580,7 @@ chmod -R 777 "$hubPath"`]);
578580
throw new Error(`Failed to find installed Unity Editor: ${unityVersion.toString()}\n > ${error}`);
579581
}
580582

581-
this.logger.ci(`Found installed Unity Editor: ${editorPath}`);
583+
this.logger.debug(`Found installed editor: "${editorPath}"`);
582584
return editorPath;
583585
}
584586

@@ -799,6 +801,8 @@ done
799801
}
800802

801803
private async installUnity(unityVersion: UnityVersion, modules: string[]): Promise<string | undefined> {
804+
this.logger.ci(`Installing Unity ${unityVersion.toString()}...`);
805+
802806
if (unityVersion.isLegacy()) {
803807
return await this.installUnity4x(unityVersion);
804808
}
@@ -848,7 +852,6 @@ done
848852
args.push('--cm');
849853
}
850854

851-
this.logger.info(`Installing Unity ${unityVersion.toString()}...`);
852855
const output = await this.Exec(args, { showCommand: true, silent: false });
853856

854857
if (output.includes(`Error while installing an editor or a module from changeset`)) {
@@ -871,7 +874,7 @@ done
871874
this.logger.info(`Running Unity ${unityVersion.toString()} installer...`);
872875

873876
try {
874-
await Exec(installerPath, ['/S', `/D=${installPath}`, '-Wait', '-NoNewWindow'], { silent: true, showCommand: true });
877+
await Exec('powershell', ['-Command', `Start-Process -FilePath \"${installerPath}\" -ArgumentList \"/S /D=${installPath}\" -Wait -NoNewWindow`], { silent: true, showCommand: true });
875878
} catch (error) {
876879
this.logger.error(`Failed to install Unity ${unityVersion.toString()}: ${error}`);
877880
} finally {

src/utilities.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export async function TryKillProcess(procInfo: ProcInfo): Promise<number | undef
236236

237237
try {
238238
pid = procInfo.pid;
239-
logger.ci(`Killing process '${procInfo.name}' with pid: ${pid}`);
239+
logger.ci(`Killing process "${procInfo.name}" with pid: ${pid}`);
240240
process.kill(pid);
241241
} catch (error) {
242242
const nodeJsException = error as NodeJS.ErrnoException;
@@ -295,10 +295,10 @@ export async function KillChildProcesses(procInfo: ProcInfo): Promise<void> {
295295
logger.debug(`Killing child processes of ${procInfo.name} with pid: ${procInfo.pid}...`);
296296
try {
297297
if (process.platform === 'win32') {
298-
const pwshCommand = 'powershell -Command "Get-CimInstance Win32_Process -Filter \'ParentProcessId=' + procInfo.pid + '\' | ForEach-Object { Stop-Process -Id $_.ProcessId -Force }"';
299-
await Exec('cmd', ['/c', pwshCommand], { silent: true });
298+
const command = `Get-CimInstance Win32_Process -Filter "ParentProcessId=${procInfo.pid}" | ForEach-Object { Stop-Process -Id $_.ProcessId -Force }`;
299+
await Exec('powershell', ['-Command', command], { silent: true, showCommand: true });
300300
} else { // linux and macos
301-
const psOutput = await Exec('ps', ['-eo', 'pid,ppid,comm'], { silent: true });
301+
const psOutput = await Exec('ps', ['-eo', 'pid,ppid,comm'], { silent: true, showCommand: false });
302302
const lines = psOutput.split('\n').slice(1); // Skip header line
303303

304304
for (const line of lines) {

0 commit comments

Comments
 (0)