Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

pbxWriter.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 pbxProj = require('./pbxProject'),
  18. util = require('util'),
  19. f = util.format,
  20. INDENT = '\t',
  21. COMMENT_KEY = /_comment$/,
  22. QUOTED = /^"(.*)"$/,
  23. EventEmitter = require('events').EventEmitter
  24. // indentation
  25. function i(x) {
  26. if (x <=0)
  27. return '';
  28. else
  29. return INDENT + i(x-1);
  30. }
  31. function comment(key, parent) {
  32. var text = parent[key + '_comment'];
  33. if (text)
  34. return text;
  35. else
  36. return null;
  37. }
  38. // copied from underscore
  39. function isObject(obj) {
  40. return obj === Object(obj)
  41. }
  42. function isArray(obj) {
  43. return Array.isArray(obj)
  44. }
  45. function pbxWriter(contents, options) {
  46. if (!options) {
  47. options = {}
  48. }
  49. if (options.omitEmptyValues === undefined) {
  50. options.omitEmptyValues = false
  51. }
  52. this.contents = contents;
  53. this.sync = false;
  54. this.indentLevel = 0;
  55. this.omitEmptyValues = options.omitEmptyValues
  56. }
  57. util.inherits(pbxWriter, EventEmitter);
  58. pbxWriter.prototype.write = function (str) {
  59. var fmt = f.apply(null, arguments);
  60. if (this.sync) {
  61. this.buffer += f("%s%s", i(this.indentLevel), fmt);
  62. } else {
  63. // do stream write
  64. }
  65. }
  66. pbxWriter.prototype.writeFlush = function (str) {
  67. var oldIndent = this.indentLevel;
  68. this.indentLevel = 0;
  69. this.write.apply(this, arguments)
  70. this.indentLevel = oldIndent;
  71. }
  72. pbxWriter.prototype.writeSync = function () {
  73. this.sync = true;
  74. this.buffer = "";
  75. this.writeHeadComment();
  76. this.writeProject();
  77. return this.buffer;
  78. }
  79. pbxWriter.prototype.writeHeadComment = function () {
  80. if (this.contents.headComment) {
  81. this.write("// %s\n", this.contents.headComment)
  82. }
  83. }
  84. pbxWriter.prototype.writeProject = function () {
  85. var proj = this.contents.project,
  86. key, cmt, obj;
  87. this.write("{\n")
  88. if (proj) {
  89. this.indentLevel++;
  90. for (key in proj) {
  91. // skip comments
  92. if (COMMENT_KEY.test(key)) continue;
  93. cmt = comment(key, proj);
  94. obj = proj[key];
  95. if (isArray(obj)) {
  96. this.writeArray(obj, key)
  97. } else if (isObject(obj)) {
  98. this.write("%s = {\n", key);
  99. this.indentLevel++;
  100. if (key === 'objects') {
  101. this.writeObjectsSections(obj)
  102. } else {
  103. this.writeObject(obj)
  104. }
  105. this.indentLevel--;
  106. this.write("};\n");
  107. } else if (this.omitEmptyValues && (obj === undefined || obj === null)) {
  108. continue;
  109. } else if (cmt) {
  110. this.write("%s = %s /* %s */;\n", key, obj, cmt)
  111. } else {
  112. this.write("%s = %s;\n", key, obj)
  113. }
  114. }
  115. this.indentLevel--;
  116. }
  117. this.write("}\n")
  118. }
  119. pbxWriter.prototype.writeObject = function (object) {
  120. var key, obj, cmt;
  121. for (key in object) {
  122. if (COMMENT_KEY.test(key)) continue;
  123. cmt = comment(key, object);
  124. obj = object[key];
  125. if (isArray(obj)) {
  126. this.writeArray(obj, key)
  127. } else if (isObject(obj)) {
  128. this.write("%s = {\n", key);
  129. this.indentLevel++;
  130. this.writeObject(obj)
  131. this.indentLevel--;
  132. this.write("};\n");
  133. } else {
  134. if (this.omitEmptyValues && (obj === undefined || obj === null)) {
  135. continue;
  136. } else if (cmt) {
  137. this.write("%s = %s /* %s */;\n", key, obj, cmt)
  138. } else {
  139. this.write("%s = %s;\n", key, obj)
  140. }
  141. }
  142. }
  143. }
  144. pbxWriter.prototype.writeObjectsSections = function (objects) {
  145. var first = true,
  146. key, obj;
  147. for (key in objects) {
  148. if (!first) {
  149. this.writeFlush("\n")
  150. } else {
  151. first = false;
  152. }
  153. obj = objects[key];
  154. if (isObject(obj)) {
  155. this.writeSectionComment(key, true);
  156. this.writeSection(obj);
  157. this.writeSectionComment(key, false);
  158. }
  159. }
  160. }
  161. pbxWriter.prototype.writeArray = function (arr, name) {
  162. var i, entry;
  163. this.write("%s = (\n", name);
  164. this.indentLevel++;
  165. for (i=0; i < arr.length; i++) {
  166. entry = arr[i]
  167. if (entry.value && entry.comment) {
  168. this.write('%s /* %s */,\n', entry.value, entry.comment);
  169. } else if (isObject(entry)) {
  170. this.write('{\n');
  171. this.indentLevel++;
  172. this.writeObject(entry);
  173. this.indentLevel--;
  174. this.write('},\n');
  175. } else {
  176. this.write('%s,\n', entry);
  177. }
  178. }
  179. this.indentLevel--;
  180. this.write(");\n");
  181. }
  182. pbxWriter.prototype.writeSectionComment = function (name, begin) {
  183. if (begin) {
  184. this.writeFlush("/* Begin %s section */\n", name)
  185. } else { // end
  186. this.writeFlush("/* End %s section */\n", name)
  187. }
  188. }
  189. pbxWriter.prototype.writeSection = function (section) {
  190. var key, obj, cmt;
  191. // section should only contain objects
  192. for (key in section) {
  193. if (COMMENT_KEY.test(key)) continue;
  194. cmt = comment(key, section);
  195. obj = section[key]
  196. if (obj.isa == 'PBXBuildFile' || obj.isa == 'PBXFileReference') {
  197. this.writeInlineObject(key, cmt, obj);
  198. } else {
  199. if (cmt) {
  200. this.write("%s /* %s */ = {\n", key, cmt);
  201. } else {
  202. this.write("%s = {\n", key);
  203. }
  204. this.indentLevel++
  205. this.writeObject(obj)
  206. this.indentLevel--
  207. this.write("};\n");
  208. }
  209. }
  210. }
  211. pbxWriter.prototype.writeInlineObject = function (n, d, r) {
  212. var output = [];
  213. var self = this
  214. var inlineObjectHelper = function (name, desc, ref) {
  215. var key, cmt, obj;
  216. if (desc) {
  217. output.push(f("%s /* %s */ = {", name, desc));
  218. } else {
  219. output.push(f("%s = {", name));
  220. }
  221. for (key in ref) {
  222. if (COMMENT_KEY.test(key)) continue;
  223. cmt = comment(key, ref);
  224. obj = ref[key];
  225. if (isArray(obj)) {
  226. output.push(f("%s = (", key));
  227. for (var i=0; i < obj.length; i++) {
  228. output.push(f("%s, ", obj[i]))
  229. }
  230. output.push("); ");
  231. } else if (isObject(obj)) {
  232. inlineObjectHelper(key, cmt, obj)
  233. } else if (self.omitEmptyValues && (obj === undefined || obj === null)) {
  234. continue;
  235. } else if (cmt) {
  236. output.push(f("%s = %s /* %s */; ", key, obj, cmt))
  237. } else {
  238. output.push(f("%s = %s; ", key, obj))
  239. }
  240. }
  241. output.push("}; ");
  242. }
  243. inlineObjectHelper(n, d, r);
  244. this.write("%s\n", output.join('').trim());
  245. }
  246. module.exports = pbxWriter;