Skip to content

Commit

Permalink
feat: added env strategy (#14)
Browse files Browse the repository at this point in the history
* feat: add env versioning strategy

* fix: check for non existing env variable

* test: fix lint error
  • Loading branch information
murat-mehmet authored Apr 1, 2024
1 parent 9e81ad3 commit c934705
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ This is similar to the `semantic` strategy described above, yet with the additio
of the minumum API level as the first two digits, followed by a zero. It also
faces the same downsides as the `semantic` strategy.

#### `env`

Read `versionCode` from environment variable `ANDROID_BUILD_NUMBER`.

#### `none`

Disable updates of the `versionCode`.
Expand Down Expand Up @@ -181,6 +185,10 @@ This strategy behaves the same as the `relative` strategy for Android.

Use the semantic version number directly.

#### `env`

Use the version from environment variable `IOS_BUILD_NUMBER`.

#### `none`

Disable updates of the `CFBundleVersion`.
Expand Down
2 changes: 2 additions & 0 deletions src/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const androidVesionStrategies = [
'increment',
'relative',
'relative-extended',
'env',
'none',
];

Expand All @@ -10,5 +11,6 @@ export const iosVesionStrategies = [
'increment',
'relative',
'semantic',
'env',
'none',
];
12 changes: 12 additions & 0 deletions src/version/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ const getNextAndroidVersionCode = (
return currentVersionCode;
}

if (strategy?.buildNumber === 'env') {
const envVersionCode = process.env.ANDROID_BUILD_NUMBER;
if (!envVersionCode) {
logger.warn(
'Could not update Android versionCode using the env strategy '
+ 'as the ANDROID_BUILD_NUMBER could not be determined.',
);
return currentVersionCode;
}
return envVersionCode;
}

if (strategy?.buildNumber === 'relative') {
const semanticBuildNumber = getSemanticBuildNumber(version, logger, 'Android');

Expand Down
13 changes: 13 additions & 0 deletions src/version/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ const getIosBundleVersion = (
return currentBundleVersion;
}

if (strategy?.buildNumber === 'env') {
const envBundleVersion = process.env.IOS_BUILD_NUMBER;
if (!envBundleVersion) {
logger.warn(
'Could not update iOS bundle version using the env strategy '
+ 'as the IOS_BUILD_NUMBER could not be determined.',
);
return currentBundleVersion;
}
return envBundleVersion;
}

if (strategy?.buildNumber === 'semantic') {
return stripPrereleaseVersion(version);
}
Expand Down Expand Up @@ -386,6 +398,7 @@ export const versionIos = (
|| (
buildNumber
&& buildNumber !== 'strict'
&& buildNumber !== 'env'
)
)
) {
Expand Down
121 changes: 121 additions & 0 deletions tests/prepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,56 @@ describe('prepare', () => {
});
});

it('update versionCode using the env strategy', async () => {
const context = createContext();
process.env.ANDROID_BUILD_NUMBER = '123';

await prepare({
skipIos: true,
versionStrategy: {
android: {
buildNumber: 'env',
},
},
}, context);

expect(fs.writeFileSync).toHaveBeenCalledWith(defaultAndroidPath, [
'versionName "1.2.3"',
'versionCode 123',
].join('\n'));

delete process.env.ANDROID_BUILD_NUMBER;
});

it('do not update versionCode using the env strategy when variable not found', async () => {
const context = createContext();

(fs.readFileSync as jest.Mock).mockImplementation((filePath) => {
if (filePath.endsWith('build.gradle')) {
return [
'versionName "1.0.0"',
'versionCode 100',
].join('\n');
}

return null;
});

await prepare({
skipIos: true,
versionStrategy: {
android: {
buildNumber: 'env',
},
},
}, context);

expect(fs.writeFileSync).toHaveBeenCalledWith(defaultAndroidPath, [
'versionName "1.2.3"',
'versionCode 100',
].join('\n'));
});

describe('iOS', () => {
beforeEach(() => {
(fs.readdirSync as jest.Mock).mockImplementation((filePath) => {
Expand Down Expand Up @@ -1008,6 +1058,77 @@ describe('prepare', () => {
});
});

it('updates using the env versioning strategy', async () => {
const context = createContext({ version: '1.2.3' });

(plist.parse as jest.Mock).mockReturnValue({
CFBundleVersion: '1.1.1',
});
process.env.IOS_BUILD_NUMBER = '2.3.4';

getBuildSetting.mockImplementation((value: string) => ({
INFOPLIST_FILE: { text: 'Test/Info.plist' },
CURRENT_PROJECT_VERSION: { text: '1.1.1' },
}[value]));

await prepare({
skipAndroid: true,
versionStrategy: {
ios: {
buildNumber: 'env',
},
},
}, context);

expect(plist.build).toHaveBeenCalledTimes(1);
expect((plist.build as jest.Mock).mock.calls[0][0].CFBundleVersion).toBe(
'2.3.4',
);

expect(buildConfig.patch).toHaveBeenCalledTimes(1);
expect(buildConfig.patch).toHaveBeenCalledWith({
buildSettings: {
CURRENT_PROJECT_VERSION: '2.3.4',
},
});

delete process.env.IOS_BUILD_NUMBER;
});

it('does not update using the env versioning strategy when variable not found', async () => {
const context = createContext({ version: '1.2.3' });

(plist.parse as jest.Mock).mockReturnValue({
CFBundleVersion: '1.1.1',
});

getBuildSetting.mockImplementation((value: string) => ({
INFOPLIST_FILE: { text: 'Test/Info.plist' },
CURRENT_PROJECT_VERSION: { text: '1.1.1' },
}[value]));

await prepare({
skipAndroid: true,
versionStrategy: {
ios: {
buildNumber: 'env',
},
},
}, context);

expect(plist.build).toHaveBeenCalledTimes(1);
expect((plist.build as jest.Mock).mock.calls[0][0].CFBundleVersion).toBe(
'1.1.1',
);

expect(buildConfig.patch).toHaveBeenCalledTimes(1);
expect(buildConfig.patch).toHaveBeenCalledWith({
buildSettings: {
CURRENT_PROJECT_VERSION: '1.1.1',
},
});
});

it.each`
previousBundleVersion | expectedBundleVersion
${'1'} | ${'2'}
Expand Down

0 comments on commit c934705

Please sign in to comment.