|
| 1 | +import { |
| 2 | + ExampleConfig, |
| 3 | + FileUtils, |
| 4 | + getPty, |
| 5 | + Resource, |
| 6 | + ResourceSettings, |
| 7 | + SpawnStatus, |
| 8 | + Utils, |
| 9 | + z, |
| 10 | +} from '@codifycli/plugin-core'; |
| 11 | +import { OS } from '@codifycli/schemas'; |
| 12 | +import os from 'node:os'; |
| 13 | +import path from 'node:path'; |
| 14 | + |
| 15 | +import { GoenvGlobalParameter } from './global-parameter.js'; |
| 16 | +import { GoVersionsParameter } from './go-versions-parameter.js'; |
| 17 | + |
| 18 | +const GOENV_ROOT = path.join(os.homedir(), '.goenv'); |
| 19 | +const GOENV_ROOT_EXPORT = 'export GOENV_ROOT="$HOME/.goenv"'; |
| 20 | +const GOENV_PATH_EXPORT = 'export PATH="$GOENV_ROOT/bin:$PATH"'; |
| 21 | +const GOENV_INIT = 'eval "$(goenv init -)"'; |
| 22 | + |
| 23 | +const schema = z |
| 24 | + .object({ |
| 25 | + goVersions: z |
| 26 | + .array(z.string()) |
| 27 | + .describe('Go versions to install via goenv (e.g. ["1.22.0", "1.21.5"])') |
| 28 | + .optional(), |
| 29 | + global: z |
| 30 | + .string() |
| 31 | + .describe('The global Go version set by goenv.') |
| 32 | + .optional(), |
| 33 | + }) |
| 34 | + .describe('goenv resource — install and manage multiple Go versions'); |
| 35 | + |
| 36 | +export type GoenvConfig = z.infer<typeof schema>; |
| 37 | + |
| 38 | +const defaultConfig: Partial<GoenvConfig> = { |
| 39 | + goVersions: [], |
| 40 | +}; |
| 41 | + |
| 42 | +const exampleBasic: ExampleConfig = { |
| 43 | + title: 'Install goenv with a global Go version', |
| 44 | + description: 'Install goenv, download Go 1.22.0, and set it as the default global version.', |
| 45 | + configs: [ |
| 46 | + { |
| 47 | + type: 'goenv', |
| 48 | + goVersions: ['1.22.0'], |
| 49 | + global: '1.22.0', |
| 50 | + }, |
| 51 | + ], |
| 52 | +}; |
| 53 | + |
| 54 | +const exampleMultiVersion: ExampleConfig = { |
| 55 | + title: 'Manage multiple Go versions', |
| 56 | + description: 'Install goenv with multiple Go versions for cross-version testing, setting the latest as the default.', |
| 57 | + configs: [ |
| 58 | + { |
| 59 | + type: 'goenv', |
| 60 | + goVersions: ['1.21.0', '1.22.0', '1.23.0'], |
| 61 | + global: '1.23.0', |
| 62 | + }, |
| 63 | + ], |
| 64 | +}; |
| 65 | + |
| 66 | +export class GoenvResource extends Resource<GoenvConfig> { |
| 67 | + getSettings(): ResourceSettings<GoenvConfig> { |
| 68 | + return { |
| 69 | + id: 'goenv', |
| 70 | + defaultConfig, |
| 71 | + exampleConfigs: { |
| 72 | + example1: exampleBasic, |
| 73 | + example2: exampleMultiVersion, |
| 74 | + }, |
| 75 | + operatingSystems: [OS.Darwin, OS.Linux], |
| 76 | + schema, |
| 77 | + parameterSettings: { |
| 78 | + goVersions: { type: 'stateful', definition: new GoVersionsParameter(), order: 1 }, |
| 79 | + global: { type: 'stateful', definition: new GoenvGlobalParameter(), order: 2 }, |
| 80 | + }, |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + override async refresh(): Promise<Partial<GoenvConfig> | null> { |
| 85 | + const $ = getPty(); |
| 86 | + const { status } = await $.spawnSafe('goenv --version'); |
| 87 | + if (status === SpawnStatus.SUCCESS) return {}; |
| 88 | + |
| 89 | + // goenv may be installed via git clone but not yet on PATH in the current session |
| 90 | + const { status: binStatus } = await $.spawnSafe( |
| 91 | + `test -f ${path.join(GOENV_ROOT, 'bin', 'goenv')}` |
| 92 | + ); |
| 93 | + return binStatus === SpawnStatus.SUCCESS ? {} : null; |
| 94 | + } |
| 95 | + |
| 96 | + override async create(): Promise<void> { |
| 97 | + if (Utils.isMacOS()) { |
| 98 | + await installOnMacOS(); |
| 99 | + } else { |
| 100 | + await installOnLinux(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + override async destroy(): Promise<void> { |
| 105 | + if (Utils.isMacOS()) { |
| 106 | + await uninstallOnMacOS(); |
| 107 | + } else { |
| 108 | + await uninstallOnLinux(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +async function installOnMacOS(): Promise<void> { |
| 114 | + const $ = getPty(); |
| 115 | + await $.spawn('brew install goenv mercurial bison', { |
| 116 | + interactive: true, |
| 117 | + env: { HOMEBREW_NO_AUTO_UPDATE: '1' }, |
| 118 | + }); |
| 119 | + await FileUtils.addToShellRc(GOENV_INIT); |
| 120 | +} |
| 121 | + |
| 122 | +async function installOnLinux(): Promise<void> { |
| 123 | + await Utils.installViaPkgMgr( |
| 124 | + 'curl git mercurial make binutils bison gcc build-essential' |
| 125 | + ); |
| 126 | + |
| 127 | + const $ = getPty(); |
| 128 | + await $.spawnSafe(`git clone https://github.com/go-nv/goenv.git ${GOENV_ROOT}`, { |
| 129 | + interactive: true, |
| 130 | + }); |
| 131 | + |
| 132 | + await FileUtils.addAllToShellRc([GOENV_ROOT_EXPORT, GOENV_PATH_EXPORT, GOENV_INIT]); |
| 133 | +} |
| 134 | + |
| 135 | +async function uninstallOnMacOS(): Promise<void> { |
| 136 | + const $ = getPty(); |
| 137 | + await $.spawnSafe('brew uninstall goenv', { |
| 138 | + env: { HOMEBREW_NO_AUTO_UPDATE: '1' }, |
| 139 | + }); |
| 140 | + await removeGoenvFromShellRc([GOENV_INIT]); |
| 141 | +} |
| 142 | + |
| 143 | +async function uninstallOnLinux(): Promise<void> { |
| 144 | + const $ = getPty(); |
| 145 | + await $.spawnSafe(`rm -rf ${GOENV_ROOT}`); |
| 146 | + await removeGoenvFromShellRc([GOENV_ROOT_EXPORT, GOENV_PATH_EXPORT, GOENV_INIT]); |
| 147 | +} |
| 148 | + |
| 149 | +async function removeGoenvFromShellRc(lines: string[]): Promise<void> { |
| 150 | + const shellRc = Utils.getPrimaryShellRc(); |
| 151 | + if (!(await FileUtils.fileExists(shellRc))) { |
| 152 | + return; |
| 153 | + } |
| 154 | + for (const line of lines) { |
| 155 | + await FileUtils.removeLineFromShellRc(line); |
| 156 | + } |
| 157 | +} |
0 commit comments