Mercurial > coderloop
view src/export_files.clj.orig @ 0:307a81e46071 tip
initial committ
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 01:17:49 -0700 |
parents | |
children |
line wrap: on
line source
1 (ns coderloop.export-files2 (:use clojure.java.io)3 (:use [clojure.contrib [duck-streams :only [file-str]]])4 (:use [rlm5 [classpath-utils :only [classpath]]6 [shell-write :only [sw]]])7 (:require clojure.string)8 (:import java.io.File)9 (:import [org.apache.commons.io FileUtils])10 )13 (defn trans-print [x] (println x) x)15 (def *nailgun* true)17 ;;; put standard base-path on the classpath18 (def standard-base-directory (file-str "~/.clojure-exports/"))20 (defn better-export-files [namespace name]21 (binding [*compile-path* (.getPath standard-base-directory)]22 ;;clear out classes -- prepare temp directory23 (FileUtils/forceMkdir standard-base-directory)24 (FileUtils/deleteDirectory standard-base-directory)25 (FileUtils/forceMkdir standard-base-directory)26 ;; make classes27 (compile (symbol (str namespace)))29 ;; make jar30 ;; (jar {:jarfile jar-name :basedir temp-class-dir})32 ;; move jar33 ;; (FileUtils/copyFileToDirectory jar-name target-lib-dir)34 ))36 (defmulti source-location class)38 (defmethod source-location clojure.lang.Var [sym]39 (let [source? (:file (meta sym))]40 (if source?41 (.getResource (clojure.lang.RT/baseLoader) source?)42 nil)))44 (defmethod source-location java.lang.Class [sym]45 (let [source? (.getCodeSource (.getProtectionDomain sym))]46 (if source?47 (.getLocation source?)48 nil)))50 (defn all-dependent-namespaces [namespace]51 (set52 (concat53 [namespace]54 (vals (ns-aliases namespace))55 (map #(.ns %)56 (filter #(= (class %) clojure.lang.Var)57 (concat58 (vals (ns-map namespace))59 (vals (ns-refers namespace))))))))61 (defn deep-dependent-namespaces [namespace-set]62 (let [new-namespaces (set (mapcat all-dependent-namespaces namespace-set))]63 ;;(println (count new-namespaces))64 (if (= new-namespaces namespace-set)65 namespace-set66 (recur new-namespaces))))68 (defn dependencies-url69 "returns all of the files necessary to succesfully run the namespace."70 [namespace]71 (set72 (remove73 nil?74 (map source-location75 (map second76 (mapcat ns-map (deep-dependent-namespaces #{namespace})))))))78 (defn dependencies-file [namespace]79 (map file-str80 (set81 (map #(.replaceFirst % "file:" "")82 (map #(if (.contains % ".jar!/")83 (clojure.string/replace % #"\.jar.*" ".jar") %)84 (map #(.getFile %) (dependencies-url namespace)))))))86 (defn classpath-files []87 (map file-str (clojure.string/split (classpath) #":")))89 ;;Every file that comes back from dependencies-file is also on the classpath.90 ;;In order to recreate the right project structure, we need to copy the files91 ;;with the appropiate classpath nesting.93 (defn bifurcate94 "split a collection between f and not f"95 [f coll]96 (list97 (filter f coll)98 (filter (comp not f) coll)))100 (defn jar? [#^java.io.File f]101 (re-matches #".*\.jar$" (.getCanonicalPath f)))103 (defn contains-file? [#^java.io.File parent #^java.io.File child]104 (let [new-child (.getParentFile child)]105 (cond (nil? new-child) false106 (= new-child parent) true107 true (recur parent new-child))))109 (defn destination [base-path-list #^java.io.File current-file #^java.io.File base-destination]110 (let [base-path (last (filter #(contains-file? % current-file) base-path-list))]111 (file-str112 (.replaceFirst (.getCanonicalPath current-file)113 (.getCanonicalPath base-path)114 (.getCanonicalPath base-destination)))))116 (defn export-dependencies [namespace #^java.io.File target]117 (let [[jars sources] (bifurcate jar? (dependencies-file namespace))118 jars (if *nailgun*119 (conj jars (file-str "~/roBin/nailgun-0.7.1/nailgun-0.7.1.jar"))120 jars)122 lib (file-str (str (.getCanonicalPath target) File/separatorChar "lib"))123 src (file-str (str (.getCanonicalPath target) File/separatorChar "src"))]124 (if *nailgun*125 (do126 (FileUtils/copyFileToDirectory (file-str "~/roBin/nailgun-0.7.1/ng") lib)127 (sw (str "chmod +x " (.getCanonicalPath target) File/separatorChar "lib/ng"))))128 (dorun (map #(FileUtils/copyFileToDirectory % lib) jars))129 (dorun (map #(FileUtils/copyFile % (destination (classpath-files) % src)) sources))))131 (defn run-script-text [namespace]133 (if *nailgun*134 (str136 "#!/usr/bin/perl138 if (`./lib/ng ng-alias 2>&1 1>/dev/null` eq \"connect: Connection refused\\n\"){140 $nailgun_init =141 \"java -Xmn500M -Xms2000M -Xmx2000M \" .142 \"-classpath ./src/:lib/*\" . \" -server \".143 \"com.martiansoftware.nailgun.NGServer \".144 \" >/dev/null 2>/dev/null &\";145 `$nailgun_init`;146 }\n\n"149 "while (`./lib/ng ng-alias 2>&1 1>/dev/null` eq \"connect: Connection refused\\n\"){150 }\n\n"152 "$command = "155 "\"./lib/ng clojure.main "156 "./src/"157 (.replace (.replace (str *ns*) \. File/separatorChar) \- \_)158 ".clj "159 " @ARGV\";\n"161 "print `$command`;\n")162 (str163 "#!/bin/bash\n"164 "java -Xmn500M -Xms2000M -Xmx2000M -server -cp ./lib/*:./src clojure.main "165 "./src/"166 (.replace (.replace (str *ns*) \. File/separatorChar) \- \_)167 ".clj "168 " $@\n")))172 (defn make-run-script [namespace base-dir name]173 (let [w (clojure.java.io/writer174 (str (.getCanonicalPath base-dir) File/separatorChar name))]175 (.write w (run-script-text namespace))176 (.close w))177 (let [f (file-str (str (.getCanonicalPath base-dir) File/separatorChar name))]178 (.setExecutable f true)))180 (defn export-files [namespace base-dir name]181 (export-dependencies namespace base-dir)182 (make-run-script namespace base-dir name))184 (defn bzip-folder [#^java.io.File destination #^java.io.File directory]185 (apply (partial sw "tar" "-cvjf" (.getCanonicalPath destination))186 (concat187 (rest188 (map #(.replaceFirst189 %190 (str (.getCanonicalPath directory) File/separatorChar ) "")191 (map str (file-seq directory))))192 [:dir (.getCanonicalPath directory)])))194 (defn bzip-export-files [directory target-dir name]195 (apply (partial sw "tar" "-cvjf"196 (str (.getCanonicalPath target-dir) File/separatorChar name))197 (concat198 [name "lib" "src"]199 [:dir (.getCanonicalPath directory)])))201 (defn export-archive202 ([name]203 (export-archive *ns* name))205 ([namespace name nailgun?]206 (binding [*nailgun* nailgun?]207 (export-archive namespace name)))209 ([namespace name]210 (FileUtils/forceMkdir standard-base-directory)211 (FileUtils/forceDelete standard-base-directory)212 (FileUtils/forceMkdir standard-base-directory)213 (let [new-dir214 (file-str215 (str216 (.getCanonicalPath standard-base-directory)217 (File/separatorChar)218 name))]220 (export-files namespace new-dir name)222 (bzip-export-files new-dir (file-str "~/coderloop/") (str name "-clojure.tar.bz2"))223 (FileUtils/copyFileToDirectory224 (file-str (str "~/coderloop/" name "-clojure.tar.bz2"))225 (file-str "~/coderloop-test")))))