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

PodsJson.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. var path = require('path');
  19. var util = require('util');
  20. var events = require('cordova-common').events;
  21. var CordovaError = require('cordova-common').CordovaError;
  22. PodsJson.FILENAME = 'pods.json';
  23. PodsJson.LIBRARY = 'libraries';
  24. PodsJson.SOURCE = 'sources';
  25. PodsJson.DECLARATION = 'declarations';
  26. function PodsJson (podsJsonPath) {
  27. this.path = podsJsonPath;
  28. this.contents = null;
  29. this.__dirty = false;
  30. var filename = this.path.split(path.sep).pop();
  31. if (filename !== PodsJson.FILENAME) {
  32. throw new CordovaError(util.format('PodsJson: The file at %s is not `%s`.', this.path, PodsJson.FILENAME));
  33. }
  34. if (!fs.existsSync(this.path)) {
  35. events.emit('verbose', util.format('pods.json: The file at %s does not exist.', this.path));
  36. events.emit('verbose', 'Creating new pods.json in platforms/ios');
  37. this.clear();
  38. this.write();
  39. } else {
  40. events.emit('verbose', 'pods.json found in platforms/ios');
  41. // load contents
  42. var contents = fs.readFileSync(this.path, 'utf8');
  43. this.contents = JSON.parse(contents);
  44. }
  45. this.__updateFormatIfNecessary();
  46. }
  47. PodsJson.prototype.__isOldFormat = function () {
  48. if (this.contents !== null) {
  49. if (this.contents.declarations === undefined ||
  50. this.contents.sources === undefined ||
  51. this.contents.libraries === undefined) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. };
  57. PodsJson.prototype.__updateFormatIfNecessary = function () {
  58. if (this.__isOldFormat()) {
  59. this.contents = {
  60. declarations: {},
  61. sources: {},
  62. libraries: this.contents
  63. };
  64. this.__dirty = true;
  65. events.emit('verbose', 'Update format of pods.json');
  66. }
  67. };
  68. PodsJson.prototype.getLibraries = function () {
  69. return this.contents[PodsJson.LIBRARY];
  70. };
  71. PodsJson.prototype.__get = function (kind, name) {
  72. return this.contents[kind][name];
  73. };
  74. PodsJson.prototype.getLibrary = function (name) {
  75. return this.__get(PodsJson.LIBRARY, name);
  76. };
  77. PodsJson.prototype.getSource = function (name) {
  78. return this.__get(PodsJson.SOURCE, name);
  79. };
  80. PodsJson.prototype.getDeclaration = function (name) {
  81. return this.__get(PodsJson.DECLARATION, name);
  82. };
  83. PodsJson.prototype.__remove = function (kind, name) {
  84. if (this.contents[kind][name]) {
  85. delete this.contents[kind][name];
  86. this.__dirty = true;
  87. events.emit('verbose', util.format('Remove from pods.json for `%s` - `%s`', name));
  88. }
  89. };
  90. PodsJson.prototype.removeLibrary = function (name) {
  91. this.__remove(PodsJson.LIBRARY, name);
  92. };
  93. PodsJson.prototype.removeSource = function (name) {
  94. this.__remove(PodsJson.SOURCE, name);
  95. };
  96. PodsJson.prototype.removeDeclaration = function (name) {
  97. this.__remove(PodsJson.DECLARATION, name);
  98. };
  99. PodsJson.prototype.clear = function () {
  100. this.contents = {
  101. declarations: {},
  102. sources: {},
  103. libraries: {}
  104. };
  105. this.__dirty = true;
  106. };
  107. PodsJson.prototype.destroy = function () {
  108. fs.unlinkSync(this.path);
  109. events.emit('verbose', util.format('Deleted `%s`', this.path));
  110. };
  111. PodsJson.prototype.write = function () {
  112. if (this.contents) {
  113. fs.writeFileSync(this.path, JSON.stringify(this.contents, null, 4));
  114. this.__dirty = false;
  115. events.emit('verbose', 'Wrote to pods.json.');
  116. }
  117. };
  118. PodsJson.prototype.__increment = function (kind, name) {
  119. var val = this.__get(kind, name);
  120. if (val) {
  121. val.count++;
  122. }
  123. };
  124. PodsJson.prototype.incrementLibrary = function (name) {
  125. this.__increment(PodsJson.LIBRARY, name);
  126. };
  127. PodsJson.prototype.incrementSource = function (name) {
  128. this.__increment(PodsJson.SOURCE, name);
  129. };
  130. PodsJson.prototype.incrementDeclaration = function (name) {
  131. this.__increment(PodsJson.DECLARATION, name);
  132. };
  133. PodsJson.prototype.__decrement = function (kind, name) {
  134. var val = this.__get(kind, name);
  135. if (val) {
  136. val.count--;
  137. if (val.count <= 0) {
  138. this.__remove(kind, name);
  139. }
  140. }
  141. };
  142. PodsJson.prototype.decrementLibrary = function (name) {
  143. this.__decrement(PodsJson.LIBRARY, name);
  144. };
  145. PodsJson.prototype.decrementSource = function (name) {
  146. this.__decrement(PodsJson.SOURCE, name);
  147. };
  148. PodsJson.prototype.decrementDeclaration = function (name) {
  149. this.__decrement(PodsJson.DECLARATION, name);
  150. };
  151. PodsJson.prototype.__setJson = function (kind, name, json) {
  152. this.contents[kind][name] = Object.assign({}, json);
  153. this.__dirty = true;
  154. events.emit('verbose', util.format('Set pods.json for `%s` - `%s`', kind, name));
  155. };
  156. // sample json for library
  157. // { name: "Eureka", spec: "4.2.0", "swift-version": "4.1", count: 1 }
  158. PodsJson.prototype.setJsonLibrary = function (name, json) {
  159. this.__setJson(PodsJson.LIBRARY, name, json);
  160. };
  161. // sample json for source
  162. // { source: "https://github.com/brightcove/BrightcoveSpecs.git", count: 1 }
  163. PodsJson.prototype.setJsonSource = function (name, json) {
  164. this.__setJson(PodsJson.SOURCE, name, json);
  165. };
  166. // sample json for declaration
  167. // { declaration: ""}
  168. PodsJson.prototype.setJsonDeclaration = function (name, json) {
  169. this.__setJson(PodsJson.DECLARATION, name, json);
  170. };
  171. PodsJson.prototype.isDirty = function () {
  172. return this.__dirty;
  173. };
  174. module.exports.PodsJson = PodsJson;