-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
4 changed files
with
115 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {getPredefinedToolchains} from "../../src/caching/gradle-user-home-utils"; | ||
|
||
describe('predefined-toolchains', () => { | ||
const OLD_ENV = process.env | ||
afterAll(() => { | ||
process.env = OLD_ENV | ||
}); | ||
|
||
describe('returns', () => { | ||
it('null if no JAVA_HOME_ envs are set', async () => { | ||
jest.resetModules() | ||
process.env = { | ||
"JAVA_HOME": "/jdks/foo_8" | ||
} | ||
|
||
const predefinedToolchains = getPredefinedToolchains() | ||
expect(predefinedToolchains).toBe(null) | ||
}) | ||
it('valid toolchains.xml if JAVA_HOME_ envs are set', async () => { | ||
jest.resetModules() | ||
process.env = { | ||
"JAVA_HOME": "/jdks/foo_8", | ||
"JAVA_HOME_8_X64": "/jdks/foo_8", | ||
"JAVA_HOME_11_X64": "/jdks/foo_11", | ||
"JAVA_HOME_21_ARM64": "/jdks/foo_21", | ||
} | ||
|
||
const predefinedToolchains = getPredefinedToolchains() | ||
expect(predefinedToolchains).toBe( | ||
// language=XML | ||
`<?xml version="1.0" encoding="UTF-8"?> | ||
<toolchains> | ||
<!-- JDK Toolchains installed by default on GitHub-hosted runners --> | ||
<toolchain> | ||
<type>jdk</type> | ||
<provides> | ||
<version>8</version> | ||
</provides> | ||
<configuration> | ||
<jdkHome>\${env.JAVA_HOME_8_X64}</jdkHome> | ||
</configuration> | ||
</toolchain> | ||
<toolchain> | ||
<type>jdk</type> | ||
<provides> | ||
<version>11</version> | ||
</provides> | ||
<configuration> | ||
<jdkHome>\${env.JAVA_HOME_11_X64}</jdkHome> | ||
</configuration> | ||
</toolchain> | ||
<toolchain> | ||
<type>jdk</type> | ||
<provides> | ||
<version>21</version> | ||
</provides> | ||
<configuration> | ||
<jdkHome>\${env.JAVA_HOME_21_ARM64}</jdkHome> | ||
</configuration> | ||
</toolchain> | ||
</toolchains> | ||
`) | ||
}) | ||
}) | ||
}) |