|
| 1 | +import { ParameterSetting, Plan, StatefulParameter, getPty } from 'codify-plugin-lib'; |
| 2 | + |
| 3 | +import { SnapConfig } from './snap.js'; |
| 4 | + |
| 5 | +export interface SnapPackage { |
| 6 | + name: string; |
| 7 | + channel?: string; |
| 8 | + classic?: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +export class SnapInstallParameter extends StatefulParameter<SnapConfig, Array<SnapPackage | string>> { |
| 12 | + |
| 13 | + getSettings(): ParameterSetting { |
| 14 | + return { |
| 15 | + type: 'array', |
| 16 | + filterInStatelessMode: (desired, current) => |
| 17 | + current.filter((c) => desired.some((d) => this.isSamePackage(d, c))), |
| 18 | + isElementEqual: this.isEqual, |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + async refresh(desired: Array<SnapPackage | string> | null, _config: Partial<SnapConfig>): Promise<Array<SnapPackage | string> | null> { |
| 23 | + const $ = getPty() |
| 24 | + const { data: installed } = await $.spawnSafe('snap list --unicode=never'); |
| 25 | + |
| 26 | + if (!installed || installed === '') { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + const r = installed.split(/\n/) |
| 31 | + .filter(Boolean) |
| 32 | + .slice(1) // Skip header line |
| 33 | + .map((l) => { |
| 34 | + const parts = l.split(/\s+/).filter(Boolean); |
| 35 | + const name = parts[0]; |
| 36 | + const channel = parts[3]; // Channel is the 4th column |
| 37 | + |
| 38 | + return { name, channel } |
| 39 | + }) |
| 40 | + .filter((pkg) => |
| 41 | + // Only return packages that are in the desired list |
| 42 | + desired?.some((d) => { |
| 43 | + if (typeof d === 'string') { |
| 44 | + return d === pkg.name; |
| 45 | + } |
| 46 | + |
| 47 | + return d.name === pkg.name; |
| 48 | + }) |
| 49 | + ) |
| 50 | + .map((installed) => { |
| 51 | + if (desired?.find((d) => typeof d === 'string' && d === installed.name)) { |
| 52 | + return installed.name; |
| 53 | + } |
| 54 | + |
| 55 | + const desiredPkg = desired?.find((d) => typeof d === 'object' && d.name === installed.name); |
| 56 | + if (desiredPkg && typeof desiredPkg === 'object' && !desiredPkg.channel && !desiredPkg.classic) { |
| 57 | + return { name: installed.name } |
| 58 | + } |
| 59 | + |
| 60 | + return installed; |
| 61 | + }) |
| 62 | + |
| 63 | + return r.length > 0 ? r : null; |
| 64 | + } |
| 65 | + |
| 66 | + async add(valueToAdd: Array<SnapPackage | string>, _plan: Plan<SnapConfig>): Promise<void> { |
| 67 | + await this.install(valueToAdd); |
| 68 | + } |
| 69 | + |
| 70 | + async modify(newValue: (SnapPackage | string)[], previousValue: (SnapPackage | string)[], _plan: Plan<SnapConfig>): Promise<void> { |
| 71 | + const valuesToAdd = newValue.filter((n) => !previousValue.some((p) => this.isSamePackage(n, p))); |
| 72 | + const valuesToRemove = previousValue.filter((p) => !newValue.some((n) => this.isSamePackage(n, p))); |
| 73 | + |
| 74 | + await this.uninstall(valuesToRemove); |
| 75 | + await this.install(valuesToAdd); |
| 76 | + } |
| 77 | + |
| 78 | + async remove(valueToRemove: (SnapPackage | string)[], _plan: Plan<SnapConfig>): Promise<void> { |
| 79 | + await this.uninstall(valueToRemove); |
| 80 | + } |
| 81 | + |
| 82 | + private async install(packages: Array<SnapPackage | string>): Promise<void> { |
| 83 | + if (!packages || packages.length === 0) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const $ = getPty(); |
| 88 | + |
| 89 | + // Install packages one by one since snap doesn't support batch installation |
| 90 | + for (const p of packages) { |
| 91 | + let command = 'snap install'; |
| 92 | + |
| 93 | + if (typeof p === 'string') { |
| 94 | + command += ` ${p}`; |
| 95 | + } else { |
| 96 | + command += ` ${p.name}`; |
| 97 | + |
| 98 | + if (p.channel) { |
| 99 | + command += ` --channel=${p.channel}`; |
| 100 | + } |
| 101 | + |
| 102 | + if (p.classic) { |
| 103 | + command += ' --classic'; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + await $.spawn(command, { requiresRoot: true, interactive: true }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private async uninstall(packages: Array<SnapPackage | string>): Promise<void> { |
| 112 | + if (!packages || packages.length === 0) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const $ = getPty(); |
| 117 | + |
| 118 | + // Uninstall packages one by one |
| 119 | + for (const p of packages) { |
| 120 | + const name = typeof p === 'string' ? p : p.name; |
| 121 | + await $.spawn(`snap remove ${name}`, { requiresRoot: true, interactive: true }); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + isSamePackage(a: SnapPackage | string, b: SnapPackage | string): boolean { |
| 126 | + if (typeof a === 'string' || typeof b === 'string') { |
| 127 | + if (typeof a === 'string' && typeof b === 'string') { |
| 128 | + return a === b; |
| 129 | + } |
| 130 | + |
| 131 | + if (typeof a === 'string' && typeof b === 'object') { |
| 132 | + return a === b.name; |
| 133 | + } |
| 134 | + |
| 135 | + if (typeof a === 'object' && typeof b === 'string') { |
| 136 | + return a.name === b; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (typeof a === 'object' && typeof b === 'object') { |
| 141 | + return a.name === b.name; |
| 142 | + } |
| 143 | + |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + isEqual(desired: SnapPackage | string, current: SnapPackage | string): boolean { |
| 148 | + if (typeof desired === 'string' || typeof current === 'string') { |
| 149 | + if (typeof desired === 'string' && typeof current === 'string') { |
| 150 | + return desired === current; |
| 151 | + } |
| 152 | + |
| 153 | + if (typeof desired === 'string' && typeof current === 'object') { |
| 154 | + return desired === current.name; |
| 155 | + } |
| 156 | + |
| 157 | + if (typeof desired === 'object' && typeof current === 'string') { |
| 158 | + return desired.name === current; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if (typeof desired === 'object' && typeof current === 'object') { |
| 163 | + // For snap, we only check name equality since channel can change |
| 164 | + // and classic is an installation flag, not a state |
| 165 | + return desired.name === current.name; |
| 166 | + } |
| 167 | + |
| 168 | + return false; |
| 169 | + } |
| 170 | +} |
0 commit comments