|
| 1 | +import { CreatePlan, Resource, ResourceSettings, SpawnStatus, getPty } from 'codify-plugin-lib'; |
| 2 | +import { ResourceConfig } from 'codify-schemas'; |
| 3 | +import * as fsSync from 'node:fs'; |
| 4 | +import * as fs from 'node:fs/promises'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | + |
| 8 | +import { codifySpawn } from '../../utils/codify-spawn.js'; |
| 9 | +import { FileUtils } from '../../utils/file-utils.js'; |
| 10 | +import { Utils } from '../../utils/index.js'; |
| 11 | +import { MacportsInstallParameter, PortPackage } from './install-parameter.js'; |
| 12 | +import schema from './macports-schema.json'; |
| 13 | + |
| 14 | +const MACPORTS_DOWNLOAD_LINKS: Record<string, string> = { |
| 15 | + '15': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-15-Sequoia.pkg', |
| 16 | + '14': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-14-Sonoma.pkg', |
| 17 | + '13': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-13-Ventura.pkg', |
| 18 | + '12': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-12-Monterey.pkg', |
| 19 | + '11': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-11-BigSur.pkg', |
| 20 | + '10': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-10.15-Catalina.pkg', |
| 21 | + '9': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-10.14-Mojave.pkg', |
| 22 | + '8': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-10.13-HighSierra.pkg', |
| 23 | + '7': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-10.12-Sierra.pkg', |
| 24 | + '6': 'https://github.com/macports/macports-base/releases/download/v2.10.5/MacPorts-2.10.5-10.11-ElCapitan.pkg', |
| 25 | +} |
| 26 | + |
| 27 | +export interface MacportsConfig extends ResourceConfig { |
| 28 | + install: Array<PortPackage | string>; |
| 29 | +} |
| 30 | + |
| 31 | +export class MacportsResource extends Resource<MacportsConfig> { |
| 32 | + |
| 33 | + override getSettings(): ResourceSettings<MacportsConfig> { |
| 34 | + return { |
| 35 | + id: 'macports', |
| 36 | + schema, |
| 37 | + parameterSettings: { |
| 38 | + install: { type: 'stateful', definition: new MacportsInstallParameter() } |
| 39 | + } |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + override async refresh(parameters: Partial<MacportsConfig>): Promise<Partial<MacportsConfig> | null> { |
| 44 | + console.log(fsSync.readFileSync(path.join(os.homedir(), '.zshrc'), 'utf8')) |
| 45 | + const $ = getPty(); |
| 46 | + |
| 47 | + const homebrewInfo = await $.spawnSafe('which port'); |
| 48 | + if (homebrewInfo.status === SpawnStatus.ERROR) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + return parameters; |
| 53 | + } |
| 54 | + |
| 55 | + override async create(plan: CreatePlan<MacportsConfig>): Promise<void> { |
| 56 | + const macOSVersion = (await codifySpawn('sw_vers --productVersion'))?.data?.split('.')?.at(0); |
| 57 | + if (!macOSVersion) { |
| 58 | + throw new Error('Unable to determine macOS version'); |
| 59 | + } |
| 60 | + |
| 61 | + const installerUrl = MACPORTS_DOWNLOAD_LINKS[macOSVersion]; |
| 62 | + if (!installerUrl) { |
| 63 | + throw new Error(`Your current macOS version ${macOSVersion} is not supported. Only ${Object.keys(MACPORTS_DOWNLOAD_LINKS)} is supported`); |
| 64 | + } |
| 65 | + |
| 66 | + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'codify-macports')); |
| 67 | + const installerPath = path.join(tmpDir, 'installer.pkg') |
| 68 | + |
| 69 | + console.log(`Downloading macports installer ${installerUrl}`) |
| 70 | + await Utils.downloadUrlIntoFile(installerPath, installerUrl); |
| 71 | + |
| 72 | + await codifySpawn(`installer -pkg "${installerPath}" -target /;`, { requiresRoot: true }) |
| 73 | + |
| 74 | + await FileUtils.addToStartupFile('') |
| 75 | + await FileUtils.addToStartupFile('export PATH=/opt/local/bin:/opt/local/sbin:$PATH') |
| 76 | + } |
| 77 | + |
| 78 | + override async destroy(): Promise<void> { |
| 79 | + await codifySpawn('port -fp uninstall installed', { requiresRoot: true, throws: false }); |
| 80 | + await codifySpawn('dscl . -delete /Users/macports', { requiresRoot: true, throws: false }); |
| 81 | + await codifySpawn('dscl . -delete /Groups/macports', { requiresRoot: true, throws: false }); |
| 82 | + await codifySpawn('rm -rf \\\n' + |
| 83 | + ' /opt/local \\\n' + |
| 84 | + ' /Applications/DarwinPorts \\\n' + |
| 85 | + ' /Applications/MacPorts \\\n' + |
| 86 | + ' /Library/LaunchDaemons/org.macports.* \\\n' + |
| 87 | + ' /Library/Receipts/DarwinPorts*.pkg \\\n' + |
| 88 | + ' /Library/Receipts/MacPorts*.pkg \\\n' + |
| 89 | + ' /Library/StartupItems/DarwinPortsStartup \\\n' + |
| 90 | + ' /Library/Tcl/darwinports1.0 \\\n' + |
| 91 | + ' /Library/Tcl/macports1.0 \\\n' + |
| 92 | + ' ~/.macports', { requiresRoot: true, throws: false }) |
| 93 | + |
| 94 | + await FileUtils.removeLineFromZshrc('export PATH=/opt/local/bin:/opt/local/sbin:$PATH'); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments