(ns krbutil) (defn methods-seq ([thing] (if (= Class (class thing)) (seq (. thing getDeclaredMethods)) (seq (. (class thing) getDeclaredMethods))))) (defn fields-seq ([thing] (if (= Class (class thing)) (seq (. thing getDeclaredFields)) (seq (. (class thing) getDeclaredFields))))) (defn constructors-seq ([thing] (if (= Class (class thing)) (seq (. thing getConstructors)) (seq (. (class thing) getConstructors))))) (defn method-modifiers-as-strings [#^java.lang.reflect.Method method] (str (. method getModifiers))) (defn method-return-type [#^java.lang.reflect.Method method] (. method getReturnType)) (defn method-name-short [#^java.lang.reflect.Method method] (. method getReturnType)) (defn method-argument-list [#^java.lang.reflect.Method method] (. method getParameterTypes)) (defn short-method-sig [#^java.lang.reflect.Method method] (str (method-modifiers-as-strings method) " " (method-return-type method) " " (method-name-short method) "(" (method-argument-list method) ")")) (defn make-subst-fn [pattern replacement] (fn [str] (.replaceAll (.toString str) pattern replacement))) ;; TODO: add in abstract/interface/class info, then list all parent ;; classes and interfaces (defn doc-class [thing] "Prints (to *out*) a summary of the class, it's members and its methods." (let [tclass (if (= Class (class thing)) (identity thing) (class thing)) trimmer (make-subst-fn "java.lang." "")] (println (format "Class %s (%s)" (.getName tclass) (.getSuperclass tclass))) (println (format " Interfaces:")) (doseq [interf (seq (.getInterfaces tclass))] (println (str " " (trimmer interf)))) (println (str " Constructors:")) (doseq [constructor (constructors-seq tclass)] (println (str " " (trimmer constructor)))) (println (str " Members:")) (doseq [field (fields-seq tclass)] (println (str " " (trimmer field)))) (println (str " Methods:")) (doseq [method (methods-seq tclass)] (println (str " " (trimmer method)))))) (defn is-dir? [path] (.isDirectory (new java.io.File path))) (defn is-file? [path] (.isFile (new java.io.File path))) (defn is-absolute? [path] (.isAbsolute (new java.io.File path))) (defn is-hidden? [path] (.isHidden (new java.io.File path))) (def *filesep* java.io.File/separator) (def *pathsep* java.io.File/pathSeparator) (defn path-concat [part & parts] (if (empty? parts) part (apply path-concat (.toString (new java.io.File part (first parts))) (rest parts)))) (assert (= "/foo" (path-concat "/" *filesep* "foo"))) (defn directory-contents [path] (if (is-dir? path) (map #(path-concat path *filesep* %) (seq (.list (new java.io.File path)))) nil)) ; (take 3 (directory-contents "/etc/")) (defn files-in-directory [path] (filter is-file? (directory-contents path))) ; (files-in-directory "/etc") (defn dirs-in-directory [path] (filter is-dir? (directory-contents path))) ; (dirs-in-directory "/etc") ;; (defn recursively-visit-directory ;; "Depth first traversal of the given paths, invoking fn on each path, ;; including those of the top level." ;; [fn & paths] ;; (if (empty? paths) ;; nil ;; (do ;; (let [[path & paths] paths] ;; (fn path) ;; (if (is-directory? path) ;; (recursively-visit-directory fn (concat ))))))) (defn expand-file-name [path] (cond (isa? path java.io.File) (java.io.File. (expand-file-name (str path))) true (if (.startsWith path "~/") (.replaceFirst path "^~(/|$)" (str (System/getProperty "user.home") "$1"))))) ;; (.exists (java.io.File. (krbutil/expand-file-name "~/personal/projects/clojure/sandbox/test.clj"))) (defn all-permutations [things] (if (= 1 (count things)) (list things) (for [head things tail (all-permutations (disj (set things) head))] (do (cons head tail))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (import '(java.io BufferedReader InputStreamReader PrintWriter StringWriter)) (defn cmd ([p] (.. Runtime getRuntime (exec (str p)))) ([p dir] (.. Runtime getRuntime (exec (str p) nil dir)))) ;; (print (cmdout (cmd "pwd" (java.io.File. "/home/mortis/tmp")))) (defn cmdout [o] (let [r (BufferedReader. (InputStreamReader. (.getInputStream o))) e (BufferedReader. (InputStreamReader. (.getErrorStream o))) sw (StringWriter.) wr (PrintWriter. sw)] (dorun (map #(.println wr %) (line-seq e))) (dorun (map #(.println wr %) (line-seq r))) (.toString (.getBuffer sw)))) ;; (count (cmdout (cmd "ls -a"))) ;; => 1081 ;;; these 2 don't work, since Runtime.exec seems to be parsing the cmdline :( ;;; (print (cmdout (cmd "/bin/bash -c \"pwd; cd /home/mortis/tmp; touch foo; pwd\""))) ;;; (print (cmdout (cmd "/bin/bash -c 'pwd; cd ~/tmp/; pwd'"))) ;; this works: ;; (print (cmdout (cmd "pwd" (java.io.File. "/home/mortis/tmp")))) (defn cmd->str [command] (cmdout (cmd command))) (defn cmd->str [command path] (cond (= (class path) java.io.File) (cmdout (cmd command path)) (= (class path) java.lang.String) (cmdout (cmd command (java.io.File. path))) true (throw "Error, don't know what that is: '" path "'"))) ;; (print (cmd->str "ls" "/home/mortis/tmp")) ;; (print (cmd->str "ls" "/home/")) ;; (def output ;; (cmd->str "mvn dependency:build-classpath" "/home/mortis/personal/projects/clojure/sandbox/software/apache/vfs")) ;; (print output) ;; (seq (.split (nth (re-find #"Dependencies classpath:\n(.+?)\n" output) 1) ;; ":")) ;; (defn mvn-deps [project-path] (let [output (cmd->str "mvn dependency:build-classpath" project-path)] (map #(str "file://" %) (seq (.split (nth (re-find #"Dependencies classpath:\n(.+?)\n" output) 1) ":"))))) (defn load-mvn-deps [project-path] (let [output (cmd->str "mvn dependency:build-classpath" project-path)] (map add-classpath (mvn-deps project-path))))