builder.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * The main builder script
  3. */
  4. //require mootools
  5. require('moo-server').apply(GLOBAL);
  6. var winston = require('winston'),
  7. fs = require('fs'),
  8. path = require("path"),
  9. util = require("util"),
  10. sys = require("sys"),
  11. when = require("promised-io").when,
  12. Promise = require("promised-io").Promise;
  13. var Builder = new Class({
  14. Implements: [Options],
  15. options: {},
  16. config: null,
  17. targets: {},
  18. logger: null,
  19. queue: [],
  20. stack: null,
  21. tasks: {},
  22. initialize: function(options, logfile) {
  23. this.setOptions(options);
  24. this.logger = new (winston.Logger)({
  25. transports: [
  26. new (winston.transports.Console)({level: "silly"}),
  27. new (winston.transports.File)({ filename: logfile, level: "silly" })
  28. ]
  29. });
  30. this.logger.emitErrs = false;
  31. if (nil(options)) {
  32. this.logger.debug("config is nil");
  33. } else {
  34. this.logger.debug("config is not nil");
  35. //this.logger.info("config: " + util.inspect(this.options, false, null));
  36. }
  37. this.loadInternalTasks();
  38. this.loadTasks(this.options.tasks);
  39. },
  40. loadTasks: function(filename) {
  41. Array.from(filename).each(function(file){
  42. var tasks = require(file).tasks;
  43. for (var name in tasks) {
  44. this.logger.info("loading task: " + name);
  45. this.tasks[name] = tasks[name];
  46. }
  47. },this);
  48. },
  49. build: function(target, config) {
  50. var p = new Promise();
  51. config = nil(config) ? this.options : config;
  52. this.logger.info("Processing target: " + target);
  53. //load the target file
  54. fn = require(this.options.targets + "/" + target + ".target");
  55. this.targets[target] = fn(config, this.logger);
  56. this.logger.info("Starting target" + util.inspect(this.targets[target], false, null));
  57. //start loading in the target's required dependencies
  58. if (!nil(this.targets[target].depends)) {
  59. this.importTargets(this.targets[target].depends);
  60. }
  61. this.queue.push(target);
  62. this.logger.info("queue order: " + util.inspect(this.queue, false, null));
  63. //begin processing targets
  64. this.runTargets().then(function(){ p.resolve(true);});
  65. return p;
  66. },
  67. loadInternalTasks: function(){
  68. var taskPath = path.normalize(__dirname + "/tasks");
  69. files = fs.readdirSync(taskPath);
  70. Array.from(files).each(function(file){
  71. this.loadTasks(taskPath + "/" + file);
  72. }, this);
  73. },
  74. importTargets: function(depends){
  75. this.logger.debug("in ImportTargets for " + util.inspect(depends,false,null));
  76. if (!nil(depends)) {
  77. Array.from(depends).each(function(d){
  78. if (!Object.keys(this.targets).contains(d)) {
  79. this.targets[d] = (require(this.options.targets + "/" + d + ".target"))(this.options,this.logger);
  80. this.logger.info("Target config for " + d + ":\n" + util.inspect(this.targets[d],false,null));
  81. if (!nil(this.targets[d].depends)) {
  82. this.importTargets(this.targets[d].depends);
  83. }
  84. this.queue.push(d);
  85. }
  86. }, this);
  87. }
  88. },
  89. runTargets: function(){
  90. var target = this.queue.shift(),
  91. p = new Promise();
  92. this.stack = Array.clone(this.targets[target].tasks);
  93. this.logger.info("\n\n!!!!!!!!!!!!!\nExecuting target: " + target);
  94. this.logger.info("Target description: " + this.targets[target].description);
  95. this.logger.info("Number of tasks: " + this.stack.length);
  96. this.executeTarget(target).then(function(){
  97. if (this.queue.length > 0) {
  98. this.runTargets();
  99. } else {
  100. p.resolve(true);
  101. }
  102. }.bind(this));
  103. return p;
  104. },
  105. executeTarget: function(){
  106. var p = new Promise();
  107. var task = this.stack.shift(),
  108. taskName = Object.keys(task)[0],
  109. options = task[taskName];
  110. this.logger.info("running task: " + taskName);
  111. this.tasks[taskName](options, this.options, this.logger).then(function(){
  112. this.logger.info("Promise resolved from task: " + taskName);
  113. if (this.stack.length === 0) {
  114. this.logger.info("No more tasks...");
  115. p.resolve(true);
  116. } else {
  117. this.logger.info("On to next task!!!");
  118. this.executeTarget().then(function(){p.resolve(true);});
  119. }
  120. }.bind(this), function(err){
  121. p.reject(err);
  122. }.bind(this));
  123. return p;
  124. }
  125. });
  126. module.exports = Builder;