Skip to content

Commit

Permalink
Fix #41: adds maven for dep search (#194)
Browse files Browse the repository at this point in the history
Suggested-by: Michiel Borkent <[email protected]>
  • Loading branch information
agzam authored Nov 12, 2023
1 parent ff7aca4 commit 03cc51e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 27 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/bb-run-tests-emacs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,21 @@ jobs:
- name: Install emacs
uses: purcell/setup-emacs@master
with:
version: 28.1
version: 29.1

- name: install buttercup
run: |-
set -e
script=$(cat <<- 'EOF'
(progn
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
(package-initialize)
(unless (package-installed-p 'buttercup)
(package-refresh-contents)
(package-install 'buttercup)))
EOF
)
emacs --batch --eval "$script"
- run: bb run test:emacs
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ See the [New Clojure project quickstart](https://blog.michielborkent.nl/new-cloj

## Unreleased

- [#194](https://github.com/babashka/neil/issues/41): `dep search` in addition to Clojars, now also searches on Maven

- [#92](https://github.com/babashka/neil/issues/180): `neil dep upgrade`: delete rogue "`" character from helptext ([@teodorlu](https://github.com/teodorlu))

## 0.2.62
Expand Down
16 changes: 10 additions & 6 deletions neil-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
((eq shell-cmd-calls 1)
(expect command :to-equal "/bin/neil dep search test-pkg")
(concat
":lib foo/test-pkg :version 1.0.0 :description \"good lib\"\n"
":lib bar/awesome-test-pkg :version 2.1.0 :description \"better lib\"\n"))
":lib foo/test-pkg :version \"1.0.0\" :description \"good lib\"\n"
":lib bar/awesome-test-pkg :version \"2.1.0\" :description \"better lib\"\n"))

((eq shell-cmd-calls 2)
(expect command :to-equal "/bin/neil dep versions foo/test-pkg")
(concat
":lib foo/test-pkg :version 1.0.0\n"
":lib bar/awesome-test-pkg :version 2.1.0\n")))))
":lib foo/test-pkg :version \"1.0.0\"\n"
":lib bar/awesome-test-pkg :version \"2.1.0\"\n")))))
(spy-on #'neil-search-annotation-fn)
(spy-on #'completing-read
:and-call-fake
Expand All @@ -64,8 +64,12 @@
((eq prompt-calls 1)
(expect prompt :to-equal "Found 2 matches for 'test-pkg':")
(expect coll :to-equal
'(("foo/test-pkg" (version . "1.0.0") (description . "\"good lib\""))
("bar/awesome-test-pkg" (version . "2.1.0") (description . "\"better lib\""))))
'(("foo/test-pkg"
(version . "\"1.0.0\"")
(description . "\"good lib\""))
("bar/awesome-test-pkg"
(version . "\"2.1.0\"")
(description . "\"better lib\""))))
"foo/test-pkg")

((eq prompt-calls 2)
Expand Down
9 changes: 6 additions & 3 deletions neil.el
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ the dependency to the project (deps.edn only)."
(when (and lib-name version)
(if (or (null build-tool)
(eq build-tool 'clojure-cli))
(format "%s {:mvn/version %S}" lib-name version)
(format "%s {:mvn/version %S}"
lib-name version)
(format "[%s %S]" lib-name version))))))

