Skip to content
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

new functions for adding folders recursively #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions lib/pbxFile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var path = require('path'),
M_EXTENSION = /[.]m$/, SOURCE_FILE = 'sourcecode.c.objc',
C_EXTENSION = /[.]c$/, C_FILE = 'sourcecode.c.c',
CPP_EXTENSION = /[.]cpp$/, CPP_FILE = 'sourcecode.cpp.cpp',
ASM_EXTENSION = /[.]s$/, ASM_FILE = 'sourcecode.asm',
H_EXTENSION = /[.]h$/, HEADER_FILE = 'sourcecode.c.h',
BUNDLE_EXTENSION = /[.]bundle$/, BUNDLE = '"wrapper.plug-in"',
XIB_EXTENSION = /[.]xib$/, XIB_FILE = 'file.xib',
Expand All @@ -12,6 +15,15 @@ var path = require('path'),
function detectLastType(path) {
if (M_EXTENSION.test(path))
return SOURCE_FILE;

if (C_EXTENSION.test(path))
return C_FILE;

if (CPP_EXTENSION.test(path))
return CPP_FILE;

if (ASM_EXTENSION.test(path))
return ASM_FILE;

if (H_EXTENSION.test(path))
return HEADER_FILE;
Expand Down
131 changes: 129 additions & 2 deletions lib/pbxProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var util = require('util'),
function pbxProject(filename) {
if (!(this instanceof pbxProject))
return new pbxProject(filename);

this.filepath = path.resolve(filename)
this.filepath = path.resolve(filename);
}

util.inherits(pbxProject, EventEmitter)
Expand Down Expand Up @@ -108,6 +108,116 @@ pbxProject.prototype.removePluginFile = function (path, opt) {
return file;
}



pbxProject.prototype.addHeaderPath = function (path) {
var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
INHERITED = '"$(inherited)"',
config, buildSettings, searchPaths;

for (config in configurations) {
buildSettings = configurations[config].buildSettings;

if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
continue;

if (!buildSettings['HEADER_SEARCH_PATHS']) {
buildSettings['HEADER_SEARCH_PATHS'] = [INHERITED];
}

buildSettings['HEADER_SEARCH_PATHS'].push('"'+path+'"');
}
}

pbxProject.prototype.addLibraryPath = function (path) {
var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
config, buildSettings, searchPaths;

for (config in configurations) {
buildSettings = configurations[config].buildSettings;

if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
continue;

if (!buildSettings['LIBRARY_SEARCH_PATHS']) {
buildSettings['LIBRARY_SEARCH_PATHS'] = [];
}

buildSettings['LIBRARY_SEARCH_PATHS'].push('"'+path+'"');
}
}

// relative paths are considered from source root
pbxProject.prototype.addFolder = function (dir) {
// solve paths
var pathToFolder = dir;
var pathToGroup = path.dirname(this.filepath);
pathToGroup = pathToGroup.substr(0, pathToGroup.lastIndexOf('.'))+"/Classes";
if (path.resolve(dir) !== dir) pathToFolder = path.join(pathToGroup, "../../", pathToFolder);
var pathRelative = path.relative(pathToGroup, pathToFolder);
var name = dir.split('/').pop();
// link to Classes group
var file = new pbxFile(name);
file.fileRef = this.generateUuid();
this.addToClassesPbxGroup(file);
// create pbxgroup for parent folder
var commentKey = f("%s_comment", file.fileRef);
var entry = { "isa": 'PBXGroup', "children": [], "name": name, "path": pathRelative, "sourceTree": '"<group>"'};
var groups = this.hash.project.objects['PBXGroup'];
groups[file.fileRef] = entry;
groups[commentKey] = name;
console.log('Adding folder: '+pathToFolder);

this.readFolder(pathToFolder, file.fileRef);
}

pbxProject.prototype.readFolder = function (dir, uuid) {
var files = fs.readdirSync(dir);
var groups = this.hash.project.objects['PBXGroup'];
for(var i in files){
//if (!files.hasOwnProperty(i)) continue;
var folder = dir+'/'+files[i];
var name = files[i];
var format = path.extname(name);
// parse directories, but don't bother with repository
if ( (fs.statSync(folder).isDirectory()) &&
(files[i] != '.git') && (files[i] != '.svn') ) {
//console.log('Sub folder: '+folder);
var fp = new pbxFile(files[i]);
fp.fileRef = this.generateUuid();
// create pbxgroup for parent folder
var commentKey = f("%s_comment", fp.fileRef);
var entry = { "isa": 'PBXGroup', "children": [], "name": '"'+name+'"', "path": '"'+name+'"', "sourceTree": '"<group>"'};
groups[fp.fileRef] = entry;
groups[commentKey] = name;
groups[uuid].children.push(pbxGroupChild(fp));
this.readFolder(folder, fp.fileRef);
}
// add source
else if ((format == '.m') ||
(format == '.c') ||
(format == '.cpp') ||
(format == '.s')) {
//console.log('Source: '+name);
var fp = new pbxFile(files[i]);
fp.fileRef = this.generateUuid();
fp.uuid = this.generateUuid();
this.addToPbxFileReferenceSection(fp); // PBXFileReference
this.addToPbxBuildFileSection(fp); // PBXBuildFile
this.addToPbxSourcesBuildPhase(fp); // PBXSourcesBuildPhase
groups[uuid].children.push(pbxGroupChild(fp));
}
// add header
else if (path.extname(name) == '.h') {
//console.log('Header: '+name);
var fp = new pbxFile(files[i]);
fp.fileRef = this.generateUuid();
this.addToPbxFileReferenceSection(fp); // PBXFileReference
groups[uuid].children.push(pbxGroupChild(fp));
}
}
}

pbxProject.prototype.addSourceFile = function (path, opt) {
var file = this.addPluginFile(path, opt);

Expand Down Expand Up @@ -238,6 +348,7 @@ pbxProject.prototype.addStaticLibrary = function (path, opt) {

this.addToPbxBuildFileSection(file); // PBXBuildFile
this.addToPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase
this.addToFrameworksPbxGroup(file);
this.addToLibrarySearchPaths(file); // make sure it gets built!

return file;
Expand Down Expand Up @@ -291,6 +402,22 @@ pbxProject.prototype.removeFromPbxFileReferenceSection = function (file) {
return file;
}

pbxProject.prototype.addToClassesPbxGroup = function (file) {
var pluginsGroup = this.pbxGroupByName('Classes');
pluginsGroup.children.push(pbxGroupChild(file));
}

pbxProject.prototype.removeFromClassesPbxGroup = function (file) {
var pluginsGroupChildren = this.pbxGroupByName('Classes').children, i;
for(i in pluginsGroupChildren) {
if(pbxGroupChild(file).value == pluginsGroupChildren[i].value &&
pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) {
pluginsGroupChildren.splice(i, 1);
break;
}
}
}

pbxProject.prototype.addToPluginsPbxGroup = function (file) {
var pluginsGroup = this.pbxGroupByName('Plugins');
pluginsGroup.children.push(pbxGroupChild(file));
Expand Down