Live coding revisited

I previously bloged about a script that enabled automatic rebuild and restart of the web server on change of the source files. The script was working great, but sometimes some strange errors start poping out.

Thanks to Nick Hemsley who submited a patch that uses FileSystemWatcher, we now get much better results!

I refactored the script a bit and here’s the result:

import System
import System.IO
import System.Threading
import System.Diagnostics

# The command to launch the web server
SERVER_CMD = argv[0]
# Determine if the web server has to be restarted
# afther a built
SERVER_RESTART = argv[1] == 'restart'
# Arguments to send to with the server command
SERVER_ARGUMENTS = join(argv[2:], ' ')

# Arguments to send to nant when building
# Typically the target that builds and some arguments
NANT_ARGUMENTS = 'build -nologo -q'

# Time to wait before watching for changes again
REBUILD_DELAY = 1000 #ms


######## Stop editing here unless you're a damn cool Boo hacker ########
watcher = FileSystemWatcher(Directory.GetCurrentDirectory(), "*.cs")
watcher.IncludeSubdirectories = true
print "Starting live server process, hit CTRL+C to stop"

serverProcess as Process
rebuild = true

while (true):
  print "nant ${NANT_ARGUMENTS}"
  buildProcess = shellp('nant', NANT_ARGUMENTS)
  buildProcess.WaitForExit()
  rebuild = false

  if (buildProcess.ExitCode == 0):
    if SERVER_RESTART or serverProcess == null:
      # Try to stop the server
      if (serverProcess != null):
        try:
          serverProcess.Kill()
        except e:
          pass
        serverProcess.WaitForExit()
      # Starts the server
      print "${SERVER_CMD} ${SERVER_ARGUMENTS}"
      serverProcess = shellp(SERVER_CMD, SERVER_ARGUMENTS)
  else:
    print "Build failed! Errors:"
    print '=' * 80
    print buildProcess.StandardOutput.ReadToEnd()
    print '=' * 80
  
  Thread.CurrentThread.Sleep(REBUILD_DELAY)
  change = watcher.WaitForChanged(WatcherChangeTypes.All)
  print "File change detected (${change.Name})"

Whenever a .cs file is modified, nant is called to recompile the app and the web server is restarted.

Usage: booi script/server.boo YourWebServer.exe restart|norestart Argument For Your Web Server

On with Mono, you got to enable MONO_MANAGED_WATCHER, or else sub directories wont trigger a rebuild.

export MONO_MANAGED_WATCHER=1
booi script/server.boo xsp2 restart --root public $*

If the restart parameter is used, the web server will be restarted on each file change.

This script is now part of Castle Generator. To be as cool as I am, get into live coding:

svn co http://svn.castleproject.org:8080/svn/castlecontrib/generator/trunk generator
cd generator
nant install
cd ..
monorail MyBlog
cd MyBlog
nant setup
script/server


and start coding!

Leave a comment

Filed under Generator, Monorail

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s