mirror of
https://github.com/gradle/gradle-build-action.git
synced 2025-06-05 07:46:11 +02:00
Use built-in library functions in preference to custom implementations
- Use built-in `hashFiles` function included in '@actions/globv0.2.0' - Use `downloadTool` and `extractZip` functions from '@actions/tool-cache'
This commit is contained in:
parent
47c9af9d7d
commit
738bda9866
6 changed files with 39 additions and 227 deletions
|
@ -1,44 +1,13 @@
|
|||
import * as crypto from 'crypto'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import * as stream from 'stream'
|
||||
import * as util from 'util'
|
||||
|
||||
import * as glob from '@actions/glob'
|
||||
|
||||
export async function hashFiles(
|
||||
baseDir: string,
|
||||
globs: string[] = ['**'],
|
||||
patterns: string[] = ['**'],
|
||||
followSymbolicLinks = false
|
||||
): Promise<string | null> {
|
||||
let hasMatch = false
|
||||
type FileHashes = Record<string, Buffer>
|
||||
const hashes: FileHashes = {}
|
||||
for await (const globPattern of globs) {
|
||||
const globMatch = `${baseDir}${path.sep}${globPattern}`
|
||||
const globber = await glob.create(globMatch, {followSymbolicLinks})
|
||||
for await (const file of globber.globGenerator()) {
|
||||
// console.log(file)
|
||||
if (!file.startsWith(`${baseDir}${path.sep}`)) {
|
||||
// console.log(`Ignore '${file}' since it is not under '${baseDir}'.`)
|
||||
continue
|
||||
}
|
||||
if (fs.statSync(file).isDirectory()) {
|
||||
// console.log(`Skip directory '${file}'.`)
|
||||
continue
|
||||
}
|
||||
const hash = crypto.createHash('sha256')
|
||||
const pipeline = util.promisify(stream.pipeline)
|
||||
await pipeline(fs.createReadStream(file), hash)
|
||||
hashes[path.relative(baseDir, file)] = hash.digest()
|
||||
hasMatch = true
|
||||
}
|
||||
}
|
||||
if (!hasMatch) return null
|
||||
const result = crypto.createHash('sha256')
|
||||
for (const file of Object.keys(hashes).sort()) {
|
||||
result.update(hashes[file])
|
||||
}
|
||||
result.end()
|
||||
return result.digest('hex')
|
||||
const combinedPatterns = patterns
|
||||
.map(pattern => `${baseDir}${path.sep}${pattern}`)
|
||||
.join('\n')
|
||||
return glob.hashFiles(combinedPatterns, {followSymbolicLinks})
|
||||
}
|
||||
|
|
|
@ -2,14 +2,11 @@ import * as fs from 'fs'
|
|||
import * as os from 'os'
|
||||
import * as path from 'path'
|
||||
import * as httpm from '@actions/http-client'
|
||||
import * as unzip from 'unzipper'
|
||||
import * as core from '@actions/core'
|
||||
import * as io from '@actions/io'
|
||||
import * as toolCache from '@actions/tool-cache'
|
||||
|
||||
import * as gradlew from './gradlew'
|
||||
|
||||
const httpc = new httpm.HttpClient('eskatos/gradle-command-action')
|
||||
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
||||
|
||||
/**
|
||||
|
@ -95,22 +92,21 @@ async function provisionGradle(version: string, url: string): Promise<string> {
|
|||
return cachedExecutable
|
||||
}
|
||||
|
||||
const home = os.homedir()
|
||||
const tmpdir = path.join(home, 'gradle-provision-tmpdir')
|
||||
const downloadsDir = path.join(tmpdir, 'downloads')
|
||||
const installsDir = path.join(tmpdir, 'installs')
|
||||
await io.mkdirP(downloadsDir)
|
||||
await io.mkdirP(installsDir)
|
||||
const tmpdir = path.join(os.homedir(), 'gradle-provision-tmpdir')
|
||||
|
||||
core.info(`Downloading ${url}`)
|
||||
|
||||
const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`)
|
||||
await httpDownload(url, downloadPath)
|
||||
const downloadPath = path.join(
|
||||
tmpdir,
|
||||
`downloads/gradle-${version}-bin.zip`
|
||||
)
|
||||
await toolCache.downloadTool(url, downloadPath)
|
||||
core.info(
|
||||
`Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}`
|
||||
)
|
||||
|
||||
await extractZip(downloadPath, installsDir)
|
||||
const installsDir = path.join(tmpdir, 'installs')
|
||||
await toolCache.extractZip(downloadPath, installsDir)
|
||||
const installDir = path.join(installsDir, `gradle-${version}`)
|
||||
core.info(`Extracted in ${installDir}`)
|
||||
|
||||
|
@ -138,38 +134,11 @@ async function httpGetGradleVersions(
|
|||
}
|
||||
|
||||
async function httpGetString(url: string): Promise<string> {
|
||||
const response = await httpc.get(url)
|
||||
const httpClient = new httpm.HttpClient('eskatos/gradle-command-action')
|
||||
const response = await httpClient.get(url)
|
||||
return response.readBody()
|
||||
}
|
||||
|
||||
async function httpDownload(url: string, localPath: string): Promise<void> {
|
||||
const response = await httpc.get(url)
|
||||
return new Promise<void>(function (resolve, reject) {
|
||||
const writeStream = fs.createWriteStream(localPath)
|
||||
response.message
|
||||
.pipe(writeStream)
|
||||
.on('close', () => {
|
||||
resolve()
|
||||
})
|
||||
.on('error', err => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function extractZip(zip: string, destination: string): Promise<void> {
|
||||
return new Promise<void>(function (resolve, reject) {
|
||||
fs.createReadStream(zip)
|
||||
.pipe(unzip.Extract({path: destination}))
|
||||
.on('close', () => {
|
||||
resolve()
|
||||
})
|
||||
.on('error', err => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
interface GradleVersionInfo {
|
||||
version: string
|
||||
downloadUrl: string
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue