Skip to content

Commit 37448c9

Browse files
committed
Deploy on emulator
1 parent 3bef0fb commit 37448c9

13 files changed

+125
-44
lines changed

lib/bootstrap.ts

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ $injector.require("platformService", "./services/platform-service");
1515
$injector.require("userSettingsService", "./services/user-settings-service");
1616
$injector.require("analyticsSettingsService", "./services/analytics-settings-service");
1717

18+
$injector.require("emulatorSettingsService", "./services/emulator-settings-service");
19+
1820
$injector.requireCommand("create", "./commands/create-project");
1921
$injector.requireCommand("platform|*list", "./commands/list-platforms");
2022
$injector.requireCommand("platform|add", "./commands/add-platform");
@@ -23,6 +25,7 @@ $injector.requireCommand("run", "./commands/run");
2325
$injector.requireCommand("prepare", "./commands/prepare");
2426
$injector.requireCommand("build", "./commands/build");
2527
$injector.requireCommand("deploy", "./commands/deploy");
28+
$injector.requireCommand("emulate", "./commands/emulate");
2629

2730
$injector.require("npm", "./node-package-manager");
2831
$injector.require("config", "./config");

lib/commands/deploy.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
///<reference path="../.d.ts"/>
22

3-
export class DeployCommand implements ICommand {
3+
export class DeployOnDeviceCommand implements ICommand {
44
constructor(private $platformService: IPlatformService) { }
55

66
execute(args: string[]): IFuture<void> {
7-
return (() => {
8-
this.$platformService.deploy(args[0]).wait();
9-
}).future<void>()();
7+
return this.$platformService.deployOnDevice(args[0]);
108
}
119
}
12-
$injector.registerCommand("deploy", DeployCommand);
10+
$injector.registerCommand("deploy", DeployOnDeviceCommand);

lib/commands/emulate.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///<reference path="../.d.ts"/>
2+
3+
export class DeployOnEmulatorCommand implements ICommand {
4+
constructor(private $platformService: IPlatformService) { }
5+
6+
execute(args: string[]): IFuture<void> {
7+
return this.$platformService.deployOnEmulator(args[0]);
8+
}
9+
}
10+
$injector.registerCommand("deploy", DeployOnEmulatorCommand);

lib/declarations.ts

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ interface INodePackageManager {
66

77
interface IStaticConfig extends Config.IStaticConfig { }
88

9+
interface IApplicationPackage {
10+
packageName: string;
11+
time: Date;
12+
}
13+

lib/definitions/platform.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ interface IPlatformService {
66
runPlatform(platform: string): IFuture<void>;
77
preparePlatform(platform: string): IFuture<void>;
88
buildPlatform(platform: string): IFuture<void>;
9-
deploy(platform: string): IFuture<void>;
9+
deployOnDevice(platform: string): IFuture<void>;
10+
deployOnEmulator(platform: string): IFuture<void>;
1011
}
1112

1213
interface IPlatformData {
1314
frameworkPackageName: string;
1415
platformProjectService: IPlatformProjectService;
16+
emulatorServices: Mobile.IEmulatorPlatformServices;
1517
projectRoot: string;
1618
normalizedPlatformName: string;
1719
buildOutputPath: string;

lib/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var knownOpts:any = {
1313
"link-to": String,
1414
"release": String,
1515
"device": Boolean,
16+
"emulator": Boolean,
1617
"version": Boolean,
1718
"help": Boolean
1819
},

lib/services/android-project-service.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ class AndroidProjectService implements IPlatformProjectService {
1010
private targetApi: string;
1111

1212
constructor(private $fs: IFileSystem,
13-
private $errors: IErrors,
14-
private $logger: ILogger,
15-
private $childProcess: IChildProcess,
16-
private $projectData: IProjectData,
17-
private $propertiesParser: IPropertiesParser) { }
13+
private $errors: IErrors,
14+
private $logger: ILogger,
15+
private $childProcess: IChildProcess,
16+
private $projectData: IProjectData,
17+
private $propertiesParser: IPropertiesParser,
18+
private $androidEmulatorServices: Mobile.IEmulatorPlatformServices) { }
1819

1920
public get platformData(): IPlatformData {
2021
return {
2122
frameworkPackageName: "tns-android",
2223
normalizedPlatformName: "Android",
2324
platformProjectService: this,
25+
emulatorServices: this.$androidEmulatorServices,
2426
projectRoot: path.join(this.$projectData.platformsDir, "android"),
2527
buildOutputPath: path.join(this.$projectData.platformsDir, "android", "bin"),
2628
validPackageNames: [
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
///<reference path="../.d.ts"/>
2+
3+
export class EmulatorSettingsService implements Mobile.IEmulatorSettingsService {
4+
private static REQURED_ANDROID_APILEVEL = 17;
5+
6+
constructor(private $injector: IInjector) { }
7+
8+
public canStart(platform: string): IFuture<boolean> {
9+
return (() => {
10+
var platformService = this.$injector.resolve("platformService"); // this should be resolved here due to cyclic dependency
11+
12+
var installedPlatforms = platformService.getInstalledPlatforms().wait();
13+
return _.contains(installedPlatforms, platform.toLowerCase());
14+
}).future<boolean>()();
15+
}
16+
17+
public get minVersion(): number {
18+
return EmulatorSettingsService.REQURED_ANDROID_APILEVEL;
19+
}
20+
}
21+
$injector.register("emulatorSettingsService", EmulatorSettingsService);

lib/services/ios-project-service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ class IOSProjectService implements IPlatformProjectService {
1515
constructor(private $projectData: IProjectData,
1616
private $fs: IFileSystem,
1717
private $childProcess: IChildProcess,
18-
private $errors: IErrors) { }
18+
private $errors: IErrors,
19+
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices) { }
1920

2021
public get platformData(): IPlatformData {
2122
return {
2223
frameworkPackageName: "tns-ios",
2324
normalizedPlatformName: "iOS",
2425
platformProjectService: this,
26+
emulatorServices: this.$iOSEmulatorServices,
2527
projectRoot: path.join(this.$projectData.platformsDir, "ios"),
2628
buildOutputPath: path.join(this.$projectData.platformsDir, "ios", "build", "device"),
2729
validPackageNames: [

lib/services/platform-service.ts

+66-31
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,11 @@ export class PlatformService implements IPlatformService {
164164
platform = platform.toLowerCase();
165165

166166
this.preparePlatform(platform).wait();
167-
168-
// We need to set device option here
169-
var cachedDeviceOption = options.device;
170-
options.device = true;
171-
this.buildPlatform(platform).wait();
172-
options.device = cachedDeviceOption;
173-
174-
this.deploy(platform).wait();
167+
if(options.emulator) {
168+
this.deployOnEmulator(platform).wait();
169+
} else {
170+
this.deployOnDevice(platform).wait();
171+
}
175172
}).future<void>()();
176173
}
177174

@@ -191,35 +188,21 @@ export class PlatformService implements IPlatformService {
191188
}).future<void>()();
192189
}
193190

194-
public deploy(platform: string): IFuture<void> {
191+
public deployOnDevice(platform: string): IFuture<void> {
195192
return (() => {
196-
platform = platform.toLowerCase();
197-
198193
this.validatePlatformInstalled(platform);
194+
platform = platform.toLowerCase();
199195

200196
var platformData = this.$platformsData.getPlatformData(platform);
201197

202-
// Get latest package that is produced from build
203-
var candidates = this.$fs.readDirectory(platformData.buildOutputPath).wait();
204-
var packages = _.filter(candidates, candidate => {
205-
return _.contains(platformData.validPackageNames, candidate);
206-
}).map(currentPackage => {
207-
currentPackage = path.join(platformData.buildOutputPath, currentPackage);
208-
209-
return {
210-
pkg: currentPackage,
211-
time: this.$fs.getFsStats(currentPackage).wait().mtime
212-
};
213-
});
214-
215-
packages = _.sortBy(packages, pkg => pkg.time ).reverse(); // We need to reverse because sortBy always sorts in ascending order
216-
217-
if(packages.length === 0) {
218-
var packageExtName = path.extname(platformData.validPackageNames[0]);
219-
this.$errors.fail("No %s found in %s directory", packageExtName, platformData.buildOutputPath)
220-
}
198+
// We need to build for device
199+
var cachedDeviceOption = options.device;
200+
options.device = true;
201+
this.buildPlatform(platform).wait();
202+
options.device = cachedDeviceOption;
221203

222-
var packageFile = packages[0].pkg;
204+
// Get latest package that is produced from build
205+
var packageFile = this.getLatestApplicationPackage(platformData).wait().packageName;
223206
this.$logger.out("Using ", packageFile);
224207

225208
this.$devicesServices.initialize(platform, options.device).wait();
@@ -229,6 +212,25 @@ export class PlatformService implements IPlatformService {
229212
}).future<void>()();
230213
}
231214

215+
public deployOnEmulator(platform: string): IFuture<void> {
216+
return (() => {
217+
this.validatePlatformInstalled(platform);
218+
platform = platform.toLowerCase();
219+
220+
var platformData = this.$platformsData.getPlatformData(platform);
221+
var emulatorServices = platformData.emulatorServices;
222+
223+
emulatorServices.checkAvailability().wait();
224+
225+
this.buildPlatform(platform).wait();
226+
227+
var packageFile = this.getLatestApplicationPackage(platformData).wait().packageName;
228+
this.$logger.out("Using ", packageFile);
229+
230+
emulatorServices.startEmulator(packageFile, options.emulator).wait();
231+
}).future<void>()();
232+
}
233+
232234
private validatePlatform(platform: string): void {
233235
if(!platform) {
234236
this.$errors.fail("No platform specified.")
@@ -299,5 +301,38 @@ export class PlatformService implements IPlatformService {
299301
});
300302
}).future<void>()();
301303
}
304+
305+
private getApplicationPackages(platformData: IPlatformData): IFuture<IApplicationPackage[]> {
306+
return (() => {
307+
// Get latest package that is produced from build
308+
var candidates = this.$fs.readDirectory(platformData.buildOutputPath).wait();
309+
var packages = _.filter(candidates, candidate => {
310+
return _.contains(platformData.validPackageNames, candidate);
311+
}).map(currentPackage => {
312+
currentPackage = path.join(platformData.buildOutputPath, currentPackage);
313+
314+
return {
315+
packageName: currentPackage,
316+
time: this.$fs.getFsStats(currentPackage).wait().mtime
317+
};
318+
});
319+
320+
return packages;
321+
}).future<IApplicationPackage[]>()();
322+
}
323+
324+
private getLatestApplicationPackage(platformData: IPlatformData): IFuture<IApplicationPackage> {
325+
return (() => {
326+
var packages = this.getApplicationPackages(platformData).wait();
327+
packages = _.sortBy(packages, pkg => pkg.time).reverse(); // We need to reverse because sortBy always sorts in ascending order
328+
329+
if (packages.length === 0) {
330+
var packageExtName = path.extname(platformData.validPackageNames[0]);
331+
this.$errors.fail("No %s found in %s directory", packageExtName, platformData.buildOutputPath);
332+
}
333+
334+
return packages[0];
335+
}).future<IApplicationPackage>()();
336+
}
302337
}
303338
$injector.register("platformService", PlatformService);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ffi": "https://github.com/icenium/node-ffi/tarball/master",
2929
"fibers": "https://github.com/icenium/node-fibers/tarball/master",
3030
"filesize": "2.0.3",
31+
"iconv-lite": "0.4.4",
3132
"log4js": "0.6.9",
3233
"mkdirp": "0.3.5",
3334
"mute-stream": "0.0.4",

test/platform-service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ testInjector.register('npm', stubs.NPMStub);
1818
testInjector.register('projectData', stubs.ProjectDataStub);
1919
testInjector.register('platformsData', stubs.PlatformsDataStub);
2020
testInjector.register('devicesServices', {});
21+
testInjector.register('androidEmulatorServices', {});
2122

2223
describe('PlatformService', function(){
2324
describe('#updatePlatforms()', function(){

0 commit comments

Comments
 (0)