A while back David Miller wrote a couple of blog posts (Porting libs to ClojureCLR: an example and Porting effort for Clojure contrib libs) about porting Clojure contrib libs to ClojureCLR. In the two posts he made it look like porting most of the libs wouldn’t be too terribly hard. After a long period of telling myself, “tomorrow I will pick a lib to port and get started”, I actually did do just that a few days ago. I am about to start a project that needs to process command line arguments and it just so happens that David flagged the tools.cli lib as a trivial port. So I decided to start my porting career with tools.cli.
I created a directory off of my usual code directory called clj-ports just to help me differentiate that code from everything else I’m working on. Next I cloned the existing github project (https://github.com/clojure/tools.cli). After cloning the project I attempted to compile the project with clojure.compile. For tools.cli the compilation went off without a hitch. Now that I have a clojure.tools.cli.clj.DLL I’m ready to start testing.
The Testing Process
Now that I have my DLL I’m ready to start up the REPL and run the tests. The first step to running the is to load the clojure.test and the clojure.tools.cli-test libraries. After loading the libraries I can run the tests. When I ran the tests and very quickly I hit my first error.
The error was a System.MissingMethodException on a call to a split method. I scoured the cli.clj file for a call to split but couldn’t find one. After taking a closer look I realized it was in the TESTING code. I opened up cli-test.clj, changed split to Split. When I re-ran the tests I hit another error. This time the test failed because it expected a Char and was passed a String.
The problem is in the testing code and is one of the parameters to the Split method call. Since it was looking for a Char and I’m passing in a one character string I thought I’d try to use (first “-“) to get around the issue. So I updated the test code and the change comment and re-ran the tests. The error message I received this time was in the cli.clj file and not the test file. Now that I have resolved the issues in the test file I can add my end of line comments following David’s instructions in his post Porting effort for Clojure contrib libs.
The new error is:
actual: System.MissingMethodException: Cannot find member startsWith matching args at .CallSite.Target (:0)
I opened up the cli.clj file and searched for startsWith calls. I found four places where the java startsWith method was used. I changed all four to use the .NET equivalent StartsWith (and of course I commented the lines accordingly). Remember, after you make a change to the source file you will need to re-compile (at least that is the way I did it) which means you’ll need to exit out of your REPL session first. After recompiling and getting my REPL session back in order I re-ran the tests. This time they all PASSED!
Now that I have all greens on my test it is time to create a new project on github called cljclr.tools.cli. After creating the project on github and setting up my machine I copied all files from the clojure.tools.cli project into the new cljclr.tools.cli project. Next, I changed the namespaces in all code files to reflect the new project name and re-ran the tests. All green! I checked the ported code into github and called the port complete. The ported project lives at cljclr.tools.cli.
Since this was my first time porting a library I wanted to make sure it worked I used the ‘application’ above to check. The first part of the main function is where the call to the cli function is which is where the available command line options are configured. The parameters are the args passed to the main function followed by a vectors that illustrate the allowed command-line options. The returned vector has three members, a map with the parameters and their values, a vector with any additional parameters passed on the command line, and the string that contains the ‘banner’ which is used to show the available parameters. After the call the cond function is used to figure out what to do. Each option has a simple println if one of the available parameters is passed in. If no parameters are passed the usage banner is printed. After compiling and running the app I was convinced that the port worked!
Since That was Easy…
I thought I’d try my hand at another lib. This time I chose to port tools.macro. I went through the same configuration process, cloning, compiling and starting up the REPL so I can run the tests. This time I hit NO errors when I ran the tests. That is the easiest type of port!
Like a gambling addict I pressed my luck and chose one more library to port. Since the algo.monads library was flagged as trivial I grabbed it. When I ran the tests I was hoping for the same results as the tools.macro port but this time I hit an error. The first error was another simple swap a java class for a .NET class: java.lang.IllegalArgumentException for System.ArgumentException and re-ran the tests. The second error was in an extend-protocol call using java.lang.String. I simply swapped the java.lang.String for a System.String call. When I ran the tests again all tests passed. Another port complete! The project can be found at cljclr.algo.monads on github.
Porting libraries to ClojureCLR wasn’t nearly as time consuming or complicated as I thought it would be. In fact I have probably spent more time on writing this blog post than I did doing the ports. Of course I picked libraries flagged as trivial ports which is probably why it didn’t take long to finish. What porting these libs over did teach me was the process: Clone, Compile, Test, fix, retest. For my next porting attempt, the data.csv library I will add a step to search the cloned files for easy fixes like startsWith to StartsWith and other mappings that David laid out in his posts. Please join the porting party and port one today! I have not tested the ports against 1.2, all of my ports were done with 1.3. I will continue to test only with 1.3 unless others feel that we need to test against 1.2. If you use any of the libraries I’ve ported and run into a bug please do not hesitate to let me know.
Projects that work ‘As Is’: clojure.tools.macro
In order to help others see which libraries have been ported and/or tested for ClojureCLR I have created a page that lays out which libraries have been ported and the location of the github project. If you have ports you’d like listed on the page please let me know. The ClojureCLR Ports Status Page is here.