mirror of
https://github.com/actions/setup-python.git
synced 2024-11-21 22:50:58 -05:00
perf: add dynamically loaded cache-dependencies module
This reduces the main bundle size by ⚠️ 1.1 MB minified! This bundle is loaded only when caching is enabled.
The reason for this huge reduction is that caching dependencies uses the `@actions/cache` package, which is an extremely large package with big dependencies.
`setup-python` is used in `setup-cpp` as a library. This optimization reduces the bundle size for that package as well.
This commit is contained in:
parent
70dcb22d26
commit
9700887518
6 changed files with 76155 additions and 99283 deletions
|
@ -8,7 +8,6 @@ import path from 'path';
|
|||
import {
|
||||
validateVersion,
|
||||
validatePythonVersionFormatForPyPy,
|
||||
isCacheFeatureAvailable,
|
||||
getVersionInputFromFile,
|
||||
getVersionInputFromPlainFile,
|
||||
getVersionInputFromTomlFile,
|
||||
|
@ -16,6 +15,7 @@ import {
|
|||
IS_WINDOWS,
|
||||
getDownloadFileName
|
||||
} from '../src/utils';
|
||||
import {isCacheFeatureAvailable} from '../src/cache-dependencies';
|
||||
|
||||
jest.mock('@actions/cache');
|
||||
jest.mock('@actions/core');
|
||||
|
|
97142
dist/cache-save/index.js
vendored
97142
dist/cache-save/index.js
vendored
File diff suppressed because one or more lines are too long
77102
dist/setup/index.js
vendored
77102
dist/setup/index.js
vendored
File diff suppressed because one or more lines are too long
42
src/cache-dependencies.ts
Normal file
42
src/cache-dependencies.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
import {getCacheDistributor} from './cache-distributions/cache-factory';
|
||||
|
||||
export async function cacheDependencies(cache: string, pythonVersion: string) {
|
||||
if (!isCacheFeatureAvailable()) {
|
||||
return;
|
||||
}
|
||||
const cacheDependencyPath =
|
||||
core.getInput('cache-dependency-path') || undefined;
|
||||
const cacheDistributor = getCacheDistributor(
|
||||
cache,
|
||||
pythonVersion,
|
||||
cacheDependencyPath
|
||||
);
|
||||
await cacheDistributor.restoreCache();
|
||||
}
|
||||
|
||||
export function isGhes(): boolean {
|
||||
const ghUrl = new URL(
|
||||
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||
);
|
||||
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
|
||||
}
|
||||
|
||||
export function isCacheFeatureAvailable(): boolean {
|
||||
if (cache.isFeatureAvailable()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isGhes()) {
|
||||
core.warning(
|
||||
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
core.warning(
|
||||
'The runner was not able to contact the cache service. Caching will be skipped'
|
||||
);
|
||||
return false;
|
||||
}
|
|
@ -5,9 +5,7 @@ import * as finderGraalPy from './find-graalpy';
|
|||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import fs from 'fs';
|
||||
import {getCacheDistributor} from './cache-distributions/cache-factory';
|
||||
import {
|
||||
isCacheFeatureAvailable,
|
||||
logWarning,
|
||||
IS_MAC,
|
||||
getVersionInputFromFile,
|
||||
|
@ -22,17 +20,6 @@ function isGraalPyVersion(versionSpec: string) {
|
|||
return versionSpec.startsWith('graalpy');
|
||||
}
|
||||
|
||||
async function cacheDependencies(cache: string, pythonVersion: string) {
|
||||
const cacheDependencyPath =
|
||||
core.getInput('cache-dependency-path') || undefined;
|
||||
const cacheDistributor = getCacheDistributor(
|
||||
cache,
|
||||
pythonVersion,
|
||||
cacheDependencyPath
|
||||
);
|
||||
await cacheDistributor.restoreCache();
|
||||
}
|
||||
|
||||
function resolveVersionInputFromDefaultFile(): string[] {
|
||||
const couples: [string, (versionFile: string) => string[]][] = [
|
||||
['.python-version', getVersionInputFromPlainFile]
|
||||
|
@ -140,7 +127,8 @@ async function run() {
|
|||
}
|
||||
core.endGroup();
|
||||
const cache = core.getInput('cache');
|
||||
if (cache && isCacheFeatureAvailable()) {
|
||||
if (cache) {
|
||||
const {cacheDependencies} = await import('./cache-dependencies');
|
||||
await cacheDependencies(cache, pythonVersion);
|
||||
}
|
||||
} else {
|
||||
|
|
26
src/utils.ts
26
src/utils.ts
|
@ -1,5 +1,4 @@
|
|||
/* eslint no-unsafe-finally: "off" */
|
||||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
import fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
@ -112,31 +111,6 @@ export function validatePythonVersionFormatForPyPy(version: string) {
|
|||
return re.test(version);
|
||||
}
|
||||
|
||||
export function isGhes(): boolean {
|
||||
const ghUrl = new URL(
|
||||
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||
);
|
||||
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
|
||||
}
|
||||
|
||||
export function isCacheFeatureAvailable(): boolean {
|
||||
if (cache.isFeatureAvailable()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isGhes()) {
|
||||
core.warning(
|
||||
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
core.warning(
|
||||
'The runner was not able to contact the cache service. Caching will be skipped'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
export function logWarning(message: string): void {
|
||||
const warningPrefix = '[warning]';
|
||||
core.info(`${warningPrefix}${message}`);
|
||||
|
|
Loading…
Reference in a new issue