├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── ReleaseNotes.md ├── profiles.clj ├── project.clj ├── src ├── com │ └── palletops │ │ └── shorthand.clj └── lein_shorthand │ └── plugin.clj └── test ├── com └── palletops │ └── shorthand_test.clj ├── test_ns.clj ├── test_ns2.clj ├── test_ns3.clj └── test_ns4.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: clojure 2 | before_script: 3 | - lein version 4 | script: lein test 5 | env: 6 | global: 7 | secure: VCvQgo7qCf9s+4InMXIvTfhDX81UY2m1gXAS4obt2LD19rzEdX9Olgpp9BywKU7rUCTAh/IpgrQ2Ino2YcpQShr/37/GSdF0MUtuVLUiVkDG7pM8jcushCMIJkfdK/wqg7bCK48EdlioLhYTupTjdNRSeWgM1gdwRGw2Is5ni2g= 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/palletops/lein-shorthand.svg?branch=develop)](https://travis-ci.org/palletops/lein-shorthand) 2 | 3 | # lein-shorthand 4 | 5 | A leiningen plugin to create clojure namespaces with short names, so 6 | you can easily call utility functions in the REPL using fully 7 | qualified symbols. 8 | 9 | In the repl, there are functions that you always want to have handy, 10 | no matter which namespace you are in. One way of achieving this is to 11 | put these functions into a namespace with a short name, like `.`, so 12 | you can refer to them easily. The original idea came from Gary 13 | Fredericks, who also wrote [dot-slash][dot-slash], which I discovered 14 | after writing this. Many thanks to Gary for suggesting lein-shorthand 15 | as the name for this plugin. 16 | 17 | The approach has also been generalized to a library at 18 | [dot-slash-2](https://github.com/gfredericks/dot-slash-2). 19 | 20 | ## Usage 21 | 22 | **Note that the group-id was changed from `com.palletops` 23 | to `com.gfredericks` at release `0.4.1`.** 24 | 25 | Add `lein-shorthand` to `:plugins` in the `:user` profile of 26 | `~/.lein/profiles.clj`: 27 | 28 | ```clj 29 | {:user 30 | {… 31 | :plugins [[com.gfredericks/lein-shorthand "0.4.1"]] 32 | …}} 33 | ``` 34 | 35 | You create shorthand namespaces using the `:shorthand` project key 36 | (again, in the `:user` profile). For example to define the `.` 37 | namespace with clojure's `pprint` and alembic's `still` function and 38 | `lein` macro: 39 | 40 | ```clj 41 | :shorthand {. [clojure.pprint/pprint 42 | alembic.still/distill 43 | alembic.still/lein]} 44 | ``` 45 | 46 | In the repl you will then be able to use `(./distill 47 | [[clj-http "1.0.0."]])` to add `clj-http` to your classpath. 48 | 49 | You can also rename symbols: 50 | 51 | ```clj 52 | :shorthand {. {pp clojure.pprint/pprint}} 53 | ``` 54 | 55 | The `:shorthand` value is a map where the keys are namespace symbols, 56 | and the values are either a sequence of fully qualified symbols for 57 | vars you want injected into in the namespace, or a map from 58 | unqualified symbol, for the name of the function in the target 59 | namespace, to the fully qualified symbol of the var it should map to. 60 | 61 | By default `:shorthand` namespaces will eagerly load all the 62 | namespaces of vars that you specify, which can increase your REPL 63 | start-up time, and increase the number of namespaces that are loaded 64 | in your REPL but not used in your projects. 65 | 66 | You can make the require of the target namespace happen lazily, on 67 | first use, by using the `:shorthand-fns` and `:shorthand-macros` keys 68 | instead of the `:shorthand`. Since the vars will not be required 69 | until first use, you have to explicitly use the `:shorthand-macros` 70 | key for macros as `lein-shorthand` has no other way of knowing that it 71 | should add `:macro` metadata to these vars. 72 | 73 | If you prefer, you can use `:shorthand` and add metadata to the 74 | symbols that you wish to use lazy require with. 75 | 76 | ```clj 77 | :shorthand {. [clojure.pprint/pprint 78 | ^:lazy alembic.still/distill 79 | ^:lazy ^:macro alembic.still/lein]} 80 | ``` 81 | 82 | ## How it works 83 | 84 | `:shorthand` defines vars in the target namespace. The vars values 85 | are set to the vars resolved by the specified symbols. The namespace 86 | of the var is eagerly required when the function is defined. This 87 | works with functions, macros and protocol functions. 88 | 89 | `:shorthand-fns`, `:shorthand-macros` and symbols annotated with 90 | `:lazy` metadata in the `:shorthand` key, define functions and macros 91 | in the target namespace, that resolve the var to inject, and replaces 92 | itself with that var. This means that the injected function isn't 93 | actually required until it is used. Since the source var is not 94 | available when the function is defined, you have to explicitly specify 95 | which symbols are macros. 96 | 97 | The shothand vars always tracks the target var. 98 | 99 | ## Differences with dot-slash 100 | 101 | `lein-shorthand` does not add anything to your classpath. 102 | 103 | `dot-slash` adds a dependency on `potemkin` to your project's 104 | classpath in order to propagate var updates to the injected 105 | vars. `lein-shorthand` achieves the same effect by using the source var 106 | as the value of the injected var, without `deref`'ing it. 107 | 108 | `lein-shorthand` uses the `:injections` key and can be composed using 109 | multiple profiles, while `dot-slash` uses `[:repl-options :init]`. 110 | 111 | ## License 112 | 113 | Copyright © 2014 Hugo Duncan 114 | 115 | Distributed under the Eclipse Public License either version 1.0 or (at 116 | your option) any later version. 117 | 118 | [dot-slash]:https://github.com/gfredericks/dot-slash "Gary Fredericks' dot-slash plugin" 119 | -------------------------------------------------------------------------------- /ReleaseNotes.md: -------------------------------------------------------------------------------- 1 | ## 0.4.1 (2017-12-02) 2 | 3 | - **Change maven group-id to `com.gfredericks`** 4 | - Fixed [clojure 1.9 compatibility](https://github.com/palletops/lein-shorthand/issues/7) 5 | 6 | ## 0.4.0 7 | 8 | - Rename project to lein-shorthand 9 | Now uses the :shorthand project key for configuration. 10 | 11 | - Use create-ns rather than in-ns 12 | Simplifies the generated code, removing a try…finally form. 13 | 14 | - Remove :shorthand-protocol-fns key 15 | This was incorrect, as the :protocol metadata requires the var of the 16 | protocol as a value. 17 | 18 | ## 0.3.1 19 | 20 | - Speed up plugin loading 21 | Avoid the `require` on `leiningen.repl`. This saves about 1.5s on `lein 22 | version` when the plugin is active. 23 | 24 | ## 0.3.0 25 | 26 | - Use metadata on :inject-ns symbols to lazy inject 27 | Use :lazy, and :macro as required, metadata on symbols specified in the 28 | :inject-ns project key to specify lazy loading. 29 | 30 | ## 0.2.0 31 | 32 | - Add Lazy injection of functions and macros 33 | Eagerly loading all the namespaces of vars that you inject, can increase 34 | your REPL start-up time, and increase the number of namespaces that are 35 | loaded in your REPL but not used in your projects. 36 | 37 | This allows `:inject-ns-fns` and `:inject-ns-macros` to be used to specify 38 | injections that will be loaded lazily. 39 | 40 | - Allow changes in vars to propagate 41 | When injecting a var, do not deref, it so that changes are propagated. 42 | This means the value of vars in the injected namespaces are themselves 43 | vars rather than functions. This should be transparent in most repl 44 | usage. 45 | 46 | ## 0.1.0 47 | 48 | - Initial release 49 | -------------------------------------------------------------------------------- /profiles.clj: -------------------------------------------------------------------------------- 1 | {:dev {:plugins [[lein-pallet-release "RELEASE"]]}} 2 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject com.gfredericks/lein-shorthand "0.4.2-SNAPSHOT" 2 | :description 3 | "A leiningen plugin to create clojure namespaces with short names, 4 | so you can easily call utility functions in the REPL using fully 5 | qualified symbols." 6 | :url "https://github.com/palletops/lein-shorthand" 7 | :license {:name "Eclipse Public License" 8 | :url "http://www.eclipse.org/legal/epl-v10.html"} 9 | :profiles {:provided {:dependencies [[org.clojure/clojure "1.6.0"] 10 | [leiningen "2.5.0"]]}} 11 | :deploy-repositories [["releases" :clojars]] 12 | :eval-in :leiningen) 13 | -------------------------------------------------------------------------------- /src/com/palletops/shorthand.clj: -------------------------------------------------------------------------------- 1 | (ns com.palletops.shorthand 2 | "Create clojure namespaces with short names, so you can easily call 3 | utility functions in the REPL using fully qualified symbols. 4 | 5 | Transforms a map of namespace and fully qualified symbols from the 6 | project :shorthand key into namespace definitions specified in the 7 | project :injections key.") 8 | 9 | (defn lazy-inject-var-fn 10 | "Return a function definition form for lazily injecting a var into a 11 | namespace." 12 | [] 13 | `(fn ~'var-fn [ns# sym# v-sym# meta-m#] 14 | (intern 15 | ns# (with-meta sym# (merge 16 | {:arglists '[[& not-yet-loaded]]} 17 | meta-m# 18 | ;; add meta from var if var available 19 | (meta (resolve v-sym#)) 20 | (meta sym#))) 21 | (fn [& args#] 22 | (let [v-ns# (symbol (namespace v-sym#))] 23 | (try 24 | (require v-ns#) 25 | (catch Exception e# 26 | (throw 27 | (ex-info 28 | (str "Problem loading namespace " v-ns# " for injections:") 29 | {:ns ns# :name sym# :var v-sym#} 30 | e#))))) 31 | (let [src-v# (resolve v-sym#) 32 | tgt-v# (resolve (symbol (name ns#) (name sym#)))] 33 | (when-not src-v# 34 | (throw 35 | (ex-info 36 | (str "Failed to inject " v-sym# ". Could not resolve it.") 37 | {:ns ns# :name sym# :var v-sym#}))) 38 | (alter-var-root tgt-v# (constantly src-v#)) 39 | (alter-meta! tgt-v# merge (dissoc (meta src-v#) :name :ns)) 40 | (apply @src-v# args#)))))) 41 | 42 | (defn inject-var-fn 43 | "Return a function definition that in namespace, ns, define sym, 44 | defined as var, v." 45 | [] 46 | `(fn [ns# sym# v#] 47 | (intern 48 | ns# 49 | (with-meta sym# (dissoc (meta v#) :name :ns)) 50 | v#))) 51 | 52 | (defn ns-entry 53 | "Return a function definition that, given a namespace entry as 54 | either a fully qualified symbol or a 2-vector of symbol and fully 55 | qualified symbol, return a map with :local-sym, :var-sym and :var-ns 56 | keys." 57 | [] 58 | `(fn [x#] 59 | (cond 60 | (symbol? x#) {:var-ns (symbol (namespace x#)) 61 | :local-sym (with-meta (symbol (name x#)) 62 | (meta x#)) 63 | :var-sym x#} 64 | (and (sequential? x#) (= 2 (count x#))) {:var-ns (symbol 65 | (namespace (second x#))) 66 | :local-sym (vary-meta 67 | (first x#) 68 | merge 69 | (meta (second x#))) 70 | :var-sym (second x#)} 71 | :else (throw (ex-info 72 | (str "Invalid namespace entry for injection. " 73 | "Must be a fully qualified symbol or 2-Tuple of " 74 | "unqualified symbol and fully qualified symbol.") 75 | {:value x# 76 | :exit-code 1}))))) 77 | 78 | (defn inject-nses 79 | "Build namespaces from a map of namespace symbol to injected vars. 80 | Each namespace symbol maps to a either a map of unqualified symbol 81 | to fully qualified symbol, or to a sequence of fully qualified symbols, 82 | in which case the symbols' names will be used as the symbol to inject." 83 | [ns-sym-map meta-m] 84 | (let [lazy-inject-var (gensym "lazy-inject-var") 85 | inject-var (gensym "inject-var") 86 | ns-sym (gensym "ns-sym") 87 | req-ns (gensym "req-ns")] 88 | `(let [~lazy-inject-var ~(lazy-inject-var-fn) 89 | ~'inject-var ~(inject-var-fn) 90 | ns-entry# ~(ns-entry)] 91 | (doseq [[~ns-sym syms#] (read-string ~(binding [*print-meta* true] 92 | (pr-str ns-sym-map))) 93 | :let [req-nses# (group-by :var-ns (map ns-entry# syms#))]] 94 | (create-ns ~ns-sym) 95 | (doseq [[~req-ns syms#] req-nses#] 96 | (doseq [{:keys [~'local-sym ~'var-sym]} syms#] 97 | (if (or ~meta-m (:lazy (meta ~'local-sym))) 98 | (~lazy-inject-var ~ns-sym ~'local-sym ~'var-sym ~meta-m) 99 | (do 100 | (try (require ~req-ns) 101 | (catch Exception e# 102 | (binding [*out* *err*] 103 | (println "Problem loading namespace" ~req-ns 104 | " for injections:" (.getMessage e#))))) 105 | (if-let [v# (resolve ~'var-sym)] 106 | (~'inject-var ~ns-sym ~'local-sym v#) 107 | (binding [*out* *err*] 108 | (println 109 | (str "Failed to inject " (pr-str ~'var-sym))))))))))))) 110 | 111 | 112 | (defn injections 113 | "Return a vector of leiningen injections for the specified ns symbol 114 | mappings. Specifying a metadata map, meta-m, makes the injection 115 | lazy. 116 | 117 | (injections {'. ['clojure.pprint/pprint]})" 118 | ([ns-sym-map meta-m] 119 | [(inject-nses ns-sym-map meta-m)]) 120 | ([ns-sym-map] 121 | (injections ns-sym-map nil))) 122 | 123 | 124 | ;; (defn f [& args] 125 | ;; (require 'clojure.pprint) 126 | ;; (let [v (resolve 'clojure.pprint/pprint)] 127 | ;; (alter-var-root #'f (constantly v)) 128 | ;; (alter-meta! #'f merge (dissoc (meta v) :name :ns)) 129 | ;; (apply @v args))) 130 | 131 | ;; (f 1) 132 | 133 | ;; (defn gg [x] 134 | ;; :gi) 135 | 136 | 137 | ;; (defn g [& args] 138 | ;; (let [v #'gg] 139 | ;; (alter-var-root #'g (constantly v)) 140 | ;; (alter-meta! #'g merge (dissoc (meta v) :name :ns)))) 141 | 142 | ;; (g 1) 143 | ;; (meta #'g) 144 | -------------------------------------------------------------------------------- /src/lein_shorthand/plugin.clj: -------------------------------------------------------------------------------- 1 | (ns lein-shorthand.plugin 2 | "Plugin to inject namespaces" 3 | (:require 4 | [com.palletops.shorthand :refer [injections]] 5 | [leiningen.core.main :as main] 6 | [leiningen.core.project :as project] 7 | [robert.hooke :refer [add-hook]])) 8 | 9 | (defn injection-profiles 10 | [project] 11 | {:plugin.lein-shorthand/injections 12 | {:injections (vec 13 | (concat 14 | (injections (:shorthand project)) 15 | (injections (:shorthand-fns project) {}) 16 | (injections (:shorthand-macros project) 17 | {:macro true})))}}) 18 | 19 | (defn middleware 20 | "Middleware to add a profile defining :injections." 21 | [project] 22 | (project/add-profiles project (injection-profiles project))) 23 | 24 | ;; We would like to be able to hook a function after the :repl profile 25 | ;; is merged, but that is not very feasible at the moment. 26 | 27 | ;; We also want to avoid require'ing leiningen.repl just to hook it,as 28 | ;; it can add considerably to lein startup time when not using the repl. 29 | 30 | (defn repl-hook 31 | [task & [project & args]] 32 | (apply task 33 | (project/merge-profiles project [:plugin.lein-shorthand/injections]) 34 | args)) 35 | 36 | (defn add-repl-hook [] 37 | (when-let [repl (resolve 'leiningen.repl/repl)] 38 | (add-hook repl repl-hook))) 39 | 40 | (defn resolve-task-hook 41 | [f & args] 42 | (let [r (apply f args)] 43 | (add-repl-hook) 44 | r)) 45 | 46 | 47 | (defn hooks 48 | [] 49 | (add-hook #'main/resolve-task resolve-task-hook)) 50 | -------------------------------------------------------------------------------- /test/com/palletops/shorthand_test.clj: -------------------------------------------------------------------------------- 1 | (ns com.palletops.shorthand-test 2 | (:require [com.palletops.shorthand :refer :all] 3 | [clojure.test :refer :all])) 4 | 5 | ;; Remove namespaces, so we can test the automated require of the 6 | ;; namespaces when running tests multiple times at the REPL. 7 | 8 | (defn unload-ns [ns-sym] 9 | (remove-ns ns-sym) 10 | (dosync (commute @#'clojure.core/*loaded-libs* disj ns-sym)) 11 | nil) 12 | 13 | (unload-ns 'test-ns) 14 | (unload-ns 'test-ns2) 15 | (unload-ns 'test-ns3) 16 | (unload-ns 'es) 17 | (unload-ns 'ls) 18 | (unload-ns 'inject-map) 19 | (unload-ns 'inject-mixed) 20 | 21 | (defmacro eager-inject-seq 22 | [] 23 | `(do 24 | ~@(injections {'es ['test-ns/f 25 | 'test-ns/m]}))) 26 | 27 | (defmacro lazy-inject-seq 28 | [] 29 | `(do 30 | ~@(injections {'ls ['test-ns2/f2]} {}) 31 | ~@(injections {'ls ['test-ns2/m2]} {:macro true}))) 32 | 33 | (eager-inject-seq) 34 | (lazy-inject-seq) 35 | 36 | (defmacro inject-map 37 | [] 38 | `(do 39 | ~@(injections {'inject-map {'f1 'test-ns/f 40 | 'm 'test-ns2/m2}}))) 41 | 42 | (inject-map) 43 | 44 | (defmacro inject-mixed 45 | [] 46 | `(do 47 | ~@(injections {'inject-mixed [['f 'test-ns3/f3] 48 | 'test-ns2/f2 49 | 'test-ns3/m3]}))) 50 | 51 | (inject-mixed) 52 | 53 | (deftest injections-test 54 | (testing "spec as symbol sequence" 55 | (testing "eager injection" 56 | (is (= :f (es/f))) 57 | (is (= '(:m) (es/m)))) 58 | (testing "lazy injection" 59 | (is (= :f2 (ls/f2)) "first invocation") 60 | (is (= :f2 (ls/f2))) 61 | (is (= '(:m2) (ls/m2)) "first invocation") 62 | (is (= '(:m2) (ls/m2))))) 63 | (testing "spec as map" 64 | (is (= :f (inject-map/f1))) 65 | (is (= '(:m2) (inject-map/m)))) 66 | (testing "inject-mixed spec" 67 | (is (= :f3 (inject-mixed/f))) 68 | (is (= :f2 (inject-mixed/f2))) 69 | (is (= '(:m3) (inject-mixed/m3))))) 70 | 71 | 72 | (defmacro inject-meta-test 73 | [s] 74 | `(do 75 | ~@(injections (read-string s)))) 76 | 77 | (unload-ns 'test-ns4) 78 | (unload-ns 'im) 79 | (inject-meta-test 80 | "{im [^:lazy test-ns4/f4 81 | [^:lazy ^:macro m4 test-ns4/m4] 82 | [m44 ^:lazy ^:macro test-ns4/m4]]}") 83 | 84 | (deftest inject-ns-test 85 | (is (= :f4 (im/f4))) 86 | (is (= '(:m4) (im/m4))) 87 | (is (= '(:m4) (im/m44)))) 88 | -------------------------------------------------------------------------------- /test/test_ns.clj: -------------------------------------------------------------------------------- 1 | (ns test-ns) 2 | 3 | (defn f [] 4 | :f) 5 | 6 | (defmacro m [] 7 | `(list :m)) 8 | -------------------------------------------------------------------------------- /test/test_ns2.clj: -------------------------------------------------------------------------------- 1 | (ns test-ns2) 2 | 3 | (defn f2 [] 4 | :f2) 5 | 6 | (defmacro m2 [] 7 | `(list :m2)) 8 | -------------------------------------------------------------------------------- /test/test_ns3.clj: -------------------------------------------------------------------------------- 1 | (ns test-ns3) 2 | 3 | (defn f3 [] 4 | :f3) 5 | 6 | (defmacro m3 [] 7 | `(list :m3)) 8 | -------------------------------------------------------------------------------- /test/test_ns4.clj: -------------------------------------------------------------------------------- 1 | (ns test-ns4) 2 | 3 | (defn f4 [] 4 | :f4) 5 | 6 | (defmacro m4 [] 7 | `(list :m4)) 8 | --------------------------------------------------------------------------------