|
| 1 | +import { ArrayStatefulParameter } from 'codify-plugin-lib'; |
| 2 | + |
| 3 | +import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js'; |
| 4 | +import { Utils } from '../../../utils/index.js'; |
| 5 | +import { JenvConfig } from './jenv.js'; |
| 6 | + |
| 7 | +export const OPENJDK_SUPPORTED_VERSIONS = [8, 11, 17, 21, 22] |
| 8 | +export const JAVA_VERSION_INTEGER = /^\d+$/; |
| 9 | + |
| 10 | +export class JenvAddParameter extends ArrayStatefulParameter<JenvConfig, string> { |
| 11 | + async refresh(desired: null | string[]): Promise<null | string[]> { |
| 12 | + const { data: jenvVersions } = await codifySpawn('jenv versions') |
| 13 | + |
| 14 | + return desired |
| 15 | + ?.filter((v) => jenvVersions.includes(v)) |
| 16 | + ?.filter(Boolean) ?? null; |
| 17 | + } |
| 18 | + |
| 19 | + async applyAddItem(param: string): Promise<void> { |
| 20 | + |
| 21 | + const isHomebrewInstalled = await Utils.isHomebrewInstalled(); |
| 22 | + |
| 23 | + // Add special handling if the user specified an integer version. We add special functionality to automatically |
| 24 | + // install java if a lts version is specified and homebrew is installed. |
| 25 | + if (JAVA_VERSION_INTEGER.test(param)) { |
| 26 | + if (!isHomebrewInstalled) { |
| 27 | + throw new Error('Homebrew not detected. Cannot automatically install java version. Jenv does not automatically install' + |
| 28 | + ' java versions, see the jenv docs: https://www.jenv.be. Please manually install a version of java and provide a path to the jenv resource') |
| 29 | + } |
| 30 | + |
| 31 | + const parsedVersion = Number.parseInt(param, 10); |
| 32 | + if (!OPENJDK_SUPPORTED_VERSIONS.includes(parsedVersion)) { |
| 33 | + throw new Error(`Unsupported version of java specified. Only [${OPENJDK_SUPPORTED_VERSIONS.join(', ')}] is supported`) |
| 34 | + } |
| 35 | + |
| 36 | + const openjdkName = (parsedVersion === 22) ? 'openjdk' : `openjdk@${param}`; |
| 37 | + const { status } = await codifySpawn(`brew list --formula -1 ${openjdkName}`, { throws: false }); |
| 38 | + |
| 39 | + // That version is not currently installed with homebrew. Let's install it |
| 40 | + if (status === SpawnStatus.ERROR) { |
| 41 | + console.log(`Homebrew detected. Attempting to install java version ${openjdkName} automatically using homebrew`) |
| 42 | + await codifySpawn(`brew install ${openjdkName}`) |
| 43 | + } |
| 44 | + |
| 45 | + const location = await this.getHomebrewInstallLocation(openjdkName); |
| 46 | + if (!location) { |
| 47 | + throw new Error('Unable to determine location of jdk installed by homebrew. Please report this to the Codify team'); |
| 48 | + } |
| 49 | + |
| 50 | + await codifySpawn(`jenv add ${location}`) |
| 51 | + |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + await codifySpawn(`jenv add ${param}`); |
| 56 | + } |
| 57 | + |
| 58 | + async applyRemoveItem(param: string): Promise<void> { |
| 59 | + const isHomebrewInstalled = await Utils.isHomebrewInstalled(); |
| 60 | + |
| 61 | + if (JAVA_VERSION_INTEGER.test(param) && isHomebrewInstalled) { |
| 62 | + const parsedVersion = Number.parseInt(param, 10); |
| 63 | + const openjdkName = (parsedVersion === 22) ? 'openjdk' : `openjdk@${param}`; |
| 64 | + |
| 65 | + const location = await this.getHomebrewInstallLocation(openjdkName); |
| 66 | + if (location) { |
| 67 | + await codifySpawn(`jenv remove ${location}`) |
| 68 | + await codifySpawn(`brew uninstall ${openjdkName}`) |
| 69 | + } |
| 70 | + |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + await codifySpawn(`jenv uninstall ${param}`); |
| 75 | + } |
| 76 | + |
| 77 | + private async getHomebrewInstallLocation(openjdkName: string): Promise<string | null> { |
| 78 | + const { data: installInfo } = await codifySpawn(`brew list --formula -1 ${openjdkName}`) |
| 79 | + |
| 80 | + // Example: /opt/homebrew/Cellar/openjdk@17/17.0.11/libexec/ |
| 81 | + const libexec = installInfo |
| 82 | + .split(/\n/) |
| 83 | + .find((l) => l.includes('libexec')) |
| 84 | + ?.split('openjdk.jdk/') |
| 85 | + ?.at(0) |
| 86 | + |
| 87 | + if (!libexec) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + return libexec + '/openjdk.jdk/Contents/Home'; |
| 92 | + } |
| 93 | +} |
0 commit comments