Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

Podfile.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. 'use strict';
  18. var fs = require('fs');
  19. var path = require('path');
  20. var util = require('util');
  21. var events = require('cordova-common').events;
  22. var Q = require('q');
  23. var superspawn = require('cordova-common').superspawn;
  24. var CordovaError = require('cordova-common').CordovaError;
  25. Podfile.FILENAME = 'Podfile';
  26. Podfile.declarationRegexpMap = {
  27. 'use_frameworks!': 'use[-_]frameworks!?',
  28. 'inhibit_all_warnings!': 'inhibit[-_]all[-_]warnings!?'
  29. };
  30. function Podfile (podFilePath, projectName, minDeploymentTarget) {
  31. this.declarationToken = '##INSERT_DECLARATION##';
  32. this.sourceToken = '##INSERT_SOURCE##';
  33. this.podToken = '##INSERT_POD##';
  34. this.path = podFilePath;
  35. this.projectName = projectName;
  36. this.minDeploymentTarget = minDeploymentTarget || '10.0';
  37. this.contents = null;
  38. this.sources = null;
  39. this.declarations = null;
  40. this.pods = null;
  41. this.__dirty = false;
  42. // check whether it is named Podfile
  43. var filename = this.path.split(path.sep).pop();
  44. if (filename !== Podfile.FILENAME) {
  45. throw new CordovaError(util.format('Podfile: The file at %s is not `%s`.', this.path, Podfile.FILENAME));
  46. }
  47. if (!projectName) {
  48. throw new CordovaError('Podfile: The projectName was not specified in the constructor.');
  49. }
  50. if (!fs.existsSync(this.path)) {
  51. events.emit('verbose', util.format('Podfile: The file at %s does not exist.', this.path));
  52. events.emit('verbose', 'Creating new Podfile in platforms/ios');
  53. this.clear();
  54. this.write();
  55. } else {
  56. events.emit('verbose', 'Podfile found in platforms/ios');
  57. // parse for pods
  58. var fileText = fs.readFileSync(this.path, 'utf8');
  59. this.declarations = this.__parseForDeclarations(fileText);
  60. this.sources = this.__parseForSources(fileText);
  61. this.pods = this.__parseForPods(fileText);
  62. }
  63. }
  64. Podfile.prototype.__parseForDeclarations = function (text) {
  65. // split by \n
  66. var arr = text.split('\n');
  67. // getting lines between "platform :ios, '10.0'"" and "target 'HelloCordova'" do
  68. var declarationsPreRE = new RegExp('platform :ios,\\s+\'[^\']+\'');
  69. var declarationsPostRE = new RegExp('target\\s+\'[^\']+\'\\s+do');
  70. var declarationRE = new RegExp('^\\s*[^#]');
  71. return arr.reduce(function (acc, line) {
  72. switch (acc.state) {
  73. case 0:
  74. if (declarationsPreRE.exec(line)) {
  75. acc.state = 1; // Start to read
  76. }
  77. break;
  78. case 1:
  79. if (declarationsPostRE.exec(line)) {
  80. acc.state = 2; // Finish to read
  81. } else {
  82. acc.lines.push(line);
  83. }
  84. break;
  85. case 2:
  86. default:
  87. // do nothing;
  88. }
  89. return acc;
  90. }, { state: 0, lines: [] })
  91. .lines
  92. .filter(function (line) {
  93. return declarationRE.exec(line);
  94. })
  95. .reduce(function (obj, line) {
  96. obj[line] = line;
  97. return obj;
  98. }, {});
  99. };
  100. Podfile.prototype.__parseForSources = function (text) {
  101. // split by \n
  102. var arr = text.split('\n');
  103. var sourceRE = new RegExp('source \'(.*)\'');
  104. return arr.filter(function (line) {
  105. var m = sourceRE.exec(line);
  106. return (m !== null);
  107. })
  108. .reduce(function (obj, line) {
  109. var m = sourceRE.exec(line);
  110. if (m !== null) {
  111. var source = m[1];
  112. obj[source] = source;
  113. }
  114. return obj;
  115. }, {});
  116. };
  117. Podfile.prototype.__parseForPods = function (text) {
  118. // split by \n
  119. var arr = text.split('\n');
  120. // aim is to match (space insignificant around the comma, comma optional):
  121. // pod 'Foobar', '1.2'
  122. // pod 'Foobar', 'abc 123 1.2'
  123. // pod 'PonyDebugger', :configurations => ['Debug', 'Beta']
  124. // var podRE = new RegExp('pod \'([^\']*)\'\\s*,?\\s*(.*)');
  125. var podRE = new RegExp('pod \'([^\']*)\'\\s*(?:,\\s*\'([^\']*)\'\\s*)?,?\\s*(.*)');
  126. // only grab lines that don't have the pod spec'
  127. return arr.filter(function (line) {
  128. var m = podRE.exec(line);
  129. return (m !== null);
  130. })
  131. .reduce(function (obj, line) {
  132. var m = podRE.exec(line);
  133. if (m !== null) {
  134. var podspec = {
  135. name: m[1]
  136. };
  137. if (m[2]) {
  138. podspec.spec = m[2];
  139. }
  140. if (m[3]) {
  141. podspec.options = m[3];
  142. }
  143. obj[m[1]] = podspec; // i.e pod 'Foo', '1.2' ==> { 'Foo' : '1.2'}
  144. }
  145. return obj;
  146. }, {});
  147. };
  148. Podfile.prototype.escapeSingleQuotes = function (string) {
  149. return string.replace('\'', '\\\'');
  150. };
  151. Podfile.prototype.getTemplate = function () {
  152. // Escaping possible ' in the project name
  153. var projectName = this.escapeSingleQuotes(this.projectName);
  154. return util.format(
  155. '# DO NOT MODIFY -- auto-generated by Apache Cordova\n' +
  156. '%s\n' +
  157. 'platform :ios, \'%s\'\n' +
  158. '%s\n' +
  159. 'target \'%s\' do\n' +
  160. '\tproject \'%s.xcodeproj\'\n' +
  161. '%s\n' +
  162. 'end\n',
  163. this.sourceToken, this.minDeploymentTarget, this.declarationToken, projectName, projectName, this.podToken);
  164. };
  165. Podfile.prototype.addSpec = function (name, spec) {
  166. name = name || '';
  167. // optional
  168. spec = spec; /* eslint no-self-assign : 0 */
  169. if (!name.length) { // blank names are not allowed
  170. throw new CordovaError('Podfile addSpec: name is not specified.');
  171. }
  172. if (typeof spec === 'string') {
  173. if (spec.startsWith(':')) {
  174. spec = { name: name, options: spec };
  175. } else {
  176. spec = { name: name, spec: spec };
  177. }
  178. }
  179. this.pods[name] = spec;
  180. this.__dirty = true;
  181. events.emit('verbose', util.format('Added pod line for `%s`', name));
  182. };
  183. Podfile.prototype.removeSpec = function (name) {
  184. if (this.existsSpec(name)) {
  185. delete this.pods[name];
  186. this.__dirty = true;
  187. }
  188. events.emit('verbose', util.format('Removed pod line for `%s`', name));
  189. };
  190. Podfile.prototype.getSpec = function (name) {
  191. return this.pods[name];
  192. };
  193. Podfile.prototype.existsSpec = function (name) {
  194. return (name in this.pods);
  195. };
  196. Podfile.prototype.addSource = function (src) {
  197. this.sources[src] = src;
  198. this.__dirty = true;
  199. events.emit('verbose', util.format('Added source line for `%s`', src));
  200. };
  201. Podfile.prototype.removeSource = function (src) {
  202. if (this.existsSource(src)) {
  203. delete this.sources[src];
  204. this.__dirty = true;
  205. }
  206. events.emit('verbose', util.format('Removed source line for `%s`', src));
  207. };
  208. Podfile.prototype.existsSource = function (src) {
  209. return (src in this.sources);
  210. };
  211. Podfile.prototype.addDeclaration = function (declaration) {
  212. this.declarations[declaration] = declaration;
  213. this.__dirty = true;
  214. events.emit('verbose', util.format('Added declaration line for `%s`', declaration));
  215. };
  216. Podfile.prototype.removeDeclaration = function (declaration) {
  217. if (this.existsDeclaration(declaration)) {
  218. delete this.declarations[declaration];
  219. this.__dirty = true;
  220. }
  221. events.emit('verbose', util.format('Removed source line for `%s`', declaration));
  222. };
  223. Podfile.proofDeclaration = function (declaration) {
  224. var list = Object.keys(Podfile.declarationRegexpMap).filter(function (key) {
  225. var regexp = new RegExp(Podfile.declarationRegexpMap[key]);
  226. return regexp.test(declaration);
  227. });
  228. if (list.length > 0) {
  229. return list[0];
  230. }
  231. return declaration;
  232. };
  233. Podfile.prototype.existsDeclaration = function (declaration) {
  234. return (declaration in this.declarations);
  235. };
  236. Podfile.prototype.clear = function () {
  237. this.sources = {};
  238. this.declarations = {};
  239. this.pods = {};
  240. this.__dirty = true;
  241. };
  242. Podfile.prototype.destroy = function () {
  243. fs.unlinkSync(this.path);
  244. events.emit('verbose', util.format('Deleted `%s`', this.path));
  245. };
  246. Podfile.prototype.write = function () {
  247. var text = this.getTemplate();
  248. var self = this;
  249. var podsString =
  250. Object.keys(this.pods).map(function (key) {
  251. var name = key;
  252. var json = self.pods[key];
  253. if (typeof json === 'string') { // compatibility for using framework tag.
  254. var spec = json;
  255. if (spec.length) {
  256. if (spec.indexOf(':') === 0) {
  257. // don't quote it, it's a specification (starts with ':')
  258. return util.format('\tpod \'%s\', %s', name, spec);
  259. } else {
  260. // quote it, it's a version
  261. return util.format('\tpod \'%s\', \'%s\'', name, spec);
  262. }
  263. } else {
  264. return util.format('\tpod \'%s\'', name);
  265. }
  266. } else {
  267. var list = ['\'' + name + '\''];
  268. if ('spec' in json && json.spec.length) {
  269. list.push('\'' + json.spec + '\'');
  270. }
  271. var options = ['tag', 'branch', 'commit', 'git', 'podspec'].filter(function (tag) {
  272. return tag in json;
  273. }).map(function (tag) {
  274. return ':' + tag + ' => \'' + json[tag] + '\'';
  275. });
  276. if ('configurations' in json) {
  277. options.push(':configurations => [' + json['configurations'].split(',').map(function (conf) { return '\'' + conf.trim() + '\''; }).join(',') + ']');
  278. }
  279. if ('options' in json) {
  280. options = [json.options];
  281. }
  282. if (options.length > 0) {
  283. list.push(options.join(', '));
  284. }
  285. return util.format('\tpod %s', list.join(', '));
  286. }
  287. }).join('\n');
  288. var sourcesString =
  289. Object.keys(this.sources).map(function (key) {
  290. var source = self.sources[key];
  291. return util.format('source \'%s\'', source);
  292. }).join('\n');
  293. var declarationString =
  294. Object.keys(this.declarations).map(function (key) {
  295. var declaration = self.declarations[key];
  296. return declaration;
  297. }).join('\n');
  298. text = text.replace(this.podToken, podsString)
  299. .replace(this.sourceToken, sourcesString)
  300. .replace(this.declarationToken, declarationString);
  301. fs.writeFileSync(this.path, text, 'utf8');
  302. this.__dirty = false;
  303. events.emit('verbose', 'Wrote to Podfile.');
  304. };
  305. Podfile.prototype.isDirty = function () {
  306. return this.__dirty;
  307. };
  308. Podfile.prototype.before_install = function (toolOptions) {
  309. toolOptions = toolOptions || {};
  310. // Template tokens in order: project name, project name, debug | release
  311. var template =
  312. '// DO NOT MODIFY -- auto-generated by Apache Cordova\n' +
  313. '#include "Pods/Target Support Files/Pods-%s/Pods-%s.%s.xcconfig"';
  314. var debugContents = util.format(template, this.projectName, this.projectName, 'debug');
  315. var releaseContents = util.format(template, this.projectName, this.projectName, 'release');
  316. var debugConfigPath = path.join(this.path, '..', 'pods-debug.xcconfig');
  317. var releaseConfigPath = path.join(this.path, '..', 'pods-release.xcconfig');
  318. fs.writeFileSync(debugConfigPath, debugContents, 'utf8');
  319. fs.writeFileSync(releaseConfigPath, releaseContents, 'utf8');
  320. return Q.resolve(toolOptions);
  321. };
  322. Podfile.prototype.install = function (requirementsCheckerFunction) {
  323. var opts = {};
  324. opts.cwd = path.join(this.path, '..'); // parent path of this Podfile
  325. opts.stdio = 'pipe';
  326. opts.printCommand = true;
  327. var first = true;
  328. var self = this;
  329. if (!requirementsCheckerFunction) {
  330. requirementsCheckerFunction = Q();
  331. }
  332. return requirementsCheckerFunction()
  333. .then(function (toolOptions) {
  334. return self.before_install(toolOptions);
  335. })
  336. .then(function (toolOptions) {
  337. if (toolOptions.ignore) {
  338. events.emit('verbose', '==== pod install start ====\n');
  339. events.emit('verbose', toolOptions.ignoreMessage);
  340. return Q.resolve();
  341. } else {
  342. return superspawn.spawn('pod', ['install', '--verbose'], opts)
  343. .progress(function (stdio) {
  344. if (stdio.stderr) { console.error(stdio.stderr); }
  345. if (stdio.stdout) {
  346. if (first) {
  347. events.emit('verbose', '==== pod install start ====\n');
  348. first = false;
  349. }
  350. events.emit('verbose', stdio.stdout);
  351. }
  352. });
  353. }
  354. })
  355. .then(function () { // done
  356. events.emit('verbose', '==== pod install end ====\n');
  357. });
  358. };
  359. module.exports.Podfile = Podfile;