|
| 1 | +import { |
| 2 | + CreatePlan, DestroyPlan, ModifyPlan, ParameterChange, RefreshContext, Resource, |
| 3 | + ResourceSettings, |
| 4 | + getPty |
| 5 | +} from 'codify-plugin-lib'; |
| 6 | +import { ResourceConfig } from 'codify-schemas'; |
| 7 | +import fs from 'node:fs/promises'; |
| 8 | +import path from 'node:path'; |
| 9 | + |
| 10 | +import { codifySpawn } from '../../../utils/codify-spawn.js'; |
| 11 | +import { FileUtils } from '../../../utils/file-utils.js'; |
| 12 | +import schema from './virtualenv-project-schema.json'; |
| 13 | + |
| 14 | +export interface VirtualenvProjectConfig extends ResourceConfig { |
| 15 | + dest: string; |
| 16 | + python?: string; |
| 17 | + noVcsIgnore?: boolean; |
| 18 | + systemSitePackages?: boolean; |
| 19 | + symlinks?: boolean; |
| 20 | + cwd?: string; |
| 21 | + automaticallyInstallRequirementsTxt?: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +// TODO: Remove path.resolve from cwd. |
| 25 | +export class VirtualenvProject extends Resource<VirtualenvProjectConfig> { |
| 26 | + |
| 27 | + getSettings(): ResourceSettings<VirtualenvProjectConfig> { |
| 28 | + return { |
| 29 | + id: 'virtualenv-project', |
| 30 | + schema, |
| 31 | + parameterSettings: { |
| 32 | + dest: { type: 'directory' }, |
| 33 | + python: { type: 'string', setting: true }, |
| 34 | + noVcsIgnore: { type: 'boolean', setting: true }, |
| 35 | + systemSitePackages: { type: 'boolean', setting: true }, |
| 36 | + symlinks: { type: 'boolean', setting: true }, |
| 37 | + cwd: { type: 'directory', setting: true }, |
| 38 | + automaticallyInstallRequirementsTxt: { type: 'boolean', setting: true }, |
| 39 | + }, |
| 40 | + allowMultiple: { |
| 41 | + identifyingParameters: ['dest'], |
| 42 | + }, |
| 43 | + dependencies: ['virtualenv', 'homebrew', 'pyenv'] |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + async refresh(parameters: Partial<VirtualenvProjectConfig>, context: RefreshContext<VirtualenvProjectConfig>): Promise<Partial<VirtualenvProjectConfig> | Partial<VirtualenvProjectConfig>[] | null> { |
| 48 | + const dir = parameters.cwd |
| 49 | + ? path.join(parameters.cwd, parameters.dest!) |
| 50 | + : parameters.dest!; |
| 51 | + |
| 52 | + if (!(await FileUtils.exists(dir))) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + if (!(await FileUtils.exists(path.join(dir, 'pyvenv.cfg')))) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + return parameters; |
| 61 | + } |
| 62 | + |
| 63 | + async create(plan: CreatePlan<VirtualenvProjectConfig>): Promise<void> { |
| 64 | + const desired = plan.desiredConfig; |
| 65 | + |
| 66 | + const command = 'virtualenv ' + |
| 67 | + (desired.python ? `-p ${desired.python} ` : '-p $(which python3) ') + |
| 68 | + (desired.noVcsIgnore ? `--no-vcs-ignore=${desired.noVcsIgnore} ` : '') + |
| 69 | + (desired.systemSitePackages ? `--system-site-packages=${desired.systemSitePackages} ` : '') + |
| 70 | + (desired.symlinks ? `--symlinks=${desired.symlinks} ` : '') + |
| 71 | + desired.dest; |
| 72 | + |
| 73 | + await codifySpawn(command, { cwd: desired.cwd ?? undefined }); |
| 74 | + |
| 75 | + if (desired.automaticallyInstallRequirementsTxt) { |
| 76 | + await codifySpawn(`source ${desired.dest}/bin/activate; pip install -r requirements.txt`, { cwd: desired.cwd }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + async destroy(plan: DestroyPlan<VirtualenvProjectConfig>): Promise<void> { |
| 81 | + const current = plan.currentConfig; |
| 82 | + |
| 83 | + const dir = current.cwd |
| 84 | + ? path.join(current.cwd, current.dest!) |
| 85 | + : current.dest!; |
| 86 | + |
| 87 | + await fs.rm(dir, { recursive: true, force: true }); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments