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

pbxWriter.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 key, obj;
  146. for (key in objects) {
  147. this.writeFlush("\n")
  148. obj = objects[key];
  149. if (isObject(obj)) {
  150. this.writeSectionComment(key, true);
  151. this.writeSection(obj);
  152. this.writeSectionComment(key, false);
  153. }
  154. }
  155. }
  156. pbxWriter.prototype.writeArray = function (arr, name) {
  157. var i, entry;
  158. this.write("%s = (\n", name);
  159. this.indentLevel++;
  160. for (i=0; i < arr.length; i++) {
  161. entry = arr[i]
  162. if (entry.value && entry.comment) {
  163. this.write('%s /* %s */,\n', entry.value, entry.comment);
  164. } else if (isObject(entry)) {
  165. this.write('{\n');
  166. this.indentLevel++;
  167. this.writeObject(entry);
  168. this.indentLevel--;
  169. this.write('},\n');
  170. } else {
  171. this.write('%s,\n', entry);
  172. }
  173. }
  174. this.indentLevel--;
  175. this.write(");\n");
  176. }
  177. pbxWriter.prototype.writeSectionComment = function (name, begin) {
  178. if (begin) {
  179. this.writeFlush("/* Begin %s section */\n", name)
  180. } else { // end
  181. this.writeFlush("/* End %s section */\n", name)
  182. }
  183. }
  184. pbxWriter.prototype.writeSection = function (section) {
  185. var key, obj, cmt;
  186. // section should only contain objects
  187. for (key in section) {
  188. if (COMMENT_KEY.test(key)) continue;
  189. cmt = comment(key, section);
  190. obj = section[key]
  191. if (obj.isa == 'PBXBuildFile' || obj.isa == 'PBXFileReference') {
  192. this.writeInlineObject(key, cmt, obj);
  193. } else {
  194. if (cmt) {
  195. this.write("%s /* %s */ = {\n", key, cmt);
  196. } else {
  197. this.write("%s = {\n", key);
  198. }
  199. this.indentLevel++
  200. this.writeObject(obj)
  201. this.indentLevel--
  202. this.write("};\n");
  203. }
  204. }
  205. }
  206. pbxWriter.prototype.writeInlineObject = function (n, d, r) {
  207. var output = [];
  208. var self = this
  209. var inlineObjectHelper = function (name, desc, ref) {
  210. var key, cmt, obj;
  211. if (desc) {
  212. output.push(f("%s /* %s */ = {", name, desc));
  213. } else {
  214. output.push(f("%s = {", name));
  215. }
  216. for (key in ref) {
  217. if (COMMENT_KEY.test(key)) continue;
  218. cmt = comment(key, ref);
  219. obj = ref[key];
  220. if (isArray(obj)) {
  221. output.push(f("%s = (", key));
  222. for (var i=0; i < obj.length; i++) {
  223. output.push(f("%s, ", obj[i]))
  224. }
  225. output.push("); ");
  226. } else if (isObject(obj)) {
  227. inlineObjectHelper(key, cmt, obj)
  228. } else if (self.omitEmptyValues && (obj === undefined || obj === null)) {
  229. continue;
  230. } else if (cmt) {
  231. output.push(f("%s = %s /* %s */; ", key, obj, cmt))
  232. } else {
  233. output.push(f("%s = %s; ", key, obj))
  234. }
  235. }
  236. output.push("}; ");
  237. }
  238. inlineObjectHelper(n, d, r);
  239. this.write("%s\n", output.join('').trim());
  240. }
  241. module.exports = pbxWriter;