Skip to content

Commit

Permalink
Cache installations in GitHub tool-cache.
Browse files Browse the repository at this point in the history
Fixes graalvm#7
  • Loading branch information
fniephaus committed Mar 1, 2022
1 parent 61450f0 commit 66dc2bf
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 30 deletions.
51 changes: 36 additions & 15 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as otypes from '@octokit/types'
import {homedir} from 'os'
import {join} from 'path'

export const IS_LINUX = process.platform === 'linux'
export const IS_MACOS = process.platform === 'darwin'
Expand All @@ -9,7 +7,6 @@ export const IS_WINDOWS = process.platform === 'win32'
export const VERSION_DEV = 'dev'
export const VERSION_LATEST = 'latest'

export const GRAALVM_BASE = join(homedir(), '.graalvm')
export const GRAALVM_FILE_EXTENSION = IS_WINDOWS ? '.zip' : '.tar.gz'
export const GRAALVM_GH_USER = 'graalvm'
export const GRAALVM_PLATFORM = IS_WINDOWS ? 'windows' : process.platform
Expand Down
13 changes: 11 additions & 2 deletions src/graalvm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as c from './constants'
import {downloadAndExtractJDK, getLatestRelease} from './utils'
import {
downloadAndExtractJDK,
downloadExtractAndCacheJDK,
getLatestRelease
} from './utils'

const GRAALVM_CE_DL_BASE =
'https://github.com/graalvm/graalvm-ce-builds/releases/download'
Expand Down Expand Up @@ -40,7 +44,8 @@ export async function setUpGraalVMRelease(
): Promise<string> {
const graalVMIdentifier = determineGraalVMIdentifier(version, javaVersion)
const downloadUrl = `${GRAALVM_CE_DL_BASE}/${GRAALVM_TAG_PREFIX}${version}/${graalVMIdentifier}${c.GRAALVM_FILE_EXTENSION}`
return downloadAndExtractJDK(downloadUrl)
const toolName = determineToolName(javaVersion)
return downloadExtractAndCacheJDK(downloadUrl, toolName, version)
}

function determineGraalVMIdentifier(
Expand All @@ -49,3 +54,7 @@ function determineGraalVMIdentifier(
): string {
return `graalvm-ce-java${javaVersion}-${c.GRAALVM_PLATFORM}-amd64-${version}`
}

function determineToolName(javaVersion: string): string {
return `graalvm-ce-java${javaVersion}-${c.GRAALVM_PLATFORM}`
}
3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as c from './constants'
import * as core from '@actions/core'
import * as graalvm from './graalvm'
import {join} from 'path'
import {mkdirP} from '@actions/io'
import {setUpDependencies} from './dependencies'
import {setUpGUComponents} from './gu'
import {setUpMandrel} from './mandrel'
Expand All @@ -27,8 +26,6 @@ async function run(): Promise<void> {
await setUpNativeImageMusl()
}

await mkdirP(c.GRAALVM_BASE)

// Download or build GraalVM
let graalVMHome
switch (graalvmVersion) {
Expand Down
9 changes: 7 additions & 2 deletions src/mandrel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as c from './constants'
import {downloadAndExtractJDK, getLatestRelease} from './utils'
import {downloadExtractAndCacheJDK, getLatestRelease} from './utils'

const MANDREL_REPO = 'mandrel'
const MANDREL_TAG_PREFIX = c.MANDREL_NAMESPACE
Expand Down Expand Up @@ -46,7 +46,8 @@ async function setUpMandrelRelease(
): Promise<string> {
const identifier = determineMandrelIdentifier(version, javaVersion)
const downloadUrl = `${MANDREL_DL_BASE}/${MANDREL_TAG_PREFIX}${version}/${identifier}${c.GRAALVM_FILE_EXTENSION}`
return downloadAndExtractJDK(downloadUrl)
const toolName = determineToolName(javaVersion)
return downloadExtractAndCacheJDK(downloadUrl, toolName, version)
}

function determineMandrelIdentifier(
Expand All @@ -55,3 +56,7 @@ function determineMandrelIdentifier(
): string {
return `mandrel-java${javaVersion}-${c.GRAALVM_PLATFORM}-amd64-${version}`
}

function determineToolName(javaVersion: string): string {
return `mandrel-java${javaVersion}-${c.GRAALVM_PLATFORM}`
}
27 changes: 23 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,37 @@ export async function getLatestRelease(
export async function downloadAndExtractJDK(
downloadUrl: string
): Promise<string> {
return findJavaHomeInSubfolder(await downloadAndExtract(downloadUrl))
}

export async function downloadExtractAndCacheJDK(
downloadUrl: string,
toolName: string,
version: string
): Promise<string> {
let toolPath = tc.find(toolName, version)
if (toolPath) {
core.info(`Found ${toolName} ${version} in tool-cache @ ${toolPath}`)
} else {
const extractDir = await downloadAndExtract(downloadUrl)
core.info(`Adding ${toolName} ${version} to tool-cache ...`)
toolPath = await tc.cacheDir(extractDir, toolName, version)
}
return findJavaHomeInSubfolder(toolPath)
}

async function downloadAndExtract(downloadUrl: string): Promise<string> {
const downloadPath = await tc.downloadTool(downloadUrl)
if (downloadUrl.endsWith('.tar.gz')) {
await tc.extractTar(downloadPath, c.GRAALVM_BASE)
return await tc.extractTar(downloadPath)
} else if (downloadUrl.endsWith('.zip')) {
await tc.extractZip(downloadPath, c.GRAALVM_BASE)
return await tc.extractZip(downloadPath)
} else {
throw new Error(`Unexpected filetype downloaded: ${downloadUrl}`)
}
return findJavaHomeInSubfolder(c.GRAALVM_BASE)
}

export function findJavaHomeInSubfolder(searchPath: string): string {
function findJavaHomeInSubfolder(searchPath: string): string {
const baseContents = readdirSync(searchPath)
if (baseContents.length === 1) {
return join(searchPath, baseContents[0], c.JDK_HOME_SUFFIX)
Expand Down

0 comments on commit 66dc2bf

Please sign in to comment.