|
| 1 | +import { isTestExecution } from '../../../common/constants'; |
| 2 | +import { exec, pathExists } from '../externalDependencies'; |
| 3 | +import { traceVerbose } from '../../../logging'; |
| 4 | +import { cache } from '../../../common/utils/decorators'; |
| 5 | + |
| 6 | +/** Wraps the "Hatch" utility, and exposes its functionality. |
| 7 | + */ |
| 8 | +export class Hatch { |
| 9 | + /** |
| 10 | + * Locating Hatch binary can be expensive, since it potentially involves spawning or |
| 11 | + * trying to spawn processes; so we only do it once per session. |
| 12 | + */ |
| 13 | + private static hatchPromise: Map<string, Promise<Hatch | undefined>> = new Map< |
| 14 | + string, |
| 15 | + Promise<Hatch | undefined> |
| 16 | + >(); |
| 17 | + |
| 18 | + /** |
| 19 | + * Creates a Hatch service corresponding to the corresponding "hatch" command. |
| 20 | + * |
| 21 | + * @param command - Command used to run hatch. This has the same meaning as the |
| 22 | + * first argument of spawn() - i.e. it can be a full path, or just a binary name. |
| 23 | + * @param cwd - The working directory to use as cwd when running hatch. |
| 24 | + */ |
| 25 | + constructor(public readonly command: string, private cwd: string) {} |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns a Hatch instance corresponding to the binary which can be used to run commands for the cwd. |
| 29 | + * |
| 30 | + * Every directory is a valid Hatch project, so this should always return a Hatch instance. |
| 31 | + */ |
| 32 | + public static async getHatch(cwd: string): Promise<Hatch | undefined> { |
| 33 | + if (Hatch.hatchPromise.get(cwd) === undefined || isTestExecution()) { |
| 34 | + Hatch.hatchPromise.set(cwd, Hatch.locate(cwd)); |
| 35 | + } |
| 36 | + return Hatch.hatchPromise.get(cwd); |
| 37 | + } |
| 38 | + |
| 39 | + private static async locate(cwd: string): Promise<Hatch | undefined> { |
| 40 | + // First thing this method awaits on should be hatch command execution, |
| 41 | + // hence perform all operations before that synchronously. |
| 42 | + const hatchPath = 'hatch'; |
| 43 | + traceVerbose(`Probing Hatch binary ${hatchPath}`); |
| 44 | + const hatch = new Hatch(hatchPath, cwd); |
| 45 | + const virtualenvs = await hatch.getEnvList(); |
| 46 | + if (virtualenvs !== undefined) { |
| 47 | + traceVerbose(`Found hatch binary ${hatchPath}`); |
| 48 | + return hatch; |
| 49 | + } |
| 50 | + traceVerbose(`Failed to find Hatch binary ${hatchPath}`); |
| 51 | + |
| 52 | + // Didn't find anything. |
| 53 | + traceVerbose(`No Hatch binary found`); |
| 54 | + return undefined; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Retrieves list of Python environments known to Hatch for this working directory. |
| 59 | + * Returns `undefined` if we failed to spawn in some way. |
| 60 | + * |
| 61 | + * Corresponds to "hatch env show --json". Swallows errors if any. |
| 62 | + */ |
| 63 | + public async getEnvList(): Promise<string[] | undefined> { |
| 64 | + return this.getEnvListCached(this.cwd); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Method created to facilitate caching. The caching decorator uses function arguments as cache key, |
| 69 | + * so pass in cwd on which we need to cache. |
| 70 | + */ |
| 71 | + @cache(30_000, true, 10_000) |
| 72 | + private async getEnvListCached(_cwd: string): Promise<string[] | undefined> { |
| 73 | + const envInfoOutput = await exec(this.command, ['env', 'show', '--json'], { |
| 74 | + cwd: this.cwd, |
| 75 | + throwOnStdErr: true, |
| 76 | + }).catch(traceVerbose); |
| 77 | + if (!envInfoOutput) { |
| 78 | + return undefined; |
| 79 | + } |
| 80 | + const envPaths = await Promise.all( |
| 81 | + Object.keys(JSON.parse(envInfoOutput.stdout)).map(async (name) => { |
| 82 | + const envPathOutput = await exec(this.command, ['env', 'find', name], { |
| 83 | + cwd: this.cwd, |
| 84 | + throwOnStdErr: true, |
| 85 | + }).catch(traceVerbose); |
| 86 | + if (!envPathOutput) return undefined; |
| 87 | + const dir = envPathOutput.stdout.trim(); |
| 88 | + return (await pathExists(dir)) ? dir : undefined; |
| 89 | + }), |
| 90 | + ); |
| 91 | + return envPaths.flatMap((r) => (r ? [r] : [])); |
| 92 | + } |
| 93 | +} |
0 commit comments