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