Skip to content

Commit ae84178

Browse files
committed
List platforms command
1 parent 604f2ae commit ae84178

File tree

4 files changed

+83
-54
lines changed

4 files changed

+83
-54
lines changed

lib/commands/platform-command.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
///<reference path="../.d.ts"/>
2+
import helpers = require("./../common/helpers");
23

34
export class ListPlatformsCommand implements ICommand {
4-
constructor(private $platformService: IPlatformService) { }
5+
constructor(private $platformService: IPlatformService,
6+
private $logger: ILogger) { }
57

68
execute(args: string[]): IFuture<void> {
79
return (() => {
10+
var availablePlatforms = this.$platformService.getAvailablePlatforms().wait();
11+
this.$logger.out("Available platforms: %s", helpers.formatListOfNames(availablePlatforms));
12+
13+
var installedPlatforms = this.$platformService.getInstalledPlatforms().wait();
14+
this.$logger.out("Installed platforms %s", helpers.formatListOfNames(installedPlatforms));
815
}).future<void>()();
916
}
1017
}
1118
$injector.registerCommand("platform|*list", ListPlatformsCommand);
1219

1320
export class AddPlatformCommand implements ICommand {
14-
constructor(private $platformService: IPlatformService) { }
21+
constructor(private $platformService: IPlatformService) { }
1522

16-
execute(args: string[]): IFuture<void> {
17-
return (() => {
18-
this.$platformService.addPlatforms(args).wait();
19-
}).future<void>()();
20-
}
23+
execute(args: string[]): IFuture<void> {
24+
return (() => {
25+
this.$platformService.addPlatforms(args).wait();
26+
}).future<void>()();
27+
}
2128
}
2229
$injector.registerCommand("platform|add", AddPlatformCommand);

lib/definitions/platform.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
interface IPlatformService {
2-
addPlatforms(platforms: string[]): IFuture<any>;
2+
addPlatforms(platforms: string[]): IFuture<any>;
3+
getInstalledPlatforms(): IFuture<string[]>;
4+
getAvailablePlatforms(): IFuture<string[]>;
35
}
46

57
interface IPlatformCapabilities {
6-
targetedOS?: string[];
7-
frameworkUrl: string;
8+
targetedOS?: string[];
9+
frameworkUrl: string;
810
}

lib/definitions/project.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ interface IProjectService {
22
createProject(projectName: string, projectId: string): IFuture<void>;
33
createPlatformSpecificProject(platform: string): IFuture<void>;
44
ensureProject(): void;
5-
projectData: IProjectData;
5+
projectData: IProjectData;
66
}
77

