Skip to content

Instantly share code, notes, and snippets.

@regularfry
Created October 22, 2009 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save regularfry/215647 to your computer and use it in GitHub Desktop.
Save regularfry/215647 to your computer and use it in GitHub Desktop.
;; Some fairly nasty elisp for a very simple purpose: to set up a new clojure-project
;; and run clojure-project in it. For those of us who Know Not The Ways Of Maven.
(defun clojure-project-setup (path)
"Set up a project structure for clojure-project and copy any
interesting-looking jars into lib/."
(interactive (list
(ido-read-directory-name
"Project root: "
(locate-dominating-file default-directory "~"))))
(if (not (file-directory-p path))
(let
((subpaths '("" "lib" "src" "test" "target/classes" "target/dependency")))
(dolist (subpath subpaths)
(make-directory (file-join path subpath) t))
(copy-fun-jars-to-project clojure-src-root path)
(clojure-project path))
(error "The directory %s already exists. I won't overwrite it." path)))
;; Internals
(defun copy-fun-jars-to-project (src-root dest-root)
"Copy important jars from the src-root into the new project."
(if (file-directory-p path)
(dolist (jar (find-fun-jars src-root))
(copy-file jar (file-join dest-root "lib")))
(error "The directory %s doesn't exist, so can't be a clojure project." dest-root)))
(defun find-fun-jars (src-root)
"Return a list of filenames of important jar files. Currently looks
for jars of the form src-root/foo/foo.jar; that is, named the same
as the folder they're in. This seems to be a convention."
(let* ((directories (select
(lambda (d) (file-directory-p (file-join src-root d)))
(directory-files src-root nil "^[^\.].*$")))
(maybe-jars (mapcar
(lambda (d) (file-join src-root d (concat d ".jar")))
directories)))
(select (lambda (j) (file-exists-p j)) maybe-jars)))
;; Utils
(defun file-join (&rest path-parts)
(defun file-join-rec (cur rest)
(if rest
(file-join-rec (concat (file-name-as-directory (car rest)) cur) (cdr rest))
cur))
(let ((rpath (reverse path-parts)))
(file-join-rec (car rpath) (cdr rpath))))
(defun select (fun lst)
(defun select-recur (running fun lst)
(if lst
(let ((fst (car lst))
(rest (cdr lst)))
(let ((new-running (if (apply fun (list fst))
(cons fst running)
running)))
(select-recur new-running fun rest)))
running))
(reverse (select-recur () fun lst)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment