Live coding with MonoRail

In my constant quest to bring Castle MonoRail closer to Ruby on Rails development style, one of the thing that kept me from choosing MonoRail over Rails for fun and pleasure was the code-compile-refresh process. Because we’re working in the static compiled world, a usual scenario looks like this.

  1. Add or remove some code
  2. Compile from the IDE or run NAnt
  3. Restart the web server (under *nix XSP needs to be restarted to load the new assembly)
  4. Refresh the browser (and wait for the app to restart AR and all other services)

This is painful! Why can’t we just code and refresh like with scripting languages?

While reading an article on reddit about Live coding, I had a idea…

# Rebuilds the project periodically and restarts the web server on success

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

 SERVER_CMD = 'xsp2'
 SERVER_ARGUMENTS = '--root public'
 NANT_ARGUMENTS = 'build -nologo -q'
 PROJECT_ASSEMBLY = 'public/bin/MyBlog.dll'
 REBUILD_DELAY = 1000 #ms

 serverProcess as Process
 lastWriteTime as DateTime

 print "Starting runner process, hit CTRL+C to stop"
 while (true):
  buildProcess = shellp('nant', NANT_ARGUMENTS)
  buildProcess.WaitForExit()

  if buildProcess.ExitCode == 0:
   if FileInfo(PROJECT_ASSEMBLY).LastWriteTime > lastWriteTime:
    print "Project recompiled, restarting the server"
    if serverProcess != null:
     try:
      # Stops the server
      serverProcess.Kill()
     except e:
      pass
     serverProcess.WaitForExit()
     serverProcess = null
    serverProcess = shellp(SERVER_CMD, SERVER_ARGUMENTS)
    lastWriteTime = FileInfo(PROJECT_ASSEMBLY).LastWriteTime
  else:
   print buildProcess.StandardOutput.ReadToEnd()

  # Waits a couple of seconds
  Thread.CurrentThread.Sleep(REBUILD_DELAY)

If you can’t see what this is about…

The other thing that keep me from dropping Rails for MonoRail is C# extra verbose and lack of fun and style. But I can’t do nothing about that, except use Boo whenever I can.

Note that this script will soon be integrated in the Generator so when you generate a new MonoRail project, the server script while run something like this.

About these ads

3 Comments

Filed under Generator, Monorail

3 responses to “Live coding with MonoRail

  1. Anonymous

    Can you make your video’s a bit larger so the text is clearer.

    Cheers
    Adam

  2. MAC

    The original video is 800×600 but Google Video shrinks it. You still can see it there: http://video.google.com/videoplay?docid=-7839057158683496907

    But the quality is still not the best

    I’ll try to find a better solution–>

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