Permalink
Browse files

the code

  • Loading branch information...
1 parent 405f4df commit 5c87d34d8f534207421ef35c254e0dfc5bbaca5a @isaacs isaacs committed May 27, 2012
Showing with 169 additions and 0 deletions.
  1. +43 −0 config.js
  2. +126 −0 server.js
View
@@ -0,0 +1,43 @@
+exports.port = process.env.PORT || 1337
+
+var fs = require('fs')
+exports.https = {
+ key: fs.readFileSync(__dirname + '/ssl/server.key'),
+ cert: fs.readFileSync(__dirname + '/ssl/server.crt')
+}
+
+if (process.platform === 'win32') {
+ exports.gitCmd = 'git'
+
+ exports.configCmd = null
+ exports.configArgs = []
+
+ exports.buildCmd = 'vcbuild.bat'
+ exports.buildArgs = ['release', 'debug']
+ if (process.arch === 'x64') {
+ exports.buildArgs.push('x64')
+ }
+
+ exports.cleanCmd = 'vcbuild.bat'
+ exports.cleanArgs = ['clean']
+
+ exports.testCmd = 'vcbuild.bat'
+ exports.testArgs = ['test-all']
+
+} else {
+ exports.gitCmd = 'git'
+
+ exports.configCmd = './configure'
+ exports.configArgs = []
+
+ exports.buildCmd = 'make'
+ var cpus = require('os').cpus()
+ var jobs = cpus && cpus.length || 4
+ exports.buildArgs = ['-j' + jobs]
+
+ exports.cleanCmd = 'make'
+ exports.cleanArgs = ['distclean']
+
+ exports.testCmd = 'make'
+ exports.testArgs = ['test-all']
+}
View
126 server.js
@@ -0,0 +1,126 @@
+var http = require('http')
+var https = require('https')
+var spawn = require('child_process').spawn
+var pushover = require('pushover')
+var path = require('path')
+var repoDir = path.resolve(__dirname, 'repos')
+var nodeRepo = path.resolve(repoDir, 'node')
+var repos = pushover(repoDir)
+var checkoutDir = path.resolve(__dirname, 'checkout')
+var nodeCheckout = path.resolve(checkoutDir, 'node')
+var StringDecoder = require('string_decoder').StringDecoder
+var fs = require('fs')
+var rimraf = require('rimraf')
+
+var config = require('./config.js')
+var gitCmd = config.gitCmd
+var testCmd = config.testCmd
+var testArgs = config.testArgs
+var cleanCmd = config.cleanCmd
+var cleanArgs = config.cleanArgs
+var configCmd = config.configCmd
+var configArgs = config.configArgs
+var buildCmd = config.buildCmd
+var buildArgs = config.buildArgs
+var httpsOpt = config.https
+var port = config.port
+
+https.createServer(httpsOpt, function (req, res) {
+ if (req.method === 'POST' && req.url === '/test') {
+ return runTests(req, res)
+ }
+ repos.handle(req, res)
+}).listen(port, function () {
+ console.error('drone listening on port %d', port)
+})
+
+var testsRunning = false
+function runTests (req, res) {
+ res.setHeader('content-type', 'text/plain')
+ if (testsRunning) {
+ res.statusCode = 503
+ return res.end('Tests already running. Try again later.')
+ }
+
+ testsRunning = true
+ // the post body is the commit-ish to test.
+ var co = ''
+ var decoder = new StringDecoder()
+ req.on('data', function (c) {
+ co += decoder.write(c)
+ })
+ req.on('end', function () {
+ runTests_(co, res)
+ })
+}
+
+function runTests_ (co, res) {
+ // if the dir isn't there then check it out.
+ fs.stat(nodeCheckout, function (er) {
+ if (er) clone(co, res)
+ else fetch(co, res)
+ })
+}
+
+function clone (co, res) {
+ pipeSpawn(gitCmd, ['clone', nodeRepo, nodeCheckout], {},
+ res, function () { fetch(co, res) })
+}
+
+function fetch (co, res) {
+ pipeSpawn(gitCmd, ['fetch', '-a', nodeRepo], {cwd: nodeCheckout},
+ res, function () { checkout(co, res) })
+}
+
+function checkout (co, res) {
+ pipeSpawn(gitCmd, ['checkout', co], {cwd: nodeCheckout},
+ res, function () { clean(res) })
+}
+
+function clean (res) {
+ pipeSpawn(gitCmd, ['clean', '-fd'], {cwd: nodeCheckout}, res, function () {
+ pipeSpawn(cleanCmd, cleanArgs, {cwd: nodeCheckout}, res, function () {
+ if (configCmd) configure(res)
+ else build(res)
+ })
+ })
+}
+
+function configure (res) {
+ var cmd = path.resolve(nodeCheckout, configCmd)
+ pipeSpawn(cmd, configArgs, {cwd: nodeCheckout},
+ res, function () { build(res) })
+}
+
+function build (res) {
+ pipeSpawn(buildCmd, buildArgs, {cwd: nodeCheckout},
+ res, function () { makeTest(res) })
+}
+
+function makeTest (res) {
+ pipeSpawn(testCmd, testArgs, {cwd: nodeCheckout},
+ res, function () { res.end('\n\nOK\n'); testsRunning = false })
+}
+
+function pipeSpawn (cmd, args, opt, res, cb) {
+ var title = cmd + ' ' + args.map(JSON.stringify).join(' ')
+ console.log(title)
+ res.write('> ' + title + '\n')
+
+ var child = spawn(cmd, args, opt)
+ child.stdout.on('data', function (c) {
+ res.write(c)
+ })
+ child.stderr.on('data', function (c) {
+ res.write(c)
+ })
+ child.on('close', function (code) {
+ if (code) {
+ res.end('ERROR: ' + title +
+ ' failed with code ' + code + '\n')
+ testsRunning = false
+ return
+ }
+ cb()
+ })
+}

0 comments on commit 5c87d34

Please sign in to comment.