Skip to content

Commit 9fdf5c7

Browse files
authored
Merge pull request #48 from Velnbur/fix/rustic-doc-versions-check-fork
Implement semver comparison for `rustic-doc` dependencies
2 parents 5a2a79d + 59353d1 commit 9fdf5c7

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

rustic-doc.el

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,45 @@
3030
(require 'xdg)
3131
(fset 'rustic-doc--xdg-data-home 'xdg-data-home)))
3232

33+
(defun rustic-doc--make-semver (&rest version-kv)
34+
"Return a new semver-like version from VERSION-KV.
35+
VERSION-KV is a plist which must have :major key, and optionaly
36+
:minor and :patch. Default values for :minor and :patch are 0.
37+
Returned value is a list of the form (major minor patch)."
38+
(let ((major (plist-get version-kv :major))
39+
(minor (or (plist-get version-kv :minor) 0))
40+
(patch (or (plist-get version-kv :patch) 0)))
41+
(list major minor patch)))
42+
43+
(defun rustic-doc--semver-from-string (version-str)
44+
"Return a semver-like version from VERSION-STR."
45+
(let* ((splitted-str (split-string version-str "\\."))
46+
(str-length (length splitted-str)))
47+
(mapcar #'string-to-number
48+
;; take only first 3 elements or less
49+
(seq-subseq splitted-str 0 (min 3 str-length)))))
50+
51+
(defun rustic-doc--semver-greater (v1 v2)
52+
"Return greater for two semver-like versions V1 and V2."
53+
(defun -semver-major (v) (nth 0 v))
54+
(defun -semver-minor (v) (nth 1 v))
55+
(defun -semver-patch (v) (nth 2 v))
56+
57+
(cond
58+
((> (-semver-major v1) (-semver-major v2)) t)
59+
((< (-semver-major v1) (-semver-major v2)) nil)
60+
((> (-semver-minor v1) (-semver-minor v2)) t)
61+
((< (-semver-minor v1) (-semver-minor v2)) nil)
62+
((> (-semver-patch v1) (-semver-patch v2)) t)
63+
((< (-semver-patch v1) (-semver-patch v2)) nil)
64+
(t nil)))
65+
66+
(defconst rustic-doc-pandoc-min-version
67+
(rustic-doc--make-semver :major 2 :minor 1))
68+
69+
(defconst rustic-doc-fd-find-min-version
70+
(rustic-doc--make-semver :major 2))
71+
3372
(defvar rustic-doc-lua-filter (concat (file-name-as-directory (getenv "HOME"))
3473
".local/bin/rustic-doc-filter.lua")
3574
"Save location for the rustic-doc lua filter.")
@@ -274,16 +313,28 @@ See buffer *cargo-makedocs* for more info")
274313
(rustic-doc--project-doc-dest)))))
275314
(message "Activate rustic-doc-mode to run `rustic-doc-convert-current-package")))
276315

316+
(defun rustic-doc--extract-version (str)
317+
"Extract semver-like version from `STR' and conver it to list.
318+
319+
Both `fd-find' and `pandoc' output their versions in the form:
320+
`<program> <semver>\n', that's why this function parses the
321+
version from first whitespace to the end of the line."
322+
(let ((start (string-match-p " " str))
323+
(end (string-match-p "\n" str)))
324+
(rustic-doc--semver-from-string (substring str start end))))
325+
277326
(defun rustic-doc--confirm-dep-versions (missing-fd)
278327
"Verify that dependencies are not too old.
279-
Do not check `fd' when MISSING-FD is non-nil."
328+
Do not check `fd' when MISSING-FD is non-nil."
280329
(when (not missing-fd)
281-
(when (> 8 (string-to-number
282-
(substring (shell-command-to-string "fd --version") 3 4)))
330+
(when (rustic-doc--semver-greater
331+
rustic-doc-fd-find-min-version
332+
(rustic-doc--extract-version (shell-command-to-string "fd --version")))
283333
(message "Your version of fd is too old, please install a recent version, maybe through cargo.")))
284334

285-
(when (>= 11 (string-to-number
286-
(substring (shell-command-to-string "pandoc --version") 9 11)))
335+
(when (rustic-doc--semver-greater
336+
rustic-doc-pandoc-min-version
337+
(rustic-doc--extract-version (shell-command-to-string "pandoc --version")))
287338
(message "Your version of pandoc is too old, please install a more recent version. See their github for more info.")))
288339

289340

test/rustic-doc-test.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,32 @@
1111
(should (file-exists-p "~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/share/doc/rust/html/std/option"))
1212
(should (file-exists-p (f-join rustic-doc-save-loc "std" "option" "enum.Option.org"))))
1313

14+
(ert-deftest rustic-doc-semver-test ()
15+
(should (equal (rustic-doc--semver-from-string "1.0.0") '(1 0 0)))
16+
(should (equal (rustic-doc--semver-from-string "1.0.0-alpha") '(1 0 0)))
17+
(should (equal (rustic-doc--semver-from-string "1.0.0.1") '(1 0 0)))
18+
19+
(setq cmp-test-vectors (list (list "1.0.0" "1.0.0.1" nil)
20+
(list "1.0.0" "1.0.0-alpha" nil)
21+
(list "2.0.1" "2.0.0" t)
22+
(list "2.1.0" "2.0.0" t)
23+
(list "2.1.1" "2.1.0" t)))
24+
25+
(dolist (test cmp-test-vectors)
26+
(let ((v1 (rustic-doc--semver-from-string (car test)))
27+
(v2 (rustic-doc--semver-from-string (cadr test)))
28+
(expected (caddr test)))
29+
(should (equal (rustic-doc--semver-greater v1 v2) expected)))))
30+
31+
(ert-deftest rustic-doc-verstion-extract-test ()
32+
(should (equal (rustic-doc--extract-version "fd 10.1.0
33+
") (list 10 1 0)))
34+
(should (equal (rustic-doc--extract-version "pandoc 3.1.11.1
35+
Features: +server +lua
36+
Scripting engine: Lua 5.4
37+
User data directory: /Users/user/.local/share/pandoc
38+
Copyright (C) 2006-2023 John MacFarlane. Web: https://pandoc.org
39+
This is free software; see the source for copying conditions. There is no
40+
warranty, not even for merchantability or fitness for a particular purpose.") (list 3 1 11))))
41+
1442
(provide 'rustic-doc-test)

0 commit comments

Comments
 (0)