Skip to content

Commit eb484c1

Browse files
Fatme HavaluovaFatme Havaluova
Fatme Havaluova
authored and
Fatme Havaluova
committed
Enable image resources from App_Resources in iOS
Fixes #520
1 parent 3061fcd commit eb484c1

8 files changed

+69
-11
lines changed

bin/nativescript.js

100644100755
File mode changed.

lib/definitions/project.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface IPlatformProjectService {
2727
interpolateData(projectRoot: string): IFuture<void>;
2828
afterCreateProject(projectRoot: string): IFuture<void>;
2929
buildProject(projectRoot: string): IFuture<void>;
30+
prepareProject(): IFuture<void>;
3031
isPlatformPrepared(projectRoot: string): IFuture<boolean>;
3132
addLibrary(platformData: IPlatformData, libraryPath: string): IFuture<void>;
3233
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean>;

lib/services/android-project-service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ class AndroidProjectService implements IPlatformProjectService {
273273
public getFrameworkFilesExtensions(): string[] {
274274
return [".jar", ".dat"];
275275
}
276+
277+
public prepareProject(): IFuture<void> {
278+
return (() => { }).future<void>()();
279+
}
276280

277281
private copy(projectRoot: string, frameworkDir: string, files: string, cpArg: string): IFuture<void> {
278282
return (() => {

lib/services/ios-project-service.ts

+45-5
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,11 @@ class IOSProjectService implements IPlatformProjectService {
182182
this.$fs.ensureDirectoryExists(targetPath).wait();
183183
shell.cp("-R", libraryPath, targetPath);
184184

185-
var pbxProjPath = path.join(platformData.projectRoot, this.$projectData.projectName + ".xcodeproj", "project.pbxproj");
186-
var project = new xcode.project(pbxProjPath);
187-
project.parseSync();
185+
let project = this.xcodeproj;
188186

189187
project.addFramework(path.join(targetPath, frameworkName + ".framework"), { customFramework: true, embed: true });
190188
project.updateBuildProperty("IPHONEOS_DEPLOYMENT_TARGET", "8.0");
191-
this.$fs.writeFile(pbxProjPath, project.writeSync()).wait();
189+
this.saveXcodeproj(project).wait();
192190
this.$logger.info("The iOS Deployment Target is now 8.0 in order to support Cocoa Touch Frameworks.");
193191
}).future<void>()();
194192
}
@@ -221,11 +219,53 @@ class IOSProjectService implements IPlatformProjectService {
221219
shell.cp("-R", path.join(cachedPackagePath, "*"), path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName)));
222220
this.$logger.info("Copied from %s at %s.", cachedPackagePath, this.platformData.projectRoot);
223221

224-
225222
var pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
226223
this.replaceFileContent(pbxprojFilePath).wait();
227224
}).future<void>()();
228225
}
226+
227+
public prepareProject(): IFuture<void> {
228+
return (() => {
229+
let project = this.xcodeproj;
230+
let resources = project.pbxGroupByName("Resources");
231+
let references = project.pbxFileReferenceSection();
232+
233+
let images = _(<any[]>resources.children)
234+
.map(resource => references[resource.value])
235+
.value();
236+
237+
let imageNames = _.map(images, image => image.name.replace(/\"/g, ""));
238+
let currentImageNames = this.$fs.readDirectory(this.platformData.appResourcesDestinationDirectoryPath).wait();
239+
240+
if(imageNames.length !== currentImageNames.length) {
241+
let imagesToAdd = _.difference(currentImageNames, imageNames);
242+
this.$logger.trace(`New images to add into xcode project: ${ imagesToAdd.length !== 0 ? imagesToAdd.join(", ") : "" }`);
243+
_.each(imagesToAdd, image => project.addResourceFile(path.join(this.platformData.appResourcesDestinationDirectoryPath, image)));
244+
245+
let imagesToRemove = _.difference(imageNames, currentImageNames);
246+
this.$logger.trace(`Images to remove from xcode project: ${ imagesToRemove.length !== 0 ? imagesToRemove.join(", ") : "" }`);
247+
_.each(imagesToRemove, image => project.removeResourceFile(path.join(this.platformData.appResourcesDestinationDirectoryPath, image)));
248+
249+
this.saveXcodeproj(project).wait();
250+
}
251+
252+
}).future<void>()();
253+
}
254+
255+
private get pbxProjPath(): string {
256+
return path.join(this.platformData.projectRoot, this.$projectData.projectName + ".xcodeproj", "project.pbxproj");
257+
}
258+
259+
private get xcodeproj(): any {
260+
let project = new xcode.project(this.pbxProjPath);
261+
project.parseSync();
262+
263+
return project;
264+
}
265+
266+
private saveXcodeproj(project: any): IFuture<void> {
267+
return this.$fs.writeFile(this.pbxProjPath, project.writeSync());
268+
}
229269

230270
private buildPathToXcodeProjectFile(version: string): string {
231271
return path.join(this.$npmInstallationManager.getCachedPackagePath(this.platformData.frameworkPackageName, version), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER), "project.pbxproj");

lib/services/platform-service.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ export class PlatformService implements IPlatformService {
160160
this.$fs.ensureDirectoryExists(platformData.appResourcesDestinationDirectoryPath).wait(); // Should be deleted
161161
var appResourcesDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
162162
if (this.$fs.exists(appResourcesDirectoryPath).wait()) {
163-
shell.cp("-Rf", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "*"), platformData.appResourcesDestinationDirectoryPath);
163+
this.$fs.deleteDirectory(platformData.appResourcesDestinationDirectoryPath).wait(); // Respect removed files
164+
shell.cp("-R", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "*"), platformData.appResourcesDestinationDirectoryPath);
164165
this.$fs.deleteDirectory(appResourcesDirectoryPath).wait();
165166
}
166167

@@ -178,6 +179,8 @@ export class PlatformService implements IPlatformService {
178179
}
179180
});
180181
this.processPlatformSpecificFiles(platform, files).wait();
182+
183+
platformData.platformProjectService.prepareProject().wait();
181184

