clojure-utils.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Alex Vong <alexvong1995@gmail.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build clojure-utils)
  19. #:use-module (guix build utils)
  20. #:use-module (ice-9 ftw)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 regex)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-8)
  25. #:use-module (srfi srfi-26)
  26. #:export (@*
  27. @@*
  28. define-with-docs
  29. %doc-regex
  30. install-doc
  31. %source-dirs
  32. %test-dirs
  33. %compile-dir
  34. package-name->jar-names
  35. %main-class
  36. %omit-source?
  37. %aot-include
  38. %aot-exclude
  39. %tests?
  40. %test-include
  41. %test-exclude
  42. %clojure-regex
  43. canonicalize-relative-path
  44. find-files*
  45. file-sans-extension
  46. relative-path->clojure-lib-string
  47. find-clojure-libs
  48. compiled-from?
  49. include-list\exclude-list
  50. eval-with-clojure
  51. create-jar))
  52. (define-syntax-rule (@* module name)
  53. "Like (@ MODULE NAME), but resolves at run time."
  54. (module-ref (resolve-interface 'module) 'name))
  55. (define-syntax-rule (@@* module name)
  56. "Like (@@ MODULE NAME), but resolves at run time."
  57. (module-ref (resolve-module 'module) 'name))
  58. (define-syntax-rule (define-with-docs name docs val)
  59. "Create top-level variable named NAME with doc string DOCS and value VAL."
  60. (begin (define name val)
  61. (set-object-property! name 'documentation docs)))
  62. (define-with-docs %doc-regex
  63. "Default regex for matching the base name of top-level documentation files."
  64. (format #f
  65. "(~a)|(\\.(html|markdown|md|txt)$)"
  66. (@@ (guix build guile-build-system)
  67. %documentation-file-regexp)))
  68. (define* (install-doc #:key
  69. doc-dirs
  70. (doc-regex %doc-regex)
  71. outputs
  72. #:allow-other-keys)
  73. "Install the following to the default documentation directory:
  74. 1. Top-level files with base name matching DOC-REGEX.
  75. 2. All files (recursively) inside DOC-DIRS.
  76. DOC-REGEX can be compiled or uncompiled."
  77. (let* ((out (assoc-ref outputs "out"))
  78. (doc (assoc-ref outputs "doc"))
  79. (name-ver (strip-store-file-name out))
  80. (dest-dir (string-append (or doc out) "/share/doc/" name-ver "/"))
  81. (doc-regex* (if (string? doc-regex)
  82. (make-regexp doc-regex)
  83. doc-regex)))
  84. (for-each (cut install-file <> dest-dir)
  85. (remove (compose file-exists?
  86. (cut string-append dest-dir <>))
  87. (scandir "./" (cut regexp-exec doc-regex* <>))))
  88. (for-each (cut copy-recursively <> dest-dir)
  89. doc-dirs)
  90. #t))
  91. (define-with-docs %source-dirs
  92. "A default list of source directories."
  93. '("src/"))
  94. (define-with-docs %test-dirs
  95. "A default list of test directories."
  96. '("test/"))
  97. (define-with-docs %compile-dir
  98. "Default directory for holding class files."
  99. "classes/")
  100. (define (package-name->jar-names name)
  101. "Given NAME, a package name like \"foo-0.9.1b\",
  102. return the list of default jar names: (\"foo-0.9.1b.jar\" \"foo.jar\")."
  103. (map (cut string-append <> ".jar")
  104. (list name
  105. (receive (base-name _)
  106. (package-name->name+version name)
  107. base-name))))
  108. (define-with-docs %main-class
  109. "Default name for main class. It should be a symbol or #f."
  110. #f)
  111. (define-with-docs %omit-source?
  112. "Include source in jars by default."
  113. #f)
  114. (define-with-docs %aot-include
  115. "A default list of symbols deciding what to compile. Note that the exclude
  116. list has priority over the include list. The special keyword #:all represents
  117. all libraries found under the source directories."
  118. '(#:all))
  119. (define-with-docs %aot-exclude
  120. "A default list of symbols deciding what not to compile.
  121. See the doc string of '%aot-include' for more details."
  122. '())
  123. (define-with-docs %tests?
  124. "Enable tests by default."
  125. #t)
  126. (define-with-docs %test-include
  127. "A default list of symbols deciding what tests to include. Note that the
  128. exclude list has priority over the include list. The special keyword #:all
  129. represents all tests found under the test directories."
  130. '(#:all))
  131. (define-with-docs %test-exclude
  132. "A default list of symbols deciding what tests to exclude.
  133. See the doc string of '%test-include' for more details."
  134. '())
  135. (define-with-docs %clojure-regex
  136. "Default regex for matching the base name of clojure source files."
  137. "\\.cljc?$")
  138. (define-with-docs canonicalize-relative-path
  139. "Like 'canonicalize-path', but for relative paths.
  140. Canonicalizations requiring the path to exist are omitted."
  141. (let ((remove.. (lambda (ls)
  142. (fold-right (match-lambda*
  143. (((and comp (not "..")) (".." comps ...))
  144. comps)
  145. ((comp (comps ...))
  146. (cons comp comps)))
  147. '()
  148. ls))))
  149. (compose (match-lambda
  150. (() ".")
  151. (ls (string-join ls "/")))
  152. remove..
  153. (cut remove (cut member <> '("" ".")) <>)
  154. (cut string-split <> #\/))))
  155. (define (find-files* base-dir . args)
  156. "Similar to 'find-files', but with BASE-DIR stripped and result
  157. canonicalized."
  158. (map canonicalize-relative-path
  159. (with-directory-excursion base-dir
  160. (apply find-files "./" args))))
  161. ;;; FIXME: should be moved to (guix build utils)
  162. (define-with-docs file-sans-extension
  163. "Strip extension from path, if any."
  164. (@@ (guix build guile-build-system)
  165. file-sans-extension))
  166. (define (relative-path->clojure-lib-string path)
  167. "Convert PATH to a clojure library string."
  168. (string-map (match-lambda
  169. (#\/ #\.)
  170. (#\_ #\-)
  171. (chr chr))
  172. (file-sans-extension path)))
  173. (define* (find-clojure-libs base-dir
  174. #:key (clojure-regex %clojure-regex))
  175. "Return the list of clojure libraries found under BASE-DIR.
  176. CLOJURE-REGEX can be compiled or uncompiled."
  177. (map (compose string->symbol
  178. relative-path->clojure-lib-string)
  179. (find-files* base-dir clojure-regex)))
  180. (define (compiled-from? class lib)
  181. "Given class file CLASS and clojure library symbol LIB, decide if CLASS
  182. results from compiling LIB."
  183. (string-prefix? (symbol->string lib)
  184. (relative-path->clojure-lib-string class)))
  185. (define* (include-list\exclude-list include-list exclude-list
  186. #:key all-list)
  187. "Given INCLUDE-LIST and EXCLUDE-LIST, replace all occurrences of #:all by
  188. slicing ALL-LIST into them and compute their list difference."
  189. (define (replace-#:all ls all-ls)
  190. (append-map (match-lambda
  191. (#:all all-ls)
  192. (x (list x)))
  193. ls))
  194. (let ((include-list* (replace-#:all include-list all-list))
  195. (exclude-list* (replace-#:all exclude-list all-list)))
  196. (lset-difference equal? include-list* exclude-list*)))
  197. (define (eval-with-clojure expr extra-paths)
  198. "Evaluate EXPR with clojure.
  199. EXPR must be a s-expression writable by guile and readable by clojure.
  200. For examples, '(require '[clojure.string]) will not work,
  201. because the guile writer converts brackets to parentheses.
  202. EXTRA-PATHS is a list of paths which will be appended to $CLASSPATH."
  203. (let* ((classpath (getenv "CLASSPATH"))
  204. (classpath* (string-join (cons classpath extra-paths) ":")))
  205. (invoke "java"
  206. "-classpath" classpath*
  207. "clojure.main"
  208. "--eval" (object->string expr))))
  209. (define* (create-jar output-jar dir-files-alist
  210. #:key
  211. (verbose? #t)
  212. (compress? #f)
  213. (main-class %main-class))
  214. "Given DIR-FILES-ALIST, an alist of the form: ((DIR . FILES) ...)
  215. Create jar named OUTPUT-JAR from FILES with DIR stripped."
  216. (let ((grouped-options (string-append "c"
  217. (if verbose? "v" "")
  218. "f"
  219. (if compress? "" "0")
  220. (if main-class "e" ""))))
  221. (apply invoke `("jar"
  222. ,grouped-options
  223. ,output-jar
  224. ,@(if main-class (list (symbol->string main-class)) '())
  225. ,@(append-map (match-lambda
  226. ((dir . files)
  227. (append-map (lambda (file)
  228. `("-C" ,dir ,file))
  229. files)))
  230. dir-files-alist)))))