Unverified Commit 884bca01 authored by Daz DeBoer's avatar Daz DeBoer
Browse files

Extracted some classes and refactored for clarity

parent 7f46dbd7
Loading
Loading
Loading
Loading
+27
Changes for src/build-results.ts: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
import * as fs from 'fs'
import * as path from 'path'

export interface BuildResult {
    get rootProjectName(): string
    get rootProjectDir(): string
    get requestedTasks(): string
    get gradleVersion(): string
    get gradleHomeDir(): string
    get buildFailed(): boolean
    get buildScanUri(): string
    get buildScanFailed(): boolean
}

export function loadBuildResults(): BuildResult[] {
    const buildResultsDir = path.resolve(process.env['RUNNER_TEMP']!, '.build-results')
    if (!fs.existsSync(buildResultsDir)) {
        return []
    }

    return fs.readdirSync(buildResultsDir).map(file => {
        // Every file in the .build-results dir should be a BuildResults JSON
        const filePath = path.join(buildResultsDir, file)
        const content = fs.readFileSync(filePath, 'utf8')
        return JSON.parse(content) as BuildResult
    })
}
+1 −1
Changes for src/cache-extract-entries.ts: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ import {
    saveCache,
    tryDelete
} from './cache-utils'
import {loadBuildResults} from './job-summary'
import {loadBuildResults} from './build-results'

const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE'

+12 −12
Changes for src/cache-reporting.ts: 12 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -7,9 +7,9 @@ import * as cache from '@actions/cache'
 */
export class CacheListener {
    cacheEntries: CacheEntryListener[] = []
    isCacheReadOnly = false
    isCacheWriteOnly = false
    isCacheDisabled = false
    cacheReadOnly = false
    cacheWriteOnly = false
    cacheDisabled = false

    get fullyRestored(): boolean {
        return this.cacheEntries.every(x => !x.wasRequestedButNotRestored())
@@ -17,9 +17,9 @@ export class CacheListener {

    get cacheStatus(): string {
        if (!cache.isFeatureAvailable()) return 'not available'
        if (this.isCacheDisabled) return 'disabled'
        if (this.isCacheWriteOnly) return 'write-only'
        if (this.isCacheReadOnly) return 'read-only'
        if (this.cacheDisabled) return 'disabled'
        if (this.cacheWriteOnly) return 'write-only'
        if (this.cacheReadOnly) return 'read-only'
        return 'enabled'
    }

@@ -156,17 +156,17 @@ function renderEntryDetails(listener: CacheListener): string {
    Requested Key : ${entry.requestedKey ?? ''}
    Restored  Key : ${entry.restoredKey ?? ''}
              Size: ${formatSize(entry.restoredSize)}
              ${getRestoredMessage(entry, listener.isCacheWriteOnly)}
              ${getRestoredMessage(entry, listener.cacheWriteOnly)}
    Saved     Key : ${entry.savedKey ?? ''}
              Size: ${formatSize(entry.savedSize)}
              ${getSavedMessage(entry, listener.isCacheReadOnly)}
              ${getSavedMessage(entry, listener.cacheReadOnly)}
`
        )
        .join('---\n')
}

function getRestoredMessage(entry: CacheEntryListener, isCacheWriteOnly: boolean): string {
    if (isCacheWriteOnly) {
function getRestoredMessage(entry: CacheEntryListener, cacheWriteOnly: boolean): string {
    if (cacheWriteOnly) {
        return '(Entry not restored: cache is write-only)'
    }
    if (entry.restoredKey === undefined) {
@@ -178,12 +178,12 @@ function getRestoredMessage(entry: CacheEntryListener, isCacheWriteOnly: boolean
    return '(Entry restored: partial match found)'
}

function getSavedMessage(entry: CacheEntryListener, isCacheReadOnly: boolean): string {
function getSavedMessage(entry: CacheEntryListener, cacheReadOnly: boolean): string {
    if (entry.unsaved) {
        return `(Entry not saved: ${entry.unsaved})`
    }
    if (entry.savedKey === undefined) {
        if (isCacheReadOnly) {
        if (cacheReadOnly) {
            return '(Entry not saved: cache is read-only)'
        }
        return '(Entry not saved: reason unknown)'
+10 −18
Changes for src/caches.ts: 10 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ export async function restore(gradleUserHome: string, cacheListener: CacheListen
        core.info('Cache is disabled: will not restore state from previous builds.')
        // Initialize the Gradle User Home even when caching is disabled.
        gradleStateCache.init()
        cacheListener.isCacheDisabled = true
        cacheListener.cacheDisabled = true
        return
    }

@@ -36,7 +36,7 @@ export async function restore(gradleUserHome: string, cacheListener: CacheListen

    if (isCacheWriteOnly()) {
        core.info('Cache is write-only: will not restore from cache.')
        cacheListener.isCacheWriteOnly = true
        cacheListener.cacheWriteOnly = true
        return
    }

@@ -46,13 +46,19 @@ export async function restore(gradleUserHome: string, cacheListener: CacheListen
}

export async function save(gradleUserHome: string, cacheListener: CacheListener): Promise<void> {
    if (!shouldSaveCaches()) {
    if (isCacheDisabled()) {
        core.info('Cache is disabled: will not save state for later builds.')
        return
    }

    if (!core.getState(CACHE_RESTORED_VAR)) {
        core.info('Cache will not be saved: not restored in main action step.')
        return
    }

    if (isCacheReadOnly()) {
        core.info('Cache is read-only: will not save state for use in subsequent builds.')
        cacheListener.isCacheReadOnly = true
        cacheListener.cacheReadOnly = true
        return
    }

@@ -60,17 +66,3 @@ export async function save(gradleUserHome: string, cacheListener: CacheListener)
        return new GradleStateCache(gradleUserHome).save(cacheListener)
    })
}

function shouldSaveCaches(): boolean {
    if (isCacheDisabled()) {
        core.info('Cache is disabled: will not save state for later builds.')
        return false
    }

    if (!core.getState(CACHE_RESTORED_VAR)) {
        core.info('Cache will not be saved: not restored in main action step.')
        return false
    }

    return true
}
+36
Changes for src/daemon-controller.ts: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as fs from 'fs'
import * as path from 'path'
import {BuildResult} from './build-results'

export class DaemonController {
    private readonly gradleHomes

    constructor(buildResults: BuildResult[]) {
        const allHomes = buildResults.map(buildResult => buildResult.gradleHomeDir)
        this.gradleHomes = Array.from(new Set(allHomes))
    }

    async stopAllDaemons(): Promise<void> {
        core.info('Stopping all Gradle daemons')

        const executions: Promise<number>[] = []
        const args = ['--stop']

        for (const gradleHome of this.gradleHomes) {
            const executable = path.resolve(gradleHome, 'bin', 'gradle')
            if (!fs.existsSync(executable)) {
                core.warning(`Gradle executable not found at ${executable}. Could not stop Gradle daemons.`)
                continue
            }
            core.info(`Stopping Gradle daemons for ${gradleHome}`)
            executions.push(
                exec.exec(executable, args, {
                    ignoreReturnCode: true
                })
            )
        }
        await Promise.all(executions)
    }
}
Loading