Skip to content

Commit d694a25

Browse files
fix: Validate environment based on specified runtime version
Add logic to validate environment based on the specified runtime version. This is required for the cases where the runtime version is not specified in the project's package.json but we need to verify if a runtime version can be used For `tns platform update <platform>` command validate the runtime version that will be installed based on the passed argument. This has two cases: 1. In case version is specified: `tns platform update [email protected]` - we'll get the 4.0.1 version and validate if it can be used with currently installed Java compiler version. 2. In case version is not specified: `tns platform update android` - we'll not validate the java version here as we know we'll install at least 4.1.0 runtime, which is compatible with all Java versions For `tns platform clean <platform>` command we need to validate the current platform immediately as the command removes it and adds the same version after that.
1 parent 47c4c70 commit d694a25

File tree

8 files changed

+49
-22
lines changed

8 files changed

+49
-22
lines changed

lib/commands/platform-clean.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class CleanCommand implements ICommand {
55
private $projectData: IProjectData,
66
private $platformService: IPlatformService,
77
private $errors: IErrors,
8-
private $platformsData: IPlatformsData) {
8+
private $platformEnvironmentRequirements: IPlatformEnvironmentRequirements) {
99
this.$projectData.initializeProjectData();
1010
}
1111

@@ -18,12 +18,15 @@ export class CleanCommand implements ICommand {
1818
this.$errors.fail("No platform specified. Please specify a platform to clean");
1919
}
2020

21+
_.each(args, platform => {
22+
this.$platformService.validatePlatform(platform, this.$projectData);
23+
});
24+
2125
for (const platform of args) {
2226
this.$platformService.validatePlatformInstalled(platform, this.$projectData);
2327

24-
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
25-
const platformProjectService = platformData.platformProjectService;
26-
await platformProjectService.validate(this.$projectData);
28+
const currentRuntimeVersion = this.$platformService.getCurrentPlatformVersion(platform, this.$projectData);
29+
await this.$platformEnvironmentRequirements.checkEnvironmentRequirements(platform, this.$projectData.projectDir, currentRuntimeVersion);
2730
}
2831

2932
return true;

lib/commands/update-platform.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ export class UpdatePlatformCommand implements ICommand {
44
constructor(private $options: IOptions,
55
private $projectData: IProjectData,
66
private $platformService: IPlatformService,
7-
private $errors: IErrors,
8-
private $platformsData: IPlatformsData) {
7+
private $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
8+
private $errors: IErrors) {
99
this.$projectData.initializeProjectData();
1010
}
1111

@@ -18,12 +18,24 @@ export class UpdatePlatformCommand implements ICommand {
1818
this.$errors.fail("No platform specified. Please specify platforms to update.");
1919
}
2020

21-
for (const arg of args) {
21+
_.each(args, arg => {
2222
const platform = arg.split("@")[0];
2323
this.$platformService.validatePlatform(platform, this.$projectData);
24-
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
25-
const platformProjectService = platformData.platformProjectService;
26-
await platformProjectService.validate(this.$projectData);
24+
});
25+
26+
for (const arg of args) {
27+
const [ platform, versionToBeInstalled ] = arg.split("@");
28+
this.$platformService.validatePlatformInstalled(platform, this.$projectData);
29+
const argsToCheckEnvironmentRequirements: string[] = [ platform ];
30+
// If version is not specified, we know the command will install the latest compatible Android runtime.
31+
// The latest compatible Android runtime supports Java version, so we do not need to pass it here.
32+
// Passing projectDir to the nativescript-doctor validation will cause it to check the runtime from the current package.json
33+
// So in this case, where we do not want to validate the runtime, just do not pass both projectDir and runtimeVersion.
34+
if (versionToBeInstalled) {
35+
argsToCheckEnvironmentRequirements.push(this.$projectData.projectDir, versionToBeInstalled);
36+
}
37+
38+
await this.$platformEnvironmentRequirements.checkEnvironmentRequirements(...argsToCheckEnvironmentRequirements);
2739
}
2840

2941
return true;

lib/common

lib/definitions/platform.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ interface IPlatformService extends IBuildPlatformAction, NodeJS.EventEmitter {
214214
* @returns {void}
215215
*/
216216
saveBuildInfoFile(platform: string, projectDir: string, buildInfoFileDirname: string): void;
217+
218+
/**
219+
* Gives information for the current version of the runtime.
220+
* @param {string} platform The platform to be checked.
221+
* @param {IProjectData} projectData The data describing the project
222+
* @returns {string} Runtime version
223+
*/
224+
getCurrentPlatformVersion(platform: string, projectData: IProjectData): string;
217225
}
218226

219227
interface IPlatformOptions extends IPlatformSpecificData, ICreateProjectOptions { }
@@ -381,5 +389,5 @@ interface IUpdateAppOptions extends IOptionalFilesToSync, IOptionalFilesToRemove
381389
}
382390

383391
interface IPlatformEnvironmentRequirements {
384-
checkEnvironmentRequirements(platform?: string, projectDir?: string): Promise<boolean>;
385-
}
392+
checkEnvironmentRequirements(platform?: string, projectDir?: string, runtimeVersion?: string): Promise<boolean>;
393+
}

