|
| 1 | +import * as glob from '@actions/glob'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as exec from '@actions/exec'; |
| 5 | + |
| 6 | +import CacheDistributor from './cache-distributor'; |
| 7 | + |
| 8 | +class PoetryCache extends CacheDistributor { |
| 9 | + constructor( |
| 10 | + private pythonVersion: string, |
| 11 | + protected patterns: string = '**/poetry.lock' |
| 12 | + ) { |
| 13 | + super('poetry', patterns); |
| 14 | + } |
| 15 | + |
| 16 | + protected async getCacheGlobalDirectories() { |
| 17 | + const poetryConfig = await this.getPoetryConfiguration(); |
| 18 | + |
| 19 | + const cacheDir = poetryConfig['cache-dir']; |
| 20 | + const virtualenvsPath = poetryConfig['virtualenvs.path'].replace( |
| 21 | + '{cache-dir}', |
| 22 | + cacheDir |
| 23 | + ); |
| 24 | + |
| 25 | + const paths = [virtualenvsPath]; |
| 26 | + |
| 27 | + if (poetryConfig['virtualenvs.in-project'] === 'true') { |
| 28 | + paths.push(path.join(process.cwd(), '.venv')); |
| 29 | + } |
| 30 | + |
| 31 | + return paths; |
| 32 | + } |
| 33 | + |
| 34 | + protected async computeKeys() { |
| 35 | + const hash = await glob.hashFiles(this.patterns); |
| 36 | + const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; |
| 37 | + const restoreKey = undefined; |
| 38 | + return { |
| 39 | + primaryKey, |
| 40 | + restoreKey |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + private async getPoetryConfiguration() { |
| 45 | + const {stdout, stderr, exitCode} = await exec.getExecOutput( |
| 46 | + 'poetry config --list' |
| 47 | + ); |
| 48 | + |
| 49 | + if (exitCode && stderr) { |
| 50 | + throw new Error( |
| 51 | + `Could not get cache folder path for poetry package manager` |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + const lines = stdout.split(os.EOL); |
| 56 | + |
| 57 | + const config = {} as { |
| 58 | + 'cache-dir': string; |
| 59 | + 'virtualenvs.in-project': string; |
| 60 | + 'virtualenvs.path': string; |
| 61 | + }; |
| 62 | + |
| 63 | + for (let line of lines) { |
| 64 | + line = line.replace(/#.*$/, ''); |
| 65 | + |
| 66 | + const [key, value] = line.split('=').map(part => part.trim()); |
| 67 | + |
| 68 | + config[key as keyof typeof config] = value; |
| 69 | + } |
| 70 | + |
| 71 | + return config; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export default PoetryCache; |
0 commit comments