Tuesday, September 22, 2009

Managing Clojure project files

When I started off with clojure I had a difficult time figuring out how to use namespace in my applications. What I did was to create a little code that would be doing the dirt job for me while I concentrate my time in learning and practicing the real thing. It is more of interop thus imperative but it still has Clojure spirit.

(defn get-path [project-name]
(let [user-name (System/getProperty "user.name")
user-home (System/getProperty "user.dir")]
(str user-home "\\src\\" user-name "\\" project-name)))

;;make-folder: string -> true
;;consumes string , creates files and folders and return true.
(defn make-folder [project-name]
(let [ project-path (get-path project-name)
file (java.io.File. project-path)
user-home (System/getProperty "user.dir")
class-path (java.io.File. user-home "\\class")]
(when (and (.mkdirs file)(.mkdirs class-path))
(.createNewFile (java.io.File. (str file "\\" project-name ".clj") ))
(.createNewFile (java.io.File. (str file "\\" project-name "_utils.clj" )))
(.createNewFile (java.io.File. (str file "\\" project-name "_extra.clj" )))


;;write-extra: string file -> nil
;; write the string to the file
(defn write-extra [str file]
(with-open [write (java.io.PrintWriter. (java.io.FileWriter. file))]
(. write println str)))

;;process-path:vector -> nil
;;reads vectors and process path and words to be written to files.
(defn process-path [[file-name file-path]]
(let [goof (subs file-name 0 ( - (count file-name) 4))
count-char (count goof)
last-five-char (if ( < 5 count-char) goof (subs goof (- count-char 5) count-char ))
last-index (.lastIndexOf file-path "src")
extract (subs file-path (+ last-index 4) (count file-path))
extract (subs extract 0 (- (count extract) 4))
remake-extract (.replace extract \\ \.)]
(if (or (= "_utils" last-five-char)(= "_extra" last-five-char))
(write-extra (str "(ns\t" (.replace remake-extract \_ \-) ")") file-path) (write-extra (str "( ns " remake-extract "\n" "( :use\t" remake-extract "-extra" "\n\t" remake-extract "-utils ) )" ) file-path) )))

(defn make-proper [mcoll]
(doseq [file-vec mcoll]
(process-path [ (str (file-vec 0)) (str (file-vec 1))] )))

;; make-project: string -> nil
;; it consumes the name of your project, it should be more than five letters word
(defn make-project [project-name]
(when (make-folder project-name)
(make-proper (apply hash-map (apply concat (map (fn [n] [ (.getName n) n])
(seq (.listFiles (java.io.File. (get-path project-name))))))))

1 comment:

ronen said...

Consider using http://code.google.com/p/syntaxhighlighter/

It can make reading code easier.


Arduino (1) C (3) Clojure (3) Perl (1) the other side (8) VBA (1)

micro's shared items