Skip to content

Commit 0284cf8

Browse files
committed
Refactor project and platform services
1 parent bef4e47 commit 0284cf8

8 files changed

+234
-171
lines changed

lib/bootstrap.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ $injector.require("projectData", "./services/project-service");
66
$injector.require("projectService", "./services/project-service");
77
$injector.require("androidProjectService", "./services/project-service");
88
$injector.require("iOSProjectService", "./services/project-service");
9+
910
$injector.require("projectTemplatesService", "./services/project-templates-service");
11+
12+
$injector.require("platformsData", "./services/platform-service");
1013
$injector.require("platformService", "./services/platform-service");
14+
$injector.require("platformProjectService", "./services/platform-service");
1115

1216
$injector.requireCommand("create", "./commands/create-project");
1317
$injector.requireCommand("platform|*list", "./commands/list-platforms");

lib/declarations.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ interface INodePackageManager {
22
cache: string;
33
load(config?: any): IFuture<void>;
44
install(where: string, what: string): IFuture<any>;
5+
tryExecuteAction(action: any, args?: any[]): IFuture<void>;
6+
downloadNpmPackage(packageName: string, pathToSave?: string): IFuture<string>;
57
}
68

79
interface IPropertiesParser {

lib/definitions/platform.d.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ interface IPlatformService {
77
buildPlatform(platform: string): IFuture<void>;
88
}
99

10-
interface IPlatformCapabilities {
10+
interface IPlatformData {
11+
frameworkPackageName: string;
12+
platformProjectService: IPlatformSpecificProjectService;
13+
projectRoot: string;
1114
targetedOS?: string[];
12-
}
15+
}
16+
17+
interface IPlatformsData {
18+
platformsNames: string[];
19+
getPlatformData(platform: string): IPlatformData;
20+
}
21+

lib/definitions/project.d.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
interface IProjectService {
22
createProject(projectName: string, projectId: string): IFuture<void>;
3-
createPlatformSpecificProject(platform: string): IFuture<void>;
4-
prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void>;
5-
buildProject(platform: string): IFuture<void>;
63
ensureProject(): void;
74
}
85

9-
interface IPlatformProjectService {
10-
createProject(): IFuture<void>;
11-
buildProject(): IFuture<void>;
12-
}
13-
146
interface IProjectData {
157
projectDir: string;
8+
projectName: string;
169
platformsDir: string;
1710
projectFilePath: string;
1811
projectId?: string;
19-
projectName?: string;
2012
}
2113

2214
interface IProjectTemplatesService {
2315
defaultTemplatePath: IFuture<string>;
24-
installAndroidFramework(whereToInstall: string): IFuture<string>
16+
}
17+
18+
interface IPlatformProjectService {
19+
createProject(platform: string): IFuture<void>;
20+
buildProject(platform: string): IFuture<void>;
21+
}
22+
23+
interface IPlatformSpecificProjectService {
24+
validate(): void;
25+
checkRequirements(): IFuture<void>;
26+
createProject(projectRoot: string, frameworkDir: string): IFuture<void>;
27+
interpolateData(projectRoot: string): void;
28+
executePlatformSpecificAction(projectRoot: string, frameworkDir: string): void;
2529
}

lib/node-package-manager.ts

+31
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
import npm = require("npm");
44
import Future = require("fibers/future");
55
import shell = require("shelljs");
6+
import path = require("path");
67

78
export class NodePackageManager implements INodePackageManager {
9+
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
10+
11+
constructor(private $logger: ILogger,
12+
private $errors: IErrors) { }
13+
814
public get cache(): string {
915
return npm.cache;
1016
}
@@ -32,5 +38,30 @@ export class NodePackageManager implements INodePackageManager {
3238
});
3339
return future;
3440
}
41+
42+
public tryExecuteAction(action: any, args?: any[]): IFuture<void> {
43+
return (() => {
44+
try {
45+
this.load().wait(); // It's obligatory to execute load before whatever npm function
46+
action(args);
47+
} catch(error) {
48+
this.$logger.debug(error);
49+
this.$errors.fail(NodePackageManager.NPM_LOAD_FAILED);
50+
}
51+
}).future<void>()();
52+
}
53+
54+
public downloadNpmPackage(packageName: string, pathToSave?: string): IFuture<string> {
55+
return (() => {
56+
var action = (packageName: string) => {
57+
this.install(pathToSave || npm.cache, packageName).wait();
58+
};
59+
60+
this.tryExecuteAction(action, [packageName]).wait();
61+
62+
return path.join(pathToSave || npm.cache, "node_modules", packageName);
63+
64+
}).future<string>()();
65+
}
3566
}
3667
$injector.register("npm", NodePackageManager);

lib/services/platform-service.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,43 @@ import path = require("path");
44
import util = require("util");
55
import helpers = require("./../common/helpers");
66

