Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrodCTaylor committed Sep 6, 2024
0 parents commit ad9b22c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.cpcache/*
*.iml
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# lein-add-libs

Use the [add-lib](https://clojure.github.io/clojure/branch-master/clojure.repl-api.html#clojure.repl.deps/add-lib) function in your repl from within a lein project.

# Installation as a Tool

Make sure you have the latest Clojure CLI version installed.

```sh
clj -Ttools install-latest :lib io.github.nubank/lein-add-libs :as lein-add-libs
```

# Invoking the tool from a REPL

```clojure
(clojure.tools.deps.interop/invoke-tool {:tool-name "lein-add-libs" :fn 'lein-add-libs.core/bootstrap})
```

Now that the bootstrap function has been successfully run. You can proceed to use `add-lib` function as you would in a deps.edn base project.

For example:
```clojure
(add-lib 'org.clojure/data.json)
(require '[clojure.data.json :as json])
(json/read-str "{\"a\":1,\"b\":2}" :key-fn keyword)
```
8 changes: 8 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{:paths ["src"]

:deps {org.clojure/clojure {:mvn/version "1.12.0"}
leiningen-core/leiningen-core {:mvn/version "2.9.1"}}

:tools/usage {:ns-default lein-add-libs.core}

:aliases {}}
20 changes: 20 additions & 0 deletions src/lein_add_libs/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns lein-add-libs.core
(:require
[clojure.string :as str]
[clojure.java.basis.impl :refer [update-basis!]]
[leiningen.core.project :as project]
[leiningen.core.classpath :refer [get-classpath]]))

(defn update-lein-basis [current-basis derived-basis]
(if current-basis current-basis derived-basis))

(defn bootstrap [& args]
(let [cp (get-classpath (project/read))
deps (filter #(str/includes? % "/.m2/") cp)
dep-parts (map (fn [dep] (butlast (drop 2 (drop-while #(not= ".m2" %) (str/split dep #"/"))))) deps)
libs (reduce (fn [res dep]
(let [formatted-dep (symbol (str (str/join "." (drop-last 2 dep)) "/" (last (drop-last dep))))]
(assoc res formatted-dep {:mvn/version (last dep) :deps/manifest :mvn}))) {} dep-parts)
repos (reduce (fn [res [repo url]] (assoc res repo url)) {} (:repositories (project/read)))
bootstrapped-basis {:libs libs :mvn/repos repos}]
(update-basis! update-lein-basis bootstrapped-basis)))

0 comments on commit ad9b22c

Please sign in to comment.