Skip to content

Commit

Permalink
Enable image resources from App_Resources in iOS
Browse files Browse the repository at this point in the history
Fixes #520
  • Loading branch information
Fatme Havaluova authored and Fatme Havaluova committed Jun 24, 2015
1 parent 3061fcd commit 653c310
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 11 deletions.
Empty file modified bin/nativescript.js
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface IPlatformProjectService {
interpolateData(projectRoot: string): IFuture<void>;
afterCreateProject(projectRoot: string): IFuture<void>;
buildProject(projectRoot: string): IFuture<void>;
prepareProject(): IFuture<void>;
isPlatformPrepared(projectRoot: string): IFuture<boolean>;
addLibrary(platformData: IPlatformData, libraryPath: string): IFuture<void>;
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean>;
Expand Down
4 changes: 4 additions & 0 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ class AndroidProjectService implements IPlatformProjectService {
public getFrameworkFilesExtensions(): string[] {
return [".jar", ".dat"];
}

public prepareProject(): IFuture<void> {
return (() => { }).future<void>()();
}

private copy(projectRoot: string, frameworkDir: string, files: string, cpArg: string): IFuture<void> {
return (() => {
Expand Down
52 changes: 47 additions & 5 deletions lib/services/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,11 @@ class IOSProjectService implements IPlatformProjectService {
this.$fs.ensureDirectoryExists(targetPath).wait();
shell.cp("-R", libraryPath, targetPath);

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

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


var pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
this.replaceFileContent(pbxprojFilePath).wait();
}).future<void>()();
}

public prepareProject(): IFuture<void> {
return (() => {
let project = this.pbxProj;
let resources = project.pbxGroupByName("Resources");

if(resources) {
let references = project.pbxFileReferenceSection();

let xcodeProjectImages = _.map(<any[]>resources.children, resource => references[resource.value].name.replace(/\"/g, ""));
this.$logger.trace("Images from Xcode project");
this.$logger.trace(xcodeProjectImages);

let appResourcesImages = this.$fs.readDirectory(this.platformData.appResourcesDestinationDirectoryPath).wait();
this.$logger.trace("Current images from App_Resources");
this.$logger.trace(appResourcesImages);

let imagesToAdd = _.difference(appResourcesImages, xcodeProjectImages);
this.$logger.trace(`New images to add into xcode project: ${imagesToAdd.join(", ")}`);
_.each(imagesToAdd, image => project.addResourceFile(path.join(this.platformData.appResourcesDestinationDirectoryPath, image)));

let imagesToRemove = _.difference(xcodeProjectImages, appResourcesImages);
this.$logger.trace(`Images to remove from xcode project: ${imagesToRemove.join(", ")}`);
_.each(imagesToRemove, image => project.removeResourceFile(path.join(this.platformData.appResourcesDestinationDirectoryPath, image)));

this.savePbxProj(project).wait();
}

}).future<void>()();
}

private get pbxProjPath(): string {
return path.join(this.platformData.projectRoot, this.$projectData.projectName + ".xcodeproj", "project.pbxproj");
}

private get pbxProj(): any {
let project = new xcode.project(this.pbxProjPath);
project.parseSync();

return project;
}

private savePbxProj(project: any): IFuture<void> {
return this.$fs.writeFile(this.pbxProjPath, project.writeSync());
}

private buildPathToXcodeProjectFile(version: string): string {
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");
Expand Down
5 changes: 4 additions & 1 deletion lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ export class PlatformService implements IPlatformService {
this.$fs.ensureDirectoryExists(platformData.appResourcesDestinationDirectoryPath).wait(); // Should be deleted
var appResourcesDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
if (this.$fs.exists(appResourcesDirectoryPath).wait()) {
shell.cp("-Rf", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "*"), platformData.appResourcesDestinationDirectoryPath);
this.$fs.deleteDirectory(platformData.appResourcesDestinationDirectoryPath).wait(); // Respect removed files
shell.cp("-R", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "*"), platformData.appResourcesDestinationDirectoryPath);
this.$fs.deleteDirectory(appResourcesDirectoryPath).wait();
}

Expand All @@ -178,6 +179,8 @@ export class PlatformService implements IPlatformService {
}
});
this.processPlatformSpecificFiles(platform, files).wait();

platformData.platformProjectService.prepareProject().wait();

// Process node_modules folder
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
Expand Down
6 changes: 5 additions & 1 deletion test/npm-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import BroccoliBuilderLib = require("../lib/tools/broccoli/builder");
import NodeModulesTreeLib = require("../lib/tools/broccoli/trees/node-modules-tree");
import PluginsServiceLib = require("../lib/services/plugins-service");
import ChildProcessLib = require("../lib/common/child-process");
import Future = require("fibers/future");

import path = require("path");
import temp = require("temp");
Expand Down Expand Up @@ -118,7 +119,10 @@ describe("Npm support tests", () => {
appDestinationDirectoryPath: appDestinationFolderPath,
appResourcesDestinationDirectoryPath: path.join(appDestinationFolderPath, "app", "App_Resources"),
frameworkPackageName: "tns-android",
normalizedPlatformName: "Android"
normalizedPlatformName: "Android",
platformProjectService: {
prepareProject: () => Future.fromResult()
}
}
};

Expand Down
10 changes: 8 additions & 2 deletions test/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ describe('Platform Service Tests', () => {
return {
appDestinationDirectoryPath: appDestFolderPath,
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
normalizedPlatformName: "iOS"
normalizedPlatformName: "iOS",
platformProjectService: {
prepareProject: () => Future.fromResult()
}
}
};

Expand Down Expand Up @@ -274,7 +277,10 @@ describe('Platform Service Tests', () => {
return {
appDestinationDirectoryPath: appDestFolderPath,
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
normalizedPlatformName: "Android"
normalizedPlatformName: "Android",
platformProjectService: {
prepareProject: () => Future.fromResult()
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ export class PlatformProjectServiceStub implements IPlatformProjectService {
afterCreateProject(projectRoot: string): IFuture<void> {
return Future.fromResult();
}
prepareProject(platformData: IPlatformData): IFuture<string> {
return Future.fromResult("");
prepareProject(): IFuture<void> {
return Future.fromResult();
}
buildProject(projectRoot: string): IFuture<void> {
return Future.fromResult();
Expand Down

0 comments on commit 653c310

Please sign in to comment.