7-
export class PlatformService implements IPlatformService {
8-
private platformCapabilities: { [key: string]: IPlatformCapabilities } = {
7+
class PlatformsData implements IPlatformsData {
8+
private platformsData: { [key: string]: IPlatformData } = {
99
ios: {
10+
frameworkPackageName: "tns-ios",
11+
platformProjectService: $injector.resolve("iOSProjectService"),
12+
projectRoot: "",
1013
targetedOS: ['darwin']
1114
},
1215
android: {
16+
frameworkPackageName: "tns-android",
17+
platformProjectService: $injector.resolve("androidProjectService"),
18+
projectRoot: ""
1319
}
1420
};
1521

16-
private platformNames = [];
22+
constructor($projectData: IProjectData) {
23+
this.platformsData["ios"].projectRoot = "";
24+
this.platformsData["android"].projectRoot = path.join($projectData.platformsDir, "android");
25+
}
26+
27+
public get platformsNames() {
28+
return Object.keys(this.platformsData);
29+
}
30+
31+
public getPlatformData(platform): IPlatformData {
32+
return this.platformsData[platform];
33+
}
34+
}
35+
$injector.register("platformsData", PlatformsData);
1736

37+
export class PlatformService implements IPlatformService {
1838
constructor(private $errors: IErrors,
1939
private $fs: IFileSystem,
2040
private $projectService: IProjectService,
21-
private $projectData: IProjectData) {
22-
this.platformNames = Object.keys(this.platformCapabilities);
23-
}
24-
25-
public getCapabilities(platform: string): IPlatformCapabilities {
26-
return this.platformCapabilities[platform];
41+
private $platformProjectService: IPlatformProjectService,
42+
private $projectData: IProjectData,
43+
private $platformsData: IPlatformsData) {
2744
}
2845

2946
public addPlatforms(platforms: string[]): IFuture<void> {
@@ -59,7 +76,7 @@ export class PlatformService implements IPlatformService {
5976
}
6077

6178
// Copy platform specific files in platforms dir
62-
this.$projectService.createPlatformSpecificProject(platform).wait();
79+
this.$platformProjectService.createProject(platform).wait();
6380

6481
}).future<void>()();
6582
}
@@ -71,14 +88,14 @@ export class PlatformService implements IPlatformService {
7188
}
7289

7390
var subDirs = this.$fs.readDirectory(this.$projectData.platformsDir).wait();
74-
return _.filter(subDirs, p => { return this.platformNames.indexOf(p) > -1; });
91+
return _.filter(subDirs, p => { return this.$platformsData.platformsNames.indexOf(p) > -1; });
7592
}).future<string[]>()();
7693
}
7794

7895
public getAvailablePlatforms(): IFuture<string[]> {
7996
return (() => {
8097
var installedPlatforms = this.getInstalledPlatforms().wait();
81-
return _.filter(this.platformNames, p => {
98+
return _.filter(this.$platformsData.platformsNames, p => {
8299
return installedPlatforms.indexOf(p) < 0 && this.isPlatformSupportedForOS(p); // Only those not already installed
83100
});
84101
}).future<string[]>()();
@@ -96,7 +113,7 @@ export class PlatformService implements IPlatformService {
96113
this.validatePlatform(platform);
97114
var normalizedPlatformName = this.normalizePlatformName(platform);
98115

99-
this.$projectService.prepareProject(normalizedPlatformName, this.platformNames).wait();
116+
//this.$projectService.prepareProject(normalizedPlatformName, this.platformNames).wait();
100117
}).future<void>()();
101118
}
102119

@@ -105,13 +122,13 @@ export class PlatformService implements IPlatformService {
105122
platform = platform.toLocaleLowerCase();
106123
this.validatePlatform(platform);
107124

108-
this.$projectService.buildProject(platform).wait();
125+
this.$platformProjectService.buildProject(platform).wait();
109126
}).future<void>()();
110127
}
111128

112129
private validatePlatform(platform: string): void {
113130
if (!this.isValidPlatform(platform)) {
114-
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(this.platformNames));
131+
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(this.$platformsData.platformsNames));
115132
}
116133

117134
if (!this.isPlatformSupportedForOS(platform)) {
@@ -120,12 +137,11 @@ export class PlatformService implements IPlatformService {
120137
}
121138

122139
private isValidPlatform(platform: string) {
123-
return this.platformCapabilities[platform];
140+
return this.$platformsData.getPlatformData(platform);
124141
}
125142

126143
private isPlatformSupportedForOS(platform: string): boolean {
127-
var platformCapabilities = this.getCapabilities(platform);
128-
var targetedOS = platformCapabilities.targetedOS;
144+
var targetedOS = this.$platformsData.getPlatformData(platform).targetedOS;
129145

130146
if(!targetedOS || targetedOS.indexOf("*") >= 0 || targetedOS.indexOf(process.platform) >= 0) {
131147
return true;
@@ -145,4 +161,4 @@ export class PlatformService implements IPlatformService {
145161
return undefined;
146162
}
147163
}
148-
$injector.register("platformService", PlatformService);
164+
$injector.register("platformService", PlatformService);

0 commit comments

Comments
 (0)