|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import { injectFromHierarchy, injectable } from 'inversify'; |
| 3 | +import { BaseInstallService } from '../install-tool/base-install.service.ts'; |
| 4 | + |
| 5 | +@injectable() |
| 6 | +@injectFromHierarchy() |
| 7 | +export class ApmInstallService extends BaseInstallService { |
| 8 | + readonly name = 'apm'; |
| 9 | + |
| 10 | + private get ghArch(): string { |
| 11 | + switch (this.envSvc.arch) { |
| 12 | + case 'arm64': |
| 13 | + return 'arm64'; |
| 14 | + case 'amd64': |
| 15 | + return 'x86_64'; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + override async install(version: string): Promise<void> { |
| 20 | + /** |
| 21 | + * APM (Agent Package Manager) ships self-contained PyInstaller `onedir` |
| 22 | + * bundles (an `apm` binary next to an `_internal` directory) as |
| 23 | + * `apm-<os>-<arch>.tar.gz` release assets, each accompanied by a |
| 24 | + * `.sha256` sidecar. |
| 25 | + * @see {@link https://github.com/microsoft/apm/releases} |
| 26 | + */ |
| 27 | + const baseUrl = `https://github.com/microsoft/apm/releases/download/v${version}/`; |
| 28 | + const filename = `apm-linux-${this.ghArch}.tar.gz`; |
| 29 | + const url = `${baseUrl}${filename}`; |
| 30 | + |
| 31 | + const checksumFile = await this.http.download({ url: `${url}.sha256` }); |
| 32 | + const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8')) |
| 33 | + .split('\n') |
| 34 | + .find((l) => l.includes(filename)) |
| 35 | + ?.split(/\s+/)[0]; |
| 36 | + |
| 37 | + const file = await this.http.download({ |
| 38 | + url, |
| 39 | + checksumType: 'sha256', |
| 40 | + expectedChecksum, |
| 41 | + }); |
| 42 | + |
| 43 | + await this.pathSvc.ensureToolPath(this.name); |
| 44 | + |
| 45 | + const path = await this.pathSvc.createVersionedToolPath(this.name, version); |
| 46 | + // strip the top-level `apm-linux-<arch>` directory so the `apm` binary and |
| 47 | + // its sibling `_internal` bundle end up directly in the versioned path. |
| 48 | + await this.compress.extract({ file, cwd: path, strip: 1 }); |
| 49 | + } |
| 50 | + |
| 51 | + override async link(version: string): Promise<void> { |
| 52 | + const src = this.pathSvc.versionedToolPath(this.name, version); |
| 53 | + await this.shellwrapper({ srcDir: src }); |
| 54 | + } |
| 55 | + |
| 56 | + override async test(_version: string): Promise<void> { |
| 57 | + await this._spawn(this.name, ['--version']); |
| 58 | + } |
| 59 | +} |
0 commit comments