Github user juliascript commented on a diff in the pull request: https://github.com/apache/cordova-ios/pull/234#discussion_r70545166 --- Diff: bin/templates/scripts/cordova/lib/podMod.js --- @@ -0,0 +1,162 @@ +var fs = require('fs'); +var util = require('util'); +var events = require('cordova-common').events; +var superspawn = require('cordova-common').superspawn; +var CordovaError = require('cordova-common').CordovaError; +var opts = {}; +/* +-- After pods are installed in a .xcworkspace, all existing ios code needs to go into the WORKSPACE file -- will need to + create a workspace file and then embed the Xcode project + + - Holly might have done some work on this, see the docs: + https://github.com/phonegap/phonegap-webview-ios not sure how applicable it can be to our case +*/ +function removeProjectFromPath (pathToProjectFile) { + var array = []; + //remove the project from the path + array = pathToProjectFile.split('/'); + array.pop(); + var path = array.join('/'); + return path; +} + +function createPodfile (projectName, pathToProjectFile) { + var path = removeProjectFromPath(pathToProjectFile); + var pathToPodfile = path + '/Podfile'; + var podfileText = util.format('platform :ios, \'8.0\'\n\ntarget \'%s\' do\n\n project \'%s\'\n\n \n\nend' , projectName, pathToProjectFile); + fs.writeFileSync(pathToPodfile, podfileText); +} + +function editPodfileSync (Podfile, pod, isRemoval) { + var podfileContents = fs.readFileSync(Podfile, 'utf8'); + //split by \n, add in the pod after the project line, shift the rest down + var podfileContentsArray = podfileContents.split('\n'); + var linesInPodfileToKeep = []; + + if (isRemoval) { + podfileContentsArray.forEach (function (lineInPodfile) { + //if the line in Podfile is the pod to rm, don't save it to new array (to be returned) + if (!lineInPodfile.includes(pod)){ + linesInPodfileToKeep.push(lineInPodfile); + } + }); + podfileContents = linesInPodfileToKeep.join('\n'); + } else { + podfileContentsArray.splice(5, 0, pod); + podfileContents = podfileContentsArray.join('\n'); + } + return podfileContents; +} + +function superspawnPodInstall (path, isPathToProjectFile) { + // change working directory for all calls of pod install to platforms/ios + if (isPathToProjectFile){ + //if the path passed leads to the project, and not the dir that contains the proj + //remove the project from the path + path = removeProjectFromPath(path); + } + opts.cwd = path; + superspawn.spawn('pod', ['install'], opts); +} + +function installPodSync (projectName, pathToProjectFile, nameOfPod, podSpec, podsJSON) { + // called from cordova project directory-- when invoked, args are as follows + // projectName = cordovaProject (name) and + // pathToProjectFile = ./path/to/cordovaProject + // nameOfPod = obj.src //from framework tag + // podSpec = obj.spec //from framework tag + // podsJSON = pods.json file in cordovaProjectDir/platforms/ios/ + + // readFileSync will currently truncate the Podfile if it exists + // if a Podfile doesn't exist, one will be created + + // this code will be run during cordova plugin add x -- which has to be run in the cordova project dir + + //----------- + //ERROR + // + //if no podName is specified, console err + if (nameOfPod === '' || nameOfPod === ' '){ + throw new CordovaError('\nERROR: name of pod is not specified\n'); + } + //----------- + + podSpec = podSpec || ''; //spec is optional + + var stringToWrite; //overwrites Podfile + var lineToInjectInPodfile; //adds pod + var path = removeProjectFromPath(pathToProjectFile); + var podfile = path + '/Podfile'; + var podfileExistsInCurrentDirectory = fs.existsSync(podfile); //bool + var podExistsInPodsJSON = podsJSON[nameOfPod]; //bool + var podRequestedForSpecChange; //bool + + if (podSpec === '') { + lineToInjectInPodfile = util.format('pod \'%s\'', nameOfPod); + podRequestedForSpecChange = false; + } else { + if (podExistsInPodsJSON){ + if (podsJSON[nameOfPod].spec == podSpec){ + //----------- + //ERROR + // + // if pod spec is the one already in the Podfile, + // do nothing to the Podfile, return error + throw new CordovaError('\nERROR: pod already installed in Podfile, according to pods.json'); + //------------ + } else { + podRequestedForSpecChange = true; + } + } else { + lineToInjectInPodfile = util.format('pod \'%s\', \'%s\'', nameOfPod, podSpec); + podRequestedForSpecChange = false; + } + } + + //does Podfile exist in the current directory? + if (podfileExistsInCurrentDirectory) { + events.emit('verbose', 'Podfile found in platforms/ios'); + //is the pod already in the Podfile? + if (podExistsInPodsJSON) { + events.emit('verbose', 'Selected pod already exists in Podfile according to pods.json'); + //if pod is in Podfile, is there a change in spec? + if (podRequestedForSpecChange) { + //if spec change requested, replace the line in the Podfile with the correct spec + events.emit('verbose', 'Pod requested for spec change'); + } // no change in spec handled above + } else if (!podExistsInPodsJSON) { + //if pod not already in Podfile, inject the line in the existing Podfile + events.emit('verbose', 'Pod not found in Podfile. Injecting now...'); + stringToWrite = editPodfileSync(podfile, lineToInjectInPodfile); + } + } else if (!podfileExistsInCurrentDirectory) { + //create the Podfile and inject the line + events.emit('verbose', 'Creating new Podfile in platforms/ios'); + createPodfile(projectName, pathToProjectFile); + events.emit('verbose', 'Adding pod to Podfile'); + stringToWrite = editPodfileSync(podfile, lineToInjectInPodfile); + } + + if (stringToWrite) { + events.emit('verbose', 'Overwriting Podfile'); + fs.writeFileSync(podfile, stringToWrite); + } else { + //the code should have returned early by now + } + events.emit('verbose', 'Pods installed in xcode workspace in platforms/ios'); +} + +function uninstallPod (projectDirectory, pod) { + //split podfile by \n + //for each, does each.includes(pod)? if so, remove that line + //that will correspond to the line in the podfile, given the amt of pods currently installed in the podfile... + var podfile = projectDirectory + '/Podfile'; + var stringToWrite = editPodfileSync(podfile, pod, true); + fs.writeFileSync(podfile, stringToWrite); +} + +module.exports = { + installPodSync : installPodSync, --- End diff -- ð
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org For additional commands, e-mail: dev-h...@cordova.apache.org