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

Podfile.js 13KB

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