Skip to content

Instantly share code, notes, and snippets.

@simon-brooke
Last active June 9, 2024 14:23
Show Gist options
  • Save simon-brooke/b7bc96e3ca92def994ff0b5b8f2bd4da to your computer and use it in GitHub Desktop.
Save simon-brooke/b7bc96e3ca92def994ff0b5b8f2bd4da to your computer and use it in GitHub Desktop.
Get the latest stable version of a Clojure (or Java) package
(ns latest-version
"Find the most current stable version identifier of a Clojure (and Java) package."
;; This builds on Yannick Scherer's [ancient-clj](https://github.com/xsc/ancient-clj),
;; which in turn builds on Chas Emerick's
;; [pomegranate](https://github.com/clj-commons/pomegranate/). It goes without saying
;; that I could not have done this without them.
;; Usage: (latest-stable-version 'org.jmonkeyengine/jme3-core)
(:require [ancient-clj.core :refer [default-loader wrap-ignore wrap-sort]]))
(def ^:dynamic sorted-no-snapshot-loader
"A package description loader which ignores snapshots and returns a list
with the highest-numbered version first. The `default-loader` is a loader
which knows only the well-known public package repositories, Maven Central
and Clojars. If you want to search other repos, you'll need to rebind."
(wrap-sort
(wrap-ignore (default-loader) [:snapshot])
:desc))
(defn stable-version?
"Does this `pkg-descriptor`, expected to be a package descriptor map as
returned by an `ancient-clj.core` loader, describe a stable version?
A stable version of a package is one which is either unqualified, or else
has `stable` as an explicit qualification."
[pkg-descriptor]
(let [quals (:qualifiers (:version-clj.core/version pkg-descriptor))]
(or (empty? quals) (quals "stable"))))
(defn latest-stable-version
"Return the version string of the latest stable version of the package with
this `pkg-id` available in the well-known repositories."
[pkg-id]
(:ancient-clj.artifact/version
(first (filter stable-version? (sorted-no-snapshot-loader pkg-id)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment