UNPKG

pbxproj

Version:

parse & stringify xcode pbxproj format

99 lines (78 loc) 2.09 kB
'use strict'; /* * Module dependencies */ var _ = require('lodash'); var handlebars = require('handlebars'); /* * Inline templates */ var OBJ_TPL = handlebars.compile('{{#if key}}{{{key}}} = {{/if}}{'); var ARRAY_TPL = handlebars.compile('{{#if key}}{{{key}}} = {{/if}}('); var VALUE_TPL = handlebars.compile('{{#if key}}{{{key}}} = {{/if}}{{{value}}};'); /* * Constructor */ var PBXProjExporter = module.exports = function () { _.bindAll(this, 'writeValue'); this.obj = {}; }; /* * Reset data and level before object iteration */ PBXProjExporter.prototype.reset = function () { this.level = 0; this.data = ''; this.writeLine('// !$*UTF8*$!'); }; /* * Write line into data regarding current level (indent) */ PBXProjExporter.prototype.writeLine = function (line) { this.data += _.range(this.level).map(function () { return '\t'; }).join('') + line + '\n'; }; /* * Main API : returns pbxproj string equivalent to a given object */ PBXProjExporter.prototype.stringify = function (obj) { if (!_.isObject(obj) || _.isArray(obj)) throw 'not an object'; this.reset(); this.writeObject(obj, null); return this.data; }; /* * Iter into Object and write serialization to data */ PBXProjExporter.prototype.writeObject = function (obj, key) { this.writeLine(OBJ_TPL({key: key})); this.level++; _.each(obj, this.writeValue); this.level--; this.writeLine('};'); }; /* * Iter into Array and write serialization to data */ PBXProjExporter.prototype.writeArray = function (array, key) { this.writeLine(ARRAY_TPL({key: key})); this.level++; _.each(array, function (value) { this.writeValue(value); }, this); this.level--; this.writeLine(');'); }; /*mak * Check value type and use correct writer (Object / Array / Value[String, Number...]) */ PBXProjExporter.prototype.writeValue = function (value, key) { if (_.isArray(value)) { this.writeArray(value, key); } else if (_.isObject(value)) { this.writeObject(value, key); } else { this.writeLine(VALUE_TPL({key: key, value: value})); } };