2020-12-28 11:02:31 +00:00
|
|
|
import UnityVersionParser from './unity-version-parser';
|
2023-07-03 15:09:20 +00:00
|
|
|
import fs from 'fs';
|
2022-01-11 11:52:29 +00:00
|
|
|
import { getInput } from '@actions/core';
|
2020-01-29 21:22:26 +00:00
|
|
|
|
2022-01-11 11:52:29 +00:00
|
|
|
const Input = {
|
|
|
|
get testModes() {
|
2023-04-21 03:23:15 +00:00
|
|
|
return ['all', 'playmode', 'editmode', 'standalone'];
|
2022-01-11 11:52:29 +00:00
|
|
|
},
|
2020-01-29 21:22:26 +00:00
|
|
|
|
2022-01-11 11:52:29 +00:00
|
|
|
isValidFolderName(folderName) {
|
2021-11-10 12:30:58 +00:00
|
|
|
const validFolderName = new RegExp(/^(\.|\.\/)?(\.?[\w~]+([ _-]?[\w~]+)*\/?)*$/);
|
2020-01-29 22:42:41 +00:00
|
|
|
|
|
|
|
return validFolderName.test(folderName);
|
2022-01-11 11:52:29 +00:00
|
|
|
},
|
2020-01-29 22:42:41 +00:00
|
|
|
|
2023-07-03 15:09:20 +00:00
|
|
|
/**
|
|
|
|
* When in package mode, we need to scrape the package's name from its package.json file
|
|
|
|
*/
|
|
|
|
getPackageNameFromPackageJson(packagePath) {
|
|
|
|
const packageJsonPath = `${packagePath}/package.json`;
|
|
|
|
if (!fs.existsSync(packageJsonPath)) {
|
|
|
|
throw new Error(`Invalid projectPath - Cannot find package.json at ${packageJsonPath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let packageJson;
|
|
|
|
|
|
|
|
try {
|
|
|
|
packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof SyntaxError) {
|
|
|
|
throw new SyntaxError(`Unable to parse package.json contents as JSON - ${error.message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Unable to parse package.json contents as JSON - unknown error ocurred`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rawPackageName = packageJson.name;
|
|
|
|
|
|
|
|
if (typeof rawPackageName !== 'string') {
|
|
|
|
throw new TypeError(
|
|
|
|
`Unable to parse package name from package.json - package name should be string, but was ${typeof rawPackageName}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rawPackageName.length === 0) {
|
|
|
|
throw new Error(`Package name from package.json is a string, but is empty`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawPackageName;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When in package mode, we need to ensure that the Tests folder is present
|
|
|
|
*/
|
|
|
|
verifyTestsFolderIsPresent(packagePath) {
|
|
|
|
if (!fs.existsSync(`${packagePath}/Tests`)) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid projectPath - Cannot find package tests folder at ${packagePath}/Tests`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-11 11:52:29 +00:00
|
|
|
getFromUser() {
|
2020-01-29 21:22:26 +00:00
|
|
|
// Input variables specified in workflow using "with" prop.
|
2022-04-03 15:59:11 +00:00
|
|
|
const unityVersion = getInput('unityVersion') || 'auto';
|
2020-12-17 13:30:51 +00:00
|
|
|
const customImage = getInput('customImage') || '';
|
2020-01-30 07:51:15 +00:00
|
|
|
const rawProjectPath = getInput('projectPath') || '.';
|
2022-11-04 11:35:06 +00:00
|
|
|
const unityLicensingServer = getInput('unityLicensingServer') || '';
|
2022-01-01 20:38:47 +00:00
|
|
|
const customParameters = getInput('customParameters') || '';
|
|
|
|
const testMode = (getInput('testMode') || 'all').toLowerCase();
|
2022-04-21 08:50:37 +00:00
|
|
|
const coverageOptions = getInput('coverageOptions') || '';
|
2020-01-30 07:51:15 +00:00
|
|
|
const rawArtifactsPath = getInput('artifactsPath') || 'artifacts';
|
2020-04-01 20:24:13 +00:00
|
|
|
const rawUseHostNetwork = getInput('useHostNetwork') || 'false';
|
2021-05-28 21:55:58 +00:00
|
|
|
const sshAgent = getInput('sshAgent') || '';
|
2022-01-01 20:38:47 +00:00
|
|
|
const gitPrivateToken = getInput('gitPrivateToken') || '';
|
2021-02-28 06:44:56 +00:00
|
|
|
const githubToken = getInput('githubToken') || '';
|
|
|
|
const checkName = getInput('checkName') || 'Test Results';
|
2023-07-03 15:09:20 +00:00
|
|
|
const rawPackageMode = getInput('packageMode') || 'false';
|
|
|
|
let packageName = '';
|
2022-06-24 10:59:01 +00:00
|
|
|
const chownFilesTo = getInput('chownFilesTo') || '';
|
2020-01-29 21:22:26 +00:00
|
|
|
|
|
|
|
// Validate input
|
2022-01-11 11:52:29 +00:00
|
|
|
if (!this.testModes.includes(testMode)) {
|
2020-01-29 21:22:26 +00:00
|
|
|
throw new Error(`Invalid testMode ${testMode}`);
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:42:41 +00:00
|
|
|
if (!this.isValidFolderName(rawProjectPath)) {
|
|
|
|
throw new Error(`Invalid projectPath "${rawProjectPath}"`);
|
|
|
|
}
|
|
|
|
|
2022-01-01 20:38:47 +00:00
|
|
|
if (!this.isValidFolderName(rawArtifactsPath)) {
|
|
|
|
throw new Error(`Invalid artifactsPath "${rawArtifactsPath}"`);
|
|
|
|
}
|
|
|
|
|
2020-04-01 20:24:13 +00:00
|
|
|
if (rawUseHostNetwork !== 'true' && rawUseHostNetwork !== 'false') {
|
|
|
|
throw new Error(`Invalid useHostNetwork "${rawUseHostNetwork}"`);
|
|
|
|
}
|
|
|
|
|
2023-07-03 15:09:20 +00:00
|
|
|
if (rawPackageMode !== 'true' && rawPackageMode !== 'false') {
|
|
|
|
throw new Error(`Invalid packageMode "${rawPackageMode}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanitize packageMode input and projectPath input since they are needed
|
|
|
|
// for input validation
|
|
|
|
const packageMode = rawPackageMode === 'true';
|
2020-01-29 21:22:26 +00:00
|
|
|
const projectPath = rawProjectPath.replace(/\/$/, '');
|
2023-07-03 15:09:20 +00:00
|
|
|
|
|
|
|
// if in package mode, attempt to get the package's name, and ensure tests are present
|
|
|
|
if (packageMode) {
|
|
|
|
if (unityVersion === 'auto') {
|
|
|
|
throw new Error(
|
|
|
|
'Package Mode is enabled, but unityVersion is set to "auto". unityVersion must manually be set in Package Mode.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
packageName = this.getPackageNameFromPackageJson(projectPath);
|
|
|
|
this.verifyTestsFolderIsPresent(projectPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitise other input
|
2020-01-29 21:22:26 +00:00
|
|
|
const artifactsPath = rawArtifactsPath.replace(/\/$/, '');
|
2020-04-01 20:24:13 +00:00
|
|
|
const useHostNetwork = rawUseHostNetwork === 'true';
|
2022-04-03 15:59:11 +00:00
|
|
|
const editorVersion =
|
|
|
|
unityVersion === 'auto' ? UnityVersionParser.read(projectPath) : unityVersion;
|
2020-01-29 21:22:26 +00:00
|
|
|
|
|
|
|
// Return sanitised input
|
|
|
|
return {
|
2022-04-03 15:59:11 +00:00
|
|
|
editorVersion,
|
2020-12-17 13:30:51 +00:00
|
|
|
customImage,
|
2020-01-29 21:22:26 +00:00
|
|
|
projectPath,
|
2022-01-01 20:38:47 +00:00
|
|
|
customParameters,
|
2020-01-29 21:22:26 +00:00
|
|
|
testMode,
|
2022-04-21 08:50:37 +00:00
|
|
|
coverageOptions,
|
2020-01-29 21:22:26 +00:00
|
|
|
artifactsPath,
|
2020-04-01 20:24:13 +00:00
|
|
|
useHostNetwork,
|
2021-05-28 21:55:58 +00:00
|
|
|
sshAgent,
|
2022-01-01 20:38:47 +00:00
|
|
|
gitPrivateToken,
|
2021-02-28 06:44:56 +00:00
|
|
|
githubToken,
|
|
|
|
checkName,
|
2023-07-03 15:09:20 +00:00
|
|
|
packageMode,
|
|
|
|
packageName,
|
2022-06-24 10:59:01 +00:00
|
|
|
chownFilesTo,
|
2022-11-04 11:35:06 +00:00
|
|
|
unityLicensingServer,
|
2020-01-29 21:22:26 +00:00
|
|
|
};
|
2022-01-11 11:52:29 +00:00
|
|
|
},
|
|
|
|
};
|
2020-01-29 21:22:26 +00:00
|
|
|
|
|
|
|
export default Input;
|