|
| 1 | +var pbxProj = require('./pbxProject'), |
| 2 | + util = require('util'), |
| 3 | + f = util.format, |
| 4 | + INDENT = ' ', |
| 5 | + COMMENT_KEY = /_comment$/, |
| 6 | + EventEmitter = require('events').EventEmitter |
| 7 | + |
| 8 | +// indentation |
| 9 | +function i(x) { |
| 10 | + if (x <=0) |
| 11 | + return ''; |
| 12 | + else |
| 13 | + return INDENT + i(x-1); |
| 14 | +} |
| 15 | + |
| 16 | +function comment(key, parent) { |
| 17 | + var text = parent[key + '_comment']; |
| 18 | + |
| 19 | + if (text) |
| 20 | + return text; |
| 21 | + else |
| 22 | + return null; |
| 23 | +} |
| 24 | + |
| 25 | +// copied from underscore |
| 26 | +function isObject(obj) { |
| 27 | + return obj === Object(obj) |
| 28 | +} |
| 29 | + |
| 30 | +function isArray(obj) { |
| 31 | + return Array.isArray(obj) |
| 32 | +} |
| 33 | + |
| 34 | +function pbxWriter(contents) { |
| 35 | + this.contents = contents; |
| 36 | + this.sync = false; |
| 37 | + this.indentLevel = 0; |
| 38 | +} |
| 39 | + |
| 40 | +util.inherits(pbxWriter, EventEmitter); |
| 41 | + |
| 42 | +pbxWriter.prototype.write = function (str) { |
| 43 | + var fmt = f.apply(null, arguments); |
| 44 | + |
| 45 | + if (this.sync) { |
| 46 | + this.buffer += f("%s%s", i(this.indentLevel), fmt); |
| 47 | + } else { |
| 48 | + // do stream write |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pbxWriter.prototype.writeSync = function () { |
| 53 | + this.sync = true; |
| 54 | + this.buffer = ""; |
| 55 | + |
| 56 | + this.writeHeadComment(); |
| 57 | + this.writeProject(); |
| 58 | + |
| 59 | + return this.buffer; |
| 60 | +} |
| 61 | + |
| 62 | +pbxWriter.prototype.writeHeadComment = function () { |
| 63 | + if (this.contents.headComment) { |
| 64 | + this.write("// %s\n", this.contents.headComment) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pbxWriter.prototype.writeProject = function () { |
| 69 | + var proj = this.contents.project, |
| 70 | + key, cmt, obj; |
| 71 | + |
| 72 | + this.write("{\n") |
| 73 | + |
| 74 | + if (proj) { |
| 75 | + this.indentLevel++; |
| 76 | + |
| 77 | + for (key in proj) { |
| 78 | + // skip comments |
| 79 | + if (COMMENT_KEY.test(key)) continue; |
| 80 | + |
| 81 | + cmt = comment(key, proj); |
| 82 | + obj = proj[key]; |
| 83 | + |
| 84 | + if (isArray(obj)) { |
| 85 | + this.write("%s = (\n", key); |
| 86 | + this.indentLevel++; |
| 87 | + |
| 88 | + this.writeArray(obj) |
| 89 | + |
| 90 | + this.indentLevel--; |
| 91 | + this.write(");\n"); |
| 92 | + } else if (isObject(obj)) { |
| 93 | + this.write("%s = {\n", key); |
| 94 | + this.indentLevel++; |
| 95 | + |
| 96 | + if (key === 'objects') { |
| 97 | + this.writeObjectsSections(obj) |
| 98 | + } else { |
| 99 | + this.writeObject(obj) |
| 100 | + } |
| 101 | + |
| 102 | + this.indentLevel--; |
| 103 | + this.write("};\n"); |
| 104 | + } else if (cmt) { |
| 105 | + this.write("%s = %s /* %s */;\n", key, obj, cmt) |
| 106 | + } else { |
| 107 | + this.write("%s = %s;\n", key, obj) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + this.indentLevel--; |
| 112 | + } |
| 113 | + |
| 114 | + this.write("}\n") |
| 115 | +} |
| 116 | + |
| 117 | +pbxWriter.prototype.writeObject = function (object) { |
| 118 | + var key, obj, cmt; |
| 119 | + |
| 120 | + for (key in object) { |
| 121 | + if (COMMENT_KEY.test(key)) continue; |
| 122 | + |
| 123 | + cmt = comment(key, object); |
| 124 | + obj = object[key]; |
| 125 | + |
| 126 | + if (cmt) { |
| 127 | + this.write("%s = %s /* %s */;\n", key, obj, cmt) |
| 128 | + } else { |
| 129 | + this.write("%s = %s;\n", key, obj) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +pbxWriter.prototype.writeObjectsSections = function (objects) { |
| 135 | + var key, obj; |
| 136 | + |
| 137 | + for (key in objects) { |
| 138 | + obj = objects[key]; |
| 139 | + |
| 140 | + if (isObject(obj)) { |
| 141 | + this.writeSectionComment(key, true); |
| 142 | + |
| 143 | + this.writeSection(obj); |
| 144 | + |
| 145 | + this.writeSectionComment(key, false); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +pbxWriter.prototype.writeArray = function (arr) { |
| 151 | + var i, entry; |
| 152 | + |
| 153 | + for (i=0; i < arr.length; i++) { |
| 154 | + entry = arr[i] |
| 155 | + |
| 156 | + if (entry.value && entry.comment) { |
| 157 | + this.write('%s /* %s */,\n', entry.value, entry.comment); |
| 158 | + } else { |
| 159 | + this.write('%s,\n', entry); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +pbxWriter.prototype.writeSectionComment = function (name, begin) { |
| 165 | + var oldIndent = this.indentLevel; |
| 166 | + |
| 167 | + this.indentLevel = 0; |
| 168 | + |
| 169 | + if (begin) { |
| 170 | + this.write("/* Begin %s section */\n", name) |
| 171 | + } else { // end |
| 172 | + this.write("/* End %s section */\n", name) |
| 173 | + } |
| 174 | + |
| 175 | + this.indentLevel = oldIndent; |
| 176 | +} |
| 177 | + |
| 178 | +pbxWriter.prototype.writeSection = function (section) { |
| 179 | + var id = section.id, |
| 180 | + comment = section[id + '_comment'], |
| 181 | + object = section[id]; |
| 182 | + |
| 183 | + this.write("%s /* %s */ = {\n", id, comment); |
| 184 | + |
| 185 | + this.indentLevel++ |
| 186 | + |
| 187 | + this.writeObject(object) |
| 188 | + |
| 189 | + this.indentLevel-- |
| 190 | + |
| 191 | + this.write("};\n"); |
| 192 | +} |
| 193 | + |
| 194 | +module.exports = pbxWriter; |
0 commit comments