Skip to content

Commit 8331c22

Browse files
Add platform on all commands
When you execute `tns run <platform>`, but you have forgotten to add the platform before that, you go in inconsistent state. Make sure to add the platform for the user when one of the following is executed: * `tns prepare <platform>` * `tns build <platform>` * `tns run <platform>` * `tns update <platform>@<version>` - this will add the specified version in case you have not added the platform before that. * `tns emulate <platform>` This way we simplify the workflow, now you can just do: 1) `tns create app1` 2) `tns run android --path app1` And the application will run on your attached device.
1 parent 2d2ff89 commit 8331c22

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

lib/commands/update-platform.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class UpdatePlatformCommand implements ICommand {
1717
this.$errors.fail("No platform specified. Please specify platforms to update.");
1818
}
1919

20-
_.each(args, arg => this.$platformService.validatePlatformInstalled(arg.split("@")[0]));
20+
_.each(args, arg => this.$platformService.validatePlatform(arg.split("@")[0]));
2121

2222
return true;
2323
}).future<boolean>()();

lib/platform-command-param.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class PlatformCommandParameter implements ICommandParameter {
66
mandatory = true;
77
validate(value: string): IFuture<boolean> {
88
return (() => {
9-
this.$platformService.validatePlatformInstalled(value);
9+
this.$platformService.validatePlatform(value);
1010
return true;
1111
}).future<boolean>()();
1212
}

lib/services/platform-service.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export class PlatformService implements IPlatformService {
146146
this.validatePlatform(platform);
147147

148148
platform = platform.toLowerCase();
149+
this.ensurePlatformInstalled(platform).wait();
149150

150151
var platformData = this.$platformsData.getPlatformData(platform);
151152
let appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
@@ -204,7 +205,7 @@ export class PlatformService implements IPlatformService {
204205
public runPlatform(platform: string): IFuture<void> {
205206
return (() => {
206207
platform = platform.toLowerCase();
207-
208+
208209
if (this.$options.emulator) {
209210
this.deployOnEmulator(platform).wait();
210211
} else {
@@ -234,20 +235,23 @@ export class PlatformService implements IPlatformService {
234235
public updatePlatforms(platforms: string[]): IFuture<void> {
235236
return (() => {
236237
_.each(platforms, platform => {
238+
var platformString = platform;
237239
var parts = platform.split("@");
238240
platform = parts[0].toLowerCase();
239241
var version = parts[1];
240-
241-
this.validatePlatformInstalled(platform);
242-
this.updatePlatform(platform, version).wait();
242+
if(this.isPlatformInstalled(platform).wait()) {
243+
this.updatePlatform(platform, version).wait();
244+
} else {
245+
this.addPlatform(platformString).wait();
246+
}
243247
});
244248
}).future<void>()();
245249
}
246250

247251
public deployOnDevice(platform: string): IFuture<void> {
248252
return (() => {
249253
platform = platform.toLowerCase();
250-
254+
this.ensurePlatformInstalled(platform).wait();
251255
var platformData = this.$platformsData.getPlatformData(platform);
252256

253257
var cachedDeviceOption = this.$options.forDevice;
@@ -276,7 +280,7 @@ export class PlatformService implements IPlatformService {
276280

277281
public deployOnEmulator(platform: string): IFuture<void> {
278282
return (() => {
279-
this.validatePlatformInstalled(platform);
283+
this.ensurePlatformInstalled(platform).wait();
280284
platform = platform.toLowerCase();
281285

282286
var platformData = this.$platformsData.getPlatformData(platform);
@@ -333,7 +337,15 @@ export class PlatformService implements IPlatformService {
333337
}
334338
}).future<void>()();
335339
}
336-
340+
341+
private ensurePlatformInstalled(platform: string): IFuture<void> {
342+
return (() => {
343+
if(!this.isPlatformInstalled(platform).wait()) {
344+
this.addPlatform(platform).wait();
345+
}
346+
}).future<void>()();
347+
}
348+
337349
private isPlatformInstalled(platform: string): IFuture<boolean> {
338350
return this.$fs.exists(path.join(this.$projectData.platformsDir, platform.toLowerCase()));
339351
}

test/platform-service.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,11 @@ describe('Platform Service Tests', () => {
227227
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
228228
normalizedPlatformName: "iOS",
229229
platformProjectService: {
230-
prepareProject: () => Future.fromResult()
230+
prepareProject: () => Future.fromResult(),
231+
validate: () => Future.fromResult(),
232+
createProject: (projectRoot: string, frameworkDir: string) => Future.fromResult(),
233+
interpolateData: (projectRoot: string) => Future.fromResult(),
234+
afterCreateProject: (projectRoot: string) => Future.fromResult()
231235
}
232236
}
233237
};
@@ -281,7 +285,11 @@ describe('Platform Service Tests', () => {
281285
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
282286
normalizedPlatformName: "Android",
283287
platformProjectService: {
284-
prepareProject: () => Future.fromResult()
288+
prepareProject: () => Future.fromResult(),
289+
validate: () => Future.fromResult(),
290+
createProject: (projectRoot: string, frameworkDir: string) => Future.fromResult(),
291+
interpolateData: (projectRoot: string) => Future.fromResult(),
292+
afterCreateProject: (projectRoot: string) => Future.fromResult()
285293
}
286294
}
287295
};

0 commit comments

Comments
 (0)