diff --git a/.prettierrc.json b/.prettierrc.json index c7cf5c8..2a13cc5 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,5 +1,5 @@ { - "printWidth": 80, + "printWidth": 120, "tabWidth": 4, "useTabs": false, "semi": false, diff --git a/src/cache-gradle-user-home.ts b/src/cache-gradle-user-home.ts index 0a2902c..e000f6f 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -5,12 +5,7 @@ import * as core from '@actions/core' import * as glob from '@actions/glob' import * as exec from '@actions/exec' -import { - AbstractCache, - getCacheKeyPrefix, - hashFileNames, - tryDelete -} from './cache-utils' +import {AbstractCache, getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils' const META_FILE_DIR = '.gradle-build-action' @@ -46,27 +41,18 @@ export class GradleUserHomeCache extends AbstractCache { await Promise.all(processes) } - private async restoreArtifactBundle( - bundle: string, - artifactPath: string - ): Promise { + private async restoreArtifactBundle(bundle: string, artifactPath: string): Promise { const bundleMetaFile = this.getBundleMetaFile(bundle) if (fs.existsSync(bundleMetaFile)) { const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim() const restoreKey = await this.restoreCache([artifactPath], cacheKey) if (restoreKey) { - core.info( - `Restored ${bundle} with key ${cacheKey} to ${artifactPath}` - ) + core.info(`Restored ${bundle} with key ${cacheKey} to ${artifactPath}`) } else { - this.debug( - `Did not restore ${bundle} with key ${cacheKey} to ${artifactPath}` - ) + this.debug(`Did not restore ${bundle} with key ${cacheKey} to ${artifactPath}`) } } else { - this.debug( - `No metafile found to restore ${bundle}: ${bundleMetaFile}` - ) + this.debug(`No metafile found to restore ${bundle}: ${bundleMetaFile}`) } } @@ -84,12 +70,8 @@ export class GradleUserHomeCache extends AbstractCache { } private removeExcludedPaths(): void { - const rawPaths: string[] = core.getMultilineInput( - EXCLUDE_PATHS_PARAMETER - ) - const resolvedPaths = rawPaths.map(x => - path.resolve(this.gradleUserHome, x) - ) + const rawPaths: string[] = core.getMultilineInput(EXCLUDE_PATHS_PARAMETER) + const resolvedPaths = rawPaths.map(x => path.resolve(this.gradleUserHome, x)) for (const p of resolvedPaths) { this.debug(`Deleting excluded path: ${p}`) @@ -111,10 +93,7 @@ export class GradleUserHomeCache extends AbstractCache { await Promise.all(processes) } - private async saveArtifactBundle( - bundle: string, - artifactPath: string - ): Promise { + private async saveArtifactBundle(bundle: string, artifactPath: string): Promise { const bundleMetaFile = this.getBundleMetaFile(bundle) const globber = await glob.create(artifactPath, { @@ -138,9 +117,7 @@ export class GradleUserHomeCache extends AbstractCache { const cacheKey = this.createCacheKey(bundle, bundleFiles) if (previouslyRestoredKey === cacheKey) { - this.debug( - `No change to previously restored ${bundle}. Not caching.` - ) + this.debug(`No change to previously restored ${bundle}. Not caching.`) } else { core.info(`Caching ${bundle} with cache key: ${cacheKey}`) await this.saveCache([artifactPath], cacheKey) @@ -154,14 +131,10 @@ export class GradleUserHomeCache extends AbstractCache { protected createCacheKey(bundle: string, files: string[]): string { const cacheKeyPrefix = getCacheKeyPrefix() - const relativeFiles = files.map(x => - path.relative(this.gradleUserHome, x) - ) + const relativeFiles = files.map(x => path.relative(this.gradleUserHome, x)) const key = hashFileNames(relativeFiles) - this.debug( - `Generating cache key for ${bundle} from files: ${relativeFiles}` - ) + this.debug(`Generating cache key for ${bundle} from files: ${relativeFiles}`) return `${cacheKeyPrefix}${bundle}-${key}` } @@ -193,9 +166,7 @@ export class GradleUserHomeCache extends AbstractCache { } protected getCachePath(): string[] { - const rawPaths: string[] = core.getMultilineInput( - INCLUDE_PATHS_PARAMETER - ) + const rawPaths: string[] = core.getMultilineInput(INCLUDE_PATHS_PARAMETER) rawPaths.push(META_FILE_DIR) const resolvedPaths = rawPaths.map(x => this.resolveCachePath(x)) this.debug(`Using cache paths: ${resolvedPaths}`) @@ -211,19 +182,10 @@ export class GradleUserHomeCache extends AbstractCache { } private getArtifactBundles(): Map { - const artifactBundleDefinition = core.getInput( - ARTIFACT_BUNDLES_PARAMETER - ) - this.debug( - `Using artifact bundle definition: ${artifactBundleDefinition}` - ) + const artifactBundleDefinition = core.getInput(ARTIFACT_BUNDLES_PARAMETER) + this.debug(`Using artifact bundle definition: ${artifactBundleDefinition}`) const artifactBundles = JSON.parse(artifactBundleDefinition) - return new Map( - Array.from(artifactBundles, ([key, value]) => [ - key, - path.resolve(this.gradleUserHome, value) - ]) - ) + return new Map(Array.from(artifactBundles, ([key, value]) => [key, path.resolve(this.gradleUserHome, value)])) } private async reportGradleUserHomeSize(label: string): Promise { @@ -233,15 +195,11 @@ export class GradleUserHomeCache extends AbstractCache { if (!fs.existsSync(this.gradleUserHome)) { return } - const result = await exec.getExecOutput( - 'du', - ['-h', '-c', '-t', '5M'], - { - cwd: this.gradleUserHome, - silent: true, - ignoreReturnCode: true - } - ) + const result = await exec.getExecOutput('du', ['-h', '-c', '-t', '5M'], { + cwd: this.gradleUserHome, + silent: true, + ignoreReturnCode: true + }) core.info(`Gradle User Home (directories >5M): ${label}`) diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 722cf3d..73b054a 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -44,11 +44,7 @@ function generateCacheKey(cacheName: string): CacheKey { // Exact match on Git SHA const cacheKey = `${cacheKeyForJobContext}-${github.context.sha}` - return new CacheKey(cacheKey, [ - cacheKeyForJobContext, - cacheKeyForJob, - cacheKeyForOs - ]) + return new CacheKey(cacheKey, [cacheKeyForJobContext, cacheKeyForJob, cacheKeyForOs]) } function determineJobContext(): string { @@ -66,9 +62,7 @@ export function hashStrings(values: string[]): string { } export function hashFileNames(fileNames: string[]): string { - return hashStrings( - fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/')) - ) + return hashStrings(fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/'))) } /** @@ -127,9 +121,7 @@ export abstract class AbstractCache { async restore(): Promise { if (this.cacheOutputExists()) { - core.info( - `${this.cacheDescription} already exists. Not restoring from cache.` - ) + core.info(`${this.cacheDescription} already exists. Not restoring from cache.`) return } @@ -143,31 +135,21 @@ export abstract class AbstractCache { restoreKeys:[${cacheKey.restoreKeys}]` ) - const cacheResult = await this.restoreCache( - this.getCachePath(), - cacheKey.key, - cacheKey.restoreKeys - ) + const cacheResult = await this.restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys) if (!cacheResult) { - core.info( - `${this.cacheDescription} cache not found. Will start with empty.` - ) + core.info(`${this.cacheDescription} cache not found. Will start with empty.`) return } core.saveState(this.cacheResultStateKey, cacheResult) - core.info( - `Restored ${this.cacheDescription} from cache key: ${cacheResult}` - ) + core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult}`) try { await this.afterRestore() } catch (error) { - core.warning( - `Restore ${this.cacheDescription} failed in 'afterRestore': ${error}` - ) + core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`) } return @@ -179,11 +161,7 @@ export abstract class AbstractCache { cacheRestoreKeys: string[] = [] ): Promise { try { - return await cache.restoreCache( - cachePath, - cacheKey, - cacheRestoreKeys - ) + return await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys) } catch (error) { if (error instanceof cache.ValidationError) { // Validation errors should fail the build action @@ -207,31 +185,23 @@ export abstract class AbstractCache { const cacheResult = core.getState(this.cacheResultStateKey) if (!cacheKey) { - this.debug( - `${this.cacheDescription} existed prior to cache restore. Not saving.` - ) + this.debug(`${this.cacheDescription} existed prior to cache restore. Not saving.`) return } if (cacheResult && cacheKey === cacheResult) { - core.info( - `Cache hit occurred on the cache key ${cacheKey}, not saving cache.` - ) + core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`) return } try { await this.beforeSave() } catch (error) { - core.warning( - `Save ${this.cacheDescription} failed in 'beforeSave': ${error}` - ) + core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`) return } - core.info( - `Caching ${this.cacheDescription} with cache key: ${cacheKey}` - ) + core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`) const cachePath = this.getCachePath() await this.saveCache(cachePath, cacheKey) @@ -240,10 +210,7 @@ export abstract class AbstractCache { protected async beforeSave(): Promise {} - protected async saveCache( - cachePath: string[], - cacheKey: string - ): Promise { + protected async saveCache(cachePath: string[], cacheKey: string): Promise { try { await cache.saveCache(cachePath, cacheKey) } catch (error) { diff --git a/src/caches.ts b/src/caches.ts index 154a41c..31b402d 100644 --- a/src/caches.ts +++ b/src/caches.ts @@ -7,9 +7,7 @@ const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR' export async function restore(buildRootDirectory: string): Promise { if (isCacheDisabled()) { - core.info( - 'Cache is disabled: will not restore state from previous builds.' - ) + core.info('Cache is disabled: will not restore state from previous builds.') return } @@ -24,9 +22,7 @@ export async function restore(buildRootDirectory: string): Promise { export async function save(): Promise { if (isCacheReadOnly()) { - core.info( - 'Cache is read-only: will not save state for use in subsequent builds.' - ) + core.info('Cache is read-only: will not save state for use in subsequent builds.') return } diff --git a/src/execution.ts b/src/execution.ts index 88105d0..c28abc1 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -3,11 +3,7 @@ import fs from 'fs' import path from 'path' import {writeInitScript} from './build-scan-capture' -export async function execute( - executable: string, - root: string, - args: string[] -): Promise { +export async function execute(executable: string, root: string, args: string[]): Promise { let buildScanUrl: string | undefined // TODO: instead of running with no-daemon, run `--stop` in post action. diff --git a/src/gradlew.ts b/src/gradlew.ts index 69815c7..59d49d8 100644 --- a/src/gradlew.ts +++ b/src/gradlew.ts @@ -17,10 +17,7 @@ export function locateGradleWrapperScript(buildRootDirectory: string): string { } function validateGradleWrapper(buildRootDirectory: string): void { - const wrapperProperties = path.resolve( - buildRootDirectory, - 'gradle/wrapper/gradle-wrapper.properties' - ) + const wrapperProperties = path.resolve(buildRootDirectory, 'gradle/wrapper/gradle-wrapper.properties') if (!fs.existsSync(wrapperProperties)) { throw new Error( `Cannot locate a Gradle wrapper properties file at '${wrapperProperties}'. Specify 'gradle-version' or 'gradle-executable' for projects without Gradle wrapper configured.` diff --git a/src/main.ts b/src/main.ts index 4a17a32..9a57948 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,10 +18,7 @@ export async function run(): Promise { const args: string[] = parseCommandLineArguments() const result = await execution.execute( - await resolveGradleExecutable( - workspaceDirectory, - buildRootDirectory - ), + await resolveGradleExecutable(workspaceDirectory, buildRootDirectory), buildRootDirectory, args ) @@ -34,9 +31,7 @@ export async function run(): Promise { if (result.buildScanUrl) { core.setFailed(`Gradle build failed: ${result.buildScanUrl}`) } else { - core.setFailed( - `Gradle build failed: process exited with status ${result.status}` - ) + core.setFailed(`Gradle build failed: process exited with status ${result.status}`) } } else { if (result.buildScanUrl) { @@ -53,10 +48,7 @@ export async function run(): Promise { run() -async function resolveGradleExecutable( - workspaceDirectory: string, - buildRootDirectory: string -): Promise { +async function resolveGradleExecutable(workspaceDirectory: string, buildRootDirectory: string): Promise { const gradleVersion = core.getInput('gradle-version') if (gradleVersion !== '' && gradleVersion !== 'wrapper') { return path.resolve(await provision.gradleVersion(gradleVersion)) @@ -73,9 +65,7 @@ async function resolveGradleExecutable( function resolveBuildRootDirectory(baseDirectory: string): string { const buildRootDirectory = core.getInput('build-root-directory') const resolvedBuildRootDirectory = - buildRootDirectory === '' - ? path.resolve(baseDirectory) - : path.resolve(baseDirectory, buildRootDirectory) + buildRootDirectory === '' ? path.resolve(baseDirectory) : path.resolve(baseDirectory, buildRootDirectory) return resolvedBuildRootDirectory } diff --git a/src/provision.ts b/src/provision.ts index d69711c..f4d0303 100644 --- a/src/provision.ts +++ b/src/provision.ts @@ -19,9 +19,7 @@ export async function gradleVersion(version: string): Promise { case 'current': return gradleCurrent() case 'rc': - core.warning( - `Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.` - ) + core.warning(`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`) return gradleReleaseCandidate() case 'release-candidate': return gradleReleaseCandidate() @@ -35,16 +33,12 @@ export async function gradleVersion(version: string): Promise { } async function gradleCurrent(): Promise { - const versionInfo = await gradleVersionDeclaration( - `${gradleVersionsBaseUrl}/current` - ) + const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`) return provisionGradle(versionInfo) } async function gradleReleaseCandidate(): Promise { - const versionInfo = await gradleVersionDeclaration( - `${gradleVersionsBaseUrl}/release-candidate` - ) + const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`) if (versionInfo && versionInfo.version && versionInfo.downloadUrl) { return provisionGradle(versionInfo) } @@ -53,16 +47,12 @@ async function gradleReleaseCandidate(): Promise { } async function gradleNightly(): Promise { - const versionInfo = await gradleVersionDeclaration( - `${gradleVersionsBaseUrl}/nightly` - ) + const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`) return provisionGradle(versionInfo) } async function gradleReleaseNightly(): Promise { - const versionInfo = await gradleVersionDeclaration( - `${gradleVersionsBaseUrl}/release-nightly` - ) + const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`) return provisionGradle(versionInfo) } @@ -74,34 +64,24 @@ async function gradle(version: string): Promise { return provisionGradle(versionInfo) } -async function gradleVersionDeclaration( - url: string -): Promise { +async function gradleVersionDeclaration(url: string): Promise { return await httpGetGradleVersion(url) } -async function findGradleVersionDeclaration( - version: string -): Promise { - const gradleVersions = await httpGetGradleVersions( - `${gradleVersionsBaseUrl}/all` - ) +async function findGradleVersionDeclaration(version: string): Promise { + const gradleVersions = await httpGetGradleVersions(`${gradleVersionsBaseUrl}/all`) return gradleVersions.find((entry: GradleVersionInfo) => { return entry.version === version }) } -async function provisionGradle( - versionInfo: GradleVersionInfo -): Promise { +async function provisionGradle(versionInfo: GradleVersionInfo): Promise { return core.group(`Provision Gradle ${versionInfo.version}`, async () => { return locateGradleAndDownloadIfRequired(versionInfo) }) } -async function locateGradleAndDownloadIfRequired( - versionInfo: GradleVersionInfo -): Promise { +async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo): Promise { const installsDir = path.join(os.homedir(), 'gradle-installations/installs') const installDir = path.join(installsDir, `gradle-${versionInfo.version}`) if (fs.existsSync(installDir)) { @@ -120,13 +100,8 @@ async function locateGradleAndDownloadIfRequired( return executable } -async function downloadAndCacheGradleDistribution( - versionInfo: GradleVersionInfo -): Promise { - const downloadPath = path.join( - os.homedir(), - `gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip` - ) +async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo): Promise { + const downloadPath = path.join(os.homedir(), `gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`) if (isCacheDisabled()) { await downloadGradleDistribution(versionInfo, downloadPath) @@ -136,14 +111,10 @@ async function downloadAndCacheGradleDistribution( const cacheKey = `gradle-${versionInfo.version}` const restoreKey = await cache.restoreCache([downloadPath], cacheKey) if (restoreKey) { - core.info( - `Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}` - ) + core.info(`Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}`) return downloadPath } - core.info( - `Gradle distribution ${versionInfo.version} not found in cache. Will download.` - ) + core.info(`Gradle distribution ${versionInfo.version} not found in cache. Will download.`) await downloadGradleDistribution(versionInfo, downloadPath) if (!isCacheReadOnly()) { @@ -151,10 +122,7 @@ async function downloadAndCacheGradleDistribution( await cache.saveCache([downloadPath], cacheKey) } catch (error) { // Fail on validation errors or non-errors (the latter to keep Typescript happy) - if ( - error instanceof cache.ValidationError || - !(error instanceof Error) - ) { + if (error instanceof cache.ValidationError || !(error instanceof Error)) { throw error } core.warning(error.message) @@ -163,16 +131,9 @@ async function downloadAndCacheGradleDistribution( return downloadPath } -async function downloadGradleDistribution( - versionInfo: GradleVersionInfo, - downloadPath: string -): Promise { +async function downloadGradleDistribution(versionInfo: GradleVersionInfo, downloadPath: string): Promise { await toolCache.downloadTool(versionInfo.downloadUrl, downloadPath) - core.info( - `Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${ - fs.statSync(downloadPath).size - })` - ) + core.info(`Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${fs.statSync(downloadPath).size})`) } function executableFrom(installDir: string): string { @@ -183,9 +144,7 @@ async function httpGetGradleVersion(url: string): Promise { return JSON.parse(await httpGetString(url)) } -async function httpGetGradleVersions( - url: string -): Promise { +async function httpGetGradleVersions(url: string): Promise { return JSON.parse(await httpGetString(url)) }