Skip to content

Commit 7ee5d0e

Browse files
committed
Add inputs 'update-toolchains-only', 'update-env-javahome', 'add-to-env-path'
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}`
1 parent 99b8673 commit 7ee5d0e

File tree

7 files changed

+89
-41
lines changed

7 files changed

+89
-41
lines changed

action.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ inputs:
4343
description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
4444
required: false
4545
overwrite-settings:
46-
description: 'Overwrite the settings.xml file if it exists. Default is "true".'
46+
description: 'Overwrite the settings.xml file if it exists. Default is "!update-toolchains-only". If explcitly set "true", it will update settings.xml regardless of "update-toolchains-only"'
4747
required: false
48-
default: true
48+
# DO NOT set a default here! The default will be propagated from input 'update-toolchains-only'!
49+
update-toolchains-only:
50+
description: 'Update toolchains.xml only. Default is "false". No update of settings.xml, no update of JAVA_HOME, no adding to PATH by default - unless "overwrite-settings", "update-env-javahome" or "add-to-env-path" are not explicitly set "true"'
51+
required: false
52+
default: false
53+
update-env-javahome:
54+
description: 'Update the JAVA_HOME environment variable. Default is "!update-toolchains-only"'
55+
required: false
56+
# DO NOT set a default here! The default will be propagated from input 'update-toolchains-only'!
57+
add-to-env-path:
58+
description: 'Add "<JDK home>/bin" to the PATH environment variable. Default is "!update-toolchains-only"'
59+
required: false
60+
# DO NOT set a default here! The default will be propagated from input 'update-toolchains-only'!
4961
gpg-private-key:
5062
description: 'GPG private key to import. Default is empty string.'
5163
required: false

src/auth.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ import * as constants from './constants';
1010
import * as gpg from './gpg';
1111
import {getBooleanInput} from './util';
1212

13-
export async function configureAuthentication() {
13+
export async function configureAuthentication(
14+
overwriteSettings: boolean
15+
) {
1416
const id = core.getInput(constants.INPUT_SERVER_ID);
1517
const username = core.getInput(constants.INPUT_SERVER_USERNAME);
1618
const password = core.getInput(constants.INPUT_SERVER_PASSWORD);
1719
const settingsDirectory =
1820
core.getInput(constants.INPUT_SETTINGS_PATH) ||
1921
path.join(os.homedir(), constants.M2_DIR);
20-
const overwriteSettings = getBooleanInput(
21-
constants.INPUT_OVERWRITE_SETTINGS,
22-
true
23-
);
2422
const gpgPrivateKey =
2523
core.getInput(constants.INPUT_GPG_PRIVATE_KEY) ||
2624
constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export const INPUT_SERVER_USERNAME = 'server-username';
1111
export const INPUT_SERVER_PASSWORD = 'server-password';
1212
export const INPUT_SETTINGS_PATH = 'settings-path';
1313
export const INPUT_OVERWRITE_SETTINGS = 'overwrite-settings';
14+
export const INPUT_UPDATE_TOOLCHAINS_ONLY = 'update-toolchains-only';
15+
export const INPUT_UPDATE_JAVA_HOME = 'update-env-javahome';
16+
export const INPUT_ADD_TO_PATH = 'add-to-env-path';
1417
export const INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
1518
export const INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
1619

src/distributions/base-installer.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
JavaInstallerOptions,
1111
JavaInstallerResults
1212
} from './base-models';
13-
import {MACOS_JAVA_CONTENT_POSTFIX} from '../constants';
13+
import {
14+
MACOS_JAVA_CONTENT_POSTFIX,
15+
INPUT_UPDATE_JAVA_HOME,
16+
INPUT_ADD_TO_PATH
17+
} from '../constants';
1418
import os from 'os';
1519

1620
export abstract class JavaBase {
@@ -20,6 +24,8 @@ export abstract class JavaBase {
2024
protected packageType: string;
2125
protected stable: boolean;
2226
protected checkLatest: boolean;
27+
protected updateEnvJavaHome: boolean;
28+
protected addToEnvPath: boolean;
2329

2430
constructor(
2531
protected distribution: string,
@@ -36,6 +42,8 @@ export abstract class JavaBase {
3642
this.architecture = installerOptions.architecture || os.arch();
3743
this.packageType = installerOptions.packageType;
3844
this.checkLatest = installerOptions.checkLatest;
45+
this.updateEnvJavaHome = installerOptions.updateEnvJavaHome;
46+
this.addToEnvPath = installerOptions.addToEnvPath;
3947
}
4048

4149
protected abstract downloadTool(
@@ -163,8 +171,16 @@ export abstract class JavaBase {
163171

164172
protected setJavaDefault(version: string, toolPath: string) {
165173
const majorVersion = version.split('.')[0];
166-
core.exportVariable('JAVA_HOME', toolPath);
167-
core.addPath(path.join(toolPath, 'bin'));
174+
if (this.updateEnvJavaHome) {
175+
core.exportVariable('JAVA_HOME', toolPath);
176+
} else {
177+
core.info(`Skip updating env.JAVA_HOME according to ${INPUT_UPDATE_JAVA_HOME}`);
178+
}
179+
if (this.addToEnvPath) {
180+
core.addPath(path.join(toolPath, 'bin'));
181+
} else {
182+
core.info(`Skip adding to env.PATH according to ${INPUT_ADD_TO_PATH}`);
183+
}
168184
core.setOutput('distribution', this.distribution);
169185
core.setOutput('path', toolPath);
170186
core.setOutput('version', version);

src/distributions/base-models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export interface JavaInstallerOptions {
33
architecture: string;
44
packageType: string;
55
checkLatest: boolean;
6+
updateEnvJavaHome: boolean;
7+
addToEnvPath: boolean;
68
}
79

810
export interface JavaInstallerResults {

src/setup-java.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ import * as path from 'path';
1313
import {getJavaDistribution} from './distributions/distribution-factory';
1414
import {JavaInstallerOptions} from './distributions/base-models';
1515

16+
interface IInstallerInputsOptions {
17+
architecture: string;
18+
packageType: string;
19+
checkLatest: boolean;
20+
distributionName: string;
21+
jdkFile: string;
22+
toolchainIds: Array<string>;
23+
updateToolchainsOnly: boolean;
24+
overwriteSettings: boolean;
25+
updateEnvJavaHome: boolean;
26+
addToEnvPath: boolean;
27+
}
28+
1629
async function run() {
1730
try {
1831
const versions = core.getMultilineInput(constants.INPUT_JAVA_VERSION);
@@ -28,6 +41,11 @@ async function run() {
2841
constants.INPUT_CACHE_DEPENDENCY_PATH
2942
);
3043
const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false);
44+
const updateToolchainsOnly = getBooleanInput(constants.INPUT_UPDATE_TOOLCHAINS_ONLY, false);
45+
const overwriteSettings = getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, !updateToolchainsOnly);
46+
const updateEnvJavaHome = getBooleanInput(constants.INPUT_UPDATE_JAVA_HOME, !updateToolchainsOnly);
47+
const addToEnvPath = getBooleanInput(constants.INPUT_ADD_TO_PATH, !updateToolchainsOnly);
48+
3149
let toolchainIds = core.getMultilineInput(constants.INPUT_MVN_TOOLCHAIN_ID);
3250

3351
core.startGroup('Installed distributions');
@@ -40,13 +58,17 @@ async function run() {
4058
throw new Error('java-version or java-version-file input expected');
4159
}
4260

43-
const installerInputsOptions: installerInputsOptions = {
61+
const installerInputsOptions: IInstallerInputsOptions = {
4462
architecture,
4563
packageType,
4664
checkLatest,
4765
distributionName,
4866
jdkFile,
49-
toolchainIds
67+
toolchainIds,
68+
updateToolchainsOnly,
69+
overwriteSettings,
70+
updateEnvJavaHome,
71+
addToEnvPath
5072
};
5173

5274
if (!versions.length) {
@@ -78,7 +100,7 @@ async function run() {
78100
const matchersPath = path.join(__dirname, '..', '..', '.github');
79101
core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
80102

81-
await auth.configureAuthentication();
103+
await auth.configureAuthentication(overwriteSettings);
82104
if (cache && isCacheFeatureAvailable()) {
83105
await restore(cache, cacheDependencyPath);
84106
}
@@ -91,7 +113,7 @@ run();
91113

92114
async function installVersion(
93115
version: string,
94-
options: installerInputsOptions,
116+
options: IInstallerInputsOptions,
95117
toolchainId = 0
96118
) {
97119
const {
@@ -100,14 +122,20 @@ async function installVersion(
100122
architecture,
101123
packageType,
102124
checkLatest,
103-
toolchainIds
125+
toolchainIds,
126+
updateToolchainsOnly,
127+
overwriteSettings,
128+
updateEnvJavaHome,
129+
addToEnvPath
104130
} = options;
105131

106132
const installerOptions: JavaInstallerOptions = {
133+
version,
107134
architecture,
108135
packageType,
109136
checkLatest,
110-
version
137+
updateEnvJavaHome,
138+
addToEnvPath
111139
};
112140

113141
const distribution = getJavaDistribution(
@@ -126,6 +154,7 @@ async function installVersion(
126154
version,
127155
distributionName,
128156
result.path,
157+
overwriteSettings || updateToolchainsOnly,
129158
toolchainIds[toolchainId]
130159
);
131160

@@ -136,12 +165,3 @@ async function installVersion(
136165
core.info(` Path: ${result.path}`);
137166
core.info('');
138167
}
139-
140-
interface installerInputsOptions {
141-
architecture: string;
142-
packageType: string;
143-
checkLatest: boolean;
144-
distributionName: string;
145-
jdkFile: string;
146-
toolchainIds: Array<string>;
147-
}

src/toolchains.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export async function configureToolchains(
1919
version: string,
2020
distributionName: string,
2121
jdkHome: string,
22+
updateToolchains: boolean,
2223
toolchainId?: string
2324
) {
2425
const vendor =
@@ -27,10 +28,6 @@ export async function configureToolchains(
2728
const settingsDirectory =
2829
core.getInput(constants.INPUT_SETTINGS_PATH) ||
2930
path.join(os.homedir(), constants.M2_DIR);
30-
const overwriteSettings = getBooleanInput(
31-
constants.INPUT_OVERWRITE_SETTINGS,
32-
true
33-
);
3431

3532
await createToolchainsSettings({
3633
jdkInfo: {
@@ -40,21 +37,21 @@ export async function configureToolchains(
4037
jdkHome
4138
},
4239
settingsDirectory,
43-
overwriteSettings
40+
updateToolchains
4441
});
4542
}
4643

4744
export async function createToolchainsSettings({
4845
jdkInfo,
4946
settingsDirectory,
50-
overwriteSettings
47+
updateToolchains
5148
}: {
5249
jdkInfo: JdkInfo;
5350
settingsDirectory: string;
54-
overwriteSettings: boolean;
51+
updateToolchains: boolean;
5552
}) {
5653
core.info(
57-
`Creating ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}`
54+
`Adding a toolchain entry in ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}`
5855
);
5956
// when an alternate m2 location is specified use only that location (no .m2 directory)
6057
// otherwise use the home/.m2/ path
@@ -72,7 +69,7 @@ export async function createToolchainsSettings({
7269
await writeToolchainsFileToDisk(
7370
settingsDirectory,
7471
updatedToolchains,
75-
overwriteSettings
72+
updateToolchains
7673
);
7774
}
7875

@@ -147,17 +144,17 @@ async function readExistingToolchainsFile(directory: string) {
147144
async function writeToolchainsFileToDisk(
148145
directory: string,
149146
settings: string,
150-
overwriteSettings: boolean
147+
updateToolchains: boolean
151148
) {
152149
const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE);
153-
const settingsExists = fs.existsSync(location);
154-
if (settingsExists && overwriteSettings) {
155-
core.info(`Overwriting existing file ${location}`);
156-
} else if (!settingsExists) {
157-
core.info(`Writing to ${location}`);
150+
const toolchainsExists = fs.existsSync(location);
151+
if (toolchainsExists && updateToolchains) {
152+
core.info(`Updating existing file ${location}`);
153+
} else if (!toolchainsExists) {
154+
core.info(`Creating file ${location}`);
158155
} else {
159156
core.info(
160-
`Skipping generation of ${location} because file already exists and overwriting is not enabled`
157+
`Skipping update of ${location} since file already exists and updating is not enabled`
161158
);
162159
return;
163160
}

0 commit comments

Comments
 (0)