182185
// Process node_modules folder
183186
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();

test/npm-support.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import BroccoliBuilderLib = require("../lib/tools/broccoli/builder");
1919
import NodeModulesTreeLib = require("../lib/tools/broccoli/trees/node-modules-tree");
2020
import PluginsServiceLib = require("../lib/services/plugins-service");
2121
import ChildProcessLib = require("../lib/common/child-process");
22+
import Future = require("fibers/future");
2223

2324
import path = require("path");
2425
import temp = require("temp");
@@ -118,7 +119,10 @@ describe("Npm support tests", () => {
118119
appDestinationDirectoryPath: appDestinationFolderPath,
119120
appResourcesDestinationDirectoryPath: path.join(appDestinationFolderPath, "app", "App_Resources"),
120121
frameworkPackageName: "tns-android",
121-
normalizedPlatformName: "Android"
122+
normalizedPlatformName: "Android",
123+
platformProjectService: {
124+
prepareProject: () => Future.fromResult()
125+
}
122126
}
123127
};
124128

test/platform-service.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ describe('Platform Service Tests', () => {
223223
return {
224224
appDestinationDirectoryPath: appDestFolderPath,
225225
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
226-
normalizedPlatformName: "iOS"
226+
normalizedPlatformName: "iOS",
227+
platformProjectService: {
228+
prepareProject: () => Future.fromResult()
229+
}
227230
}
228231
};
229232

@@ -274,7 +277,10 @@ describe('Platform Service Tests', () => {
274277
return {
275278
appDestinationDirectoryPath: appDestFolderPath,
276279
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
277-
normalizedPlatformName: "Android"
280+
normalizedPlatformName: "Android",
281+
platformProjectService: {
282+
prepareProject: () => Future.fromResult()
283+
}
278284
}
279285
};
280286

test/stubs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ export class PlatformProjectServiceStub implements IPlatformProjectService {
276276
afterCreateProject(projectRoot: string): IFuture<void> {
277277
return Future.fromResult();
278278
}
279-
prepareProject(platformData: IPlatformData): IFuture<string> {
280-
return Future.fromResult("");
279+
prepareProject(): IFuture<void> {
280+
return Future.fromResult();
281281
}
282282
buildProject(projectRoot: string): IFuture<void> {
283283
return Future.fromResult();

0 commit comments

Comments
 (0)