Skip to content

Commit 6b9dc0f

Browse files
committed
Fixes for prepare command
1 parent 9bb0c15 commit 6b9dc0f

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

lib/definitions/project.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interface IProjectService {
22
createProject(projectName: string, projectId: string): IFuture<void>;
33
createPlatformSpecificProject(platform: string): IFuture<void>;
4-
prepareProject(platform: string, platforms: string[]): IFuture<void>;
4+
prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void>;
55
buildProject(platform: string): IFuture<void>;
66
ensureProject(): void;
77
projectData: IProjectData;

lib/services/platform-service.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ export class PlatformService implements IPlatformService {
9090

9191
public preparePlatform(platform: string): IFuture<void> {
9292
return (() => {
93+
platform = platform.toLowerCase();
9394
this.validatePlatform(platform);
95+
var normalizedPlatformName = this.normalizePlatformName(platform);
9496

95-
this.$projectService.prepareProject(platform, this.platformNames).wait();
97+
this.$projectService.prepareProject(normalizedPlatformName, this.platformNames).wait();
9698
}).future<void>()();
9799
}
98100

99101
public buildPlatform(platform: string): IFuture<void> {
100102
return (() => {
103+
platform = platform.toLocaleLowerCase();
101104
this.validatePlatform(platform);
102105

103106
this.$projectService.buildProject(platform).wait();
@@ -128,5 +131,16 @@ export class PlatformService implements IPlatformService {
128131

129132
return false;
130133
}
134+
135+
private normalizePlatformName(platform: string): string {
136+
switch(platform) {
137+
case "android":
138+
return "Android";
139+
case "ios":
140+
return "iOS";
141+
}
142+
143+
return undefined;
144+
}
131145
}
132146
$injector.register("platformService", PlatformService);

lib/services/project-service.ts

+22-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class ProjectService implements IProjectService {
1111
private static DEFAULT_PROJECT_ID = "com.telerik.tns.HelloWorld";
1212
private static DEFAULT_PROJECT_NAME = "HelloNativescript";
1313
public static APP_FOLDER_NAME = "app";
14+
private static APP_RESOURCES_FOLDER_NAME = "App_Resources";
1415
public static PROJECT_FRAMEWORK_DIR = "framework";
1516

1617
public projectData: IProjectData = null;
@@ -130,32 +131,41 @@ export class ProjectService implements IProjectService {
130131
}).future<void>()();
131132
}
132133

133-
public prepareProject(platform: string, platforms: string[]): IFuture<void> {
134+
public prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void> {
134135
return (() => {
136+
var platform = normalizedPlatformName.toLowerCase();
135137
var assetsDirectoryPath = path.join(this.projectData.platformsDir, platform, "assets");
136-
shell.cp("-r",path.join(this.projectData.projectDir, ProjectService.APP_FOLDER_NAME), assetsDirectoryPath);
138+
var appResourcesDirectoryPath = path.join(assetsDirectoryPath, ProjectService.APP_FOLDER_NAME, ProjectService.APP_RESOURCES_FOLDER_NAME);
139+
shell.cp("-r", path.join(this.projectData.projectDir, ProjectService.APP_FOLDER_NAME), assetsDirectoryPath);
140+
141+
if(this.$fs.exists(appResourcesDirectoryPath).wait()) {
142+
shell.cp("-r", path.join(appResourcesDirectoryPath, normalizedPlatformName, "*"), path.join(this.projectData.platformsDir, platform, "res"));
143+
this.$fs.deleteDirectory(appResourcesDirectoryPath).wait();
144+
}
137145

138146
var files = helpers.enumerateFilesInDirectorySync(path.join(assetsDirectoryPath, ProjectService.APP_FOLDER_NAME));
139-
var pattern = util.format("%s%s%s", path.sep, ProjectService.APP_FOLDER_NAME, path.sep);
147+
var platformsAsString = platforms.join("|");
148+
140149
_.each(files, fileName => {
141-
if(ProjectService.shouldExcludeFile(platform, platforms, fileName.split(pattern)[1])) {
150+
var platformInfo = ProjectService.parsePlatformSpecificFileName(path.basename(fileName), platformsAsString);
151+
var shouldExcludeFile = platformInfo && platformInfo.platform !== platform;
152+
if(shouldExcludeFile) {
142153
this.$fs.deleteFile(fileName).wait();
154+
} else if(platformInfo && platformInfo.onDeviceName) {
155+
this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait();
143156
}
144157
});
145-
}).future<void>()();
146-
}
147158

148-
private static shouldExcludeFile(platform: string, platforms: string[], fileName: string): boolean {
149-
var platformInfo = ProjectService.parsePlatformSpecificFileName(fileName, platforms);
150-
return platformInfo && platformInfo.platform !== platform;
159+
}).future<void>()();
151160
}
152161

153-
private static parsePlatformSpecificFileName(fileName: string, platforms: string[]): any {
154-
var regex = util.format("^(.+?)\.(%s)(\..+?)$", platforms.join("|"));
162+
private static parsePlatformSpecificFileName(fileName: string, platforms: string): any {
163+
var regex = util.format("^(.+?)\.(%s)(\..+?)$", platforms);
155164
var parsed = fileName.toLowerCase().match(new RegExp(regex, "i"));
156165
if (parsed) {
157166
return {
158-
platform: parsed[2]
167+
platform: parsed[2],
168+
onDeviceName: parsed[1] + parsed[3]
159169
};
160170
}
161171
return undefined;

0 commit comments

Comments
 (0)