Skip to content

Commit 181df6a

Browse files
committed
Preserve file extension when downloading
Fixes #195
1 parent 00d42bd commit 181df6a

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

dist/main.js

Lines changed: 19 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/musl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as c from '../constants.js'
22
import * as core from '@actions/core'
33
import * as tc from '@actions/tool-cache'
44
import { join } from 'path'
5+
import { downloadFile } from '../utils.js'
56

67
const MUSL_NAME = 'musl-toolchain'
78
const MUSL_VERSION = '1.2.5-oracle-00001'
@@ -16,7 +17,7 @@ export async function setUpNativeImageMusl(): Promise<void> {
1617
core.info(`Found ${MUSL_NAME} ${MUSL_VERSION} in tool-cache @ ${toolPath}`)
1718
} else {
1819
core.startGroup(`Setting up musl for GraalVM Native Image...`)
19-
const muslDownloadPath = await tc.downloadTool(
20+
const muslDownloadPath = await downloadFile(
2021
`https://gds.oracle.com/download/bfs/archive/musl-toolchain-${MUSL_VERSION}-linux-amd64.tar.gz`
2122
)
2223
const muslExtractPath = await tc.extractTar(muslDownloadPath)

src/graalvm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as c from './constants.js'
22
import * as core from '@actions/core'
33
import * as semver from 'semver'
44
import {
5+
downloadFile,
56
downloadAndExtractJDK,
67
downloadExtractAndCacheJDK,
78
getContents,
@@ -10,7 +11,6 @@ import {
1011
getTaggedRelease
1112
} from './utils.js'
1213
import { downloadGraalVM, downloadGraalVMEELegacy } from './gds.js'
13-
import { downloadTool } from '@actions/tool-cache'
1414
import { basename } from 'path'
1515

1616
const GRAALVM_DL_BASE = 'https://download.oracle.com/graalvm'
@@ -153,7 +153,7 @@ function determineToolName(javaVersion: string, isCommunity: boolean) {
153153

154154
async function downloadGraalVMJDK(downloadUrl: string, javaVersion: string): Promise<string> {
155155
try {
156-
return await downloadTool(downloadUrl)
156+
return await downloadFile(downloadUrl)
157157
} catch (error) {
158158
if (error instanceof Error && error.message.includes('404')) {
159159
// Not Found
@@ -265,7 +265,7 @@ async function downloadGraalVMCELegacy(version: string, javaVersion: string): Pr
265265
const graalVMIdentifier = determineGraalVMLegacyIdentifier(false, version, javaVersion)
266266
const downloadUrl = `${GRAALVM_CE_DL_BASE}/${GRAALVM_TAG_PREFIX}${version}/${graalVMIdentifier}${c.GRAALVM_FILE_EXTENSION}`
267267
try {
268-
return await downloadTool(downloadUrl)
268+
return await downloadFile(downloadUrl)
269269
} catch (error) {
270270
if (error instanceof Error && error.message.includes('404')) {
271271
// Not Found

src/liberica.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as c from './constants.js'
22
import * as semver from 'semver'
3-
import { downloadExtractAndCacheJDK, getTaggedRelease, getMatchingTags } from './utils.js'
4-
import { downloadTool } from '@actions/tool-cache'
3+
import { downloadFile, downloadExtractAndCacheJDK, getTaggedRelease, getMatchingTags } from './utils.js'
54
import { spawnSync } from 'child_process'
65

76
const LIBERICA_GH_USER = 'bell-sw'
@@ -13,7 +12,7 @@ export async function setUpLiberica(javaVersion: string, javaPackage: string): P
1312
const resolvedJavaVersion = await findLatestLibericaJavaVersion(javaVersion)
1413
const downloadUrl = await findLibericaURL(resolvedJavaVersion, javaPackage)
1514
const toolName = determineToolName(javaVersion, javaPackage)
16-
return downloadExtractAndCacheJDK(async () => downloadTool(downloadUrl), toolName, javaVersion)
15+
return downloadExtractAndCacheJDK(async () => downloadFile(downloadUrl), toolName, javaVersion)
1716
}
1817

1918
export async function findLatestLibericaJavaVersion(javaVersion: string): Promise<string> {

src/utils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import * as semver from 'semver'
55
import * as tc from '@actions/tool-cache'
66
import * as fs from 'fs'
77
import { ExecOptions, exec as e } from '@actions/exec'
8+
import { ok } from 'assert'
89
import { readFileSync, readdirSync } from 'fs'
910
import { createHash } from 'crypto'
10-
import { join } from 'path'
11+
import { extname, join } from 'path'
1112
import { tmpdir } from 'os'
1213
import { GitHub } from '@actions/github/lib/utils.js'
1314

@@ -86,6 +87,22 @@ export async function downloadExtractAndCacheJDK(
8687
return findJavaHomeInSubfolder(toolPath)
8788
}
8889

90+
/**
91+
* This copy of tc.downloadTool() preserves the file extension in the dest.
92+
* The file extension is required on Windows runners without .NET to extract zip files correctly.
93+
* See #195 and https://github.com/actions/toolkit/blob/6b63a2bfc339a753a113d2266323a4d52d84dee0/packages/tool-cache/src/tool-cache.ts#L44
94+
*/
95+
export async function downloadFile(downloadUrl: string): Promise<string> {
96+
const dest = join(_getTempDirectory(), crypto.randomUUID(), extname(downloadUrl))
97+
return tc.downloadTool(downloadUrl, dest)
98+
}
99+
100+
function _getTempDirectory(): string {
101+
const tempDirectory = process.env['RUNNER_TEMP'] || ''
102+
ok(tempDirectory, 'Expected RUNNER_TEMP to be defined')
103+
return tempDirectory
104+
}
105+
89106
export function calculateSHA256(filePath: string): string {
90107
const hashSum = createHash('sha256')
91108
hashSum.update(readFileSync(filePath))

0 commit comments

Comments
 (0)