diff --git a/lib/pbxFile.js b/lib/pbxFile.js index 3f1a7ff..74447c9 100644 --- a/lib/pbxFile.js +++ b/lib/pbxFile.js @@ -1,5 +1,7 @@ var $path = require('path'), util = require('util'), + HEADER_FILE_TYPE_SUFFIX = ".h", + SOURCE_CODE_FILE_TYPE_PREFIX = "sourcecode.", M_EXTENSION = /[.]m$/, SOURCE_FILE = 'sourcecode.c.objc', C_EXTENSION = /[.]c$/, C_SOURCE_FILE = 'sourcecode.c', H_EXTENSION = /[.]h$/, HEADER_FILE = 'sourcecode.c.h', @@ -32,6 +34,14 @@ function fileTypes() { } } +function isSourceOrHeaderFileType(fileType) { + return fileType.startsWith(SOURCE_CODE_FILE_TYPE_PREFIX); +} + +function isHeaderFileType(fileType) { + return fileType.endsWith(HEADER_FILE_TYPE_SUFFIX); +} + function detectLastType(path) { if (M_EXTENSION.test(path)) return SOURCE_FILE; @@ -65,10 +75,10 @@ function detectLastType(path) { if (ARCHIVE_EXTENSION.test(path)) return ARCHIVE; - + if (PNG_EXTENSION.test(path)) return PNG_IMAGE; - + // dunno return 'unknown'; } @@ -98,7 +108,7 @@ function correctPath(file, filepath) { } function correctGroup(file) { - if (file.lastType == SOURCE_FILE) { + if (isSourceOrHeaderFileType(file.lastType) && !isHeaderFileType(file.lastType)) { return 'Sources'; } else if (file.lastType == DYLIB || file.lastType == ARCHIVE || file.lastType == FRAMEWORK) { return 'Frameworks'; @@ -137,5 +147,7 @@ function pbxFile(filepath, opt) { module.exports = { pbxFile: pbxFile, - fileTypes: fileTypes + fileTypes: fileTypes, + isSourceOrHeaderFileType, + isHeaderFileType, } diff --git a/lib/pbxProject.js b/lib/pbxProject.js index 1758c5d..dd2b467 100644 --- a/lib/pbxProject.js +++ b/lib/pbxProject.js @@ -7,12 +7,12 @@ var util = require('util'), pbxWriter = require('./pbxWriter'), pbxFile = require('./pbxFile').pbxFile, pbxFileTypes = require('./pbxFile').fileTypes, + isSourceOrHeaderFileType = require('./pbxFile').isSourceOrHeaderFileType, + isHeaderFileType = require('./pbxFile').isHeaderFileType, fs = require('fs'), parser = require('./parser/pbxproj'), COMMENT_KEY = /_comment$/, - NO_SPECIAL_SYMBOLS = /^[a-zA-Z0-9_\.\$]+\.[a-zA-Z]+$/, - HEADER_FILE_TYPE_SUFFIX = ".h", - SOURCE_CODE_FILE_TYPE_PREFIX = "sourcecode."; + NO_SPECIAL_SYMBOLS = /^[a-zA-Z0-9_\.\$]+\.[a-zA-Z]+$/; function pbxProject(filename) { if (!(this instanceof pbxProject)) @@ -114,7 +114,7 @@ pbxProject.prototype.removePluginFile = function (path, opt) { pbxProject.prototype.addSourceFile = function (path, opt) { - + var file = this.addPluginFile(path, opt); if (!file) return false; @@ -180,14 +180,14 @@ pbxProject.prototype.addResourceFile = function (path, opt) { pbxProject.prototype.removeResourceFile = function (path, opt) { var file = new pbxFile(path, opt); file.target = opt ? opt.target : undefined; - + correctForResourcesPath(file, this); this.removeFromPbxBuildFileSection(file); // PBXBuildFile this.removeFromPbxFileReferenceSection(file); // PBXFileReference this.removeFromResourcesPbxGroup(file); // PBXGroup this.removeFromPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase - + return file; } @@ -197,7 +197,7 @@ pbxProject.prototype.addFramework = function (fpath, opt) { if (this.hasFile(file.path)) return false; file.uuid = this.generateUuid(); - file.fileRef = this.generateUuid(); + file.fileRef = this.generateUuid(); file.target = opt ? opt.target : undefined; @@ -205,7 +205,7 @@ pbxProject.prototype.addFramework = function (fpath, opt) { this.addToPbxFileReferenceSection(file); // PBXFileReference this.addToFrameworksPbxGroup(file); // PBXGroup this.addToPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase - + if (opt) { if (opt.customFramework == true) { this.addToFrameworkSearchPaths(file); @@ -226,7 +226,7 @@ pbxProject.prototype.removeFramework = function (fpath, opt) { this.removeFromPbxFileReferenceSection(file); // PBXFileReference this.removeFromFrameworksPbxGroup(file); // PBXGroup this.removeFromPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase - + if (opt) { if (opt.customFramework == true) { this.removeFromFrameworkSearchPaths(file); @@ -235,7 +235,7 @@ pbxProject.prototype.removeFramework = function (fpath, opt) { this.removeFromPbxEmbedFrameworksBuildPhase(file, opt); } } - + return file; } @@ -282,7 +282,7 @@ pbxProject.prototype.removeFromPbxBuildFileSection = function (file) { if(this.pbxBuildFileSection()[uuid].fileRef_comment == file.basename) { file.uuid = uuid; delete this.pbxBuildFileSection()[uuid]; - + var commentKey = f("%s_comment", file.uuid); delete this.pbxBuildFileSection()[commentKey]; } @@ -307,7 +307,7 @@ pbxProject.prototype.findMainPbxGroup = function () { pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceTree, opt) { var oldGroup = this.pbxGroupByName(name); - if (oldGroup) { + if (oldGroup) { this.removePbxGroup(name, path); } @@ -334,7 +334,7 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT } for (var index = 0; index < filePathsArray.length; index++) { - + var filePath = filePathsArray[index], filePathQuoted = "\"" + filePath + "\""; if (filePathToReference[filePath]) { @@ -355,12 +355,12 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT pbxGroup.children.push(pbxGroupChild(file)); var files = fs.readdirSync(filePath).map(p => $path.join(filePath, p)); this.addPbxGroup(files, $path.basename(filePath), filePath, null, {uuid: file.uuid}); - }else if (file.lastType.startsWith(SOURCE_CODE_FILE_TYPE_PREFIX)) { + }else if (isSourceOrHeaderFileType(file.lastType)) { file.uuid = this.generateUuid(); file.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(file); // PBXFileReference this.addToPbxBuildFileSection(file); // PBXBuildFile - if (!file.lastType.endsWith(HEADER_FILE_TYPE_SUFFIX)) { + if (!isHeaderFileType(file.lastType)) { this.addToPbxSourcesBuildPhase(file); } pbxGroup.children.push(pbxGroupChild(file)); @@ -387,7 +387,7 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT pbxProject.prototype.removePbxGroup = function(name, path) { var group = this.pbxGroupByName(name); - if (!group) { + if (!group) { return; } @@ -396,9 +396,9 @@ pbxProject.prototype.removePbxGroup = function(name, path) { for(i in children) { var file = new pbxFile($path.join(path, children[i].comment)); file.fileRef = children[i].value; - file.uuid = file.fileRef; + file.uuid = file.fileRef; this.removePbxGroup(children[i].comment, $path.join(path, children[i].comment)); - this.removeFromPbxFileReferenceSection(file); + this.removeFromPbxFileReferenceSection(file); this.removeFromPbxBuildFileSection(file); this.removeFromPbxSourcesBuildPhase(file); } @@ -493,7 +493,7 @@ pbxProject.prototype.addToFrameworksPbxGroup = function (file) { pbxProject.prototype.removeFromFrameworksPbxGroup = function (file) { var pluginsGroupChildren = this.pbxGroupByName('Frameworks').children; - + for(i in pluginsGroupChildren) { if(pbxGroupChild(file).value == pluginsGroupChildren[i].value && pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) { @@ -513,7 +513,7 @@ pbxProject.prototype.removeFromPbxSourcesBuildPhase = function (file) { for(i in sources.files) { if(sources.files[i].comment == longComment(file)) { sources.files.splice(i, 1); - break; + break; } } } @@ -551,15 +551,15 @@ pbxProject.prototype.removeFromPbxFrameworksBuildPhase = function (file) { pbxProject.prototype.addToPbxEmbedFrameworksBuildPhase = function (file) { var embeddedFrameworkAttributes = ['CodeSignOnCopy', 'RemoveHeadersOnCopy']; - + var embedFile = new pbxFile(file.path); embedFile.settings = { ATTRIBUTES: embeddedFrameworkAttributes }; embedFile.uuid = this.generateUuid(); embedFile.fileRef = file.fileRef; embedFile.group = 'Embed Frameworks'; - + this.addToPbxBuildFileSection(embedFile); - + var embedFrameworks = this.pbxEmbedFrameworksBuildPhaseObj(); embedFrameworks.files.push(pbxBuildPhaseObj(embedFile)); } @@ -567,7 +567,7 @@ pbxProject.prototype.addToPbxEmbedFrameworksBuildPhase = function (file) { pbxProject.prototype.removeFromPbxEmbedFrameworksBuildPhase = function (file, opt) { var embedFile = new pbxFile(file.path); embedFile.group = 'Embed Frameworks'; - + var embedFrameworks = this.pbxEmbedFrameworksBuildPhaseObj(); for (var i in embedFrameworks.files) { if (embedFrameworks.files[i].comment == longComment(file, opt)) { @@ -579,14 +579,14 @@ pbxProject.prototype.removeFromPbxEmbedFrameworksBuildPhase = function (file, op pbxProject.prototype.addXCConfigurationList = function (configurationObjectsArray, defaultConfigurationName, comment) { var pbxBuildConfigurationSection = this.pbxXCBuildConfigurationSection(), - pbxXCConfigurationListSection = this.pbxXCConfigurationList(), + pbxXCConfigurationListSection = this.pbxXCConfigurationList(), xcConfigurationListUuid = this.generateUuid(), commentKey = f("%s_comment", xcConfigurationListUuid), - xcConfigurationList = { + xcConfigurationList = { isa: 'XCConfigurationList', buildConfigurations: [], defaultConfigurationIsVisible: 0, - defaultConfigurationName: defaultConfigurationName + defaultConfigurationName: defaultConfigurationName }; for (var index = 0; index < configurationObjectsArray.length; index++) { @@ -598,12 +598,12 @@ pbxProject.prototype.addXCConfigurationList = function (configurationObjectsArra pbxBuildConfigurationSection[configurationCommentKey] = configuration.name; xcConfigurationList.buildConfigurations.push({value: configurationUuid, comment: configuration.name}); } - + if (pbxXCConfigurationListSection) { pbxXCConfigurationListSection[xcConfigurationListUuid] = xcConfigurationList; pbxXCConfigurationListSection[commentKey] = comment; } - + return {uuid: xcConfigurationListUuid, xcConfigurationList: xcConfigurationList}; } @@ -615,13 +615,13 @@ pbxProject.prototype.addTargetDependency = function (target, dependencyTargets) if (typeof nativeTargets[target] == "undefined") throw new Error("Invalid target: " + target); - + for (var index = 0; index < dependencyTargets.length; index++) { var dependencyTarget = dependencyTargets[index]; if (typeof nativeTargets[dependencyTarget] == "undefined") throw new Error("Invalid target: " + dependencyTarget); } - + var pbxTargetDependency = 'PBXTargetDependency', pbxContainerItemProxy = 'PBXContainerItemProxy', pbxTargetDependencySection = this.hash.project.objects[pbxTargetDependency], @@ -640,16 +640,16 @@ pbxProject.prototype.addTargetDependency = function (target, dependencyTargets) containerPortal_comment: this.hash.project['rootObject_comment'], proxyType: 1, remoteGlobalIDString: dependencyTargetUuid, - remoteInfo: nativeTargets[dependencyTargetUuid].name + remoteInfo: nativeTargets[dependencyTargetUuid].name }, targetDependency = { isa: pbxTargetDependency, - target: dependencyTargetUuid, + target: dependencyTargetUuid, target_comment: nativeTargets[dependencyTargetCommentKey], targetProxy: itemProxyUuid, targetProxy_comment: pbxContainerItemProxy }; - + if (pbxContainerItemProxySection && pbxTargetDependencySection) { pbxContainerItemProxySection[itemProxyUuid] = itemProxy; pbxContainerItemProxySection[itemProxyCommentKey] = pbxContainerItemProxy; @@ -658,7 +658,7 @@ pbxProject.prototype.addTargetDependency = function (target, dependencyTargets) nativeTargets[target].dependencies.push({value: targetDependencyUuid, comment: pbxTargetDependency}) } } - + return {uuid: target, target: nativeTargets[target]}; } @@ -675,11 +675,11 @@ pbxProject.prototype.addBuildPhase = function (filePathsArray, isa, comment) { runOnlyForDeploymentPostprocessing: 0 }, filePathToBuildFile = {}; - + for (var key in buildFileSection) { // only look for comments if (!COMMENT_KEY.test(key)) continue; - + var buildFileKey = key.split(COMMENT_KEY)[0], buildFile = buildFileSection[buildFileKey]; fileReference = fileReferenceSection[buildFile.fileRef]; @@ -687,7 +687,7 @@ pbxProject.prototype.addBuildPhase = function (filePathsArray, isa, comment) { if (!fileReference) continue; var pbxFileObj = new pbxFile(fileReference.path); - + filePathToBuildFile[fileReference.path] = {uuid: buildFileKey, basename: pbxFileObj.basename, group: pbxFileObj.group}; } @@ -703,14 +703,14 @@ pbxProject.prototype.addBuildPhase = function (filePathsArray, isa, comment) { buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePathQuoted])); continue; } - + file.uuid = this.generateUuid(); file.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(file); // PBXFileReference this.addToPbxBuildFileSection(file); // PBXBuildFile buildPhase.files.push(pbxBuildPhaseObj(file)); } - + if (section) { section[buildPhaseUuid] = buildPhase; section[commentKey] = comment; @@ -744,7 +744,7 @@ pbxProject.prototype.pbxXCConfigurationList = function () { } pbxProject.prototype.pbxGroupByName = function (name) { - return this.pbxItemByComment(name, 'PBXGroup'); + return this.pbxItemByComment(name, 'PBXGroup'); } pbxProject.prototype.pbxTargetByName = function (name) { @@ -801,7 +801,7 @@ pbxProject.prototype.buildPhase = function (group,target) { var buildPhase = buildPhases[i]; if (buildPhase.comment==group) return buildPhase.value+"_comment"; - } + } } pbxProject.prototype.buildPhaseObject = function (name, group, target) { @@ -813,12 +813,12 @@ pbxProject.prototype.buildPhaseObject = function (name, group, target) { // only look for comments if (!COMMENT_KEY.test(key)) continue; - + // select the proper buildPhase if (buildPhase && buildPhase!=key) continue; if (section[key] == group) { - sectionKey = key.split(COMMENT_KEY)[0]; + sectionKey = key.split(COMMENT_KEY)[0]; return section[sectionKey]; } } @@ -1046,28 +1046,28 @@ function pbxFileReferenceObj(file) { obj.lastKnownFileType = file.lastType; obj.name = file.basename; obj.path = file.path; - + if(obj.name.indexOf("\"") !== -1) { obj.name = obj.name.replace(/\"/g, "\\\""); obj.path = obj.path.replace(/\"/g, "\\\""); } - + if(!file.basename.match(NO_SPECIAL_SYMBOLS)) { obj.name = "\"" + obj.name + "\""; } - + if(!file.path.match(NO_SPECIAL_SYMBOLS)) { obj.path = "\"" + obj.path + "\""; } - + obj.sourceTree = file.sourceTree; if (file.fileEncoding) obj.fileEncoding = file.fileEncoding; - + if (file.explicitFileType) obj.explicitFileType = file.explicitFileType; - + if ('includeInIndex' in file) obj.includeInIndex = file.includeInIndex; diff --git a/package.json b/package.json index 87e7603..6d3ef3c 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "name": "xcode", "description": "parser for xcodeproj/project.pbxproj files", "main":"index.js", - "version": "1.5.0-NativeScript", + "version": "1.5.1-NativeScript", "repository": { "url": "https://github.com/alunny/node-xcode.git" },