Skip to content

Commit

Permalink
Show available libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
eval committed May 2, 2024
1 parent ece9baf commit 3ce84fe
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 78 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Changelog

## v0.12.0 (2024-04-29)
## v0.12.0 (2024-05-02)

- Clojure 1.12.0-alpha10
- Clojure 1.12.0-alpha11
- (vendored) compliment: suggest Class/.method
- show available libraries and versions on start (also :deps/ls)
- prevent dep-clashes by vendoring&renaming some deps
- fix completions for current ns

## v0.11.0 (2024-03-02)
Expand Down
87 changes: 24 additions & 63 deletions src/eval/deps_try.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@

(def init-cp (get-classpath))

(defn- run-clojure [opts & args]
(defn- run-repl [opts & args]
#_(prn ::run-clojure :opts opts :args args)
(let [[opts args] (if (map? opts) [opts args] [nil (cons opts args)])]
(fs/with-temp-dir [tmp {}]
(apply p/sh (merge {:dir (str tmp)} opts)
(apply p/exec (merge {:dir (str tmp)} opts)
"clojure" args))))

(defn- deps->cp [deps]
#_(prn ::deps->cp :deps deps)
(when (seq deps)
(string/trim (:out (run-clojure "-Spath" "-Sdeps" (str {:paths [] :deps deps}))))))

(def ^:private dev? (nil? (io/resource "VERSION")))

(def ^:private version
Expand Down Expand Up @@ -71,23 +66,12 @@
nil]]
(print (string/join \newline (interpose nil usage)))))

(defn- print-version []
(defn- full-version []
(let [bin (if dev? "deps-try-dev" "deps-try")]
(println (str bin " " version))))

(def ^:private clojure-cli-version-re #"^(\d+)\.(\d+)\.(\d+)\.(\d+)")
(str bin " " version)))

(defn- parse-clojure-cli-version [s]
(map parse-long (rest (re-find clojure-cli-version-re s))))

(defn- at-least-version? [version-or-above version]
(let [[major1 minor1 patch1 build1] (parse-clojure-cli-version version-or-above)
[major2 minor2 patch2 build2] (parse-clojure-cli-version version)]
(or (< major1 major2)
(and (= major1 major2) (< minor1 minor2))
(and (= major1 major2) (= minor1 minor2) (< patch1 patch2))
(and (= major1 major2) (= minor1 minor2) (= patch1 patch2) (or (= build1 build2)
(< build1 build2))))))
(defn- print-version []
(println (full-version)))

