Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add in better feedback from the build process.

  • Loading branch information...
commit 58abfa0fb5269598fea23513ffc7e2504d143527 1 parent 4c20972
Tim Caswell authored February 02, 2010

Showing 2 changed files with 87 additions and 21 deletions. Show diff stats Hide diff stats

  1. 54  build.js
  2. 54  builder.js
54  build.js
@@ -115,8 +115,46 @@ function loop(obj, callback) {
115 115
   }
116 116
 }
117 117
 
118  
-function render(data) {
  118
+// combo library.  Allows to group several callbacks.
  119
+function Combo(callback) {
  120
+  this.callback = callback;
  121
+  this.items = 0;
  122
+  this.results = [];
  123
+}
  124
+Combo.prototype = {
  125
+  add: function () {
  126
+    var self = this;
  127
+    this.items++;
  128
+    return function () {
  129
+      self.check(self.items - 1, arguments);
  130
+    };
  131
+  },
  132
+  check: function (id, arguments) {
  133
+    this.results[id] = Array.prototype.slice.call(arguments);
  134
+    this.items--;
  135
+    if (this.items == 0) {
  136
+      this.callback.apply(this, this.results);
  137
+    }
  138
+  }
  139
+};
  140
+
  141
+
  142
+function render(data, next) {
119 143
   var Helpers, haml;
  144
+  var group = new Combo(function() {
  145
+    var args = Array.prototype.slice.call(arguments);
  146
+    next(args.map(function (arg) {
  147
+      return arg[0];
  148
+    }).join("\n"))
  149
+  });
  150
+
  151
+  function write_file(filename, content) {
  152
+    var cb = group.add();
  153
+    File.write(filename, content).addCallback(function () {
  154
+      cb("Wrote " + content.length + " bytes to " + filename);
  155
+    })
  156
+  }
  157
+
120 158
   Helpers = {
121 159
     github: function (name) {
122 160
       return "http://github.com/" + name;
@@ -138,7 +176,7 @@ function render(data) {
138 176
   // Generate a page for each author...
139 177
   loop(data.authors, function (name, props) {
140 178
     props.link = name.toLowerCase().replace(/ /g, "_") + ".html";
141  
-    File.write(PUBLIC_DIR + "/" + props.link, haml("layout", {
  179
+    write_file(PUBLIC_DIR + "/" + props.link, haml("layout", {
142 180
       title: "About " + name,
143 181
       content: haml("author", props)
144 182
     }));
@@ -148,31 +186,33 @@ function render(data) {
148 186
   loop(data.articles, function (name, props) {
149 187
     props.link = name + ".html";
150 188
     props.author = data.authors[props.author];
151  
-    File.write(PUBLIC_DIR + "/" + props.link, haml("layout", {
  189
+    write_file(PUBLIC_DIR + "/" + props.link, haml("layout", {
152 190
       title: props.title,
153 191
       content: haml("article", props)
154 192
     }));
155 193
   });
156 194
 
157 195
   // Generate a index page...
158  
-  File.write(PUBLIC_DIR + "/index.html", haml("layout", {
  196
+  write_file(PUBLIC_DIR + "/index.html", haml("layout", {
159 197
     title: "Index",
160 198
     content: haml("index", data)
161 199
   }));
162 200
 
163 201
   // Write the static files as is...
164 202
   loop(data.static, function (filename, content) {
165  
-    File.write(PUBLIC_DIR + "/" + filename, content.content || content);
  203
+    write_file(PUBLIC_DIR + "/" + filename, content.content || content);
166 204
   });
167 205
 
168 206
 }
169 207
 
170  
-exports.build = function () {
  208
+exports.build = function (next) {
171 209
   // Kick off the process
172 210
   main([
173 211
     ["articles", ARTICLE_DIR, /^(.*)\.(markdown)$/],
174 212
     ["authors", AUTHOR_DIR, /^(.*)\.(markdown)$/],
175 213
     ["templates", SKIN_DIR, /^(.*)\.(haml)$/],
176 214
     ["static", SKIN_DIR, /^(.*)\.([^.]+)\.([^.]+)$/]
177  
-  ], render);
  215
+  ], function (data) {
  216
+    render(data, next);
  217
+  });
178 218
 };
54  builder.js
... ...
@@ -1,10 +1,16 @@
1  
-var Markdown = require('markdown'),
2  
-    Url = require('url'),
  1
+var QueryString = require('querystring'),
3 2
     Http = require('http'),
4 3
     Build = require('./build');
5 4
 process.mixin(require('sys'));
6 5
 var PORT = 4242;
7 6
 
  7
+function log(message) {
  8
+  var date = new Date();
  9
+  message.split("\n").forEach(function (line) {
  10
+    puts(date + " - " + line);
  11
+  })
  12
+}
  13
+
8 14
 Http.createServer(function (req, res) {
9 15
   var body = "";
10 16
   req.setBodyEncoding('utf8');
@@ -13,27 +19,47 @@ Http.createServer(function (req, res) {
13 19
   });
14 20
   req.addListener('complete', function () {
15 21
     try {
16  
-      body = Url.parse("?" + body, true).query.payload;
17  
-      body = JSON.parse(body);
  22
+      body = QueryString.parse(body).query.payload;
  23
+      log("Received GitHub POST hook: " + body);
18 24
     } catch(e) {
19 25
       body = false;
  26
+      log("Manual refresh");
20 27
     }
21  
-    build(body);
22  
-    res.sendHeader(200, {'Content-Type': 'text/plain'});
23  
-    res.sendBody('Thanks for the tip!');
24  
-    res.finish();
  28
+    build(body, function (output) {
  29
+      res.sendHeader(200, {'Content-Type': 'text/plain'});
  30
+      res.sendBody(output);
  31
+      res.finish();
  32
+    });
25 33
   });
26 34
 
27 35
 }).listen(PORT);
28 36
 
29  
-function build(pull) {
30  
-  if (pull) {
  37
+// simple semaphore
  38
+var building = false;
  39
+
  40
+function build(data, next) {
  41
+
  42
+  function real_build() {
  43
+    if (building) {
  44
+      return;
  45
+    }
  46
+    building = true;
  47
+
  48
+    log("Rebuilding site");
  49
+    Build.build(function (output) {
  50
+      log(output + "\nDone!");
  51
+      building = false;
  52
+      next(output);
  53
+    });
  54
+  }
  55
+
  56
+  if (data) {
  57
+    log("Pulling latest changes to content");
31 58
     exec("cd data && git pull origin master").addCallback(function (stdout, stderr) {
32  
-      puts(stdout);
33  
-      Build.build();
  59
+      real_build();
34 60
     });
35 61
   } else {
36  
-    Build.build();
  62
+    real_build();
37 63
   }
38 64
 }
39  
-puts('Server running at http://127.0.0.1:' + PORT + '/');
  65
+log('Server running at http://127.0.0.1:' + PORT + '/');

0 notes on commit 58abfa0

Please sign in to comment.
Something went wrong with that request. Please try again.