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