Skip to content

Commit 480b882

Browse files
committed
Run and prepare commands
1 parent ae84178 commit 480b882

File tree

6 files changed

+128
-32
lines changed

6 files changed

+128
-32
lines changed

lib/bootstrap.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ $injector.require("platformService", "./services/platform-service");
1111
$injector.requireCommand("create", "./commands/create-project-command");
1212
$injector.requireCommand("platform|*list", "./commands/platform-command");
1313
$injector.requireCommand("platform|add", "./commands/platform-command");
14+
$injector.requireCommand("run", "./commands/run-command");
15+
$injector.requireCommand("prepare", "./commands/run-command");
16+
$injector.requireCommand("build", "./commands/run-command");
1417

1518
$injector.require("npm", "./node-package-manager");

lib/commands/run-command.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
///<reference path="../.d.ts"/>
2+
3+
export class RunCommand implements ICommand {
4+
constructor(private $platformService: IPlatformService) { }
5+
6+
execute(args: string[]): IFuture<void> {
7+
return (() => {
8+
this.$platformService.runPlatform(args[0]).wait();
9+
}).future<void>()();
10+
}
11+
}
12+
$injector.registerCommand("run", RunCommand);
13+
14+
export class PrepareCommand implements ICommand {
15+
constructor(private $platformService: IPlatformService) { }
16+
17+
execute(args: string[]): IFuture<void> {
18+
return (() => {
19+
this.$platformService.preparePlatform(args[0]).wait();
20+
}).future<void>()();
21+
}
22+
}
23+
$injector.registerCommand("prepare", PrepareCommand);
24+
25+
export class BuildCommand implements ICommand {
26+
constructor(private $platformService: IPlatformService) { }
27+
28+
execute(args: string[]): IFuture<void> {
29+
return (() => {
30+
this.$platformService.buildPlatform(args[0]).wait();
31+
}).future<void>()();
32+
}
33+
}
34+
$injector.registerCommand("build", BuildCommand);

lib/definitions/platform.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
interface IPlatformService {
2-
addPlatforms(platforms: string[]): IFuture<any>;
2+
addPlatforms(platforms: string[]): IFuture<void>;
33
getInstalledPlatforms(): IFuture<string[]>;
44
getAvailablePlatforms(): IFuture<string[]>;
5+
runPlatform(platform: string): IFuture<void>;
6+
preparePlatform(platform: string): IFuture<void>;
7+
buildPlatform(platform: string): IFuture<void>;
58
}
69

710
interface IPlatformCapabilities {

lib/definitions/project.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
interface IProjectService {
22
createProject(projectName: string, projectId: string): IFuture<void>;
33
createPlatformSpecificProject(platform: string): IFuture<void>;
4+
prepareProject(platform: string): IFuture<void>;
5+
buildProject(platform: string): IFuture<void>;
46
ensureProject(): void;
57
projectData: IProjectData;
68
}
79

810
interface IAndroidProjectService {
911
createProject(projectData: IProjectData): IFuture<void>;
12+
prepareProject(projectData: IProjectData): IFuture<void>;
1013
}
1114

1215
interface IiOSProjectService {

lib/services/platform-service.ts

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class PlatformService implements IPlatformService {
2222
return this.platformCapabilities[platform];
2323
}
2424

25-
public addPlatforms(platforms: string[]): IFuture<any> {
25+
public addPlatforms(platforms: string[]): IFuture<void> {
2626
return (() => {
2727
this.$projectService.ensureProject();
2828

@@ -39,7 +39,26 @@ export class PlatformService implements IPlatformService {
3939
this.addPlatform(platform.toLowerCase()).wait();
4040
});
4141

42-
}).future<any>()();
42+
}).future<void>()();
43+
}
44+
45+
private addPlatform(platform: string): IFuture<void> {
46+
return(() => {
47+
platform = platform.split("@")[0];
48+
var platformPath = path.join(this.$projectService.projectData.platformsDir, platform);
49+
50+
// TODO: Check for version compatability if the platform is in format platform@version. This should be done in PR for semanting versioning
51+
52+
this.validatePlatform(platform);
53+
54+
if (this.$fs.exists(platformPath).wait()) {
55+
this.$errors.fail("Platform %s already added", platform);
56+
}
57+
58+
// Copy platform specific files in platforms dir
59+
this.$projectService.createPlatformSpecificProject(platform).wait();
60+
61+
}).future<void>()();
4362
}
4463

4564
public getInstalledPlatforms(): IFuture<string[]> {
@@ -62,29 +81,34 @@ export class PlatformService implements IPlatformService {
6281
}).future<string[]>()();
6382
}
6483

