/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ const util = require('util'); const f = util.format; const EventEmitter = require('events').EventEmitter; const path = require('path'); const crypto = require('crypto'); const fork = require('child_process').fork; const PBXWriter = require('./pbxWriter'); const PBXFile = require('./pbxFile'); const fs = require('fs'); const parser = require('./parser/pbxproj'); const plist = require('simple-plist'); const COMMENT_KEY = /_comment$/; // MARK: Start of Typings /** * A list of known build phase types. * @typedef {'PBXCopyFilesBuildPhase' | * 'PBXResourcesBuildPhase' | * 'PBXFrameworksBuildPhase' | * 'PBXHeadersBuildPhase' | * 'PBXShellScriptBuildPhase' | * 'PBXSourcesBuildPhase' * } BuildPhaseType */ /** * The result object returned when adding a build phase. * @typedef {object} AddBuildPhaseResults * @property {string} uuid Build Phase UUID * @property {object} buildPhase Build Phase object */ /** * A list of valid target types used for copy files build configurations. * @typedef {'application' * | 'app_extension' * | 'bundle' * | 'command_line_tool' * | 'dynamic_library' * | 'framework' * | 'frameworks' * | 'static_library' * | 'unit_test_bundle' * | 'watch_app' * | 'watch2_app' * | 'watch_extension' * | 'watch2_extension' * } CopyFilesTargetType */ /** * Options used when adding a PBXShellScriptBuildPhase. * @typedef {object} ShellScriptOptions * @property {string[]} inputPaths input paths * @property {string[]} outputPaths output Paths * @property {string} shellPath shell paths * @property {0 | 1} runOnlyForDeploymentPostprocessing run script for install builds only * @property {1} [alwaysOutOfDate] disables dependency analysis when set to true */ // MARK: End of Typings // MARK: Start of Internals /** * A list of restricted object keys. */ const RESTRICTED_OBJECT_KEYS = new Set(['__proto__', 'prototype', 'constructor']); // MARK: End of Internals function PBXProject (filename) { if (!(this instanceof PBXProject)) { return new PBXProject(filename); } this.filepath = path.resolve(filename); } util.inherits(PBXProject, EventEmitter); PBXProject.prototype.parse = function (cb) { const worker = fork(path.join(__dirname, 'parseJob.js'), [this.filepath]); worker.on('message', function (msg) { if (msg.name === 'SyntaxError' || msg.code) { this.emit('error', msg); } else { this.hash = msg; this.emit('end', null, msg); } }.bind(this)); if (cb) { this.on('error', cb); this.on('end', cb); } return this; }; PBXProject.prototype.parseSync = function () { const file_contents = fs.readFileSync(this.filepath, 'utf-8'); this.hash = parser.parse(file_contents); return this; }; PBXProject.prototype.writeSync = function (options) { this.writer = new PBXWriter(this.hash, options); return this.writer.writeSync(); }; PBXProject.prototype.allUuids = function () { const sections = this.hash.project.objects; let uuids = []; let section; for (const key in sections) { section = sections[key]; uuids = uuids.concat(Object.keys(section)); } uuids = uuids.filter(function (str) { return !COMMENT_KEY.test(str) && str.length === 24; }); return uuids; }; PBXProject.prototype.generateUuid = function () { const id = crypto.randomUUID() .replace(/-/g, '') .substr(0, 24) .toUpperCase(); if (this.allUuids().indexOf(id) >= 0) { return this.generateUuid(); } else { return id; } }; PBXProject.prototype.addPluginFile = function (path, opt) { const file = new PBXFile(path, opt); file.plugin = true; // durr correctForPluginsPath(file, this); // null is better for early errors if (this.hasFile(file.path)) return null; file.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(file); // PBXFileReference this.addToPluginsPbxGroup(file); // PBXGroup return file; }; PBXProject.prototype.removePluginFile = function (path, opt) { const file = new PBXFile(path, opt); correctForPluginsPath(file, this); this.removeFromPbxFileReferenceSection(file); // PBXFileReference this.removeFromPluginsPbxGroup(file); // PBXGroup return file; }; PBXProject.prototype.addProductFile = function (targetPath, opt) { const file = new PBXFile(targetPath, opt); file.includeInIndex = 0; file.fileRef = this.generateUuid(); file.target = opt ? opt.target : undefined; file.group = opt ? opt.group : undefined; file.uuid = this.generateUuid(); file.path = file.basename; this.addToPbxFileReferenceSection(file); this.addToProductsPbxGroup(file); // PBXGroup return file; }; PBXProject.prototype.removeProductFile = function (path, opt) { const file = new PBXFile(path, opt); this.removeFromProductsPbxGroup(file); // PBXGroup return file; }; /** * * @param path {String} * @param opt {Object} see pbxFile for avail options * @param group {String} group key * @returns {Object} file; see pbxFile */ PBXProject.prototype.addSourceFile = function (path, opt, group) { let file; if (group) { file = this.addFile(path, group, opt); } else { file = this.addPluginFile(path, opt); } if (!file) return false; file.target = opt ? opt.target : undefined; file.uuid = this.generateUuid(); this.addToPbxBuildFileSection(file); // PBXBuildFile this.addToPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase return file; }; /** * * @param path {String} * @param opt {Object} see pbxFile for avail options * @param group {String} group key * @returns {Object} file; see pbxFile */ PBXProject.prototype.removeSourceFile = function (path, opt, group) { let file; if (group) { file = this.removeFile(path, group, opt); } else { file = this.removePluginFile(path, opt); } file.target = opt ? opt.target : undefined; this.removeFromPbxBuildFileSection(file); // PBXBuildFile this.removeFromPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase return file; }; /** * * @param path {String} * @param opt {Object} see pbxFile for avail options * @param group {String} group key * @returns {Object} file; see pbxFile */ PBXProject.prototype.addHeaderFile = function (path, opt, group) { if (group) { return this.addFile(path, group, opt); } else { return this.addPluginFile(path, opt); } }; /** * * @param path {String} * @param opt {Object} see pbxFile for avail options * @param group {String} group key * @returns {Object} file; see pbxFile */ PBXProject.prototype.removeHeaderFile = function (path, opt, group) { if (group) { return this.removeFile(path, group, opt); } else { return this.removePluginFile(path, opt); } }; /** * * @param path {String} * @param opt {Object} see pbxFile for avail options * @param group {String} group key * @returns {Object} file; see pbxFile */ PBXProject.prototype.addResourceFile = function (path, opt, group) { opt = opt || {}; let file; if (opt.plugin) { file = this.addPluginFile(path, opt); if (!file) return false; } else { file = new PBXFile(path, opt); if (this.hasFile(file.path)) return false; } file.uuid = this.generateUuid(); file.target = opt ? opt.target : undefined; if (!opt.plugin) { correctForResourcesPath(file, this); file.fileRef = this.generateUuid(); } if (!opt.variantGroup) { this.addToPbxBuildFileSection(file); // PBXBuildFile this.addToPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase } if (!opt.plugin) { this.addToPbxFileReferenceSection(file); // PBXFileReference if (group) { if (this.getPBXGroupByKey(group)) { this.addToPbxGroup(file, group); // Group other than Resources (i.e. 'splash') } else if (this.getPBXVariantGroupByKey(group)) { this.addToPbxVariantGroup(file, group); // PBXVariantGroup } } else { this.addToResourcesPbxGroup(file); // PBXGroup } } return file; }; /** * * @param path {String} * @param opt {Object} see pbxFile for avail options * @param group {String} group key * @returns {Object} file; see pbxFile */ PBXProject.prototype.removeResourceFile = function (path, opt, group) { const file = new PBXFile(path, opt); file.target = opt ? opt.target : undefined; correctForResourcesPath(file, this); this.removeFromPbxBuildFileSection(file); // PBXBuildFile this.removeFromPbxFileReferenceSection(file); // PBXFileReference if (group) { if (this.getPBXGroupByKey(group)) { this.removeFromPbxGroup(file, group); // Group other than Resources (i.e. 'splash') } else if (this.getPBXVariantGroupByKey(group)) { this.removeFromPbxVariantGroup(file, group); // PBXVariantGroup } } else { this.removeFromResourcesPbxGroup(file); // PBXGroup } this.removeFromPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase return file; }; PBXProject.prototype.addFramework = function (fpath, opt) { const customFramework = opt && opt.customFramework === true; const link = !opt || (opt.link === undefined || opt.link); // defaults to true if not specified const embed = opt && opt.embed; // defaults to false if not specified if (opt) { delete opt.embed; } const file = new PBXFile(fpath, opt); file.uuid = this.generateUuid(); 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 if (link) { this.addToPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase } if (customFramework) { this.addToFrameworkSearchPaths(file); if (embed) { opt.embed = embed; const 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 return embeddedFile; } } return file; }; PBXProject.prototype.removeFramework = function (fpath, opt) { if (opt) { delete opt.embed; } const file = new PBXFile(fpath, opt); file.target = opt ? opt.target : undefined; this.removeFromPbxBuildFileSection(file); // PBXBuildFile this.removeFromPbxFileReferenceSection(file); // PBXFileReference this.removeFromFrameworksPbxGroup(file); // PBXGroup this.removeFromPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase if (opt && opt.customFramework) { this.removeFromFrameworkSearchPaths(file); } opt = opt || {}; opt.embed = true; const embeddedFile = new PBXFile(fpath, opt); embeddedFile.fileRef = file.fileRef; this.removeFromPbxBuildFileSection(embeddedFile); // PBXBuildFile this.removeFromPbxEmbedFrameworksBuildPhase(embeddedFile); // PBXCopyFilesBuildPhase return file; }; PBXProject.prototype.addCopyfile = function (fpath, opt) { let file = new PBXFile(fpath, opt); // catch duplicates if (this.hasFile(file.path)) { file = this.hasFile(file.path); } file.fileRef = file.uuid = this.generateUuid(); file.target = opt ? opt.target : undefined; this.addToPbxBuildFileSection(file); // PBXBuildFile this.addToPbxFileReferenceSection(file); // PBXFileReference this.addToPbxCopyfilesBuildPhase(file); // PBXCopyFilesBuildPhase return file; }; PBXProject.prototype.pbxCopyfilesBuildPhaseObj = function (target) { return this.buildPhaseObject('PBXCopyFilesBuildPhase', 'Copy Files', target); }; PBXProject.prototype.addToPbxCopyfilesBuildPhase = function (file) { const sources = this.buildPhaseObject('PBXCopyFilesBuildPhase', 'Copy Files', file.target); sources.files.push(pbxBuildPhaseObj(file)); }; PBXProject.prototype.removeCopyfile = function (fpath, opt) { const file = new PBXFile(fpath, opt); file.target = opt ? opt.target : undefined; this.removeFromPbxBuildFileSection(file); // PBXBuildFile this.removeFromPbxFileReferenceSection(file); // PBXFileReference this.removeFromPbxCopyfilesBuildPhase(file); // PBXFrameworksBuildPhase return file; }; PBXProject.prototype.removeFromPbxCopyfilesBuildPhase = function (file) { const sources = this.pbxCopyfilesBuildPhaseObj(file.target); for (const i in sources.files) { if (sources.files[i].comment === longComment(file)) { sources.files.splice(i, 1); break; } } }; PBXProject.prototype.addStaticLibrary = function (path, opt) { opt = opt || {}; let file; if (opt.plugin) { file = this.addPluginFile(path, opt); if (!file) return false; } else { file = new PBXFile(path, opt); if (this.hasFile(file.path)) return false; } file.uuid = this.generateUuid(); file.target = opt ? opt.target : undefined; if (!opt.plugin) { file.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(file); // PBXFileReference } this.addToPbxBuildFileSection(file); // PBXBuildFile this.addToPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase this.addToLibrarySearchPaths(file); // make sure it gets built! return file; }; // helper addition functions PBXProject.prototype.addToPbxBuildFileSection = function (file) { const commentKey = f('%s_comment', file.uuid); this.pbxBuildFileSection()[file.uuid] = pbxBuildFileObj(file); this.pbxBuildFileSection()[commentKey] = pbxBuildFileComment(file); }; PBXProject.prototype.removeFromPbxBuildFileSection = function (file) { let uuid; for (uuid in this.pbxBuildFileSection()) { if (this.pbxBuildFileSection()[uuid].fileRef_comment === file.basename) { file.uuid = uuid; delete this.pbxBuildFileSection()[uuid]; const commentKey = f('%s_comment', uuid); delete this.pbxBuildFileSection()[commentKey]; } } }; PBXProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceTree) { const groups = this.hash.project.objects.PBXGroup; const pbxGroupUuid = this.generateUuid(); const commentKey = f('%s_comment', pbxGroupUuid); const pbxGroup = { isa: 'PBXGroup', children: [], name, path, sourceTree: sourceTree || '""' }; const fileReferenceSection = this.pbxFileReferenceSection(); const filePathToReference = {}; for (const key in fileReferenceSection) { // only look for comments if (!COMMENT_KEY.test(key)) continue; const fileReferenceKey = key.split(COMMENT_KEY)[0]; const fileReference = fileReferenceSection[fileReferenceKey]; filePathToReference[fileReference.path] = { fileRef: fileReferenceKey, basename: fileReferenceSection[key] }; } for (let index = 0; index < filePathsArray.length; index++) { const filePath = filePathsArray[index]; const filePathQuoted = '"' + filePath + '"'; if (filePathToReference[filePath]) { pbxGroup.children.push(pbxGroupChild(filePathToReference[filePath])); continue; } else if (filePathToReference[filePathQuoted]) { pbxGroup.children.push(pbxGroupChild(filePathToReference[filePathQuoted])); continue; } const file = new PBXFile(filePath); file.uuid = this.generateUuid(); file.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(file); // PBXFileReference this.addToPbxBuildFileSection(file); // PBXBuildFile pbxGroup.children.push(pbxGroupChild(file)); } if (groups) { groups[pbxGroupUuid] = pbxGroup; groups[commentKey] = name; } return { uuid: pbxGroupUuid, pbxGroup }; }; PBXProject.prototype.removePbxGroup = function (groupName) { const section = this.hash.project.objects.PBXGroup; let itemKey; for (const key in section) { // only look for comments if (!COMMENT_KEY.test(key)) continue; if (section[key] === groupName) { itemKey = key.split(COMMENT_KEY)[0]; delete section[itemKey]; } } }; PBXProject.prototype.addToPbxProjectSection = function (target) { const newTarget = { value: target.uuid, comment: pbxNativeTargetComment(target.pbxNativeTarget) }; this.pbxProjectSection()[this.getFirstProject().uuid].targets.push(newTarget); }; PBXProject.prototype.addToPbxNativeTargetSection = function (target) { const commentKey = f('%s_comment', target.uuid); this.pbxNativeTargetSection()[target.uuid] = target.pbxNativeTarget; this.pbxNativeTargetSection()[commentKey] = target.pbxNativeTarget.name; }; PBXProject.prototype.addToPbxFileReferenceSection = function (file) { const commentKey = f('%s_comment', file.fileRef); this.pbxFileReferenceSection()[file.fileRef] = pbxFileReferenceObj(file); this.pbxFileReferenceSection()[commentKey] = pbxFileReferenceComment(file); }; PBXProject.prototype.removeFromPbxFileReferenceSection = function (file) { const refObj = pbxFileReferenceObj(file); for (const i in this.pbxFileReferenceSection()) { if (this.pbxFileReferenceSection()[i].name === refObj.name || ('"' + this.pbxFileReferenceSection()[i].name + '"') === refObj.name || this.pbxFileReferenceSection()[i].path === refObj.path || ('"' + this.pbxFileReferenceSection()[i].path + '"') === refObj.path) { file.fileRef = file.uuid = i; delete this.pbxFileReferenceSection()[i]; break; } } const commentKey = f('%s_comment', file.fileRef); if (this.pbxFileReferenceSection()[commentKey] !== undefined) { delete this.pbxFileReferenceSection()[commentKey]; } return file; }; PBXProject.prototype.addToXcVersionGroupSection = function (file) { if (!file.models || !file.currentModel) { throw new Error('Cannot create a XCVersionGroup section from not a data model document file'); } const commentKey = f('%s_comment', file.fileRef); if (!this.xcVersionGroupSection()[file.fileRef]) { this.xcVersionGroupSection()[file.fileRef] = { isa: 'XCVersionGroup', children: file.models.map(function (el) { return el.fileRef; }), currentVersion: file.currentModel.fileRef, name: path.basename(file.path), path: file.path, sourceTree: '""', versionGroupType: 'wrapper.xcdatamodel' }; this.xcVersionGroupSection()[commentKey] = path.basename(file.path); } }; PBXProject.prototype.addToPluginsPbxGroup = function (file) { const pluginsGroup = this.pbxGroupByName('Plugins'); if (!pluginsGroup) { this.addPbxGroup([file.path], 'Plugins'); } else { pluginsGroup.children.push(pbxGroupChild(file)); } }; PBXProject.prototype.removeFromPluginsPbxGroup = function (file) { if (!this.pbxGroupByName('Plugins')) { return null; } const pluginsGroupChildren = this.pbxGroupByName('Plugins').children; for (const i in pluginsGroupChildren) { if (pbxGroupChild(file).value === pluginsGroupChildren[i].value && pbxGroupChild(file).comment === pluginsGroupChildren[i].comment) { pluginsGroupChildren.splice(i, 1); break; } } }; PBXProject.prototype.addToResourcesPbxGroup = function (file) { const pluginsGroup = this.pbxGroupByName('Resources'); if (!pluginsGroup) { this.addPbxGroup([file.path], 'Resources'); } else { pluginsGroup.children.push(pbxGroupChild(file)); } }; PBXProject.prototype.removeFromResourcesPbxGroup = function (file) { if (!this.pbxGroupByName('Resources')) { return null; } const pluginsGroupChildren = this.pbxGroupByName('Resources').children; for (const i in pluginsGroupChildren) { if (pbxGroupChild(file).value === pluginsGroupChildren[i].value && pbxGroupChild(file).comment === pluginsGroupChildren[i].comment) { pluginsGroupChildren.splice(i, 1); break; } } }; PBXProject.prototype.addToFrameworksPbxGroup = function (file) { const pluginsGroup = this.pbxGroupByName('Frameworks'); if (!pluginsGroup) { this.addPbxGroup([file.path], 'Frameworks'); } else { pluginsGroup.children.push(pbxGroupChild(file)); } }; PBXProject.prototype.removeFromFrameworksPbxGroup = function (file) { if (!this.pbxGroupByName('Frameworks')) { return null; } const pluginsGroupChildren = this.pbxGroupByName('Frameworks').children; for (const i in pluginsGroupChildren) { if (pbxGroupChild(file).value === pluginsGroupChildren[i].value && pbxGroupChild(file).comment === pluginsGroupChildren[i].comment) { pluginsGroupChildren.splice(i, 1); break; } } }; PBXProject.prototype.addToPbxEmbedFrameworksBuildPhase = function (file) { const sources = this.pbxEmbedFrameworksBuildPhaseObj(file.target); if (sources) { sources.files.push(pbxBuildPhaseObj(file)); } }; PBXProject.prototype.removeFromPbxEmbedFrameworksBuildPhase = function (file) { const sources = this.pbxEmbedFrameworksBuildPhaseObj(file.target); if (sources) { const files = []; for (const i in sources.files) { if (sources.files[i].comment !== longComment(file)) { files.push(sources.files[i]); } } sources.files = files; } }; PBXProject.prototype.addToProductsPbxGroup = function (file) { const productsGroup = this.pbxGroupByName('Products'); if (!productsGroup) { this.addPbxGroup([file.path], 'Products'); } else { productsGroup.children.push(pbxGroupChild(file)); } }; PBXProject.prototype.removeFromProductsPbxGroup = function (file) { if (!this.pbxGroupByName('Products')) { return null; } const productsGroupChildren = this.pbxGroupByName('Products').children; for (const i in productsGroupChildren) { if (pbxGroupChild(file).value === productsGroupChildren[i].value && pbxGroupChild(file).comment === productsGroupChildren[i].comment) { productsGroupChildren.splice(i, 1); break; } } }; PBXProject.prototype.addToPbxSourcesBuildPhase = function (file) { const sources = this.pbxSourcesBuildPhaseObj(file.target); sources.files.push(pbxBuildPhaseObj(file)); }; PBXProject.prototype.removeFromPbxSourcesBuildPhase = function (file) { const sources = this.pbxSourcesBuildPhaseObj(file.target); for (const i in sources.files) { if (sources.files[i].comment === longComment(file)) { sources.files.splice(i, 1); break; } } }; PBXProject.prototype.addToPbxResourcesBuildPhase = function (file) { const sources = this.pbxResourcesBuildPhaseObj(file.target); sources.files.push(pbxBuildPhaseObj(file)); }; PBXProject.prototype.removeFromPbxResourcesBuildPhase = function (file) { const sources = this.pbxResourcesBuildPhaseObj(file.target); for (const i in sources.files) { if (sources.files[i].comment === longComment(file)) { sources.files.splice(i, 1); break; } } }; PBXProject.prototype.addToPbxFrameworksBuildPhase = function (file) { const sources = this.pbxFrameworksBuildPhaseObj(file.target); sources.files.push(pbxBuildPhaseObj(file)); }; PBXProject.prototype.removeFromPbxFrameworksBuildPhase = function (file) { const sources = this.pbxFrameworksBuildPhaseObj(file.target); for (const i in sources.files) { if (sources.files[i].comment === longComment(file)) { sources.files.splice(i, 1); break; } } }; PBXProject.prototype.addXCConfigurationList = function (configurationObjectsArray, defaultConfigurationName, comment) { const pbxBuildConfigurationSection = this.pbxXCBuildConfigurationSection(); const pbxXCConfigurationListSection = this.pbxXCConfigurationList(); const xcConfigurationListUuid = this.generateUuid(); const commentKey = f('%s_comment', xcConfigurationListUuid); const xcConfigurationList = { isa: 'XCConfigurationList', buildConfigurations: [], defaultConfigurationIsVisible: 0, defaultConfigurationName }; for (let index = 0; index < configurationObjectsArray.length; index++) { const configuration = configurationObjectsArray[index]; const configurationUuid = this.generateUuid(); const configurationCommentKey = f('%s_comment', configurationUuid); pbxBuildConfigurationSection[configurationUuid] = configuration; pbxBuildConfigurationSection[configurationCommentKey] = configuration.name; xcConfigurationList.buildConfigurations.push({ value: configurationUuid, comment: configuration.name }); } if (pbxXCConfigurationListSection) { pbxXCConfigurationListSection[xcConfigurationListUuid] = xcConfigurationList; pbxXCConfigurationListSection[commentKey] = comment; } return { uuid: xcConfigurationListUuid, xcConfigurationList }; }; PBXProject.prototype.addTargetDependency = function (target, dependencyTargets) { if (!target) { return undefined; } const nativeTargets = this.pbxNativeTargetSection(); if (typeof nativeTargets[target] === 'undefined') { throw new Error('Invalid target: ' + target); } for (let index = 0; index < dependencyTargets.length; index++) { const dependencyTarget = dependencyTargets[index]; if (typeof nativeTargets[dependencyTarget] === 'undefined') { throw new Error('Invalid target: ' + dependencyTarget); } } const pbxTargetDependency = 'PBXTargetDependency'; const pbxContainerItemProxy = 'PBXContainerItemProxy'; const pbxTargetDependencySection = this.hash.project.objects[pbxTargetDependency]; const pbxContainerItemProxySection = this.hash.project.objects[pbxContainerItemProxy]; for (let index = 0; index < dependencyTargets.length; index++) { const dependencyTargetUuid = dependencyTargets[index]; const dependencyTargetCommentKey = f('%s_comment', dependencyTargetUuid); const targetDependencyUuid = this.generateUuid(); const targetDependencyCommentKey = f('%s_comment', targetDependencyUuid); const itemProxyUuid = this.generateUuid(); const itemProxyCommentKey = f('%s_comment', itemProxyUuid); const itemProxy = { isa: pbxContainerItemProxy, containerPortal: this.hash.project.rootObject, containerPortal_comment: this.hash.project.rootObject_comment, proxyType: 1, remoteGlobalIDString: dependencyTargetUuid, remoteInfo: nativeTargets[dependencyTargetUuid].name }; const targetDependency = { isa: pbxTargetDependency, target: dependencyTargetUuid, target_comment: nativeTargets[dependencyTargetCommentKey], targetProxy: itemProxyUuid, targetProxy_comment: pbxContainerItemProxy }; if (pbxContainerItemProxySection && pbxTargetDependencySection) { pbxContainerItemProxySection[itemProxyUuid] = itemProxy; pbxContainerItemProxySection[itemProxyCommentKey] = pbxContainerItemProxy; pbxTargetDependencySection[targetDependencyUuid] = targetDependency; pbxTargetDependencySection[targetDependencyCommentKey] = pbxTargetDependency; nativeTargets[target].dependencies.push({ value: targetDependencyUuid, comment: pbxTargetDependency }); } } return { uuid: target, target: nativeTargets[target] }; }; /** * Add files to specific build phase by defining the build phase type. * * @example * // Adding a file to Resource Build Phase. * myProject.addBuildPhase( * ['MyAssets.xcassets'], * 'PBXResourcesBuildPhase', * 'Resources' * ); * * @param {string[]} filePathsArray collection of file paths * @param {BuildPhaseType} buildPhaseType target build phase * @param {string} comment name of the build phase. (e.g. "Copy My Files") * @param {string} target UUID of the PBXNativeTarget (defaults to first target) * @param {CopyFilesTargetType|ShellScriptOptions} optionsOrFolderType either sell script options or copy files target type * @param {string} subfolderPath copy files subfolder path (default: "") * @returns {AddBuildPhaseResults} object containing the build phase & uuid */ PBXProject.prototype.addBuildPhase = function (filePathsArray, buildPhaseType, comment, target, optionsOrFolderType, subfolderPath) { // Xcode may introduce new build phase types so the buildPhaseType // param will not be validated against a fixed set of values. // Instead, it will reject object keys that shouldn't be writable. if (RESTRICTED_OBJECT_KEYS.has(buildPhaseType)) { throw new Error(`"${buildPhaseType}" is restricted and cannot be used as a build phase type.`); } const fileReferenceSection = this.pbxFileReferenceSection(); const buildFileSection = this.pbxBuildFileSection(); const buildPhaseUuid = this.generateUuid(); const buildPhaseTargetUuid = target || this.getFirstTarget().uuid; const commentKey = f('%s_comment', buildPhaseUuid); let buildPhase = { isa: buildPhaseType, buildActionMask: 2147483647, files: [], runOnlyForDeploymentPostprocessing: 0 }; const filePathToBuildFile = {}; if (buildPhaseType === 'PBXCopyFilesBuildPhase') { buildPhase = pbxCopyFilesBuildPhaseObj(buildPhase, optionsOrFolderType, subfolderPath, comment); } else if (buildPhaseType === 'PBXShellScriptBuildPhase') { buildPhase = pbxShellScriptBuildPhaseObj(buildPhase, optionsOrFolderType, comment); } if (!this.hash.project.objects[buildPhaseType]) { this.hash.project.objects[buildPhaseType] = {}; } if (!this.hash.project.objects[buildPhaseType][buildPhaseUuid]) { this.hash.project.objects[buildPhaseType][buildPhaseUuid] = buildPhase; this.hash.project.objects[buildPhaseType][commentKey] = comment; } if (this.hash.project.objects.PBXNativeTarget[buildPhaseTargetUuid].buildPhases) { this.hash.project.objects.PBXNativeTarget[buildPhaseTargetUuid].buildPhases.push({ value: buildPhaseUuid, comment }); } for (const key in buildFileSection) { // only look for comments if (!COMMENT_KEY.test(key)) continue; const buildFileKey = key.split(COMMENT_KEY)[0]; const buildFile = buildFileSection[buildFileKey]; const fileReference = fileReferenceSection[buildFile.fileRef]; // If a file reference does not define either the name or path property, // it will not be included to the filePathToBuildFile lookup table. // If the file reference defines both properies, the path property will // take priority. const fileIdentifier = fileReference?.path || fileReference?.name; if (!fileIdentifier) { continue; } const pbxFileObj = new PBXFile(fileIdentifier); // Creates a lookup table of existing PBXBuildFile entries keyed by fileIdentifier. // Used later when adding files to the build phase to reuse the existing PBXBuildFile // instead of generating new PBXBuildFile. filePathToBuildFile[fileIdentifier] = { uuid: buildFileKey, basename: pbxFileObj.basename, group: pbxFileObj.group }; } for (let index = 0; index < filePathsArray.length; index++) { const filePath = filePathsArray[index]; const filePathQuoted = '"' + filePath + '"'; // Checks for an existing PBXBuildFile entry from the filePathToBuildFile lookup table. if (filePathToBuildFile[filePath]) { buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePath])); continue; } else if (filePathToBuildFile[filePathQuoted]) { buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePathQuoted])); continue; } // If no PBXBuildFile entry is found in the lookup table, a new entry is created // with a new UUID. It is then: // 1. Added to the PBXFileReferenceSection // 2. Added to the PBXBuildFileSection // 3. Pushed onto the build phase file list. const file = new PBXFile(filePath); file.uuid = this.generateUuid(); file.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(file); this.addToPbxBuildFileSection(file); buildPhase.files.push(pbxBuildPhaseObj(file)); } return { uuid: buildPhaseUuid, buildPhase }; }; // helper access functions PBXProject.prototype.pbxProjectSection = function () { return this.hash.project.objects.PBXProject; }; PBXProject.prototype.pbxBuildFileSection = function () { return this.hash.project.objects.PBXBuildFile; }; PBXProject.prototype.pbxXCBuildConfigurationSection = function () { return this.hash.project.objects.XCBuildConfiguration; }; PBXProject.prototype.pbxFileReferenceSection = function () { return this.hash.project.objects.PBXFileReference; }; PBXProject.prototype.pbxNativeTargetSection = function () { return this.hash.project.objects.PBXNativeTarget; }; PBXProject.prototype.xcVersionGroupSection = function () { if (typeof this.hash.project.objects.XCVersionGroup !== 'object') { this.hash.project.objects.XCVersionGroup = {}; } return this.hash.project.objects.XCVersionGroup; }; PBXProject.prototype.pbxXCConfigurationList = function () { return this.hash.project.objects.XCConfigurationList; }; PBXProject.prototype.pbxGroupByName = function (name) { const groups = this.hash.project.objects.PBXGroup; let groupKey; for (const key in groups) { // only look for comments if (!COMMENT_KEY.test(key)) continue; if (groups[key] === name) { groupKey = key.split(COMMENT_KEY)[0]; return groups[groupKey]; } } return null; }; PBXProject.prototype.pbxTargetByName = function (name) { return this.pbxItemByComment(name, 'PBXNativeTarget'); }; PBXProject.prototype.findTargetKey = function (name) { const targets = this.hash.project.objects.PBXNativeTarget; for (const key in targets) { // only look for comments if (COMMENT_KEY.test(key)) continue; const target = targets[key]; if (target.name === name) { return key; } } return null; }; PBXProject.prototype.pbxItemByComment = function (name, pbxSectionName) { const section = this.hash.project.objects[pbxSectionName]; let itemKey; for (const key in section) { // only look for comments if (!COMMENT_KEY.test(key)) continue; if (section[key] === name) { itemKey = key.split(COMMENT_KEY)[0]; return section[itemKey]; } } return null; }; PBXProject.prototype.pbxSourcesBuildPhaseObj = function (target) { return this.buildPhaseObject('PBXSourcesBuildPhase', 'Sources', target); }; PBXProject.prototype.pbxResourcesBuildPhaseObj = function (target) { return this.buildPhaseObject('PBXResourcesBuildPhase', 'Resources', target); }; PBXProject.prototype.pbxFrameworksBuildPhaseObj = function (target) { return this.buildPhaseObject('PBXFrameworksBuildPhase', 'Frameworks', target); }; PBXProject.prototype.pbxEmbedFrameworksBuildPhaseObj = function (target) { return this.buildPhaseObject('PBXCopyFilesBuildPhase', 'Embed Frameworks', target); }; // Find Build Phase from group/target PBXProject.prototype.buildPhase = function (group, target) { if (!target) { return undefined; } const nativeTargets = this.pbxNativeTargetSection(); if (typeof nativeTargets[target] === 'undefined') { throw new Error('Invalid target: ' + target); } const nativeTarget = nativeTargets[target]; const buildPhases = nativeTarget.buildPhases; for (const i in buildPhases) { const buildPhase = buildPhases[i]; if (buildPhase.comment === group) { return buildPhase.value + '_comment'; } } }; PBXProject.prototype.buildPhaseObject = function (name, group, target) { const section = this.hash.project.objects[name]; let sectionKey; const buildPhase = this.buildPhase(group, target); for (const key in section) { // 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]; return section[sectionKey]; } } return null; }; PBXProject.prototype.addBuildProperty = function (prop, value, build_name) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); let configuration; for (const key in configurations) { configuration = configurations[key]; if (!build_name || configuration.name === build_name) { configuration.buildSettings[prop] = value; } } }; PBXProject.prototype.removeBuildProperty = function (prop, build_name) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); let configuration; for (const key in configurations) { configuration = configurations[key]; if ( configuration.buildSettings[prop] && (!build_name || configuration.name === build_name) ) { delete configuration.buildSettings[prop]; } } }; /** * * @param prop {String} * @param value {String|Array|Object|Number|Boolean} * @param build {String} Release or Debug * @param targetName {String} the target which will be updated */ PBXProject.prototype.updateBuildProperty = function (prop, value, build, targetName) { const validConfigs = []; if (targetName) { const target = this.pbxTargetByName(targetName); const targetBuildConfigs = target && target.buildConfigurationList; const xcConfigList = this.pbxXCConfigurationList(); // Collect the UUID's from the configuration of our target for (const configName in xcConfigList) { if (!COMMENT_KEY.test(configName) && targetBuildConfigs === configName) { const buildVariants = xcConfigList[configName].buildConfigurations; for (const item of buildVariants) { validConfigs.push(item.value); } break; } } } const configs = this.pbxXCBuildConfigurationSection(); for (const configName in configs) { if (!COMMENT_KEY.test(configName)) { if (targetName && !validConfigs.includes(configName)) continue; const config = configs[configName]; if ((build && config.name === build) || (!build)) { config.buildSettings[prop] = value; } } } }; PBXProject.prototype.updateProductName = function (name) { this.updateBuildProperty('PRODUCT_NAME', '"' + name + '"'); }; PBXProject.prototype.removeFromFrameworkSearchPaths = function (file) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const SEARCH_PATHS = 'FRAMEWORK_SEARCH_PATHS'; let config; let buildSettings; let searchPaths; const new_path = searchPathForFile(file, this); for (config in configurations) { buildSettings = configurations[config].buildSettings; if (unquote(buildSettings.PRODUCT_NAME) !== this.productName) { continue; } searchPaths = buildSettings[SEARCH_PATHS]; if (searchPaths && Array.isArray(searchPaths)) { const matches = searchPaths.filter(function (p) { return p.indexOf(new_path) > -1; }); matches.forEach(function (m) { const idx = searchPaths.indexOf(m); searchPaths.splice(idx, 1); }); } } }; PBXProject.prototype.addToFrameworkSearchPaths = function (file) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const INHERITED = '"$(inherited)"'; let config; let buildSettings; for (config in configurations) { buildSettings = configurations[config].buildSettings; if (unquote(buildSettings.PRODUCT_NAME) !== this.productName) { continue; } if (!buildSettings.FRAMEWORK_SEARCH_PATHS || buildSettings.FRAMEWORK_SEARCH_PATHS === INHERITED) { buildSettings.FRAMEWORK_SEARCH_PATHS = [INHERITED]; } buildSettings.FRAMEWORK_SEARCH_PATHS.push(searchPathForFile(file, this)); } }; PBXProject.prototype.removeFromLibrarySearchPaths = function (file) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const SEARCH_PATHS = 'LIBRARY_SEARCH_PATHS'; let config; let buildSettings; let searchPaths; const new_path = searchPathForFile(file, this); for (config in configurations) { buildSettings = configurations[config].buildSettings; if (unquote(buildSettings.PRODUCT_NAME) !== this.productName) { continue; } searchPaths = buildSettings[SEARCH_PATHS]; if (searchPaths && Array.isArray(searchPaths)) { const matches = searchPaths.filter(function (p) { return p.indexOf(new_path) > -1; }); matches.forEach(function (m) { const idx = searchPaths.indexOf(m); searchPaths.splice(idx, 1); }); } } }; PBXProject.prototype.addToLibrarySearchPaths = function (file) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const INHERITED = '"$(inherited)"'; let config; let buildSettings; 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 === INHERITED) { buildSettings.LIBRARY_SEARCH_PATHS = [INHERITED]; } if (typeof file === 'string') { buildSettings.LIBRARY_SEARCH_PATHS.push(file); } else { buildSettings.LIBRARY_SEARCH_PATHS.push(searchPathForFile(file, this)); } } }; PBXProject.prototype.removeFromHeaderSearchPaths = function (file) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const SEARCH_PATHS = 'HEADER_SEARCH_PATHS'; let config; let buildSettings; const new_path = searchPathForFile(file, this); for (config in configurations) { buildSettings = configurations[config].buildSettings; if (unquote(buildSettings.PRODUCT_NAME) !== this.productName) { continue; } if (buildSettings[SEARCH_PATHS]) { const matches = buildSettings[SEARCH_PATHS].filter(function (p) { return p.indexOf(new_path) > -1; }); matches.forEach(function (m) { const idx = buildSettings[SEARCH_PATHS].indexOf(m); buildSettings[SEARCH_PATHS].splice(idx, 1); }); } } }; PBXProject.prototype.addToHeaderSearchPaths = function (file) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const INHERITED = '"$(inherited)"'; let config; let buildSettings; 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]; } if (typeof file === 'string') { buildSettings.HEADER_SEARCH_PATHS.push(file); } else { buildSettings.HEADER_SEARCH_PATHS.push(searchPathForFile(file, this)); } } }; PBXProject.prototype.addToOtherLinkerFlags = function (flag) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const INHERITED = '"$(inherited)"'; const OTHER_LDFLAGS = 'OTHER_LDFLAGS'; let config; let buildSettings; for (config in configurations) { buildSettings = configurations[config].buildSettings; if (unquote(buildSettings.PRODUCT_NAME) !== this.productName) { continue; } if (!buildSettings[OTHER_LDFLAGS] || buildSettings[OTHER_LDFLAGS] === INHERITED) { buildSettings[OTHER_LDFLAGS] = [INHERITED]; } buildSettings[OTHER_LDFLAGS].push(flag); } }; PBXProject.prototype.removeFromOtherLinkerFlags = function (flag) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); const OTHER_LDFLAGS = 'OTHER_LDFLAGS'; let config; let buildSettings; for (config in configurations) { buildSettings = configurations[config].buildSettings; if (unquote(buildSettings.PRODUCT_NAME) !== this.productName) { continue; } if (buildSettings[OTHER_LDFLAGS]) { const matches = buildSettings[OTHER_LDFLAGS].filter(function (p) { return p.indexOf(flag) > -1; }); matches.forEach(function (m) { const idx = buildSettings[OTHER_LDFLAGS].indexOf(m); buildSettings[OTHER_LDFLAGS].splice(idx, 1); }); } } }; PBXProject.prototype.addToBuildSettings = function (buildSetting, value) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); let config; let buildSettings; for (config in configurations) { buildSettings = configurations[config].buildSettings; buildSettings[buildSetting] = value; } }; PBXProject.prototype.removeFromBuildSettings = function (buildSetting) { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); let config; let buildSettings; for (config in configurations) { buildSettings = configurations[config].buildSettings; if (buildSettings[buildSetting]) { delete buildSettings[buildSetting]; } } }; // a JS getter. hmmm PBXProject.prototype.__defineGetter__('productName', function () { const configurations = nonComments(this.pbxXCBuildConfigurationSection()); let config; let productName; for (config in configurations) { productName = configurations[config].buildSettings.PRODUCT_NAME; if (productName) { return unquote(productName); } } }); // check if file is present PBXProject.prototype.hasFile = function (filePath) { const files = nonComments(this.pbxFileReferenceSection()); let file; let id; for (id in files) { file = files[id]; if (file.path === filePath || file.path === ('"' + filePath + '"')) { return file; } } return false; }; PBXProject.prototype.addTarget = function (name, type, subfolder, bundleId) { // Setup uuid and name of new target const targetUuid = this.generateUuid(); const targetType = type; const targetSubfolder = (subfolder || name).replace(/\\/g, '/'); const targetName = name.trim(); const targetBundleId = bundleId; // Check type against list of allowed target types if (!targetName) { throw new Error('Target name missing.'); } // Check type against list of allowed target types if (!targetType) { throw new Error('Target type missing.'); } // Check type against list of allowed target types if (!producttypeForTargettype(targetType)) { throw new Error('Target type invalid: ' + targetType); } // Build Configuration: Create let buildConfigurationsList = [ { name: 'Debug', isa: 'XCBuildConfiguration', buildSettings: { GCC_PREPROCESSOR_DEFINITIONS: ['"DEBUG=1"', '"$(inherited)"'], INFOPLIST_FILE: '"' + path.posix.join(targetSubfolder, targetSubfolder + '-Info.plist' + '"'), LD_RUNPATH_SEARCH_PATHS: '"$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"', PRODUCT_NAME: '"' + targetName + '"', SKIP_INSTALL: 'YES' } }, { name: 'Release', isa: 'XCBuildConfiguration', buildSettings: { INFOPLIST_FILE: '"' + path.posix.join(targetSubfolder, targetSubfolder + '-Info.plist' + '"'), LD_RUNPATH_SEARCH_PATHS: '"$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"', PRODUCT_NAME: '"' + targetName + '"', SKIP_INSTALL: 'YES' } } ]; // Add optional bundleId to build configuration if (targetBundleId) { buildConfigurationsList = buildConfigurationsList.map((elem) => { elem.buildSettings.PRODUCT_BUNDLE_IDENTIFIER = '"' + targetBundleId + '"'; return elem; }); } // Build Configuration: Add const buildConfigurations = this.addXCConfigurationList(buildConfigurationsList, 'Release', 'Build configuration list for PBXNativeTarget "' + targetName + '"'); // Product: Create const productName = targetName; const productType = producttypeForTargettype(targetType); const productFileType = filetypeForProducttype(productType); const productFile = this.addProductFile(productName, { group: 'Copy Files', target: targetUuid, explicitFileType: productFileType }); // Product: Add to build file list this.addToPbxBuildFileSection(productFile); // Target: Create const target = { uuid: targetUuid, pbxNativeTarget: { isa: 'PBXNativeTarget', name: '"' + targetName + '"', productName: '"' + targetName + '"', productReference: productFile.fileRef, productType: '"' + producttypeForTargettype(targetType) + '"', buildConfigurationList: buildConfigurations.uuid, buildPhases: [], buildRules: [], dependencies: [] } }; // Target: Add to PBXNativeTarget section this.addToPbxNativeTargetSection(target); // Product: Embed (only for "extension"-type targets) if (targetType === 'app_extension') { // Create CopyFiles phase in first target this.addBuildPhase([], 'PBXCopyFilesBuildPhase', 'Copy Files', this.getFirstTarget().uuid, targetType); // Add product to CopyFiles phase this.addToPbxCopyfilesBuildPhase(productFile); // this.addBuildPhaseToTarget(newPhase.buildPhase, this.getFirstTarget().uuid) } else if (targetType === 'watch2_app') { // Create CopyFiles phase in first target this.addBuildPhase( [targetName + '.app'], 'PBXCopyFilesBuildPhase', 'Embed Watch Content', this.getFirstTarget().uuid, targetType, '"$(CONTENTS_FOLDER_PATH)/Watch"' ); } else if (targetType === 'watch2_extension') { // Create CopyFiles phase in watch target (if exists) const watch2Target = this.getTarget(producttypeForTargettype('watch2_app')); if (watch2Target) { this.addBuildPhase( [targetName + '.appex'], 'PBXCopyFilesBuildPhase', 'Embed App Extensions', watch2Target.uuid, targetType ); } } // Target: Add uuid to root project this.addToPbxProjectSection(target); // Target: Add dependency for this target to other targets if (targetType === 'watch2_extension') { const watch2Target = this.getTarget(producttypeForTargettype('watch2_app')); if (watch2Target) { this.addTargetDependency(watch2Target.uuid, [target.uuid]); } } else { this.addTargetDependency(this.getFirstTarget().uuid, [target.uuid]); } // Return target on success return target; }; // helper object creation functions function pbxBuildFileObj (file) { const obj = Object.create(null); obj.isa = 'PBXBuildFile'; obj.fileRef = file.fileRef; obj.fileRef_comment = file.basename; if (file.settings) obj.settings = file.settings; return obj; } function pbxFileReferenceObj (file) { const fileObject = { isa: 'PBXFileReference', name: '"' + file.basename + '"', path: '"' + file.path.replace(/\\/g, '/') + '"', sourceTree: file.sourceTree, fileEncoding: file.fileEncoding, lastKnownFileType: file.lastKnownFileType, explicitFileType: file.explicitFileType, includeInIndex: file.includeInIndex }; return fileObject; } function pbxGroupChild (file) { const obj = Object.create(null); obj.value = file.fileRef; obj.comment = file.basename; return obj; } function pbxBuildPhaseObj (file) { const obj = Object.create(null); obj.value = file.uuid; obj.comment = longComment(file); return obj; } function pbxCopyFilesBuildPhaseObj (obj, folderType, subfolderPath, phaseName) { // Add additional properties for 'CopyFiles' build phase const DESTINATION_BY_TARGETTYPE = { application: 'wrapper', app_extension: 'plugins', bundle: 'wrapper', command_line_tool: 'wrapper', dynamic_library: 'products_directory', framework: 'shared_frameworks', frameworks: 'frameworks', static_library: 'products_directory', unit_test_bundle: 'wrapper', watch_app: 'wrapper', watch2_app: 'products_directory', watch_extension: 'plugins', watch2_extension: 'plugins' }; const SUBFOLDERSPEC_BY_DESTINATION = { absolute_path: 0, executables: 6, frameworks: 10, java_resources: 15, plugins: 13, products_directory: 16, resources: 7, shared_frameworks: 11, shared_support: 12, wrapper: 1, xpc_services: 0 }; obj.name = '"' + phaseName + '"'; obj.dstPath = subfolderPath || '""'; obj.dstSubfolderSpec = SUBFOLDERSPEC_BY_DESTINATION[DESTINATION_BY_TARGETTYPE[folderType]]; return obj; } function pbxShellScriptBuildPhaseObj (obj, options, phaseName) { obj.name = '"' + phaseName + '"'; obj.inputPaths = options.inputPaths || []; obj.outputPaths = options.outputPaths || []; obj.shellPath = options.shellPath; obj.shellScript = '"' + options.shellScript.replace(/"/g, '\\"') + '"'; // By default, runOnlyForDeploymentPostprocessing is set to 0. // Any truthy value, it will be set to 1, including any non-empty strings. if (options.runOnlyForDeploymentPostprocessing) { obj.runOnlyForDeploymentPostprocessing = 1; } // By default, alwaysOutOfDate is not set and treated as false. // Any truthy value, it will be set to 1, including any non-empty strings. if (options.alwaysOutOfDate) { obj.alwaysOutOfDate = 1; } return obj; } function pbxBuildFileComment (file) { return longComment(file); } function pbxFileReferenceComment (file) { return file.basename || path.basename(file.path); } function pbxNativeTargetComment (target) { return target.name; } function longComment (file) { return f('%s in %s', file.basename, file.group); } // respect path function correctForPluginsPath (file, project) { return correctForPath(file, project, 'Plugins'); } function correctForResourcesPath (file, project) { return correctForPath(file, project, 'Resources'); } function correctForPath (file, project, group) { const r_group_dir = new RegExp('^' + group + '[\\\\/]'); if (project.pbxGroupByName(group) && project.pbxGroupByName(group).path) { file.path = file.path.replace(r_group_dir, ''); } return file; } function searchPathForFile (file, proj) { const plugins = proj.pbxGroupByName('Plugins'); const pluginsPath = plugins ? plugins.path : null; let fileDir = path.dirname(file.path); if (fileDir === '.') { fileDir = ''; } else { fileDir = '/' + fileDir; } if (file.plugin && pluginsPath) { return '"\\"$(SRCROOT)/' + unquote(pluginsPath) + '\\""'; } else if (file.customFramework && file.dirname) { return '"\\"' + file.dirname + '\\""'; } else { return '"\\"$(SRCROOT)/' + proj.productName + fileDir + '\\""'; } } function nonComments (obj) { const keys = Object.keys(obj); const newObj = {}; let i = 0; for (i; i < keys.length; i++) { if (!COMMENT_KEY.test(keys[i])) { newObj[keys[i]] = obj[keys[i]]; } } return newObj; } function unquote (str) { if (str) return str.replace(/^"(.*)"$/, '$1'); } function producttypeForTargettype (targetType) { const PRODUCTTYPE_BY_TARGETTYPE = { application: 'com.apple.product-type.application', app_extension: 'com.apple.product-type.app-extension', bundle: 'com.apple.product-type.bundle', command_line_tool: 'com.apple.product-type.tool', dynamic_library: 'com.apple.product-type.library.dynamic', framework: 'com.apple.product-type.framework', static_library: 'com.apple.product-type.library.static', unit_test_bundle: 'com.apple.product-type.bundle.unit-test', watch_app: 'com.apple.product-type.application.watchapp', watch2_app: 'com.apple.product-type.application.watchapp2', watch_extension: 'com.apple.product-type.watchkit-extension', watch2_extension: 'com.apple.product-type.watchkit2-extension' }; return PRODUCTTYPE_BY_TARGETTYPE[targetType]; } function filetypeForProducttype (productType) { const FILETYPE_BY_PRODUCTTYPE = { 'com.apple.product-type.application': '"wrapper.application"', 'com.apple.product-type.app-extension': '"wrapper.app-extension"', 'com.apple.product-type.bundle': '"wrapper.plug-in"', 'com.apple.product-type.tool': '"compiled.mach-o.dylib"', 'com.apple.product-type.library.dynamic': '"compiled.mach-o.dylib"', 'com.apple.product-type.framework': '"wrapper.framework"', 'com.apple.product-type.library.static': '"archive.ar"', 'com.apple.product-type.bundle.unit-test': '"wrapper.cfbundle"', 'com.apple.product-type.application.watchapp': '"wrapper.application"', 'com.apple.product-type.application.watchapp2': '"wrapper.application"', 'com.apple.product-type.watchkit-extension': '"wrapper.app-extension"', 'com.apple.product-type.watchkit2-extension': '"wrapper.app-extension"' }; return FILETYPE_BY_PRODUCTTYPE[productType]; } PBXProject.prototype.getFirstProject = function () { // Get pbxProject container const pbxProjectContainer = this.pbxProjectSection(); // Get first pbxProject UUID const firstProjectUuid = Object.keys(pbxProjectContainer)[0]; // Get first pbxProject const firstProject = pbxProjectContainer[firstProjectUuid]; return { uuid: firstProjectUuid, firstProject }; }; PBXProject.prototype.getFirstTarget = function () { // Get first target's UUID const firstTargetUuid = this.getFirstProject().firstProject.targets[0].value; // Get first pbxNativeTarget const firstTarget = this.pbxNativeTargetSection()[firstTargetUuid]; return { uuid: firstTargetUuid, firstTarget }; }; PBXProject.prototype.getTarget = function (productType) { // Find target by product type const targets = this.getFirstProject().firstProject.targets; const nativeTargets = this.pbxNativeTargetSection(); for (let i = 0; i < targets.length; i++) { const target = targets[i]; const targetUuid = target.value; if (nativeTargets[targetUuid].productType === '"' + productType + '"') { // Get pbxNativeTarget const nativeTarget = this.pbxNativeTargetSection()[targetUuid]; return { uuid: targetUuid, target: nativeTarget }; } } return null; }; /** * NEW ***/ PBXProject.prototype.addToPbxGroupType = function (file, groupKey, groupType) { const group = this.getPBXGroupByKeyAndType(groupKey, groupType); if (group && group.children !== undefined) { if (typeof file === 'string') { // Group Key const childGroup = { value: file }; if (this.getPBXGroupByKey(file)) { childGroup.comment = this.getPBXGroupByKey(file).name; } else if (this.getPBXVariantGroupByKey(file)) { childGroup.comment = this.getPBXVariantGroupByKey(file).name; } group.children.push(childGroup); } else { // File Object group.children.push(pbxGroupChild(file)); } } }; PBXProject.prototype.addToPbxVariantGroup = function (file, groupKey) { this.addToPbxGroupType(file, groupKey, 'PBXVariantGroup'); }; PBXProject.prototype.addToPbxGroup = function (file, groupKey) { this.addToPbxGroupType(file, groupKey, 'PBXGroup'); }; PBXProject.prototype.pbxCreateGroupWithType = function (name, pathName, groupType) { // Create object const model = { isa: '"' + groupType + '"', children: [], name, sourceTree: '""' }; if (pathName) model.path = pathName; const key = this.generateUuid(); // Create comment const commendId = key + '_comment'; // Reject object keys that shouldn't be writable. if (RESTRICTED_OBJECT_KEYS.has(groupType)) { throw new Error(`"${groupType}" is restricted and cannot be used as a group type.`); } // add obj and commentObj to groups; let groups = this.hash.project.objects[groupType]; if (!groups) { groups = this.hash.project.objects[groupType] = {}; } groups[commendId] = name; groups[key] = model; return key; }; PBXProject.prototype.pbxCreateVariantGroup = function (name) { return this.pbxCreateGroupWithType(name, undefined, 'PBXVariantGroup'); }; PBXProject.prototype.pbxCreateGroup = function (name, pathName) { return this.pbxCreateGroupWithType(name, pathName, 'PBXGroup'); }; PBXProject.prototype.removeFromPbxGroupAndType = function (file, groupKey, groupType) { const group = this.getPBXGroupByKeyAndType(groupKey, groupType); if (group) { const groupChildren = group.children; for (const i in groupChildren) { if (pbxGroupChild(file).value === groupChildren[i].value && pbxGroupChild(file).comment === groupChildren[i].comment) { groupChildren.splice(i, 1); break; } } } }; PBXProject.prototype.removeFromPbxGroup = function (file, groupKey) { this.removeFromPbxGroupAndType(file, groupKey, 'PBXGroup'); }; PBXProject.prototype.removeFromPbxVariantGroup = function (file, groupKey) { this.removeFromPbxGroupAndType(file, groupKey, 'PBXVariantGroup'); }; PBXProject.prototype.getPBXGroupByKeyAndType = function (key, groupType) { return this.hash.project.objects[groupType][key]; }; PBXProject.prototype.getPBXGroupByKey = function (key) { return this.hash.project.objects.PBXGroup[key]; }; PBXProject.prototype.getPBXVariantGroupByKey = function (key) { return this.hash.project.objects.PBXVariantGroup[key]; }; PBXProject.prototype.findPBXGroupKeyAndType = function (criteria, groupType) { const groups = this.hash.project.objects[groupType]; let target; for (const key in groups) { // only look for comments if (COMMENT_KEY.test(key)) continue; const group = groups[key]; if (criteria && criteria.path && criteria.name) { if (criteria.path === group.path && criteria.name === group.name) { target = key; break; } } else if (criteria && criteria.path) { if (criteria.path === group.path) { target = key; break; } } else if (criteria && criteria.name) { if (criteria.name === group.name) { target = key; break; } } } return target; }; PBXProject.prototype.findPBXGroupKey = function (criteria) { return this.findPBXGroupKeyAndType(criteria, 'PBXGroup'); }; PBXProject.prototype.findPBXVariantGroupKey = function (criteria) { return this.findPBXGroupKeyAndType(criteria, 'PBXVariantGroup'); }; PBXProject.prototype.addLocalizationVariantGroup = function (name) { const groupKey = this.pbxCreateVariantGroup(name); const resourceGroupKey = this.findPBXGroupKey({ name: 'Resources' }); this.addToPbxGroup(groupKey, resourceGroupKey); const localizationVariantGroup = { uuid: this.generateUuid(), fileRef: groupKey, basename: name }; this.addToPbxBuildFileSection(localizationVariantGroup); // PBXBuildFile this.addToPbxResourcesBuildPhase(localizationVariantGroup); // PBXResourcesBuildPhase return localizationVariantGroup; }; PBXProject.prototype.addKnownRegion = function (name) { if (!this.pbxProjectSection()[this.getFirstProject().uuid].knownRegions) { this.pbxProjectSection()[this.getFirstProject().uuid].knownRegions = []; } if (!this.hasKnownRegion(name)) { this.pbxProjectSection()[this.getFirstProject().uuid].knownRegions.push(name); } }; PBXProject.prototype.removeKnownRegion = function (name) { const regions = this.pbxProjectSection()[this.getFirstProject().uuid].knownRegions; if (regions) { for (let i = 0; i < regions.length; i++) { if (regions[i] === name) { regions.splice(i, 1); break; } } this.pbxProjectSection()[this.getFirstProject().uuid].knownRegions = regions; } }; PBXProject.prototype.hasKnownRegion = function (name) { const regions = this.pbxProjectSection()[this.getFirstProject().uuid].knownRegions; if (regions) { for (const i in regions) { if (regions[i] === name) { return true; } } } return false; }; PBXProject.prototype.getPBXObject = function (name) { return this.hash.project.objects[name]; }; PBXProject.prototype.addFile = function (path, group, opt) { const file = new PBXFile(path, opt); // null is better for early errors if (this.hasFile(file.path)) return null; file.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(file); // PBXFileReference if (this.getPBXGroupByKey(group)) { this.addToPbxGroup(file, group); // PBXGroup } else if (this.getPBXVariantGroupByKey(group)) { this.addToPbxVariantGroup(file, group); // PBXVariantGroup } return file; }; PBXProject.prototype.removeFile = function (path, group, opt) { const file = new PBXFile(path, opt); this.removeFromPbxFileReferenceSection(file); // PBXFileReference if (this.getPBXGroupByKey(group)) { this.removeFromPbxGroup(file, group); // PBXGroup } else if (this.getPBXVariantGroupByKey(group)) { this.removeFromPbxVariantGroup(file, group); // PBXVariantGroup } return file; }; PBXProject.prototype.getBuildProperty = function (prop, build, targetName) { let target; const validConfigs = []; if (targetName) { const target = this.pbxTargetByName(targetName); const targetBuildConfigs = target && target.buildConfigurationList; const xcConfigList = this.pbxXCConfigurationList(); // Collect the UUID's from the configuration of our target for (const configName in xcConfigList) { if (!COMMENT_KEY.test(configName) && targetBuildConfigs === configName) { const buildVariants = xcConfigList[configName].buildConfigurations; for (const item of buildVariants) { validConfigs.push(item.value); } break; } } } const configs = this.pbxXCBuildConfigurationSection(); for (const configName in configs) { if (!COMMENT_KEY.test(configName)) { if (targetName && !validConfigs.includes(configName)) continue; const config = configs[configName]; if ((build && config.name === build) || (build === undefined)) { if (config.buildSettings[prop] !== undefined) { target = config.buildSettings[prop]; } } } } return target; }; PBXProject.prototype.getBuildConfigByName = function (name) { const target = {}; const configs = this.pbxXCBuildConfigurationSection(); for (const configName in configs) { if (!COMMENT_KEY.test(configName)) { const config = configs[configName]; if (config.name === name) { target[configName] = config; } } } return target; }; PBXProject.prototype.addDataModelDocument = function (filePath, group, opt) { if (!group) { group = 'Resources'; } if (!this.getPBXGroupByKey(group)) { group = this.findPBXGroupKey({ name: group }); } const file = new PBXFile(filePath, opt); if (!file || this.hasFile(file.path)) return null; file.fileRef = this.generateUuid(); this.addToPbxGroup(file, group); if (!file) return false; file.target = opt ? opt.target : undefined; file.uuid = this.generateUuid(); this.addToPbxBuildFileSection(file); this.addToPbxSourcesBuildPhase(file); file.models = []; let currentVersionName; const modelFiles = fs.readdirSync(file.path); for (const index in modelFiles) { const modelFileName = modelFiles[index]; const modelFilePath = path.join(filePath, modelFileName); if (modelFileName === '.xccurrentversion') { currentVersionName = plist.readFileSync(modelFilePath)._XCCurrentVersionName; continue; } const modelFile = new PBXFile(modelFilePath); modelFile.fileRef = this.generateUuid(); this.addToPbxFileReferenceSection(modelFile); file.models.push(modelFile); if (currentVersionName && currentVersionName === modelFileName) { file.currentModel = modelFile; } } if (!file.currentModel) { file.currentModel = file.models[0]; } this.addToXcVersionGroupSection(file); return file; }; PBXProject.prototype.addTargetAttribute = function (prop, value, target) { const attributes = this.getFirstProject().firstProject.attributes; if (attributes.TargetAttributes === undefined) { attributes.TargetAttributes = {}; } target = target || this.getFirstTarget(); // Reject object keys that shouldn't be writable. if (RESTRICTED_OBJECT_KEYS.has(target.uuid)) { throw new Error(`"${target.uuid}" is restricted and cannot be used as a uuid.`); } if (attributes.TargetAttributes[target.uuid] === undefined) { attributes.TargetAttributes[target.uuid] = {}; } attributes.TargetAttributes[target.uuid][prop] = value; }; PBXProject.prototype.removeTargetAttribute = function (prop, target) { const attributes = this.getFirstProject().firstProject.attributes; target = target || this.getFirstTarget(); if (attributes.TargetAttributes && attributes.TargetAttributes[target.uuid]) { delete attributes.TargetAttributes[target.uuid][prop]; } }; module.exports = PBXProject;