88
interface IAndroidProjectService {
@@ -14,8 +14,8 @@ interface IiOSProjectService {
1414
}
1515

1616
interface IProjectData {
17-
projectDir: string;
18-
platformsDir: string;
17+
projectDir: string;
18+
platformsDir: string;
1919
projectFilePath: string;
2020
projectId?: string;
2121
projectName?: string;

lib/services/platform-service.ts

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,65 @@ import path = require("path");
44
import helpers = require("./../common/helpers");
55

66
export class PlatformService implements IPlatformService {
7-
constructor(private $errors: IErrors,
8-
private $fs: IFileSystem,
9-
private $projectService: IProjectService) { }
10-
11-
private platformCapabilities: { [key: string]: IPlatformCapabilities } = {
12-
ios: {
13-
targetedOS: ['darwin'],
14-
frameworkUrl: ""
15-
},
16-
android: {
17-
frameworkUrl: ""
18-
}
19-
};
20-
21-
public getCapabilities(platform: string): IPlatformCapabilities {
22-
return this.platformCapabilities[platform];
23-
}
24-
25-
public addPlatforms(platforms: string[]): IFuture<any> {
26-
return (() => {
7+
constructor(private $errors: IErrors,
8+
private $fs: IFileSystem,
9+
private $projectService: IProjectService) { }
10+
11+
private platformCapabilities: { [key: string]: IPlatformCapabilities } = {
12+
ios: {
13+
targetedOS: ['darwin'],
14+
frameworkUrl: ""
15+
},
16+
android: {
17+
frameworkUrl: ""
18+
}
19+
};
20+
21+
public getCapabilities(platform: string): IPlatformCapabilities {
22+
return this.platformCapabilities[platform];
23+
}
24+
25+
public addPlatforms(platforms: string[]): IFuture<any> {
26+
return (() => {
2727
this.$projectService.ensureProject();
2828

29-
if(!platforms || platforms.length === 0) {
30-
this.$errors.fail("No platform specified. Please specify a platform to add");
31-
}
29+
if(!platforms || platforms.length === 0) {
30+
this.$errors.fail("No platform specified. Please specify a platform to add");
31+
}
3232

33-
var platformsDir = this.$projectService.projectData.platformsDir;
34-
if(!this.$fs.exists(platformsDir).wait()) {
35-
this.$fs.createDirectory(platformsDir).wait();
36-
}
33+
var platformsDir = this.$projectService.projectData.platformsDir;
34+
if(!this.$fs.exists(platformsDir).wait()) {
35+
this.$fs.createDirectory(platformsDir).wait();
36+
}
3737

38-
_.each(platforms, platform => {
39-
this.addPlatform(platform.toLowerCase()).wait();
40-
});
38+
_.each(platforms, platform => {
39+
this.addPlatform(platform.toLowerCase()).wait();
40+
});
4141

42-
}).future<any>()();
43-
}
42+
}).future<any>()();
43+
}
4444

45-
private addPlatform(platform: string): IFuture<void> {
45+
public getInstalledPlatforms(): IFuture<string[]> {
46+
return(() => {
47+
if(!this.$fs.exists(this.$projectService.projectData.platformsDir).wait()) {
48+
return [];
49+
}
50+
51+
var subDirs = this.$fs.readDirectory(this.$projectService.projectData.platformsDir).wait();
52+
return _.filter(subDirs, p => { return Object.keys(this.platformCapabilities).indexOf(p) > -1; });
53+
}).future<string[]>()();
54+
}
55+
56+
public getAvailablePlatforms(): IFuture<string[]> {
57+
return (() => {
58+
var installedPlatforms = this.getInstalledPlatforms().wait();
59+
return _.filter(_.keys(this.platformCapabilities), p => {
60+
return installedPlatforms.indexOf(p) < 0 && this.isPlatformSupportedForOS(p); // Only those not already installed
61+
});
62+
}).future<string[]>()();
63+
}
64+
65+
private addPlatform(platform: string): IFuture<void> {
4666
return(() => {
4767
platform = platform.split("@")[0];
4868
var platformPath = path.join(this.$projectService.projectData.platformsDir, platform);
@@ -65,21 +85,21 @@ export class PlatformService implements IPlatformService {
6585
this.$projectService.createPlatformSpecificProject(platform).wait();
6686

6787
}).future<void>()();
68-
}
88+
}
6989

7090
private isValidPlatform(platform: string) {
7191
return this.platformCapabilities[platform];
7292
}
7393

7494
private isPlatformSupportedForOS(platform: string): boolean {
75-
var platformCapabilities = this.getCapabilities(platform);
76-
var targetedOS = platformCapabilities.targetedOS;
95+
var platformCapabilities = this.getCapabilities(platform);
96+
var targetedOS = platformCapabilities.targetedOS;
7797

78-
if(!targetedOS || targetedOS.indexOf("*") >= 0 || targetedOS.indexOf(process.platform) >= 0) {
79-
return true;
80-
}
98+
if(!targetedOS || targetedOS.indexOf("*") >= 0 || targetedOS.indexOf(process.platform) >= 0) {
99+
return true;
100+
}
81101

82-
return false;
83-
}
102+
return false;
103+
}
84104
}
85105
$injector.register("platformService", PlatformService);

0 commit comments

Comments
 (0)