(perform-action
(lambda (exe args)
(let* ((res (shell-command-to-string
(format "%s %s" exe args)))
(res (if (or (string-match-p "Unable to find\\|Error" res))
(res (if (or (string-match-p "Error\\|Usage: neil dep search" res)
(and (string-match-p "Unable to find.*Maven" res)
(string-match-p "Unable to find.*Clojars" res)))
(user-error res)
(seq-filter
(lambda (x) (string-match-p ":lib" x))
Expand Down Expand Up @@ -122,7 +125,7 @@ the dependency to the project (deps.edn only)."
(completing-read
(format "Choose version of %s:" lib-name)
(funcall keep-order (seq-map (lambda (x) (alist-get 'version x)) versions)))))
(alist-get 'version (cdr (assoc lib-name res)))))
(read (or (alist-get 'version (cdr (assoc lib-name res))) "nil"))))
(dep-str (funcall format-dep-str lib-name version)))

(when (and neil-inject-dep-to-project-p
Expand Down
67 changes: 51 additions & 16 deletions src/babashka/neil.clj
Original file line number Diff line number Diff line change
Expand Up @@ -500,22 +500,58 @@ will return libraries with 'test framework' in their description.
See http://github.com/clojars/clojars-web/wiki/Search-Query-Syntax for
details on the search syntax.")))

(defn dep-search-maven [search-term]
(let [url (format
"https://search.maven.org/solrsearch/select?q=%s&rows=20&wt=json"
(url-encode search-term))
keys-m {:g :group_name
:a :jar_name
:timestamp :created
:latestVersion :version}
add-desc (fn [{:keys [group_name jar_name] :as m}]
;; Maven doesn't provide package description through its API,
;; but that doesn't mean we should just leave it blank
(assoc
m :description
(format "%s/%s on Maven" group_name jar_name)))
res (->> url curl-get-json :response :docs
(map #(some->
%
(clojure.set/rename-keys keys-m)
(select-keys (vals keys-m))
add-desc)))]
(if (empty? res)
(binding [*out* *err*]
(println "Unable to find" search-term "on Maven."))
res)))

(defn dep-search-clojars [search-term]
(let [url (format
"https://clojars.org/search?format=json&q=%s"
(url-encode search-term))
{search-results :results
results-count :count} (curl-get-json url)]
(if (zero? results-count)
(binding [*out* *err*]
(println "Unable to find" search-term "on Clojars."))
search-results)))

(defn dep-search [{:keys [opts]}]
(if (or (:help opts) (not (:search-term opts)))
(print-dep-search-help)
(let [search-term (:search-term opts)
url (str "https://clojars.org/search?format=json&q=" (url-encode search-term))
{search-results :results
results-count :count} (curl-get-json url)]
(when (zero? results-count)
(binding [*out* *err*]
(println "Unable to find" search-term "on Clojars.")
(System/exit 1)))
(doseq [search-result search-results]
(prn :lib (symbol (:group_name search-result)
(:jar_name search-result))
:version (:version search-result)
:description (:description search-result))))))
(let [{:keys [search-term]} opts]
(if (or (:help opts)
(not (string? search-term))
(str/blank? search-term))
(print-dep-search-help)
(let [search-results (->> [dep-search-maven
dep-search-clojars]
(map #(% search-term))
(apply concat))]
(when (empty? search-results) (System/exit 1))
(doseq [search-result search-results]
(prn :lib (symbol (:group_name search-result)
(:jar_name search-result))
:version (:version search-result)
:description (:description search-result)))))))

(defn git-url->lib [git-url]
(when git-url
Expand Down Expand Up @@ -703,7 +739,6 @@ Examples:

(doseq [dep-upgrade upgrades] (do-dep-upgrade opts dep-upgrade))))


(defn print-help [_]
(println (str/trim "
Usage: neil <subcommand> <options>
Expand Down
16 changes: 15 additions & 1 deletion tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,21 @@
#_(is (any? (run-dep-subcommand "search" "org.clojure/tools.cli")))
(is (any? (run-dep-subcommand "search" "babashka nrepl")))
(is (thrown-with-msg? Exception #"Unable to find"
(run-dep-subcommand "search" "%22searchTermThatIsn'tFound"))))
(run-dep-subcommand "search" "%22searchTermThatIsn'tFound")))
(is (some #(str/starts-with? % "Usage: neil dep search")
(run-dep-subcommand "search" "42"))
"passing non-string shows help")
(is (some #(str/starts-with? % "Usage: neil dep search")
(run-dep-subcommand "search" " "))
"passing blank string shows help")
(is (some
(partial
re-matches
(re-pattern
(str ":lib org.clojure/clojurescript "
":version \"\\d+(\\.\\d+)+\" "
":description \"org.clojure/clojurescript on Maven\"")))
(run-dep-subcommand "search" "\"org.clojure/clojurescript\""))))

(defn run-license [filename subcommand & [args]]
(let [args (or args "")]
Expand Down

0 comments on commit 03cc51e

Please sign in to comment.