Add input to set version code
Use action input `androidVersionCode` when provided. Generate the androidVersionCode from the version otherwise.
This commit is contained in:
committed by
Webber Takken
parent
401ddcaae0
commit
bdc3a88d22
33
src/model/android-versioning.js
Normal file
33
src/model/android-versioning.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as semver from 'semver';
|
||||
|
||||
export default class AndroidVersioning {
|
||||
static determineVersionCode(version, inputVersionCode) {
|
||||
if (!inputVersionCode) {
|
||||
return AndroidVersioning.versionToVersionCode(version);
|
||||
}
|
||||
return inputVersionCode;
|
||||
}
|
||||
|
||||
static versionToVersionCode(version) {
|
||||
const parsedVersion = semver.parse(version);
|
||||
|
||||
if (!parsedVersion) {
|
||||
core.warning(`Could not parse "${version}" to semver, defaulting android version code to 1`);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The greatest value Google Plays allows is 2100000000.
|
||||
// Allow for 3 patch digits, 3 minor digits and 3 major digits.
|
||||
const versionCode =
|
||||
parsedVersion.major * 1000000 + parsedVersion.minor * 1000 + parsedVersion.patch;
|
||||
|
||||
if (versionCode >= 1000000000) {
|
||||
throw new Error(
|
||||
`Generated versionCode ${versionCode} is dangerously close to the maximum allowed number 2100000000. Consider a different versioning scheme to be able to continue updating your application.`,
|
||||
);
|
||||
}
|
||||
core.info(`Using android versionCode ${versionCode}`);
|
||||
return versionCode;
|
||||
}
|
||||
}
|
||||
27
src/model/android-versioning.test.js
Normal file
27
src/model/android-versioning.test.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import AndroidVersioning from './android-versioning';
|
||||
|
||||
describe('Android Versioning', () => {
|
||||
describe('versionToVersionCode', () => {
|
||||
it('defaults to 1 when version is not a valid semver', () => {
|
||||
expect(AndroidVersioning.versionToVersionCode('abcd')).toBe(1);
|
||||
});
|
||||
|
||||
it('returns a number', () => {
|
||||
expect(AndroidVersioning.versionToVersionCode('123.456.789')).toBe(123456789);
|
||||
});
|
||||
|
||||
it('throw when generated version code is too large', () => {
|
||||
expect(() => AndroidVersioning.versionToVersionCode('1234.0.0')).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('determineVersionCode', () => {
|
||||
it('defaults to parsed version', () => {
|
||||
expect(AndroidVersioning.determineVersionCode('1.2.3', '')).toBe(1002003);
|
||||
});
|
||||
|
||||
it('use specified code', () => {
|
||||
expect(AndroidVersioning.determineVersionCode('1.2.3', 2)).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import AndroidVersioning from './android-versioning';
|
||||
import Input from './input';
|
||||
import Platform from './platform';
|
||||
import Versioning from './versioning';
|
||||
@@ -10,6 +11,11 @@ class BuildParameters {
|
||||
Input.specifiedVersion,
|
||||
);
|
||||
|
||||
const androidVersionCode = AndroidVersioning.determineVersionCode(
|
||||
buildVersion,
|
||||
Input.androidVersionCode,
|
||||
);
|
||||
|
||||
return {
|
||||
version: Input.unityVersion,
|
||||
platform: Input.targetPlatform,
|
||||
@@ -19,6 +25,7 @@ class BuildParameters {
|
||||
buildFile,
|
||||
buildMethod: Input.buildMethod,
|
||||
buildVersion,
|
||||
androidVersionCode,
|
||||
customParameters: Input.customParameters,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -30,6 +30,22 @@ describe('BuildParameters', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('returns the android version code with provided input', async () => {
|
||||
const mockValue = '42';
|
||||
jest.spyOn(Input, 'androidVersionCode', 'get').mockReturnValue(mockValue);
|
||||
await expect(BuildParameters.create()).resolves.toEqual(
|
||||
expect.objectContaining({ androidVersionCode: mockValue }),
|
||||
);
|
||||
});
|
||||
|
||||
it('returns the android version code from version by default', async () => {
|
||||
const mockValue = '';
|
||||
jest.spyOn(Input, 'androidVersionCode', 'get').mockReturnValue(mockValue);
|
||||
await expect(BuildParameters.create()).resolves.toEqual(
|
||||
expect.objectContaining({ androidVersionCode: 1003037 }),
|
||||
);
|
||||
});
|
||||
|
||||
it('returns the platform', async () => {
|
||||
const mockValue = 'somePlatform';
|
||||
jest.spyOn(Input, 'targetPlatform', 'get').mockReturnValue(mockValue);
|
||||
|
||||
@@ -29,6 +29,7 @@ class Docker {
|
||||
buildMethod,
|
||||
buildVersion,
|
||||
customParameters,
|
||||
androidVersionCode,
|
||||
} = parameters;
|
||||
|
||||
const command = `docker run \
|
||||
@@ -47,6 +48,7 @@ class Docker {
|
||||
--env BUILD_FILE="${buildFile}" \
|
||||
--env BUILD_METHOD="${buildMethod}" \
|
||||
--env VERSION="${buildVersion}" \
|
||||
--env ANDROID_VERSION_CODE="${androidVersionCode}" \
|
||||
--env CUSTOM_PARAMETERS="${customParameters}" \
|
||||
--env HOME=/github/home \
|
||||
--env GITHUB_REF \
|
||||
|
||||
@@ -25,7 +25,7 @@ describe('Docker', () => {
|
||||
const parameters = {
|
||||
workspace: Action.rootFolder,
|
||||
projectPath: `${Action.rootFolder}/test-project`,
|
||||
buildName: 'someBulidName',
|
||||
buildName: 'someBuildName',
|
||||
buildsPath: 'build',
|
||||
method: '',
|
||||
};
|
||||
|
||||
@@ -41,6 +41,10 @@ class Input {
|
||||
return core.getInput('version') || '';
|
||||
}
|
||||
|
||||
static get androidVersionCode() {
|
||||
return core.getInput('androidVersionCode');
|
||||
}
|
||||
|
||||
static get allowDirtyBuild() {
|
||||
const input = core.getInput('allowDirtyBuild') || 'false';
|
||||
|
||||
|
||||
@@ -118,6 +118,19 @@ describe('Input', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('androidVersionCode', () => {
|
||||
it('defaults to null', () => {
|
||||
expect(Input.androidVersionCode).toBeFalsy();
|
||||
});
|
||||
|
||||
it('takes input from the users workflow', () => {
|
||||
const mockValue = '42';
|
||||
const spy = jest.spyOn(core, 'getInput').mockReturnValue(mockValue);
|
||||
expect(Input.androidVersionCode).toStrictEqual(mockValue);
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('allowDirtyBuild', () => {
|
||||
it('returns the default value', () => {
|
||||
expect(Input.allowDirtyBuild).toStrictEqual('false');
|
||||
|
||||
Reference in New Issue
Block a user