continuous compilation
A few weeks ago I decided to dive a bit into google go and write a little URL shortener. So far go feels very dynamic and lightweight, while it does not need an 800 pound gorilla of a runtime environment. It still has not reached a 1.0 release and in some parts of the standard library you see, that it is work in progress, but the language is here to stay.
One of the nice properties of go is the super-fast compilation speed, which must have been an answer to this. My development setup for go is vim, git and a makefile, which are great text based tools all the way down.
Given the fact that the compiler is so fast and I was getting a bit tired of typing make, I looked for a better solution to compile my code continuously while working on it. I wanted something automatic, like in an IDE, while sticking to the tools I have.
Luckily the good Linux operating system has everything I needed to implement that, so doing a full build every time I save my code is as simple as the following commandline.
Now let’s look at what that does: The inotifywait command is from the inotify-tools package, which uses inotify to watch for file-system events. The nice thing about inotify is, that the kernel informs you about file-system events and you don’t have to poll for changes yourself. You should really add all of inotify to your toolbox, it can be a great technology for solving part of different bigger problems, like automatic image resizing.
The command as shown here sets up recursive (-r) file system watches on the src directory and waits for close_write events, which are fired, as soon as I save any file in vim or any other tool. The -m switch puts inotifywait in monitor mode, which means it will run forever in the foreground and emit one line of text for each event. Every time one line is emitted, bash will read it and call make to build my project. Pretty simple and pretty powerful and the best thing is, that inotifywait does the right thing, meaning it will start watching new files as they appear in the src directory.
Since I am using vim, I could have used a combination of :makeprg and :errorformat, :quickfix and :autocmd, but I wanted something portable, that I could easily reuse under other circumstances.
Happy hacking!