tools: also look in the Android SDK folder Android Studio installs and in Homebrew, for apps started without a shell PATH

This commit is contained in:
CRTkafa 2026-10-11 09:51:22 +08:00
parent bd3ab20e1d
commit f9a53f508e
4 changed files with 50 additions and 4 deletions

View file

@ -2,6 +2,7 @@
// storebound: ship a mobile app or game to Google Play and the App Store from the terminal. // storebound: ship a mobile app or game to Google Play and the App Store from the terminal.
// Run `storebound help` for the command list. Config: storebound.config.ts; credentials: env or .storebound/. // Run `storebound help` for the command list. Config: storebound.config.ts; credentials: env or .storebound/.
import { parseArgs } from './lib/args'; import { parseArgs } from './lib/args';
import { toolPath } from './lib/tools';
import { loadConfig } from './lib/config'; import { loadConfig } from './lib/config';
import type { Ctx } from './lib/ctx'; import type { Ctx } from './lib/ctx';
@ -78,6 +79,8 @@ docs: README.md and the skills/ folder of the toolchain`);
} }
async function main(): Promise<void> { async function main(): Promise<void> {
// tools (adb, emulator, ffmpeg…) are also looked up where they usually live, for apps started without a shell PATH
process.env.PATH = toolPath(process.env);
const argv = process.argv.slice(2); const argv = process.argv.slice(2);
const [a, b] = argv; const [a, b] = argv;
if (!a || a === 'help' || a === '--help' || a === '-h') return help(); if (!a || a === 'help' || a === '--help' || a === '-h') return help();

View file

@ -42,7 +42,7 @@ export function childEnv(extra: Record<string, string> = {}, machine = false): R
// never inherited: the session secret, and the owner's confirmation (serve passes it per call, after a verified token) // never inherited: the session secret, and the owner's confirmation (serve passes it per call, after a verified token)
for (const [k, v] of Object.entries(process.env)) if (v !== undefined && !k.startsWith('STOREBOUND_SESSION') && k !== 'STOREBOUND_OWNER_CONFIRMED') base[k] = v; for (const [k, v] of Object.entries(process.env)) if (v !== undefined && !k.startsWith('STOREBOUND_SESSION') && k !== 'STOREBOUND_OWNER_CONFIRMED') base[k] = v;
// app-managed tools (desktop data folder) come first on PATH // app-managed tools (desktop data folder) come first on PATH
if (base.STOREBOUND_TOOL_PATH) base.PATH = toolPath(base); base.PATH = toolPath(base);
return { ...base, ...extra, NO_COLOR: '1', ...(machine ? { STOREBOUND_MACHINE: '1', STOREBOUND_SERVE: '1' } : {}) }; return { ...base, ...extra, NO_COLOR: '1', ...(machine ? { STOREBOUND_MACHINE: '1', STOREBOUND_SERVE: '1' } : {}) };
} }

View file

@ -23,10 +23,25 @@ export interface ToolStatus {
install: { kind: 'download' | 'link'; url: string; note: string }; install: { kind: 'download' | 'link'; url: string; note: string };
} }
/** PATH with the app-managed tool folders first. */ /** Folders where tools usually live but that are often missing from PATH (apps started from the Dock or Finder get
export function toolPath(env: Record<string, string | undefined> = process.env): string { * a minimal PATH): the Android SDK that Android Studio installs, Homebrew, and the usual local bin folders. */
export function wellKnownToolDirs(env: Record<string, string | undefined> = process.env, platform: string = process.platform): string[] {
const home = env.HOME ?? env.USERPROFILE ?? '';
if (!home) return []; // a bare environment (tests, sandboxes) keeps exactly its own PATH
const sdks = [env.ANDROID_HOME, env.ANDROID_SDK_ROOT,
platform === 'darwin' ? join(home, 'Library/Android/sdk') : platform === 'win32' ? join(env.LOCALAPPDATA ?? join(home, 'AppData/Local'), 'Android/Sdk') : join(home, 'Android/Sdk')];
const dirs = sdks.filter((d): d is string => !!d).flatMap(d => [join(d, 'platform-tools'), join(d, 'emulator')]);
if (platform === 'darwin') dirs.push('/opt/homebrew/bin', '/usr/local/bin');
else if (platform !== 'win32') dirs.push('/usr/local/bin', join(home, '.local/bin'));
return [...new Set(dirs)].filter(d => existsSync(d));
}
/** PATH with the app-managed tool folders first and the well-known tool folders after the user's own PATH. */
export function toolPath(env: Record<string, string | undefined> = process.env, platform: string = process.platform): string {
const extra = (env.STOREBOUND_TOOL_PATH ?? '').split(delimiter).filter(Boolean); const extra = (env.STOREBOUND_TOOL_PATH ?? '').split(delimiter).filter(Boolean);
return [...extra, env.PATH ?? ''].filter(Boolean).join(delimiter); const own = (env.PATH ?? '').split(delimiter).filter(Boolean);
const known = wellKnownToolDirs(env, platform).filter(d => !own.includes(d));
return [...extra, ...own, ...known].join(delimiter);
} }
/** First line of `<cmd> <args>` (version banners), or null. */ /** First line of `<cmd> <args>` (version banners), or null. */

28
tests/tools-path.test.ts Normal file
View file

@ -0,0 +1,28 @@
import { describe, expect, test } from 'bun:test';
import { mkdirSync, mkdtempSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { toolPath, wellKnownToolDirs } from '../cli/lib/tools';
describe('tool folders outside PATH', () => {
test('finds the Android SDK Android Studio installs, after the own PATH', () => {
const home = mkdtempSync(join(tmpdir(), 'sb-home-'));
mkdirSync(join(home, 'Library/Android/sdk/platform-tools'), { recursive: true });
mkdirSync(join(home, 'Library/Android/sdk/emulator'), { recursive: true });
const env = { HOME: home, PATH: '/usr/bin:/bin' };
expect(wellKnownToolDirs(env, 'darwin')).toContain(join(home, 'Library/Android/sdk/platform-tools'));
const p = toolPath(env, 'darwin').split(':');
expect(p.slice(0, 2)).toEqual(['/usr/bin', '/bin']);
expect(p).toContain(join(home, 'Library/Android/sdk/emulator'));
});
test('ANDROID_HOME wins, missing folders are skipped, app folders come first', () => {
const sdk = mkdtempSync(join(tmpdir(), 'sb-sdk-'));
mkdirSync(join(sdk, 'platform-tools'));
const env = { HOME: '/nonexistent', ANDROID_HOME: sdk, PATH: '/usr/bin', STOREBOUND_TOOL_PATH: '/app/tools' };
const p = toolPath(env, 'linux').split(':');
expect(p[0]).toBe('/app/tools');
expect(p).toContain(join(sdk, 'platform-tools'));
expect(p).not.toContain(join(sdk, 'emulator'));
});
});