Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle ClojureDocs export.edn #96

Merged
merged 7 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

* [#96](https://github.com/clojure-emacs/orchard/pull/96): Update to bundle ClojureDocs documents.

## 0.5.11 (2020-07-10)

### Bugs Fixed
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ VERSION ?= 1.10

TEST_PROFILES := +test

test-resources/clojuredocs/export.edn:
curl -o $@ https://clojuredocs-edn.netlify.com/export.compact.edn
resources/clojuredocs/export.edn:
curl -o $@ https://clojuredocs-edn.netlify.app/export.compact.min.edn

test: test-resources/clojuredocs/export.edn
test:
lein with-profile +$(VERSION),$(TEST_PROFILES) test

test-watch: test-resources/clojuredocs/export.edn
Expand Down
1 change: 1 addition & 0 deletions resources/clojuredocs/export.edn

Large diffs are not rendered by default.

88 changes: 32 additions & 56 deletions src/orchard/clojuredocs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
(:import
(java.io IOException)
(java.net URL)
(java.time Instant)
(javax.net.ssl HttpsURLConnection)))

(def cache (atom {}))
Expand All @@ -21,9 +20,6 @@
"orchard"
"clojuredocs"
"export.edn"]))
(def cache-updating-threshold
"One week. Unit is millisecond."
604800000)

(def connect-timeout
"Timeout value for checking connection. Unit is millisecond."
Expand Down Expand Up @@ -57,49 +53,37 @@
(finally
(.disconnect conn))))))

(defn load-cache!
(defn load-docs-if-not-loaded!
"Load exported docs from bundled or cached file when no docs are loaded.
The Cached file take priority."
{:added "0.5"}
[]
(when (empty? @cache)
(let [cache-file (io/file cache-file-name)]
(load-cache-file!
(if (.exists cache-file)
cache-file
(io/resource "clojuredocs/export.edn"))))))

(defn update-cache!
"Load exported docs file from ClojureDocs, and store it as a cache.
A EDN format file is expected to the `export-edn-url` argument.

If `export-edn-url` is omitted, `default-edn-file-url` is used.

The loaded EDN file will be cached in `cache-file-name`.
If the cached file is older than `cache-updating-threshold`,
the cached file will be updated automatically.

If cached file is not existing and `export-edn-url` is not accessible, `IOException` is thrown.
If export-edn-url is not a URL for remote host, `IllegalArgumentException` is thrown."
If `export-edn-url` is not accessible, `IOException` is thrown.
If `export-edn-url` is not a URL for remote host, `IllegalArgumentException` is thrown."
{:added "0.5"}
([]
(load-cache! default-edn-file-url))
(update-cache! default-edn-file-url))
([export-edn-url]
(let [cache-file (io/file cache-file-name)
now-milli (-> (Instant/now) .getEpochSecond (* 1000))
exists? (.exists cache-file)
valid? (when exists?
(< (- now-milli (.lastModified cache-file))
cache-updating-threshold))
[downloadable? conn-ex] (when (or (not exists?) (not valid?))
(test-remote-url export-edn-url))]

;; exists? | N | N | Y | Y | Y | Y |
;; valid? | - | - | N | Y | N | Y |
;; downloadable? | N | Y | N | N | Y | Y |
;; --------------+---+---+---+---+---+---+
;; error | x | | | | | |
;; download | | x | | | x | |
;; use existing | | | x | x | | x |
(cond
(and (not exists?) (not downloadable?))
;; connection check not to wait too long
[downloadable? conn-ex] (test-remote-url export-edn-url)]
(if (not downloadable?)
(throw conn-ex)

(or (and (not exists?) downloadable?)
(and (not valid?) downloadable?))
(do (write-cache-file! export-edn-url)
(load-cache-file! cache-file))

(empty? @cache)
(load-cache-file! cache-file)))))
(load-cache-file! cache-file))))))

(defn clean-cache!
"Clean the cached ClojureDocs export file and the in memory cache."
Expand All @@ -110,26 +94,20 @@

(defn get-doc
"Get data for `var-name`.
If `export-edn-url` is omitted, `default-edn-file-url` is used."
Bundled documentation will be used when there is no cached documentation."
{:added "0.5"}
([var-name]
(get-doc var-name default-edn-file-url))
([var-name export-edn-url]
(load-cache! export-edn-url)
(get @cache (keyword var-name))))
[var-name]
(load-docs-if-not-loaded!)
(get @cache (keyword var-name)))

(defn find-doc
"Find documentation matching `ns` and `sym` from the cached documentation.
Cache will be updated when there is no cached documentation or when the cached documentation is old.

If `export-edn-url` is omitted, `default-edn-file-url` is used.
Bundled documentation will be used when there is no cached documentation.

Return nil if there is no matching documentation."
{:added "0.5"}
([ns sym]
(find-doc ns sym default-edn-file-url))
([ns sym export-edn-url]
(get-doc (keyword ns sym) export-edn-url)))
[ns sym]
(get-doc (keyword ns sym)))

(defn- var-name
"Convert `v`'s name to a string we can use with `get-doc`."
Expand All @@ -146,12 +124,10 @@
"Resolve `sym` in the context of `ns` and look up the documentation
for the resulting var."
{:added "0.5"}
([ns sym]
(resolve-and-find-doc ns sym default-edn-file-url))
([ns sym export-edn-url]
(if (special-symbol? sym)
(find-doc "clojure.core" (str sym) export-edn-url)
(some-> (try-ns-resolve ns sym) var-name (get-doc export-edn-url)))))
[ns sym]
(if (special-symbol? sym)
(find-doc "clojure.core" (str sym))
(some-> (try-ns-resolve ns sym) var-name get-doc)))

(defn- kw-to-sym [kw]
(symbol (subs (str kw) 1)))
Expand All @@ -160,5 +136,5 @@
"Get the see-alsos for `var-name` if any."
{:added "0.5"}
[var-name]
(if-let [see-alsos (:see-alsos (get-doc var-name))]
(when-let [see-alsos (:see-alsos (get-doc var-name))]
(map kw-to-sym see-alsos)))
Loading