Skip to content

Commit 6cf93fd

Browse files
committed
Exclude platform specific files when prepare command is executed
1 parent 3481c75 commit 6cf93fd

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

lib/definitions/project.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
interface IProjectService {
22
createProject(projectName: string, projectId: string): IFuture<void>;
33
createPlatformSpecificProject(platform: string): IFuture<void>;
4-
prepareProject(platform: string): IFuture<void>;
4+
prepareProject(platform: string, platforms: string[]): IFuture<void>;
55
buildProject(platform: string): IFuture<void>;
66
ensureProject(): void;
77
projectData: IProjectData;
88
}
99

1010
interface IAndroidProjectService {
1111
createProject(projectData: IProjectData): IFuture<void>;
12-
prepareProject(projectData: IProjectData): IFuture<void>;
1312
buildProject(projectData: IProjectData): IFuture<void>;
1413
}
1514

lib/services/platform-service.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
///<reference path="../.d.ts"/>
22

33
import path = require("path");
4+
import util = require("util");
45
import helpers = require("./../common/helpers");
56

67
export class PlatformService implements IPlatformService {
7-
constructor(private $errors: IErrors,
8-
private $fs: IFileSystem,
9-
private $projectService: IProjectService) { }
10-
118
private platformCapabilities: { [key: string]: IPlatformCapabilities } = {
129
ios: {
1310
targetedOS: ['darwin']
@@ -16,6 +13,14 @@ export class PlatformService implements IPlatformService {
1613
}
1714
};
1815

16+
private platformNames = [];
17+
18+
constructor(private $errors: IErrors,
19+
private $fs: IFileSystem,
20+
private $projectService: IProjectService) {
21+
this.platformNames = Object.keys(this.platformCapabilities);
22+
}
23+
1924
public getCapabilities(platform: string): IPlatformCapabilities {
2025
return this.platformCapabilities[platform];
2126
}
@@ -66,14 +71,14 @@ export class PlatformService implements IPlatformService {
6671
}
6772

6873
var subDirs = this.$fs.readDirectory(this.$projectService.projectData.platformsDir).wait();
69-
return _.filter(subDirs, p => { return Object.keys(this.platformCapabilities).indexOf(p) > -1; });
74+
return _.filter(subDirs, p => { return this.platformNames.indexOf(p) > -1; });
7075
}).future<string[]>()();
7176
}
7277

7378
public getAvailablePlatforms(): IFuture<string[]> {
7479
return (() => {
7580
var installedPlatforms = this.getInstalledPlatforms().wait();
76-
return _.filter(_.keys(this.platformCapabilities), p => {
81+
return _.filter(this.platformNames, p => {
7782
return installedPlatforms.indexOf(p) < 0 && this.isPlatformSupportedForOS(p); // Only those not already installed
7883
});
7984
}).future<string[]>()();
@@ -89,7 +94,7 @@ export class PlatformService implements IPlatformService {
8994
return (() => {
9095
this.validatePlatform(platform);
9196

92-
this.$projectService.prepareProject(platform).wait();
97+
this.$projectService.prepareProject(platform, this.platformNames).wait();
9398
}).future<void>()();
9499
}
95100

@@ -101,7 +106,7 @@ export class PlatformService implements IPlatformService {
101106

102107
private validatePlatform(platform): void {
103108
if (!this.isValidPlatform(platform)) {
104-
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(_.keys(this.platformCapabilities)));
109+
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(this.platformNames));
105110
}
106111

107112
if (!this.isPlatformSupportedForOS(platform)) {

lib/services/project-service.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path = require("path");
44
import options = require("./../options");
55
import shell = require("shelljs");
66
import osenv = require("osenv");
7+
import util = require("util");
8+
import helpers = require("./../common/helpers");
79

810
export class ProjectService implements IProjectService {
911
private static DEFAULT_PROJECT_ID = "com.telerik.tns.HelloWorld";
@@ -149,12 +151,37 @@ export class ProjectService implements IProjectService {
149151
}).future<void>()();
150152
}
151153

152-
public prepareProject(platform: string): IFuture<void> {
154+
public prepareProject(platform: string, platforms: string[]): IFuture<void> {
153155
return (() => {
154-
this.executePlatformSpecificAction(platform, "prepareProject").wait();
156+
var assetsDirectoryPath = path.join(this.projectData.platformsDir, platform, "assets");
157+
shell.cp("-r",path.join(this.projectData.projectDir, ProjectService.APP_FOLDER_NAME), assetsDirectoryPath);
158+
159+
var files = helpers.enumerateFilesInDirectorySync(path.join(assetsDirectoryPath, ProjectService.APP_FOLDER_NAME));
160+
var pattern = util.format("%s%s%s", path.sep, ProjectService.APP_FOLDER_NAME ,path.sep);
161+
_.each(files, fileName => {
162+
if(ProjectService.shouldExcludeFile(platform, platforms, fileName.split(pattern)[1])) {
163+
this.$fs.deleteFile(fileName).wait();
164+
}
165+
});
155166
}).future<void>()();
156167
}
157168

169+
private static shouldExcludeFile(platform: string, platforms: string[], fileName: string): boolean {
170+
var platformInfo = ProjectService.parsePlatformSpecificFileName(fileName, platforms);
171+
return platformInfo && platformInfo.platform !== platform;
172+
}
173+
174+
private static parsePlatformSpecificFileName(fileName: string, platforms: string[]): any {
175+
var regex = util.format("^(.+?)\.(%s)(\..+?)$", platforms.join("|"));
176+
var parsed = fileName.toLowerCase().match(new RegExp(regex, "i"));
177+
if (parsed) {
178+
return {
179+
platform: parsed[2]
180+
};
181+
}
182+
return undefined;
183+
}
184+
158185
public buildProject(platform: string): IFuture<void> {
159186
return (() => {
160187
this.executePlatformSpecificAction(platform, "buildProject").wait();
@@ -259,14 +286,6 @@ class AndroidProjectService implements IAndroidProjectService {
259286
}).future<any>()();
260287
}
261288

262-
public prepareProject(projectData: IProjectData): IFuture<void> {
263-
return (() => {
264-
var projectDir = path.join(projectData.projectDir, "platforms", "android");
265-
// Copy app into assets
266-
shell.cp("-r", path.join(projectData.projectDir, ProjectService.APP_FOLDER_NAME), path.join(projectDir, "assets"));
267-
}).future<void>()();
268-
}
269-
270289
public buildProject(projectData: IProjectData): IFuture<void> {
271290
return (() => {
272291

0 commit comments

Comments
 (0)