Skip to content

Commit 27823a8

Browse files
committed
Fix up empty values for versionCode
1 parent 7f9e319 commit 27823a8

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { copy } from '@ionic/utils-fs';
2+
import { join } from 'path';
3+
import tempy from 'tempy';
4+
5+
import { Context, loadContext } from '../../src/ctx';
6+
import { Operation } from '../../src/definitions';
7+
import Op from '../../src/operations/android/version';
8+
import { makeOp } from '../utils';
9+
10+
describe('op: android.versionCode', () => {
11+
let dir: string;
12+
let ctx: Context;
13+
14+
beforeEach(async () => {
15+
dir = tempy.directory();
16+
17+
await copy('../common/test/fixtures/ios-and-android', dir);
18+
19+
ctx = await loadContext(dir);
20+
ctx.args.quiet = true;
21+
});
22+
23+
it('shouldn\'t break build when updating empty versionCode', async () => {
24+
const op: Operation = makeOp('android', 'versionCode', '');
25+
await Op(ctx, op as Operation);
26+
expect(await ctx.project.android?.getVersionCode()).toBe(1);
27+
});
28+
29+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { copy, readFile, rm } from '@ionic/utils-fs';
2+
import { join } from 'path';
3+
import tempy from 'tempy';
4+
5+
import { Context, loadContext } from '../../src/ctx';
6+
import { IosCopyOperation, Operation } from '../../src/definitions';
7+
import Op from '../../src/operations/ios/buildVersion';
8+
9+
import { makeOp } from '../utils';
10+
11+
describe('op: ios.buildVersion', () => {
12+
let dir: string;
13+
let ctx: Context;
14+
15+
beforeEach(async () => {
16+
dir = tempy.directory();
17+
18+
await copy('../common/test/fixtures/ios-and-android', dir);
19+
20+
ctx = await loadContext(dir);
21+
ctx.args.quiet = true;
22+
});
23+
24+
afterEach(async () => {
25+
await rm(dir, { force: true, recursive: true });
26+
});
27+
28+
it('shouldn\'t break build when updating empty buildNumber', async () => {
29+
const op: IosCopyOperation = makeOp('ios', 'buildNumber', '');
30+
31+
await Op(ctx, op as Operation);
32+
33+
expect(ctx.project.ios?.getBuildProperty(null, null, 'CURRENT_PROJECT_VERSION')).toBe(1);
34+
});
35+
});

packages/project/src/android/project.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ export class AndroidProject extends PlatformProject {
271271
}
272272

273273
setVersionCode(versionCode: number) {
274-
return this.appBuildGradle?.setVersionCode(versionCode);
274+
if ((versionCode as any) === '') {
275+
versionCode = 1;
276+
}
277+
278+
return this.appBuildGradle?.setVersionCode(typeof versionCode === 'number' ? versionCode : 1);
275279
}
276280

277281
async getVersionCode(): Promise<number | null> {

packages/project/src/ios/project.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ export class IosProject extends PlatformProject {
196196
* If the `targetName` is null the main app target is used. If the `buildName` is null the value is set for both builds (Debug/Release);
197197
*/
198198
async setBuild(targetName: IosTargetName | null, buildName: IosBuildName | null, buildNumber: number | null) {
199+
if ((buildNumber as any) === '') {
200+
// This shouldn't happen but can
201+
buildNumber = 1;
202+
this.pbxProject?.updateBuildProperty('CURRENT_PROJECT_VERSION', 1, buildName, targetName);
203+
}
204+
199205
this.pbxProject?.updateBuildProperty('CURRENT_PROJECT_VERSION', buildNumber ?? 1, buildName, targetName);
200206

201207
this.log(`setBuild`, targetName, buildName, `to ${buildNumber ?? 1}`);

packages/project/src/util/pbx.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@ export function pbxSerializeString(value: string) {
2525

2626
// Remove any quotes at the beginning and end of the string value
2727
export function pbxReadString(value: string) {
28-
return value?.replace(/(^")+|("$)+/g, '');
28+
if (typeof value === 'string') {
29+
return value?.replace(/(^")+|("$)+/g, '');
30+
} else {
31+
return value;
32+
}
2933
}

0 commit comments

Comments
 (0)