gradle/sources/src/caching/gradle-user-home-utils.ts
daz faf1d7ca7a
Copy the raw JDK path into toolchains.xml
Previously we were relying on Gradle to substitute JDK environment variables
in toolchains.xml. With this change, the actual path to the JDK is encoded instead.
This should avoid issues where Gradle is not able to successfully resolve the
envioronment variable.
2025-01-20 10:20:43 -07:00

53 lines
1.7 KiB
TypeScript

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 {
// Get the version and path for each JAVA_HOME env var
const javaHomeEnvs = Object.entries(process.env)
.filter(([key]) => key.startsWith('JAVA_HOME_') && process.env[key])
.map(([key, value]) => ({
jdkVersion: key.match(/JAVA_HOME_(\d+)_/)?.[1] ?? null,
jdkPath: value as string
}))
.filter(env => env.jdkVersion !== null)
if (javaHomeEnvs.length === 0) {
return null
}
// language=XML
return `<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<!-- JDK Toolchains installed by default on GitHub-hosted runners -->
${javaHomeEnvs
.map(
({jdkVersion, jdkPath}) => ` <toolchain>
<type>jdk</type>
<provides>
<version>${jdkVersion}</version>
</provides>
<configuration>
<jdkHome>${jdkPath}</jdkHome>
</configuration>
</toolchain>`
)
.join('\n')}
</toolchains>\n`
}
export function mergeToolchainContent(existingToolchainContent: string, preInstalledToolchains: string): string {
const appendedContent = preInstalledToolchains.split('<toolchains>').pop()!
return existingToolchainContent.replace('</toolchains>', appendedContent)
}