Skip to content

Commit ae041ca

Browse files
committed
feat: validation for runtime version for --aab, linting, tests fix
1 parent f310f1a commit ae041ca

9 files changed

+59
-14
lines changed

lib/commands/build.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,25 @@ export class BuildAndroidCommand extends BuildCommandBase implements ICommand {
119119
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
120120
$platformService: IPlatformService,
121121
$bundleValidatorHelper: IBundleValidatorHelper,
122+
protected $androidBundleValidatorHelper: IAndroidBundleValidatorHelper,
122123
protected $logger: ILogger) {
123124
super($options, $errors, $projectData, $platformsData, $devicePlatformsConstants, $platformService, $bundleValidatorHelper);
124125
}
125126

126127
public async execute(args: string[]): Promise<void> {
127128
const buildResult = await this.executeCore([this.$platformsData.availablePlatforms.Android]);
128129

129-
if(this.$options.aab) {
130+
if (this.$options.aab) {
130131
this.$logger.info("Link to documentation article");
131132
}
132133

133-
return buildResult
134+
return buildResult;
134135
}
135136

136137
public async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> {
137138
const platform = this.$devicePlatformsConstants.Android;
138139
super.validatePlatform(platform);
139-
140+
this.$androidBundleValidatorHelper.validateRuntimeVersion(this.$projectData);
140141
let result = await super.canExecuteCommandBase(platform, { notConfiguredEnvOptions: { hideSyncToPreviewAppOption: true }});
141142
if (result.canExecute) {
142143
if (this.$options.release && (!this.$options.keyStorePath || !this.$options.keyStorePassword || !this.$options.keyStoreAlias || !this.$options.keyStoreAliasPassword)) {

lib/constants.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const CONFIG_NS_APP_RESOURCES_ENTRY = "appResourcesPath";
4040
export const CONFIG_NS_APP_ENTRY = "appPath";
4141
export const DEPENDENCIES_JSON_NAME = "dependencies.json";
4242
export const APK_EXTENSION_NAME = ".apk";
43-
export const AAB_EXTENSION_NAME = ".aab"
43+
export const AAB_EXTENSION_NAME = ".aab";
4444
export const HASHES_FILE_NAME = ".nshashes";
4545

4646
export class PackageVersion {
@@ -244,4 +244,7 @@ export class BundleValidatorMessages {
244244
public static NotSupportedVersion = `The NativeScript CLI requires nativescript-dev-webpack %s or later to work properly. After updating nativescript-dev-webpack you need to ensure "webpack.config.js" file is up to date with the one in the new version of nativescript-dev-webpack. You can automatically update it using "./node_modules/.bin/update-ns-webpack --configs" command.`;
245245
}
246246

247-
export const AAB_NOT_SUPPORTED_BY_COMMNAND_MESSAGE = "This command does not support --aab (Android Application Bundle) parameter.";
247+
export class AndroidBundleValidatorMessages {
248+
public static AAB_NOT_SUPPORTED_BY_COMMNAND_MESSAGE = "This command does not support --aab (Android Application Bundle) parameter.";
249+
public static RUNTIME_VERSION_TOO_LOW = "Android Application Bundle (--aab) option requires NativeScript Android Runtime (tns-android) version %s and above.";
250+
}

lib/declarations.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,13 @@ interface IAndroidBundleValidatorHelper {
871871
* @return {void}
872872
*/
873873
validateNoAab(): void;
874+
875+
/**
876+
* Validates android runtime version is sufficient to support bundling option --aab.
877+
* @param {IProjectData} projectData DTO with information about the project.
878+
* @return {void}
879+
*/
880+
validateRuntimeVersion(projectData: IProjectData): void
874881
}
875882

876883
interface INativeScriptCloudExtensionService {

lib/helpers/android-bundle-validator-helper.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
import { AAB_NOT_SUPPORTED_BY_COMMNAND_MESSAGE } from "../constants";
1+
import * as semver from "semver";
2+
import * as util from "util";
3+
import { AndroidBundleValidatorMessages, TNS_ANDROID_RUNTIME_NAME } from "../constants";
24

35
export class AndroidBundleValidatorHelper implements IAndroidBundleValidatorHelper {
6+
public static MIN_RUNTIME_VERSION = "5.0.0";
7+
48
constructor(protected $projectData: IProjectData,
59
protected $errors: IErrors,
6-
protected $options: IOptions) {
10+
protected $options: IOptions,
11+
protected $projectDataService: IProjectDataService) {
712
}
813

9-
public validateNoAab(minSupportedVersion?: string): void {
10-
if(this.$options.aab) {
11-
this.$errors.fail(AAB_NOT_SUPPORTED_BY_COMMNAND_MESSAGE);
14+
public validateNoAab(): void {
15+
if (this.$options.aab) {
16+
this.$errors.fail(AndroidBundleValidatorMessages.AAB_NOT_SUPPORTED_BY_COMMNAND_MESSAGE);
17+
}
18+
}
19+
20+
public validateRuntimeVersion(projectData: IProjectData): void {
21+
if (this.$options.aab) {
22+
const androidRuntimeInfo = this.$projectDataService.getNSValue(projectData.projectDir, TNS_ANDROID_RUNTIME_NAME);
23+
const androidRuntimeVersion = androidRuntimeInfo ? androidRuntimeInfo.version : "";
24+
25+
if (androidRuntimeVersion) {
26+
const shouldSkipCheck = !semver.valid(androidRuntimeVersion) && !semver.validRange(androidRuntimeVersion);
27+
if (!shouldSkipCheck) {
28+
const isAndroidBundleSupported = semver.gte(semver.coerce(androidRuntimeVersion), semver.coerce(AndroidBundleValidatorHelper.MIN_RUNTIME_VERSION));
29+
if (!isAndroidBundleSupported) {
30+
this.$errors.failWithoutHelp(util.format(AndroidBundleValidatorMessages.RUNTIME_VERSION_TOO_LOW, AndroidBundleValidatorHelper.MIN_RUNTIME_VERSION));
31+
}
32+
}
33+
}
1234
}
1335
}
1436
}

lib/services/android-project-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
7878
getValidBuildOutputData: (buildOptions: IBuildOutputOptions): IValidBuildOutputData => {
7979
const buildMode = buildOptions.release ? Configurations.Release.toLowerCase() : Configurations.Debug.toLowerCase();
8080

81-
if(buildOptions.androidBundle) {
81+
if (buildOptions.androidBundle) {
8282
return {
8383
packageNames: [
8484
`${constants.APP_FOLDER_NAME}${constants.AAB_EXTENSION_NAME}`
8585
]
86-
}
86+
};
8787
}
8888

8989
return {

lib/services/platform-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,10 @@ export class PlatformService extends EventEmitter implements IPlatformService {
616616
}
617617

618618
private getBuildOutputPath(platform: string, platformData: IPlatformData, options: IBuildOutputOptions): string {
619-
if(options.androidBundle) {
619+
if (options.androidBundle) {
620620
return platformData.bundleBuildOutputPath;
621621
}
622-
622+
623623
if (platform.toLowerCase() === this.$devicePlatformsConstants.iOS.toLowerCase()) {
624624
return options.buildForDevice ? platformData.deviceBuildOutputPath : platformData.emulatorBuildOutputPath;
625625
}

test/debug.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function createTestInjector(): IInjector {
2929
testInjector.register('fs', FileSystem);
3030
testInjector.register('errors', stubs.ErrorsStub);
3131
testInjector.register('hostInfo', {});
32+
testInjector.register("androidBundleValidatorHelper", stubs.AndroidBundleValidatorHelper);
3233
testInjector.register("analyticsService", {
3334
trackException: async (): Promise<void> => undefined,
3435
checkConsent: async (): Promise<void> => undefined,

test/platform-service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ describe('Platform Service Tests', () => {
937937
platformsData.getPlatformData = (platform: string) => {
938938
return {
939939
deviceBuildOutputPath: "",
940+
normalizedPlatformName: "",
940941
platformProjectService: {
941942
buildProject: () => Promise.resolve(),
942943
on: () => ({}),

test/stubs.ts

+10
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,16 @@ export class AndroidResourcesMigrationServiceStub implements IAndroidResourcesMi
849849
}
850850
}
851851

852+
export class AndroidBundleValidatorHelper implements IAndroidBundleValidatorHelper {
853+
validateNoAab() {
854+
return true;
855+
}
856+
857+
validateRuntimeVersion() {
858+
859+
}
860+
}
861+
852862
export class InjectorStub extends Yok implements IInjector {
853863
constructor() {
854864
super();

0 commit comments

Comments
 (0)