mirror of
https://github.com/gradle/actions.git
synced 2025-11-26 17:09:10 +08:00
Create toolchains.xml dynamically based on envs (#150)
Different runners have different JDKs installed, so using a hard-coded list for `toolchains.xml` doesn't work. With this change, the file is generated based on the available `JAVA_HOME_*` environment variables. Fixes #89 Thanks @hfhbd for the contribution! Co-authored-by: hfhbd <22521688+hfhbd@users.noreply.github.com>
This commit is contained in:
44
sources/src/caching/gradle-user-home-utils.ts
Normal file
44
sources/src/caching/gradle-user-home-utils.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
export function readResourceFileAsString(...paths: string[]): string {
|
||||
// Resolving relative to __dirname will allow node to find the resource at runtime
|
||||
const absolutePath = path.resolve(__dirname, '..', '..', '..', 'sources', 'src', 'resources', ...paths)
|
||||
return fs.readFileSync(absolutePath, 'utf8')
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over all `JAVA_HOME_{version}_{arch}` envs and construct the toolchain.xml.
|
||||
*
|
||||
* @VisibleForTesting
|
||||
*/
|
||||
export function getPredefinedToolchains(): string | null {
|
||||
const javaHomeEnvs: string[] = []
|
||||
for (const javaHomeEnvsKey in process.env) {
|
||||
if (javaHomeEnvsKey.startsWith('JAVA_HOME_')) {
|
||||
javaHomeEnvs.push(javaHomeEnvsKey)
|
||||
}
|
||||
}
|
||||
if (javaHomeEnvs.length === 0) {
|
||||
return null
|
||||
}
|
||||
// language=XML
|
||||
let toolchainsXml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<toolchains>
|
||||
<!-- JDK Toolchains installed by default on GitHub-hosted runners -->
|
||||
`
|
||||
for (const javaHomeEnv of javaHomeEnvs) {
|
||||
const version = javaHomeEnv.match(/JAVA_HOME_(\d+)_/)?.[1]!
|
||||
toolchainsXml += ` <toolchain>
|
||||
<type>jdk</type>
|
||||
<provides>
|
||||
<version>${version}</version>
|
||||
</provides>
|
||||
<configuration>
|
||||
<jdkHome>\${env.${javaHomeEnv}}</jdkHome>
|
||||
</configuration>
|
||||
</toolchain>\n`
|
||||
}
|
||||
toolchainsXml += `</toolchains>\n`
|
||||
return toolchainsXml
|
||||
}
|
||||
Reference in New Issue
Block a user