65-
private addPlatform(platform: string): IFuture<void> {
66-
return(() => {
67-
platform = platform.split("@")[0];
68-
var platformPath = path.join(this.$projectService.projectData.platformsDir, platform);
84+
public runPlatform(platform: string): IFuture<void> {
85+
return (() => {
6986

70-
// TODO: Check for version compatability if the platform is in format platform@version. This should be done in PR for semanting versioning
87+
}).future<void>()();
88+
}
7189

72-
if (!this.isValidPlatform(platform)) {
73-
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(_.keys(this.platformCapabilities)));
74-
}
90+
public preparePlatform(platform: string): IFuture<void> {
91+
return (() => {
92+
this.validatePlatform(platform);
7593

76-
if (!this.isPlatformSupportedForOS(platform)) {
77-
this.$errors.fail("Applications for platform %s can not be built on this OS - %s", platform, process.platform);
78-
}
94+
this.$projectService.prepareProject(platform).wait();
95+
}).future<void>()();
96+
}
7997

80-
if (this.$fs.exists(platformPath).wait()) {
81-
this.$errors.fail("Platform %s already added", platform);
82-
}
98+
public buildPlatform(platform: string): IFuture<void> {
99+
return (() => {
100+
this.$projectService.buildProject(platform);
101+
}).future<void>()();
102+
}
83103

84-
// Copy platform specific files in platforms dir
85-
this.$projectService.createPlatformSpecificProject(platform).wait();
104+
private validatePlatform(platform): void {
105+
if (!this.isValidPlatform(platform)) {
106+
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(_.keys(this.platformCapabilities)));
107+
}
86108

87-
}).future<void>()();
109+
if (!this.isPlatformSupportedForOS(platform)) {
110+
this.$errors.fail("Applications for platform %s can not be built on this OS - %s", platform, process.platform);
111+
}
88112
}
89113

90114
private isValidPlatform(platform: string) {

lib/services/project-service.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,46 @@ export class ProjectService implements IProjectService {
145145

146146
public createPlatformSpecificProject(platform: string): IFuture<void> {
147147
return(() => {
148+
this.executePlatformSpecificAction(platform, "createProject").wait();
149+
}).future<void>()();
150+
}
151+
152+
public prepareProject(platform: string): IFuture<void> {
153+
return (() => {
154+
this.executePlatformSpecificAction(platform, "prepareProject").wait();
155+
}).future<void>()();
156+
}
157+
158+
public buildProject(platform: string): IFuture<void> {
159+
return (() => {
160+
this.executePlatformSpecificAction(platform, "buildProject").wait();
161+
}).future<void>()();
162+
}
163+
164+
private executePlatformSpecificAction(platform, functionName: string): IFuture<void> {
165+
return (() => {
148166
switch (platform) {
149167
case "android":
150-
// TODO: set default values for project name and project id
151-
this.$androidProjectService.createProject(this.projectData).wait();
168+
this.executeFunctionByName(functionName, this.$androidProjectService, [this.projectData]).wait();
152169
break;
153170
case "ios":
154-
this.$iOSProjectService.createProject(this.projectData).wait();
171+
this.executeFunctionByName(functionName, this.$iOSProjectService, [this.projectData]).wait();
155172
break;
156173
}
157174
}).future<void>()();
158175
}
159176

177+
private executeFunctionByName(functionName, context , args: any[] ): IFuture<any> {
178+
return (() => {
179+
var namespaces = functionName.split(".");
180+
var func = namespaces.pop();
181+
for (var i = 0; i < namespaces.length; i++) {
182+
context = context[namespaces[i]];
183+
}
184+
return context[func].apply(context, args).wait();
185+
}).future<any>()();
186+
}
187+
160188
private getCustomAppPath(): string {
161189
var customAppPath = options["copy-from"] || options["link-to"];
162190
if(customAppPath) {
@@ -221,23 +249,28 @@ class AndroidProjectService implements IAndroidProjectService {
221249
shell.sed('-i', /__NAME__/, projectData.projectName, path.join(projectDir, '.project'));
222250
shell.sed('-i', /__PACKAGE__/, packageName, path.join(projectDir, "AndroidManifest.xml"));
223251

224-
// Copy app into assets
225-
shell.cp("-r", path.join(projectData.projectDir, ProjectService.APP_FOLDER_NAME), path.join(projectDir, "assets"));
226-
227252
this.runAndroidUpdate(projectDir, targetApi).wait();
228253

229254
this.$logger.out("Project successfully created.");
230255

231256
}).future<any>()();
232257
}
233258

259+
public prepareProject(projectData: IProjectData): IFuture<void> {
260+
return (() => {
261+
var projectDir = path.join(projectData.projectDir, "platforms", "android");
262+
// Copy app into assets
263+
shell.cp("-r", path.join(projectData.projectDir, ProjectService.APP_FOLDER_NAME), path.join(projectDir, "assets"));
264+
}).future<void>()();
265+
}
266+
234267
private runAndroidUpdate(projectPath: string, targetApi): IFuture<void> {
235268
return (() => {
236269
this.$childProcess.exec("android update project --subprojects --path " + projectPath + " --target " + targetApi).wait();
237270
}).future<void>()();
238271
}
239272

240-
private validatePackageName(packageName: string): boolean {
273+
private validatePackageName(packageName: string): void {
241274
//Make the package conform to Java package types
242275
//Enforce underscore limitation
243276
if (!/^[a-zA-Z]+(\.[a-zA-Z0-9][a-zA-Z0-9_]*)+$/.test(packageName)) {
@@ -248,11 +281,9 @@ class AndroidProjectService implements IAndroidProjectService {
248281
if(/\b[Cc]lass\b/.test(packageName)) {
249282
this.$errors.fail("class is a reserved word");
250283
}
251-
252-
return true;
253284
}
254285

255-
private validateProjectName(projectName: string): boolean {
286+
private validateProjectName(projectName: string): void {
256287
if (projectName === '') {
257288
this.$errors.fail("Project name cannot be empty");
258289
}
@@ -261,8 +292,6 @@ class AndroidProjectService implements IAndroidProjectService {
261292
if (/^[0-9]/.test(projectName)) {
262293
this.$errors.fail("Project name must not begin with a number");
263294
}
264-
265-
return true;
266295
}
267296

268297
private get frameworkDir(): string {

0 commit comments

Comments
 (0)