(defn- print-message [msg {:keys [msg-type]}]
(let [no-color? (or (System/getenv "NO_COLOR") (= "dumb" (System/getenv "TERM")))
Expand All @@ -105,56 +89,32 @@
(defn- print-error [m]
(print-message m {:msg-type :error}))

(defn- warn-unless-minimum-clojure-cli-version [minimum version]
(when-not (at-least-version? minimum version)
(print-warning (str "Adding (additional) libraries to this REPL-session via ':deps/try some/lib' won't work as it requires Clojure CLI version >= " minimum " (current: " version ")."))))

(defn- print-error-and-exit! [m]
(print-error m)
(System/exit 1))

(defn- tdeps-verbose->map [s]
(let [[cp & pairs] (reverse (string/split-lines s))
keywordize (comp keyword #(string/replace % \_ \-) name)] ;; `name` makes it also usable for keywords
(update-keys
(into {:cp cp}
(map #(string/split % #" += +") (filter seq pairs))) keywordize)))


(defn- start-repl! [{requested-deps :deps
prepare :prepare
{recipe-deps :deps
ns-only :ns-only
recipe-location :location} :recipe :as _args}]
#_(prn ::args _args)
(let [default-deps {'org.clojure/clojure {:mvn/version "1.12.0-alpha11"}}
{:keys [cp-file]
default-cp :cp
tdeps-version :version
:as _tdeps-paths} (-> (run-clojure "-Sverbose" "-Spath"
"-Sdeps" (str {:paths [] :deps default-deps}))
:out
string/trim
tdeps-verbose->map)

basis-file (string/replace cp-file #".cp$" ".basis")
requested-cp (deps->cp requested-deps)
recipe-cp (deps->cp recipe-deps)
classpath (cond-> (str (fs/cwd) fs/path-separator
default-cp fs/path-separator
init-cp fs/path-separator
requested-cp)
recipe-cp (str fs/path-separator recipe-cp))
jvm-opts [(str "-Dclojure.basis=" basis-file)]]
(warn-unless-minimum-clojure-cli-version "1.11.1.1273" tdeps-version)
(let [cmd (cond-> ["java" "-classpath" classpath]
(seq jvm-opts) (into jvm-opts)
:always (into ["clojure.main" "-m" "eval.deps-try.try"])
prepare (into ["-P"])
recipe-location (into (if ns-only
["--recipe-ns" recipe-location]
["--recipe" recipe-location])))]
(apply p/exec cmd))))
;; TODO re-enable?
;; see https://github.com/clojure/brew-install/blob/1.11.3/CHANGELOG.md
#_(warn-unless-minimum-clojure-cli-version "1.11.1.1273" tdeps-version)
(let [paths (into ["."] ;; needed for clojure.java.io/resource
(string/split init-cp #":"))
deps (merge
{'org.clojure/clojure {:mvn/version "1.12.0-alpha11"}}
recipe-deps
requested-deps)
main-args (cond-> ["--version" version]
recipe-location (into (if ns-only
["--recipe-ns" recipe-location]
["--recipe" recipe-location]))
prepare (conj "-P"))]
(apply run-repl "-Sdeps" (str {:paths paths
:deps deps})
"-M" "-m" "eval.deps-try.try" main-args)))

(defn- recipe-manifest-contents [{:keys [refresh] :as _cli-opts}]
(let [remote-manifest-file "https://raw.githubusercontent.com/eval/deps-try/master/recipes/manifest.edn"
Expand Down Expand Up @@ -285,4 +245,5 @@
(catch Exception e
(ex-data e)))


#_:end)
3 changes: 3 additions & 0 deletions src/eval/deps_try/ansi_escape.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@
(def ^:const bg-white-b,, "Bright White BG color.",, "\033[107m")

;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

(defn wrap [escape s]
(str escape s reset))
30 changes: 25 additions & 5 deletions src/eval/deps_try/deps.clj → src/eval/deps_try/deps.cljc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(ns eval.deps-try.deps
{:clj-kondo/config '{:lint-as {eval.deps-try.util/pred-> clojure.core/->}}}
(:require [clojure.string :as str]
[clojure.tools.gitlibs :as gitlib]
[eval.deps-try.fs :as fs]
[eval.deps-try.process :refer [process]]
[eval.deps-try.util :as util]))
(:require
#?@(:bb [] :clj [clojure.java.basis])
[clojure.string :as str]
[clojure.tools.gitlibs :as gitlib]
[eval.deps-try.ansi-escape :as ansi]
[eval.deps-try.fs :as fs]
[eval.deps-try.process :refer [process]]
[eval.deps-try.util :as util]))

(def ^:private git-services
[[:github {:dep-url-re #"^(?:io|com)\.github\.([^/]+)\/(.+)"
Expand Down Expand Up @@ -362,6 +365,23 @@
#_(-> (doto prn))
(resolve-deps)))

#?(:bb nil
:clj
(defn deps-available []
(->> (clojure.java.basis/current-basis)
:libs
(filter (comp #(every? empty? %) :parents val)))))

#?(:bb nil
:clj
(defn fmt-deps-available []
(let [fmt-dep (fn [[dname {sha :git/sha version :mvn/version}]]
(str " ⚡️ " (ansi/wrap ansi/bold dname)
" " (ansi/wrap ansi/fg-cyan
(or (some-> sha (subs 0 7))
version))))]
(str/join \newline (map fmt-dep (deps-available))))))

(comment
(set! clojure.core/*print-namespace-maps* false)

Expand Down
2 changes: 1 addition & 1 deletion src/eval/deps_try/rr_service.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
(mapcat :paths))]
(into java-cp-sans-cwd basis-cp)))

(defmethod clj-reader/-complete ::service [self word options]
(defmethod clj-reader/-complete ::service [_self word options]
(let [options (merge {:ns *ns*} options)
options (if (:extra-metadata options)
options
Expand Down
36 changes: 29 additions & 7 deletions src/eval/deps_try/try.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[clojure.pprint :as pp]
[clojure.repl :as clj-repl]
[deps-try.cli :as cli]
[eval.deps-try.ansi-escape :as ansi]
[eval.deps-try.deps :as try-deps]
[eval.deps-try.fs :as fs]
[eval.deps-try.history :as history]
Expand All @@ -28,18 +29,25 @@
(defn- warm-up-completion-cache! []
(clj-line-reader/-complete {:rebel-readline.service/type ::rebel-service/service} "nil" {}))


(defmethod rebel-readline/command-doc :deps/ls [_]
(str "Show available deps"))

(defmethod rebel-readline/command :deps/ls [_]
(println (try-deps/fmt-deps-available)))

(defmethod rebel-readline/command-doc :deps/try [_]
(str "Add dependencies (e.g. `:deps/try metosin/malli`)"))


(defmethod rebel-readline/command :deps/try [[_ & args]]
(if (seq args)
(let [{:keys [deps error]} (try-deps/parse-dep-args {:deps (map str args)})]
(if-not error
(do ((requiring-resolve 'clojure.repl.deps/add-libs) deps)
(warm-up-completion-cache!))
(rebel-tools/display-error error)))
(rebel-tools/display-warning "Usage: :deps/try metosin/malli \"0.9.2\" https://github.com/user/project some-ref \"~/some/project\"")))
(rebel-tools/display-warning
"Usage: :deps/try metosin/malli \"0.9.2\" https://github.com/user/project some-ref \"~/some/project\"")))


(defmethod rebel-readline/command-doc :recipe/help [_]
Expand Down Expand Up @@ -114,7 +122,18 @@
;; line-reader
;; service

(defn repl [{:deps-try/keys [data-path recipe] :as opts}]
(defn- repl-help-message [{:keys [version]}]
(str (ansi/wrap ansi/bold "🩴 Version: ") version \newline
(ansi/wrap ansi/bold "🏡 Home: ") "https://github.com/eval/deps-try" \newline
(ansi/wrap ansi/bold "🐻‍❄️ Perks / sponsor: ") "https://polar.sh/eval/deps-try" \newline
(ansi/wrap ansi/bold "🆘 Help: ")
"Type " (ansi/wrap ansi/fg-cyan-b ":repl/help") \newline

\newline
(ansi/wrap ansi/bold "🧺 Available libraries: ") \newline
(try-deps/fmt-deps-available)))

(defn repl [{:deps-try/keys [data-path recipe version] :as opts}]
(rebel-core/with-line-reader
(let [history-file (doto (fs/path data-path "history")
(ensure-file-exists!))]
Expand All @@ -135,7 +154,7 @@
(when recipe
(swap! api/*line-reader* assoc :deps-try/recipe recipe)
(rebel-tools/display-warning (recipe-instructions recipe)))
(println (rebel-core/help-message))
(println (repl-help-message {:version version}))
(apply
clojure.main/repl
(-> {:print rebel-main/syntax-highlight-prn
Expand All @@ -157,8 +176,10 @@
`(swap! api/*line-reader* dissoc :repl/just-caught))

(defn -main [& args]
(let [opts (cli/parse-opts args {:restrict [:recipe :recipe-ns :prepare]
:spec {:recipe {}
#_(prn ::args args)
(let [opts (cli/parse-opts args {:restrict [:recipe :recipe-ns :prepare :version]
:spec {:version {}
:recipe {}
:prepare {:alias :P}}})
data-path (fs/xdg-data-home "deps-try")]
(ensure-path-exists! data-path)
Expand All @@ -167,7 +188,8 @@
(binding [*debug-log* false] ;; via --debug flag?
(rebel-core/ensure-terminal
(let [recipe-path ((some-fn :recipe :recipe-ns) opts)
repl-opts (cond-> {:deps-try/data-path data-path
repl-opts (cond-> {:deps-try/version (:version opts)
:deps-try/data-path data-path
:caught (fn [ex]
(persist-just-caught ex)
(clojure.main/repl-caught ex))
Expand Down

0 comments on commit 3ce84fe

Please sign in to comment.