librelist archives

« back to archive

Running an arbitrary command before building or running?

Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-18 @ 14:56
Hi, I’m using Lein 1.7.1, and my project includes a CoffeeScript file.

I’d like to set up my project so that whenever it builds or runs, it will run:
  coffee -c resources/public/*.coffee

Is there a quick/simple/easy way to do this? I hope I don’t have to
write a plugin to accomplish this.

Thanks,
Avi

Avi Flax » Arc90 » http://arc90.com
Kindling » Innovation through collaboration » http://kindlingapp.com
Readability » Read comfortably anytime, anywhere » http://readability.com

Re: [leiningen] Running an arbitrary command before building or running?

From:
Phil Hagelberg
Date:
2012-07-18 @ 18:14
Avi Flax <avif@arc90.com> writes:

> Hi, I’m using Lein 1.7.1, and my project includes a CoffeeScript file.
>
> I’d like to set up my project so that whenever it builds or runs, it will run:
>   coffee -c resources/public/*.coffee
>
> Is there a quick/simple/easy way to do this? I hope I don’t have to
> write a plugin to accomplish this.

It's much easier to do this in Leiningen 2.x, but in 1.x you can use
robert-hooke to perform extra calls before every eval-in-project call:

It would look something like this:

(add-hook #'leiningen.compile/eval-in-project
          (fn [eip & args]
            (clojure.java.shell "mycommand")
            (apply eip args)))

See https://github.com/technomancy/robert-hooke for details.

-Phil

Re: [leiningen] Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-18 @ 19:12
On Wed, Jul 18, 2012 at 2:14 PM, Phil Hagelberg <phil@hagelb.org> wrote:
> It's much easier to do this in Leiningen 2.x, but in 1.x you can use
> robert-hooke to perform extra calls before every eval-in-project call:
>
> It would look something like this:
>
> (add-hook #'leiningen.compile/eval-in-project
>           (fn [eip & args]
>             (clojure.java.shell "mycommand")
>             (apply eip args)))

Thanks Phil!

I get the concept of add-hook (and it’s pretty cool) but I don’t seem
to understand Leiningen enough to understand where exactly I should
put this code. Would it go in my core namespace? Or…?

Thanks,
Avi

Re: [leiningen] Running an arbitrary command before building or running?

From:
Phil Hagelberg
Date:
2012-07-18 @ 23:05
Avi Flax <avif@arc90.com> writes:

> I get the concept of add-hook (and it’s pretty cool) but I don’t seem
> to understand Leiningen enough to understand where exactly I should
> put this code. Would it go in my core namespace? Or…?

Oh, you can put this in project.clj, but you'll need to require
robert.hooke, leiningen.compile, and clojure.java.shell first.

-Phil

Re: [leiningen] Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-18 @ 23:29
On Wed, Jul 18, 2012 at 7:05 PM, Phil Hagelberg <phil@hagelb.org> wrote:
> Oh, you can put this in project.clj, but you'll need to require
> robert.hooke, leiningen.compile, and clojure.java.shell first.

Ah, of course, that makes sense! Thanks!

Unfortunately I seem to be doing something wrong; when I run “lein run” I get:

Exception in thread "main" java.lang.ClassNotFoundException:
robert.hooke (project.clj:19)

Here’s the contents of my project.clj:

(defproject ixdictionary "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [ring "1.1.1"]
                 [tnrglobal/bishop "1.1.0"]
                 [cheshire "4.0.1"]
                 [hiccup "1.0.0"]
                 [korma "0.3.0-beta11"]
                 [clj-http "0.5.0"]
                 [org.clojure/data.xml "0.0.6"]]
  :dev-dependencies [[com.h2database/h2 "1.3.167"]
                     [org.drift-db/drift-db-h2 "1.1.0"]
                     [robert/hooke "1.1.2"]]
  :warn-on-reflection true
  :main ixdictionary.core
  :plugins [[lein-ring "0.7.1"]]
  :ring {:handler ixdictionary.core/ring-handler})

(require [robert.hooke :as hooke]
         [leiningen.compile]
         [clojure.java.shell :as shell])

;; hook to compile Coffeescript before Leiningen does stuff
(hooke/add-hook #'leiningen.compile/eval-in-project
  (fn [eip & args]
    (shell/sh "coffee" "-c" "resources/public/*.coffee")
    (apply eip args)))

Thanks for all the help!
Avi

Re: [leiningen] Running an arbitrary command before building or running?

From:
Phil Hagelberg
Date:
2012-07-19 @ 00:22
Avi Flax <avif@arc90.com> writes:

> (require [robert.hooke :as hooke]
>          [leiningen.compile]
>          [clojure.java.shell :as shell])

You should be fine if you quote the symbols here.

-Phil

Re: [leiningen] Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-19 @ 15:50
On Wed, Jul 18, 2012 at 8:22 PM, Phil Hagelberg <phil@hagelb.org> wrote:
> You should be fine if you quote the symbols here.

It’s working! Thanks so much Phil!

Avi

Re: [leiningen] Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-20 @ 18:34
Phil, thanks again for your help!

I’ve written up how to get this working in a quick blog post:


http://blog.arc90.com/2012/07/19/compile-your-coffeescript-along-with-your-clojure/

Have a great weekend!

Avi

Re: [leiningen] Running an arbitrary command before building or running?

From:
Phil Hagelberg
Date:
2012-07-20 @ 20:49
Avi Flax <avif@arc90.com> writes:

> I’ve written up how to get this working in a quick blog post:
>
> 
http://blog.arc90.com/2012/07/19/compile-your-coffeescript-along-with-your-clojure/

Cool. Do you think you could add a note that this slightly more awkward
approach is only necessary because you're still using Leiningen 1.x?
It's much cleaner with :prep-tasks in 2.x.

thanks,
Phil

Re: [leiningen] Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-20 @ 21:11
On Jul 20, 2012, at 16:49, Phil Hagelberg <phil@hagelb.org> wrote:

> Do you think you could add a note that this slightly more awkward
> approach is only necessary because you're still using Leiningen 1.x?
> It's much cleaner with :prep-tasks in 2.x.

I'd be happy to. In fact, I'll update the post to illustrate both 
approaches. So… how does :prep-tasks work?

Re: [leiningen] Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-23 @ 15:13
On Fri, Jul 20, 2012 at 5:11 PM, Avi Flax <avif@arc90.com> wrote:
> I'd be happy to. In fact, I'll update the post to illustrate both 
approaches. So… how does :prep-tasks work?

Phil, is there documentation for this somewhere? I’m sorry if this is
the kind of thing that’d be obvious to people versed in Maven… I’ve
always kinda hated Maven.

Re: [leiningen] Running an arbitrary command before building or running?

From:
Phil Hagelberg
Date:
2012-07-23 @ 21:12
On Mon, Jul 23, 2012 at 8:13 AM, Avi Flax <avif@arc90.com> wrote:
>> I'd be happy to. In fact, I'll update the post to illustrate both 
approaches. So… how does :prep-tasks work?
>
> Phil, is there documentation for this somewhere? I’m sorry if this is
> the kind of thing that’d be obvious to people versed in Maven… I’ve
> always kinda hated Maven.

Sorry, documenting this is on the TODO.

The gist is that every task in :prep-tasks runs before any
eval-in-project call. This defaults to ["javac" "compile"], but you
can customize it in project.clj. In your case you'd need a
coffeescript task implemented by a plugin, but adding ["coffeescript"]
to :prep-tasks would do the trick, since it would be merged with the
default value.

-Phil

Re: [leiningen] Running an arbitrary command before building or running?

From:
Avi Flax
Date:
2012-07-24 @ 15:12
On Mon, Jul 23, 2012 at 5:12 PM, Phil Hagelberg <phil@hagelb.org> wrote:
> Sorry, documenting this is on the TODO.

No worries, I know the focus right now is on fixing bugs and
implementing missing features so you can ship 2.0.0-final. It’s a real
pain to maintain two active versions of a codebase.

> The gist is that every task in :prep-tasks runs before any
> eval-in-project call. This defaults to ["javac" "compile"], but you
> can customize it in project.clj. In your case you'd need a
> coffeescript task implemented by a plugin, but adding ["coffeescript"]
> to :prep-tasks would do the trick, since it would be merged with the
> default value.

OK, that’s helpful, thanks! Unfortunately it’s still a little bit
beyond my Maven expertise. (A big reason I like Leiningen is because I
don’t have to know too much about Maven.) Hopefully at some point I’ll
give :prep-tasks a go; in the meantime I’ve updated my post with your
description of the approach.

Thanks again!
Avi