|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { injectFromHierarchy, injectable } from 'inversify'; |
| 4 | +import { BaseInstallService } from '../../install-tool/base-install.service'; |
| 5 | +import { BasePrepareService } from '../../prepare-tool/base-prepare.service'; |
| 6 | +import { isFourPartVersion } from '../../utils'; |
| 7 | + |
| 8 | +@injectable() |
| 9 | +@injectFromHierarchy() |
| 10 | +export class CabalPrepareService extends BasePrepareService { |
| 11 | + readonly name = 'cabal'; |
| 12 | +} |
| 13 | + |
| 14 | +@injectable() |
| 15 | +@injectFromHierarchy() |
| 16 | +export class CabalInstallService extends BaseInstallService { |
| 17 | + readonly name = 'cabal'; |
| 18 | + |
| 19 | + private get arch(): string { |
| 20 | + switch (this.envSvc.arch) { |
| 21 | + case 'arm64': |
| 22 | + return 'aarch64'; |
| 23 | + case 'amd64': |
| 24 | + return 'x86_64'; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + override async install(version: string): Promise<void> { |
| 29 | + const baseUrl = `https://downloads.haskell.org/~cabal/cabal-install-${version}/`; |
| 30 | + // use static deb10 binary as it is compatible with all supported ubuntu versions |
| 31 | + const filename = `cabal-install-${version}-${this.arch}-linux-deb10.tar.xz`; |
| 32 | + |
| 33 | + const checksumFile = await this.http.download({ |
| 34 | + url: `${baseUrl}SHA256SUMS`, |
| 35 | + }); |
| 36 | + const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8')) |
| 37 | + .split('\n') |
| 38 | + .find((l) => l.includes(filename)) |
| 39 | + ?.split(' ')[0]; |
| 40 | + |
| 41 | + const file = await this.http.download({ |
| 42 | + url: `${baseUrl}${filename}`, |
| 43 | + checksumType: 'sha256', |
| 44 | + expectedChecksum, |
| 45 | + }); |
| 46 | + |
| 47 | + await this.pathSvc.ensureToolPath(this.name); |
| 48 | + |
| 49 | + const path = join( |
| 50 | + await this.pathSvc.createVersionedToolPath(this.name, version), |
| 51 | + 'bin', |
| 52 | + ); |
| 53 | + await fs.mkdir(path); |
| 54 | + await this.compress.extract({ |
| 55 | + file, |
| 56 | + cwd: path, |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + override validate(version: string): Promise<boolean> { |
| 61 | + return Promise.resolve(isFourPartVersion(version)); |
| 62 | + } |
| 63 | + |
| 64 | + override async link(version: string): Promise<void> { |
| 65 | + const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin'); |
| 66 | + |
| 67 | + await this.shellwrapper({ srcDir: src }); |
| 68 | + } |
| 69 | + |
| 70 | + override async test(_version: string): Promise<void> { |
| 71 | + await this._spawn('cabal', ['--version']); |
| 72 | + } |
| 73 | +} |
0 commit comments