Skip to content

Kddimitrov/fix add framework #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 71 additions & 20 deletions lib/pbxProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,32 +338,43 @@ pbxProject.prototype.addFramework = function(fpath, opt) {
file.fileRef = this.generateUuid();
file.target = opt ? opt.target : undefined;

if (this.hasFile(file.path)) return false;

this.addToPbxBuildFileSection(file); // PBXBuildFile
this.addToPbxFileReferenceSection(file); // PBXFileReference
this.addToFrameworksPbxGroup(file); // PBXGroup
var fileReference = this.hasFile(file.path);
if (fileReference) {
var key = this.getFileKey(file.path);
file.fileRef = key;
} else {
this.addToPbxFileReferenceSection(file); // PBXFileReference
this.addToFrameworksPbxGroup(file); // PBXGroup
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need this as well

this.addToPbxBuildFileSection(file);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to do that if we won't add it to the FrameworksBuildPhase.

}

if (link) {
this.addToPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase
const buildFileUuid = this.addToPbxFrameworksBuildPhase(file);
if(buildFileUuid === file.uuid) { // PBXFrameworksBuildPhase)
this.addToPbxBuildFileSection(file); // PBXBuildFile
} else {
file.uuid = buildFileUuid;
}
}

if (customFramework) {
this.addToFrameworkSearchPaths(file);

if (embed) {
opt.embed = embed;
var embeddedFile = new pbxFile(fpath, opt);

embeddedFile.uuid = this.generateUuid();
embeddedFile.fileRef = file.fileRef;

//keeping a separate PBXBuildFile entry for Embed Frameworks
this.addToPbxBuildFileSection(embeddedFile); // PBXBuildFile

this.addToPbxEmbedFrameworksBuildPhase(embeddedFile); // PBXCopyFilesBuildPhase
opt.embed = embed;
var embeddedFile = new pbxFile(fpath, opt);

embeddedFile.uuid = this.generateUuid();
embeddedFile.fileRef = file.fileRef;
embeddedFile.target = file.target;
const embedBuildFileUuid = this.addToPbxEmbedFrameworksBuildPhase(embeddedFile);
if(embedBuildFileUuid === embeddedFile.uuid) { // PBXCopyFilesBuildPhase
//keeping a separate PBXBuildFile entry for Embed Frameworks
this.addToPbxBuildFileSection(embeddedFile); // PBXBuildFile
} else {
embeddedFile.uuid = embedBuildFileUuid;
}

return embeddedFile;
return embeddedFile;
}
}

Expand Down Expand Up @@ -856,10 +867,26 @@ pbxProject.prototype.removeFromFrameworksPbxGroup = function(file) {
}
}

function getReferenceInPbxBuildFile(buildFileReferences, fileReference) {
var buildFileSection = this.pbxBuildFileSection();
for(let buildFileReference of buildFileReferences) {
if(buildFileSection[buildFileReference.value] && buildFileSection[buildFileReference.value].fileRef === fileReference.fileRef){
return buildFileReference.value;
}
}
}

pbxProject.prototype.addToPbxEmbedFrameworksBuildPhase = function (file) {
var sources = this.pbxEmbedFrameworksBuildPhaseObj(file.target);

if (sources) {
var referenceUuid = getReferenceInPbxBuildFile.call(this, sources.files, file)
if(referenceUuid){
return referenceUuid;
}

sources.files.push(pbxBuildPhaseObj(file));
return file.uuid;
}
}

Expand Down Expand Up @@ -933,7 +960,16 @@ pbxProject.prototype.removeFromPbxResourcesBuildPhase = function(file) {

pbxProject.prototype.addToPbxFrameworksBuildPhase = function(file) {
var sources = this.pbxFrameworksBuildPhaseObj(file.target);
sources.files.push(pbxBuildPhaseObj(file));

if (sources) {
var frameworkBuildUuid = getReferenceInPbxBuildFile.call(this, sources.files, file);
if (frameworkBuildUuid) {
return frameworkBuildUuid;
}

sources.files.push(pbxBuildPhaseObj(file));
return file.uuid;
}
}

pbxProject.prototype.removeFromPbxFrameworksBuildPhase = function(file) {
Expand Down Expand Up @@ -1353,8 +1389,10 @@ pbxProject.prototype.addToFrameworkSearchPaths = function(file) {
|| buildSettings['FRAMEWORK_SEARCH_PATHS'] === INHERITED) {
buildSettings['FRAMEWORK_SEARCH_PATHS'] = [INHERITED];
}

buildSettings['FRAMEWORK_SEARCH_PATHS'].push(searchPathForFile(file, this));
var searchPath = searchPathForFile(file, this);
if(buildSettings['FRAMEWORK_SEARCH_PATHS'].indexOf(searchPath) < 0){
buildSettings['FRAMEWORK_SEARCH_PATHS'].push(searchPath);
}
}
}

Expand Down Expand Up @@ -1565,6 +1603,19 @@ pbxProject.prototype.hasFile = function(filePath) {
return false;
}

pbxProject.prototype.getFileKey = function(filePath) {
var files = nonComments(this.pbxFileReferenceSection()),
file, id;
for (id in files) {
file = files[id];
if (file.path == filePath || file.path == ('"' + filePath + '"')) {
return id;
}
}

return false;
}

pbxProject.prototype.addTarget = function(name, type, subfolder) {

// Setup uuid and name of new target
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "nativescript-dev-xcode",
"description": "parser for xcodeproj/project.pbxproj files",
"main": "index.js",
"version": "0.1.0",
"version": "0.2.0",
"repository": {
"url": "https://github.com/NativeScript/nativescript-dev-xcode.git"
},
Expand Down
14 changes: 12 additions & 2 deletions test/addFramework.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,20 @@ exports.addFramework = {
test.done();
},
'duplicate entries': {
'should return false': function (test) {
'should return same build file': function (test) {
var newFile = proj.addFramework('libsqlite3.dylib');
var sameFile = proj.addFramework('libsqlite3.dylib');

test.ok(!proj.addFramework('libsqlite3.dylib'));
test.equal(newFile.uuid, sameFile.uuid);
test.equal(newFile.fileRef, sameFile.fileRef);
test.done();
},
'should return different build file with same ref for different target': function (test) {
var newFile = proj.addFramework('libsqlite3.dylib');
var differentFile = proj.addFramework('libsqlite3.dylib', { target: "1D6058900D05DD3D006BFB54"});

test.notEqual(newFile.uuid, differentFile.uuid);
test.equal(newFile.fileRef, differentFile.fileRef);
test.done();
}
},
Expand Down
12 changes: 10 additions & 2 deletions test/fixtures/full-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,15 @@
],
"runOnlyForDeploymentPostprocessing": 0
},
"1D60588F0D05DD3D006BFB54_comment": "Frameworks"
"1D60588F0D05DD3D006BFB54_comment": "Frameworks",
"2D60588F0D05DD3D006BFB55": {
"isa": "PBXFrameworksBuildPhase",
"buildActionMask": 2147483647,
"files": [
],
"runOnlyForDeploymentPostprocessing": 0
},
"2D60588F0D05DD3D006BFB55_comment": "Frameworks"
},
"PBXGroup": {
"080E96DDFE201D6D7F000001": {
Expand Down Expand Up @@ -866,7 +874,7 @@
"comment": "Sources"
},
{
"value": "1D60588F0D05DD3D006BFB54",
"value": "2D60588F0D05DD3D006BFB55",
"comment": "Frameworks"
}
],
Expand Down