Default unityVersion to auto to use ProjectSettings/ProjectVersion.txt (#162) (#188)

This commit is contained in:
David Finol 2020-12-28 23:36:31 -06:00 committed by GitHub
parent 602b0b45d7
commit 32acb22fec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 90 additions and 13 deletions

View File

@ -4,8 +4,8 @@ description: 'Build Unity projects for different platforms.'
inputs:
unityVersion:
required: false
default: ''
description: 'Version of unity to use for building the project.'
default: 'auto'
description: 'Version of unity to use for building the project. Use "auto" to get from your ProjectSettings/ProjectVersion.txt'
customImage:
required: false
default: ''

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
import AndroidVersioning from './android-versioning';
import Input from './input';
import Platform from './platform';
import UnityVersioning from './unity-versioning';
import Versioning from './versioning';
class BuildParameters {
@ -10,6 +11,12 @@ class BuildParameters {
Input.targetPlatform,
Input.androidAppBundle,
);
const unityVersion = UnityVersioning.determineUnityVersion(
Input.projectPath,
Input.unityVersion,
);
const buildVersion = await Versioning.determineVersion(
Input.versioningStrategy,
Input.specifiedVersion,
@ -21,7 +28,7 @@ class BuildParameters {
);
return {
version: Input.unityVersion,
version: unityVersion,
customImage: Input.customImage,
runnerTempPath: process.env.RUNNER_TEMP,

View File

@ -1,4 +1,5 @@
import Versioning from './versioning';
import UnityVersioning from './unity-versioning';
import BuildParameters from './build-parameters';
import Input from './input';
import Platform from './platform';
@ -7,6 +8,10 @@ const determineVersion = jest
.spyOn(Versioning, 'determineVersion')
.mockImplementation(() => '1.3.37');
const determineUnityVersion = jest
.spyOn(UnityVersioning, 'determineUnityVersion')
.mockImplementation(() => '2019.2.11f1');
afterEach(() => {
jest.clearAllMocks();
});
@ -22,12 +27,9 @@ describe('BuildParameters', () => {
expect(determineVersion).toHaveBeenCalledTimes(1);
});
it('returns the version', async () => {
const mockValue = 'someVersion';
jest.spyOn(Input, 'unityVersion', 'get').mockReturnValue(mockValue);
await expect(BuildParameters.create()).resolves.toEqual(
expect.objectContaining({ version: mockValue }),
);
it('determines the unity version only once', async () => {
await BuildParameters.create();
expect(determineUnityVersion).toHaveBeenCalledTimes(1);
});
it('returns the android version code with provided input', async () => {

View File

@ -1,6 +1,6 @@
import ImageTag from './image-tag';
describe('UnityImageVersion', () => {
describe('ImageTag', () => {
const some = {
repository: 'test1',
name: 'test2',

View File

@ -9,7 +9,7 @@ const core = require('@actions/core');
*/
class Input {
static get unityVersion() {
return core.getInput('unityVersion');
return core.getInput('unityVersion') || 'auto';
}
static get customImage() {

View File

@ -10,7 +10,7 @@ afterEach(() => {
describe('Input', () => {
describe('unityVersion', () => {
it('returns the default value', () => {
expect(Input.unityVersion).toStrictEqual('');
expect(Input.unityVersion).toStrictEqual('auto');
});
it('takes input from the users workflow', () => {

View File

@ -0,0 +1,33 @@
import * as core from '@actions/core';
import * as fs from 'fs';
import * as path from 'path';
export default class UnityVersioning {
static get versionPattern() {
return /20\d{2}\.\d\.\w{3,4}|3/;
}
static determineUnityVersion(projectPath, unityVersion) {
if (unityVersion === 'auto') {
return UnityVersioning.read(projectPath);
}
return unityVersion;
}
static read(projectPath) {
const filePath = path.join(projectPath, 'ProjectSettings', 'ProjectVersion.txt');
if (!fs.existsSync(filePath)) {
core.warning(`Could not find "${filePath}", keeping unityVersion as "auto"`);
return 'auto';
}
return UnityVersioning.parse(fs.readFileSync(filePath, 'utf8'));
}
static parse(projectVersionTxt) {
const matches = projectVersionTxt.match(UnityVersioning.versionPattern);
if (!matches || matches.length === 0) {
throw new Error(`Failed to parse version from "${projectVersionTxt}".`);
}
return matches[0];
}
}

View File

@ -0,0 +1,35 @@
import UnityVersioning from './unity-versioning';
describe('Unity Versioning', () => {
describe('parse', () => {
it('throws for empty string', () => {
expect(() => UnityVersioning.parse('')).toThrow(Error);
});
it('parses from ProjectVersion.txt', () => {
const projectVersionContents = `m_EditorVersion: 2019.2.11f1
m_EditorVersionWithRevision: 2019.2.11f1 (5f859a4cfee5)`;
expect(UnityVersioning.parse(projectVersionContents)).toBe('2019.2.11f1');
});
});
describe('read', () => {
it('does not throw', () => {
expect(() => UnityVersioning.read('')).not.toThrow();
});
it('reads from test-project', () => {
expect(UnityVersioning.read('./test-project')).toBe('2019.2.11f1');
});
});
describe('determineUnityVersion', () => {
it('defaults to parsed version', () => {
expect(UnityVersioning.determineUnityVersion('./test-project', 'auto')).toBe('2019.2.11f1');
});
it('use specified unityVersion', () => {
expect(UnityVersioning.determineUnityVersion('./test-project', '1.2.3')).toBe('1.2.3');
});
});
});