mirror of
https://github.com/actions/setup-java.git
synced 2025-06-28 12:04:14 +00:00
Changes in detail: ------------------ - action.yml: - add inputs: - update-toolchains-only - update-env-javahome - add-to-env-path - update description for input "overwrite-settings" - remove default value of input "overwrite-settings", since the default is now propagated from input 'update-toolchains-only' - base-models.ts: - extend interface JavaInstallerOptions: - add fields: - updateEnvJavaHome: boolean; - addToEnvPath: boolean; - constants.ts: - add constant INPUT_UPDATE_TOOLCHAINS_ONLY = 'update-toolchains-only' - auth.ts: - function configureAuthentication(): - add parameter: - overwriteSettings: boolean - remove the now obsolete const overwriteSettings - toolchains.ts: - function configureToolchains(...): - add parameter updateToolchains: boolean - remove the now obsolete const overwriteSettings - improve variable naming: - rename any occurrence of 'overwriteSettings' by 'updateToolchains' - add field updateToolchains: boolean to the parameter object - function writeToolchainsFileToDisk(...): - improve variable naming: - rename variable 'settingsExists' by 'toolchainsExists' - update wording of info logs to be more applicable - setup-java.ts: - interface installerInputsOptions: - rename to IInstallerInputsOption to meet common coding convention - add fields: - updateToolchainsOnly: boolean; - overwriteSettings: boolean; - updateEnvJavaHome: boolean; - addToEnvPath: boolean; - function run(): - add const: - const updateToolchainsOnly: - get as boolean from input 'update-toolchains-only', default: false - const overwriteSettings: - get as boolean from input 'overwrite-settings', default: !updateToolchainsOnly - const updateEnvJavaHome: - get as boolean input 'update-env-javahome', default: !updateToolchainsOnly - const addToEnvPath: - get as boolean input 'add-to-env-path', default: !updateToolchainsOnly - extend const installerInputsOptions to match with IInstallerInputsOption: - add field updateToolchainsOnly - add field overwriteSettings - add field updateEnvJavaHome - add field addToEnvPath - update call of auth.configureAuthentication() to auth.configureAuthentication(overwriteSettings) - function installVersion(...): - add const and init from parameter options: - updateToolchainsOnly, overwriteSettings, updateEnvJavaHome, addToEnvPath - init the additional fields of installerInputsOptions accordingly - call toolchains.configureToolchains(...): - with parameter updateToolchains= overwriteSettings || updateToolchainsOnly - base-installer.ts: - add constants to import from constants: - INPUT_UPDATE_JAVA_HOME - INPUT_ADD_TO_PATH - add fields: - protected updateEnvJavaHome: boolean; - protected addToEnvPath: boolean; - ctor: - init these fields from JavaInstallerOptions accoprdingly - function setJavaDefault(...): - if updateEnvJavaHome is false: - SKIP updating env.JAVA_HOME - log info: `Skip updating env.JAVA_HOME according to ${INPUT_UPDATE_JAVA_HOME}` - if addToEnvPath is false: - SKIP adding toolchain path to env.PATH - log info: `Skip adding to env.PATH according to ${INPUT_ADD_TO_PATH}`
166 lines
4 KiB
TypeScript
166 lines
4 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as os from 'os';
|
|
import * as path from 'path';
|
|
import * as core from '@actions/core';
|
|
import * as io from '@actions/io';
|
|
import * as constants from './constants';
|
|
|
|
import {getBooleanInput} from './util';
|
|
import {create as xmlCreate} from 'xmlbuilder2';
|
|
|
|
interface JdkInfo {
|
|
version: string;
|
|
vendor: string;
|
|
id: string;
|
|
jdkHome: string;
|
|
}
|
|
|
|
export async function configureToolchains(
|
|
version: string,
|
|
distributionName: string,
|
|
jdkHome: string,
|
|
updateToolchains: boolean,
|
|
toolchainId?: string
|
|
) {
|
|
const vendor =
|
|
core.getInput(constants.INPUT_MVN_TOOLCHAIN_VENDOR) || distributionName;
|
|
const id = toolchainId || `${vendor}_${version}`;
|
|
const settingsDirectory =
|
|
core.getInput(constants.INPUT_SETTINGS_PATH) ||
|
|
path.join(os.homedir(), constants.M2_DIR);
|
|
|
|
await createToolchainsSettings({
|
|
jdkInfo: {
|
|
version,
|
|
vendor,
|
|
id,
|
|
jdkHome
|
|
},
|
|
settingsDirectory,
|
|
updateToolchains
|
|
});
|
|
}
|
|
|
|
export async function createToolchainsSettings({
|
|
jdkInfo,
|
|
settingsDirectory,
|
|
updateToolchains
|
|
}: {
|
|
jdkInfo: JdkInfo;
|
|
settingsDirectory: string;
|
|
updateToolchains: boolean;
|
|
}) {
|
|
core.info(
|
|
`Adding a toolchain entry in ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}`
|
|
);
|
|
// when an alternate m2 location is specified use only that location (no .m2 directory)
|
|
// otherwise use the home/.m2/ path
|
|
await io.mkdirP(settingsDirectory);
|
|
const originalToolchains = await readExistingToolchainsFile(
|
|
settingsDirectory
|
|
);
|
|
const updatedToolchains = generateToolchainDefinition(
|
|
originalToolchains,
|
|
jdkInfo.version,
|
|
jdkInfo.vendor,
|
|
jdkInfo.id,
|
|
jdkInfo.jdkHome
|
|
);
|
|
await writeToolchainsFileToDisk(
|
|
settingsDirectory,
|
|
updatedToolchains,
|
|
updateToolchains
|
|
);
|
|
}
|
|
|
|
// only exported for testing purposes
|
|
export function generateToolchainDefinition(
|
|
original: string,
|
|
version: string,
|
|
vendor: string,
|
|
id: string,
|
|
jdkHome: string
|
|
) {
|
|
let xmlObj;
|
|
if (original?.length) {
|
|
xmlObj = xmlCreate(original)
|
|
.root()
|
|
.ele({
|
|
toolchain: {
|
|
type: 'jdk',
|
|
provides: {
|
|
version: `${version}`,
|
|
vendor: `${vendor}`,
|
|
id: `${id}`
|
|
},
|
|
configuration: {
|
|
jdkHome: `${jdkHome}`
|
|
}
|
|
}
|
|
});
|
|
} else
|
|
xmlObj = xmlCreate({
|
|
toolchains: {
|
|
'@xmlns': 'http://maven.apache.org/TOOLCHAINS/1.1.0',
|
|
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
|
|
'@xsi:schemaLocation':
|
|
'http://maven.apache.org/TOOLCHAINS/1.1.0 https://maven.apache.org/xsd/toolchains-1.1.0.xsd',
|
|
toolchain: [
|
|
{
|
|
type: 'jdk',
|
|
provides: {
|
|
version: `${version}`,
|
|
vendor: `${vendor}`,
|
|
id: `${id}`
|
|
},
|
|
configuration: {
|
|
jdkHome: `${jdkHome}`
|
|
}
|
|
}
|
|
]
|
|
}
|
|
});
|
|
|
|
return xmlObj.end({
|
|
format: 'xml',
|
|
wellFormed: false,
|
|
headless: false,
|
|
prettyPrint: true,
|
|
width: 80
|
|
});
|
|
}
|
|
|
|
async function readExistingToolchainsFile(directory: string) {
|
|
const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE);
|
|
if (fs.existsSync(location)) {
|
|
return fs.readFileSync(location, {
|
|
encoding: 'utf-8',
|
|
flag: 'r'
|
|
});
|
|
}
|
|
return '';
|
|
}
|
|
|
|
async function writeToolchainsFileToDisk(
|
|
directory: string,
|
|
settings: string,
|
|
updateToolchains: boolean
|
|
) {
|
|
const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE);
|
|
const toolchainsExists = fs.existsSync(location);
|
|
if (toolchainsExists && updateToolchains) {
|
|
core.info(`Updating existing file ${location}`);
|
|
} else if (!toolchainsExists) {
|
|
core.info(`Creating file ${location}`);
|
|
} else {
|
|
core.info(
|
|
`Skipping update of ${location} since file already exists and updating is not enabled`
|
|
);
|
|
return;
|
|
}
|
|
|
|
return fs.writeFileSync(location, settings, {
|
|
encoding: 'utf-8',
|
|
flag: 'w'
|
|
});
|
|
}
|