|
| 1 | +import { |
| 2 | + ExampleConfig, |
| 3 | + Resource, |
| 4 | + ResourceSettings, |
| 5 | + SpawnStatus, |
| 6 | + Utils, |
| 7 | + PackageManager, |
| 8 | + getPty, |
| 9 | + z, |
| 10 | +} from '@codifycli/plugin-core'; |
| 11 | +import { OS } from '@codifycli/schemas'; |
| 12 | + |
| 13 | +import { XcodesSelectedParameter } from './selected-parameter.js'; |
| 14 | +import { XcodeVersionsParameter } from './xcode-versions-parameter.js'; |
| 15 | + |
| 16 | +const schema = z |
| 17 | + .object({ |
| 18 | + xcodeVersions: z |
| 19 | + .array(z.string()) |
| 20 | + .describe( |
| 21 | + 'List of Xcode versions to install via xcodes (e.g. ["15.2", "14.3.1"]). ' + |
| 22 | + 'Installing Xcode requires Apple ID credentials — xcodes will prompt interactively or use ' + |
| 23 | + 'the XCODES_USERNAME and XCODES_PASSWORD environment variables for non-interactive installs.' |
| 24 | + ) |
| 25 | + .optional(), |
| 26 | + selected: z |
| 27 | + .string() |
| 28 | + .describe( |
| 29 | + 'The active Xcode version to select (e.g. "15.2"). ' + |
| 30 | + 'Must be one of the installed xcodeVersions. Equivalent to running xcodes select.' |
| 31 | + ) |
| 32 | + .optional(), |
| 33 | + }) |
| 34 | + .describe('xcodes resource — install and manage multiple Xcode versions via the xcodes CLI'); |
| 35 | + |
| 36 | +export type XcodesConfig = z.infer<typeof schema>; |
| 37 | + |
| 38 | +const defaultConfig: Partial<XcodesConfig> = { |
| 39 | + xcodeVersions: [], |
| 40 | +}; |
| 41 | + |
| 42 | +const exampleStandardSetup: ExampleConfig = { |
| 43 | + title: 'Install a specific Xcode version', |
| 44 | + description: 'Install xcodes and a specific Xcode release, setting it as the active version — a common setup for iOS teams standardising on a single Xcode version.', |
| 45 | + configs: [{ |
| 46 | + type: 'xcodes', |
| 47 | + xcodeVersions: ['15.4'], |
| 48 | + selected: '15.4', |
| 49 | + os: ['macOS'], |
| 50 | + }], |
| 51 | +}; |
| 52 | + |
| 53 | +const exampleMultiVersion: ExampleConfig = { |
| 54 | + title: 'Install multiple Xcode versions', |
| 55 | + description: 'Install several Xcode versions side by side and set the latest stable release as active — useful when supporting multiple iOS SDK targets.', |
| 56 | + configs: [{ |
| 57 | + type: 'xcodes', |
| 58 | + xcodeVersions: ['14.3.1', '15.4'], |
| 59 | + selected: '15.4', |
| 60 | + os: ['macOS'], |
| 61 | + }], |
| 62 | +}; |
| 63 | + |
| 64 | +export class XcodesResource extends Resource<XcodesConfig> { |
| 65 | + getSettings(): ResourceSettings<XcodesConfig> { |
| 66 | + return { |
| 67 | + id: 'xcodes', |
| 68 | + defaultConfig, |
| 69 | + exampleConfigs: { |
| 70 | + example1: exampleStandardSetup, |
| 71 | + example2: exampleMultiVersion, |
| 72 | + }, |
| 73 | + operatingSystems: [OS.Darwin], |
| 74 | + schema, |
| 75 | + parameterSettings: { |
| 76 | + xcodeVersions: { type: 'stateful', definition: new XcodeVersionsParameter(), order: 1 }, |
| 77 | + selected: { type: 'stateful', definition: new XcodesSelectedParameter(), order: 2 }, |
| 78 | + }, |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + override async refresh(): Promise<Partial<XcodesConfig> | null> { |
| 83 | + const $ = getPty(); |
| 84 | + const { status } = await $.spawnSafe('which xcodes'); |
| 85 | + return status === SpawnStatus.SUCCESS ? {} : null; |
| 86 | + } |
| 87 | + |
| 88 | + override async create(): Promise<void> { |
| 89 | + await Utils.installViaPkgMgr('xcodes', undefined, PackageManager.BREW); |
| 90 | + } |
| 91 | + |
| 92 | + override async destroy(): Promise<void> { |
| 93 | + await Utils.uninstallViaPkgMgr('xcodes', undefined, PackageManager.BREW); |
| 94 | + } |
| 95 | +} |
0 commit comments