lib/services/doctor-service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class DoctorService implements IDoctorService {
1717
private $terminalSpinnerService: ITerminalSpinnerService,
1818
private $versionsService: IVersionsService) { }
1919

20-
public async printWarnings(configOptions?: { trackResult: boolean , projectDir?: string }): Promise<void> {
20+
public async printWarnings(configOptions?: { trackResult: boolean , projectDir?: string, runtimeVersion?: string }): Promise<void> {
2121
const infos = await this.$terminalSpinnerService.execute<NativeScriptDoctor.IInfo[]>({
2222
text: `Getting environment information ${EOL}`
23-
}, () => doctor.getInfos({ projectDir: configOptions && configOptions.projectDir }));
23+
}, () => doctor.getInfos({ projectDir: configOptions && configOptions.projectDir, androidRuntimeVersion: configOptions && configOptions.runtimeVersion }));
2424

2525
const warnings = infos.filter(info => info.type === constants.WARNING_TYPE_NAME);
2626
const hasWarnings = warnings.length > 0;
@@ -80,12 +80,12 @@ class DoctorService implements IDoctorService {
8080
});
8181
}
8282

83-
public async canExecuteLocalBuild(platform?: string, projectDir?: string): Promise<boolean> {
83+
public async canExecuteLocalBuild(platform?: string, projectDir?: string, runtimeVersion?: string): Promise<boolean> {
8484
await this.$analyticsService.trackEventActionInGoogleAnalytics({
8585
action: TrackActionNames.CheckLocalBuildSetup,
8686
additionalData: "Starting",
8787
});
88-
const infos = await doctor.getInfos({ platform, projectDir });
88+
const infos = await doctor.getInfos({ platform, projectDir, androidRuntimeVersion: runtimeVersion });
8989

9090
const warnings = this.filterInfosByType(infos, constants.WARNING_TYPE_NAME);
9191
const hasWarnings = warnings.length > 0;

lib/services/platform-environment-requirements.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
3030
"deploy": "tns cloud deploy"
3131
};
3232

33-
public async checkEnvironmentRequirements(platform?: string, projectDir?: string): Promise<boolean> {
33+
public async checkEnvironmentRequirements(platform?: string, projectDir?: string, runtimeVersion?: string): Promise<boolean> {
3434
if (process.env.NS_SKIP_ENV_CHECK) {
3535
await this.$analyticsService.trackEventActionInGoogleAnalytics({
3636
action: TrackActionNames.CheckEnvironmentRequirements,
@@ -39,7 +39,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
3939
return true;
4040
}
4141

42-
const canExecute = await this.$doctorService.canExecuteLocalBuild(platform, projectDir);
42+
const canExecute = await this.$doctorService.canExecuteLocalBuild(platform, projectDir, runtimeVersion);
4343
if (!canExecute) {
4444
if (!isInteractive()) {
4545
await this.$analyticsService.trackEventActionInGoogleAnalytics({
@@ -71,7 +71,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
7171
if (selectedOption === PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME) {
7272
await this.$doctorService.runSetupScript();
7373

74-
if (await this.$doctorService.canExecuteLocalBuild(platform, projectDir)) {
74+
if (await this.$doctorService.canExecuteLocalBuild(platform, projectDir, runtimeVersion)) {
7575
return true;
7676
}
7777

@@ -102,7 +102,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
102102

103103
if (selectedOption === PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME) {
104104
await this.processBothCloudBuildsAndSetupScript();
105-
if (await this.$doctorService.canExecuteLocalBuild(platform, projectDir)) {
105+
if (await this.$doctorService.canExecuteLocalBuild(platform, projectDir, runtimeVersion)) {
106106
return true;
107107
}
108108

lib/services/platform-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class PlatformService extends EventEmitter implements IPlatformService {
7474
}
7575
}
7676

77-
private getCurrentPlatformVersion(platform: string, projectData: IProjectData): string {
77+
public getCurrentPlatformVersion(platform: string, projectData: IProjectData): string {
7878
const platformData = this.$platformsData.getPlatformData(platform, projectData);
7979
const currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
8080
let version: string;

test/stubs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ export class PlatformServiceStub extends EventEmitter implements IPlatformServic
812812
public async trackActionForPlatform(actionData: ITrackPlatformAction): Promise<void> {
813813
return null;
814814
}
815+
816+
public getCurrentPlatformVersion(platform: string, projectData: IProjectData): string {
817+
return null;
818+
}
815819
}
816820

817821
export class EmulatorPlatformService implements IEmulatorPlatformService {

0 commit comments

Comments
 (0)