Skip to content

Commit 07a6c97

Browse files
update android sdk search
1 parent eece0e0 commit 07a6c97

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/android-sdk.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { UnityEditor } from './unity-editor';
77
import {
88
isProcessElevated,
99
ReadFileContents,
10-
ResolveGlobToPath
10+
ResolveGlobToPath,
11+
ResolvePathCandidates,
1112
} from './utilities';
12-
import { satisfies } from 'semver';
1313

1414
const logger = Logger.instance;
1515

@@ -86,16 +86,16 @@ async function getJDKPath(editor: UnityEditor): Promise<string> {
8686
}
8787

8888
async function getSdkManager(editor: UnityEditor): Promise<string> {
89-
let globPath: string[] = [];
89+
let globCandidates: string[][] = [];
9090
if (editor.version.range('>=2019.0.0 <2021.0.0')) {
9191
logger.debug('Using sdkmanager bundled with Unity 2019 and 2020');
9292
switch (process.platform) {
9393
case 'darwin':
9494
case 'linux':
95-
globPath = [editor.editorRootPath, '**', 'AndroidPlayer', '**', 'sdkmanager'];
95+
globCandidates = [[editor.editorRootPath, '**', 'AndroidPlayer', '**', 'sdkmanager']];
9696
break;
9797
case 'win32':
98-
globPath = [editor.editorRootPath, '**', 'AndroidPlayer', '**', 'sdkmanager.bat'];
98+
globCandidates = [[editor.editorRootPath, '**', 'AndroidPlayer', '**', 'sdkmanager.bat']];
9999
break;
100100
default:
101101
throw new Error(`Unsupported platform: ${process.platform}`);
@@ -105,10 +105,10 @@ async function getSdkManager(editor: UnityEditor): Promise<string> {
105105
switch (process.platform) {
106106
case 'darwin':
107107
case 'linux':
108-
globPath = [editor.editorRootPath, '**', 'AndroidPlayer', '**', 'cmdline-tools', '**', 'sdkmanager'];
108+
globCandidates = [[editor.editorRootPath, '**', 'AndroidPlayer', '**', 'cmdline-tools', '**', 'sdkmanager']];
109109
break;
110110
case 'win32':
111-
globPath = [editor.editorRootPath, '**', 'AndroidPlayer', '**', 'cmdline-tools', '**', 'sdkmanager.bat'];
111+
globCandidates = [[editor.editorRootPath, '**', 'AndroidPlayer', '**', 'cmdline-tools', '**', 'sdkmanager.bat']];
112112
break;
113113
default:
114114
throw new Error(`Unsupported platform: ${process.platform}`);
@@ -121,23 +121,32 @@ async function getSdkManager(editor: UnityEditor): Promise<string> {
121121
throw new Error('Android installation not found: No system ANDROID_SDK_ROOT or ANDROID_HOME defined');
122122
}
123123

124+
const sdkManagerBinary = process.platform === 'win32' ? 'sdkmanager.bat' : 'sdkmanager';
124125
switch (process.platform) {
125126
case 'darwin':
126127
case 'linux':
127-
globPath = [systemSdkPath, 'cmdline-tools', 'latest', 'bin', 'sdkmanager'];
128-
break;
129128
case 'win32':
130-
globPath = [systemSdkPath, 'cmdline-tools', 'latest', 'bin', 'sdkmanager.bat'];
129+
globCandidates = [
130+
[systemSdkPath, 'cmdline-tools', 'latest', 'bin', sdkManagerBinary],
131+
[systemSdkPath, 'cmdline-tools', '**', 'bin', sdkManagerBinary],
132+
[systemSdkPath, 'tools', 'bin', sdkManagerBinary]
133+
];
131134
break;
132135
default:
133136
throw new Error(`Unsupported platform: ${process.platform}`);
134137
}
135138
}
136139

137-
const sdkmanagerPath = await ResolveGlobToPath(globPath);
140+
const sdkmanagerPath = await ResolvePathCandidates(globCandidates);
138141

139142
if (!sdkmanagerPath) {
140-
throw new Error(`Failed to resolve sdkmanager in ${globPath}`);
143+
const normalizedCandidates = globCandidates.map(candidate => path.join(...candidate).split(path.sep).join('/'));
144+
if (normalizedCandidates.length > 0) {
145+
logger.ci(`sdkmanager glob candidates:\n${normalizedCandidates.map(candidate => ` > ${candidate}`).join('\n')}`);
146+
} else {
147+
logger.ci('sdkmanager glob candidates:\n > <none>');
148+
}
149+
throw new Error('Failed to resolve sdkmanager in expected locations');
141150
}
142151

143152
await fs.promises.access(sdkmanagerPath, fs.constants.R_OK);

src/utilities.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ export async function ResolveGlobToPath(globs: string[]): Promise<string> {
2626
throw new Error(`No accessible file found for glob pattern: ${path.normalize(globPath)}`);
2727
}
2828

29+
/**
30+
* Resolves a list of glob patterns to the first matching file path.
31+
* @param globsList A list of arrays of path segments that may include glob patterns.
32+
* @returns The first matching file path, or undefined if none found.
33+
*/
34+
export async function ResolvePathCandidates(globsList: string[][]): Promise<string | undefined> {
35+
for (const globPath of globsList) {
36+
try {
37+
return await ResolveGlobToPath(globPath);
38+
} catch (error) {
39+
const joinedPath = path.join(...globPath);
40+
logger.debug(`Failed to resolve sdkmanager using glob: ${joinedPath}`);
41+
}
42+
}
43+
44+
return undefined;
45+
}
46+
2947
/**
3048
* Prompts the user for input, masking the input with asterisks.
3149
* @param prompt The prompt message to display.

0 commit comments

Comments
 (0)