arroyo/arroyo-utils.el

85 lines
3.6 KiB
EmacsLisp

;; [[file:arroyo-utils.org::*Arroyo Dependency Sorting][Arroyo Dependency Sorting:1]]
(add-to-list 'arroyo-db-keywords "ARROYO_MODULE_DEP" nil #'equal)
(add-to-list 'arroyo-db-keywords "ARROYO_MODULE_WANTS" nil #'equal)
(add-to-list 'arroyo-db-keywords "ARROYO_MODULE_WANTED" nil #'equal)
;; Arroyo Dependency Sorting:1 ends here
;; [[file:arroyo-utils.org::*Arroyo Dependency Sorting][Arroyo Dependency Sorting:2]]
(defun arroyo-utils--format-dep-pairs (line-fmt final-fmt want-rows wanted-rows)
(->> (append (-map (pcase-lambda (`(,file ,keyword ,value))
(list (file-relative-name file org-roam-directory) value))
want-rows)
(-map (pcase-lambda (`(,file ,keyword ,value))
(list value (file-relative-name file org-roam-directory)))
wanted-rows))
(-map (pcase-lambda (`(,thing ,dep))
(format line-fmt dep thing)))
(s-join "\n")
(format final-fmt)))
(defun arroyo-utils--filter-files (rows files)
(-filter (pcase-lambda (`(,file ,keyword, value))
(or (not files)
(-contains? files file)))
rows))
(defun arroyo-utils--order-command (want-rows wanted-rows)
(arroyo-utils--format-dep-pairs "%s %s" "tsort <<EOF\n%s\nEOF"
want-rows wanted-rows))
(defun arroyo-utils-order-modules (&optional files skip-cmd)
(let* ((want-rows
(arroyo-utils--filter-files
(arroyo-db-query [:select [file keyword value] :from keywords
:where (in keyword $v1)]
(vector "ARROYO_MODULE_DEP" "ARROYO_MODULE_WANTS"))
files))
(wanted-rows
(arroyo-utils--filter-files
(arroyo-db-query [:select [file keyword value] :from keywords
:where (= keyword "ARROYO_MODULE_WANTED")])
files))
(wanted-files (-map #'car wanted-rows))
(want-files (-map #'car want-rows))
(missing
(-difference files (-union wanted-files want-files)))
(cmd (arroyo-utils--order-command want-rows wanted-rows)))
(if skip-cmd
cmd
(append
(->> cmd
(shell-command-to-string)
(s-split "\n")
(-remove #'string-empty-p))
missing))))
;; Arroyo Dependency Sorting:2 ends here
;; [[file:arroyo-utils.org::*Arroyo Dependency Sorting][Arroyo Dependency Sorting:3]]
(defun arroyo-utils--gen-digraph (want-rows wanted-rows)
(arroyo-utils--format-dep-pairs "\"%2s\" -> \"%1s\";" "digraph {\n%s\n}"
want-rows wanted-rows))
(defun arroyo-utils--write-digraph (contents &optional file)
(let ((file (or file "/tmp/arroyo.dot")))
(with-current-buffer (find-file-noselect file)
(erase-buffer)
(insert contents)
(save-buffer))
file))
(defun arroyo-utils-module-digraph ()
(interactive)
(let* ((want-rows (arroyo-db-query [:select [file keyword value] :from keywords
:where (in keyword $v1)]
(vector "ARROYO_MODULE_DEP" "ARROYO_MODULE_WANTS")))
(wanted-rows (arroyo-db-query [:select [file keyword value] :from keywords
:where (= keyword "ARROYO_MODULE_WANTED")])))
(->> (arroyo-utils--gen-digraph want-rows wanted-rows)
(arroyo-utils--write-digraph)
(find-file))))
;; Arroyo Dependency Sorting:3 ends here
;; [[file:arroyo-utils.org::*Footmatter][Footmatter:1]]
(provide 'arroyo-utils)
;; Footmatter:1 ends here