Podfile.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. var fs = require('fs'),
  18. path = require('path'),
  19. util = require('util'),
  20. events = require('cordova-common').events,
  21. Q = require('q'),
  22. superspawn = require('cordova-common').superspawn,
  23. CordovaError = require('cordova-common').CordovaError;
  24. Podfile.FILENAME = 'Podfile';
  25. function Podfile(podFilePath, projectName) {
  26. this.podToken = '##INSERT_POD##';
  27. this.path = podFilePath;
  28. this.projectName = projectName;
  29. this.contents = null;
  30. this.pods = null;
  31. this.__dirty = false;
  32. // check whether it is named Podfile
  33. var filename = this.path.split(path.sep).pop();
  34. if (filename !== Podfile.FILENAME) {
  35. throw new CordovaError(util.format('Podfile: The file at %s is not `%s`.', this.path, Podfile.FILENAME));
  36. }
  37. if (!projectName) {
  38. throw new CordovaError('Podfile: The projectName was not specified in the constructor.');
  39. }
  40. if (!fs.existsSync(this.path)) {
  41. events.emit('verbose', util.format('Podfile: The file at %s does not exist.', this.path));
  42. events.emit('verbose', 'Creating new Podfile in platforms/ios');
  43. this.clear();
  44. this.write();
  45. } else {
  46. events.emit('verbose', 'Podfile found in platforms/ios');
  47. // parse for pods
  48. this.pods = this.__parseForPods(fs.readFileSync(this.path, 'utf8'));
  49. }
  50. }
  51. Podfile.prototype.__parseForPods = function(text) {
  52. // split by \n
  53. var arr = text.split('\n');
  54. // aim is to match (space insignificant around the comma, comma optional):
  55. // pod 'Foobar', '1.2'
  56. // pod 'Foobar', 'abc 123 1.2'
  57. // pod 'PonyDebugger', :configurations => ['Debug', 'Beta']
  58. var podRE = new RegExp('pod \'(\\w+)\'\\s*,?\\s*(.*)');
  59. // only grab lines that don't have the pod spec'
  60. return arr.filter(function(line) {
  61. var m = podRE.exec(line);
  62. return (m !== null);
  63. })
  64. .reduce(function(obj, line){
  65. var m = podRE.exec(line);
  66. if (m !== null) {
  67. // strip out any single quotes around the value m[2]
  68. var podSpec = m[2].replace(/^\'|\'$/g, '');
  69. obj[m[1]] = podSpec; // i.e pod 'Foo', '1.2' ==> { 'Foo' : '1.2'}
  70. }
  71. return obj;
  72. }, {});
  73. };
  74. Podfile.prototype.getTemplate = function() {
  75. return util.format(
  76. '# DO NOT MODIFY -- auto-generated by Apache Cordova\n' +
  77. 'platform :ios, \'8.0\'\n' +
  78. 'target \'%s\' do\n' +
  79. '\tproject \'%s.xcodeproj\'\n' +
  80. '%s\n' +
  81. 'end\n',
  82. this.projectName, this.projectName, this.podToken);
  83. };
  84. Podfile.prototype.addSpec = function(name, spec) {
  85. name = name || '';
  86. spec = spec; // optional
  87. if (!name.length) { // blank names are not allowed
  88. throw new CordovaError('Podfile addSpec: name is not specified.');
  89. }
  90. this.pods[name] = spec;
  91. this.__dirty = true;
  92. events.emit('verbose', util.format('Added pod line for `%s`', name));
  93. };
  94. Podfile.prototype.removeSpec = function(name) {
  95. if (this.existsSpec(name)) {
  96. delete this.pods[name];
  97. this.__dirty = true;
  98. }
  99. events.emit('verbose', util.format('Removed pod line for `%s`', name));
  100. };
  101. Podfile.prototype.getSpec = function(name) {
  102. return this.pods[name];
  103. };
  104. Podfile.prototype.existsSpec = function(name) {
  105. return (name in this.pods);
  106. };
  107. Podfile.prototype.clear = function() {
  108. this.pods = {};
  109. this.__dirty = true;
  110. };
  111. Podfile.prototype.destroy = function() {
  112. fs.unlinkSync(this.path);
  113. events.emit('verbose', util.format('Deleted `%s`', this.path));
  114. };
  115. Podfile.prototype.write = function() {
  116. var text = this.getTemplate();
  117. var self = this;
  118. var podsString =
  119. Object.keys(this.pods).map(function(key) {
  120. var name = key;
  121. var spec = self.pods[key];
  122. if (spec.length) {
  123. if (spec.indexOf(':') === 0) {
  124. // don't quote it, it's a specification (starts with ':')
  125. return util.format('\tpod \'%s\', %s', name, spec);
  126. } else {
  127. // quote it, it's a version
  128. return util.format('\tpod \'%s\', \'%s\'', name, spec);
  129. }
  130. } else {
  131. return util.format('\tpod \'%s\'', name);
  132. }
  133. })
  134. .join('\n');
  135. text = text.replace(this.podToken, podsString);
  136. fs.writeFileSync(this.path, text, 'utf8');
  137. this.__dirty = false;
  138. events.emit('verbose', 'Wrote to Podfile.');
  139. };
  140. Podfile.prototype.isDirty = function() {
  141. return this.__dirty;
  142. };
  143. Podfile.prototype.before_install = function() {
  144. // Template tokens in order: project name, project name, debug | release
  145. var template =
  146. '// DO NOT MODIFY -- auto-generated by Apache Cordova\n' +
  147. '#include "Pods/Target Support Files/Pods-%s/Pods-%s.%s.xcconfig"';
  148. var debugContents = util.format(template, this.projectName, this.projectName, 'debug');
  149. var releaseContents = util.format(template, this.projectName, this.projectName, 'release');
  150. var debugConfigPath = path.join(this.path, '..', 'pods-debug.xcconfig');
  151. var releaseConfigPath = path.join(this.path, '..', 'pods-release.xcconfig');
  152. fs.writeFileSync(debugConfigPath, debugContents, 'utf8');
  153. fs.writeFileSync(releaseConfigPath, releaseContents, 'utf8');
  154. return Q.resolve();
  155. };
  156. Podfile.prototype.install = function(requirementsCheckerFunction) {
  157. var opts = {};
  158. opts.cwd = path.join(this.path, '..'); // parent path of this Podfile
  159. opts.stdio = 'pipe';
  160. var first = true;
  161. var self = this;
  162. if (!requirementsCheckerFunction) {
  163. requirementsCheckerFunction = Q();
  164. }
  165. return requirementsCheckerFunction()
  166. .then(function() {
  167. return self.before_install();
  168. })
  169. .then(function() {
  170. return superspawn.spawn('pod', ['install', '--verbose'], opts)
  171. .progress(function (stdio){
  172. if (stdio.stderr) { console.error(stdio.stderr); }
  173. if (stdio.stdout) {
  174. if (first) {
  175. events.emit('verbose', '==== pod install start ====\n');
  176. first = false;
  177. }
  178. events.emit('verbose', stdio.stdout);
  179. }
  180. });
  181. })
  182. .then(function() { // done
  183. events.emit('verbose', '==== pod install end ====\n');
  184. })
  185. .fail(function(error){
  186. throw error;
  187. });
  188. };
  189. module.exports.Podfile = Podfile;