Skip to content

Commit

Permalink
Move some path functions to leiningen.util.paths. Add defdeprecated.
Browse files Browse the repository at this point in the history
Rearrange some of leiningen.core while we're at it.
  • Loading branch information
technomancy committed May 29, 2011
1 parent fa172db commit 6e18fc4
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 60 deletions.
117 changes: 57 additions & 60 deletions src/leiningen/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,43 @@
(:use [leiningen.util.ns :only [namespaces-matching]]
[clojure.string :only [split]]
[clojure.walk :only [walk]]
[robert.hooke :only [add-hook]])
[robert.hooke :only [add-hook]]
[clojure.java.io :only [file]])
(:require [lancet.core :as lancet]
[leiningen.util.paths :as paths])
(:import (java.io File)
(org.apache.maven.artifact.versioning DefaultArtifactVersion)))

(defmacro defdeprecated [old new]
`(let [new# ~(str (.getName (:ns (meta (resolve new)))) "/" (name new))
warn# (delay (println "Warning:" '~old "is deprecated; use" new#))]
(defn ~(vary-meta old assoc :doc (format "Compatibility alias for %s" new))
[& args#]
(force warn#)
(apply ~(resolve new) args#))))

(defdeprecated home-dir paths/leiningen-home)

(defdeprecated ns->path paths/ns->path)

(defdeprecated normalize-path paths/normalize-path)

(defn user-init
"Load the user's ~/.lein/init.clj file, if present."
[]
(let [init-file (File. (paths/leiningen-home) "init.clj")]
(when (.exists init-file)
(load-file (.getAbsolutePath init-file)))))

(defn user-settings
"Look up the settings map from init.clj or an empty map if it doesn't exist."
[]
(if-let [settings-var (resolve 'user/settings)]
@settings-var
{}))

;;; defproject

(def ^{:private true} project nil)

(defn- unquote-project [args]
Expand All @@ -17,18 +50,13 @@
identity
args))

(defn ^{:internal true} normalize-path [project-root path]
(when path
(let [f (File. path)]
(.getAbsolutePath (if (.isAbsolute f) f (File. project-root path))))))

(defmacro defproject [project-name version & args]
;; This is necessary since we must allow defproject to be eval'd in
;; any namespace due to load-file; we can't just create a var with
;; def or we would not have access to it once load-file returned.
`(let [m# (apply hash-map ~(cons 'list (unquote-project args)))
root# ~(.getParent (File. *file*))
normalize-path# (partial ~normalize-path root#)]
normalize-path# (partial ~paths/normalize-path root#)]
(alter-var-root #'project
(fn [_#] (assoc m#
:name ~(name project-name)
Expand Down Expand Up @@ -75,41 +103,13 @@
":target-dir.")))
#'project))

(defn exit
"Call System/exit. Defined as a function so that rebinding is possible."
([code]
(shutdown-agents)
(System/exit code))
([] (exit 0)))

(defn abort
"Print msg to standard err and exit with a value of 1."
[& msg]
(binding [*out* *err*]
(apply println msg)
(exit 1)))

(defn home-dir
"Returns full path to Lein home dir ($LEIN_HOME or $HOME/.lein) if it exists"
[]
(.getAbsolutePath (doto (if-let [lein-home (System/getenv "LEIN_HOME")]
(File. lein-home)
(File. (System/getProperty "user.home") ".lein"))
.mkdirs)))

(defn user-init
"Load the user's ~/.lein/init.clj file, if present."
[]
(let [init-file (File. (home-dir) "init.clj")]
(when (.exists init-file)
(load-file (.getAbsolutePath init-file)))))

(defn user-settings
"Look up the settings map from init.clj or an empty map if it doesn't exist."
[]
(if-let [settings-var (resolve 'user/settings)]
@settings-var
{}))
(defn read-project
([file]
(try (binding [*ns* (the-ns 'leiningen.core)]
(load-file file))
project
(catch java.io.FileNotFoundException _)))
([] (read-project "project.clj")))

(def default-repos {"central" {:url "http://repo1.maven.org/maven2"
:snapshots false}
Expand All @@ -131,13 +131,21 @@
(into {} (for [[id settings] (:repositories project)]
[id (init-settings id settings)]))))

(defn read-project
([file]
(try (binding [*ns* (the-ns 'leiningen.core)]
(load-file file))
project
(catch java.io.FileNotFoundException _)))
([] (read-project "project.clj")))
(defn exit
"Call System/exit. Defined as a function so that rebinding is possible."
([code]
(shutdown-agents)
(System/exit code))
([] (exit 0)))

(defn abort
"Print msg to standard err and exit with a value of 1."
[& msg]
(binding [*out* *err*]
(apply println msg)
(exit 1)))

;;; Task execution

(def aliases (atom {"--help" "help" "-h" "help" "-?" "help" "-v" "version"
"--version" "version" "überjar" "uberjar" "cp" "classpath"
Expand Down Expand Up @@ -174,17 +182,6 @@
(when (System/getenv "DEBUG")
(.printStackTrace e)))))))

(defn ns->path [n]
(str (.. (str n)
(replace \- \_)
(replace \. \/))
".clj"))

(defn path->ns [path]
(.. (.replaceAll path "\\.clj" "")
(replace \_ \-)
(replace \/ \.)))

(defn arglists [task-name]
(:arglists (meta (resolve-task task-name))))

Expand Down
62 changes: 62 additions & 0 deletions src/leiningen/util/paths.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(ns leiningen.util.paths
(:use [clojure.java.io :only [file]]))

(defn ^{:internal true} normalize-path [project-root path]
(when path
(let [f (file path)]
(.getAbsolutePath (if (.isAbsolute f) f (file project-root path))))))

(defn- get-by-pattern
"Gets a value from map m, but uses the keys as regex patterns, trying
to match against k instead of doing an exact match."
[m k]
(m (first (drop-while #(nil? (re-find (re-pattern %) k))
(keys m)))))

(def ^{:private true} native-names
{"Mac OS X" :macosx "Windows" :windows "Linux" :linux
"FreeBSD" :freebsd "OpenBSD" :openbsd
"amd64" :x86_64 "x86_64" :x86_64 "x86" :x86 "i386" :x86
"arm" :arm "SunOS" :solaris "sparc" :sparc})

(defn get-os
"Returns a keyword naming the host OS."
[]
(get-by-pattern native-names (System/getProperty "os.name")))

(defn get-arch
"Returns a keyword naming the host architecture"
[]
(get-by-pattern native-names (System/getProperty "os.arch")))

(defn native-path
"Returns a File representing the directory where native libs for the
current platform are located."
[project]
(when (and (get-os) (get-arch))
(let [osdir (name (get-os))
archdir (name (get-arch))
f (file "native" osdir archdir)]
(if (.exists f)
f
nil))))

(defn leiningen-home
"Returns full path to Lein home dir ($LEIN_HOME or $HOME/.lein) if it exists"
[]
(.getAbsolutePath (doto (if-let [lein-home (System/getenv "LEIN_HOME")]
(file lein-home)
(file (System/getProperty "user.home") ".lein"))
.mkdirs)))

(defn ns->path [n]
(str (.. (str n)
(replace \- \_)
(replace \. \/))
".clj"))

(defn path->ns [path]
(.. (.replaceAll path "\\.clj" "")
(replace \_ \-)
(replace \/ \.)))

0 comments on commit 6e18fc4

Please sign in to comment.