28 August 2012

Git branch in prompt

Just a quick tip about how to display the git branch currently checked out when using the bash prompt. Put this little routine in your .bash_profile or similar:

# Git branch in prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\[\e]0;\h - \w\a\]\n\[\e[32m\]\u@\h \[\e[36m\]\$(parse_git_branch) \[\e[33m\]\w\[\e[0m\]\n\$ "

This will give you a prompt like this when you are standing in a git directory:

user@host (master) /cygdrive/c/repos/project/
In addition to being useful, it is ansi color coded so it will look good, too.

25 August 2012

Adding jars to local repository with Leiningen

Last night I thought I should experiment a little with Enlive, an html library written in Clojure. Enlive makes it very easy to scrape web-pages, apparently.
However, I could not find a jar for Enlive in the central Maven repository, neither was there one in Clojars. I did find some that other people had built, but I never trust strangers on the net. ;) So I figured I should build it myself and put it in my local Maven repository. This is how I did it:

First clone the repository from Github

git clone https://github.com/cgrand/enlive.git
Then build it with Leiningen

cd enlive
lein install
Now, this install the pom-file to my local repository, but it won't put the jar in the repository. At this point I was contemplating using Maven to install the jar, or even manually renaming and copying the jar to the correct folder in the repository but I thought that surely Leiningen must have a way to do this.

So I found a plugin to do it. Shantanu Kumar has made a plugin (lein-localrepo) for exactly this purpose. I am using Leiningen 2 so I added lein-localrepo to my ~/.lein/profiles.clj:

{:user {:plugins [[lein-localrepo "0.4.0"]]}}
After that I could use the handy coords command pointed at my generated jar to find Maven coordinates for the jar and pipe them into the install command to put the jar at the correct position in my local repository.

> lein localrepo coords target/enlive-1.0.1.jar
target/enlive-1.0.1.jar enlive/enlive 1.0.1

> lein localrepo coords target/enlive-1.0.1.jar | xargs lein localrepo install

WARNING: using numeric exit values in plugins is deprecated.
Plugins should use leiningen.core.main/abort instead.
Support for this will be removed before the stable 2.0.0 release.
localrepo failed.
I do get an error message, but the install command completed successfully anyway. Now on to scraping some web pages.