clojure-utils.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. "^(README.*|.*\\.html|.*\\.org|.*\\.md|\\.markdown|\\.txt)$")
  65. (define* (install-doc #:key
  66. doc-dirs
  67. (doc-regex %doc-regex)
  68. outputs
  69. #:allow-other-keys)
  70. "Install the following to the default documentation directory:
  71. 1. Top-level files with base name matching DOC-REGEX.
  72. 2. All files (recursively) inside DOC-DIRS.
  73. DOC-REGEX can be compiled or uncompiled."
  74. (let* ((out (assoc-ref outputs "out"))
  75. (doc (assoc-ref outputs "doc"))
  76. (name-ver (strip-store-file-name out))
  77. (dest-dir (string-append (or doc out) "/share/doc/" name-ver "/"))
  78. (doc-regex* (if (string? doc-regex)
  79. (make-regexp doc-regex)
  80. doc-regex)))
  81. (for-each (cut install-file <> dest-dir)
  82. (remove (compose file-exists?
  83. (cut string-append dest-dir <>))
  84. (scandir "./" (cut regexp-exec doc-regex* <>))))
  85. (for-each (cut copy-recursively <> dest-dir)
  86. doc-dirs)
  87. #t))
  88. (define-with-docs %source-dirs
  89. "A default list of source directories."
  90. '("src/"))
  91. (define-with-docs %test-dirs
  92. "A default list of test directories."
  93. '("test/"))
  94. (define-with-docs %compile-dir
  95. "Default directory for holding class files."
  96. "classes/")
  97. (define (package-name->jar-names name)
  98. "Given NAME, a package name like \"foo-0.9.1b\",
  99. return the list of default jar names: (\"foo-0.9.1b.jar\" \"foo.jar\")."
  100. (map (cut string-append <> ".jar")
  101. (list name
  102. (receive (base-name _)
  103. (package-name->name+version name)
  104. base-name))))
  105. (define-with-docs %main-class
  106. "Default name for main class. It should be a symbol or #f."
  107. #f)
  108. (define-with-docs %omit-source?
  109. "Include source in jars by default."
  110. #f)
  111. (define-with-docs %aot-include
  112. "A default list of symbols deciding what to compile. Note that the exclude
  113. list has priority over the include list. The special keyword #:all represents
  114. all libraries found under the source directories."
  115. '(#:all))
  116. (define-with-docs %aot-exclude
  117. "A default list of symbols deciding what not to compile.
  118. See the doc string of '%aot-include' for more details."
  119. '())
  120. (define-with-docs %tests?
  121. "Enable tests by default."
  122. #t)
  123. (define-with-docs %test-include
  124. "A default list of symbols deciding what tests to include. Note that the
  125. exclude list has priority over the include list. The special keyword #:all
  126. represents all tests found under the test directories."
  127. '(#:all))
  128. (define-with-docs %test-exclude
  129. "A default list of symbols deciding what tests to exclude.
  130. See the doc string of '%test-include' for more details."
  131. '())
  132. (define-with-docs %clojure-regex
  133. "Default regex for matching the base name of clojure source files."
  134. "\\.cljc?$")
  135. (define-with-docs canonicalize-relative-path
  136. "Like 'canonicalize-path', but for relative paths.
  137. Canonicalizations requiring the path to exist are omitted."
  138. (let ((remove.. (lambda (ls)
  139. (fold-right (match-lambda*
  140. (((and comp (not "..")) (".." comps ...))
  141. comps)
  142. ((comp (comps ...))
  143. (cons comp comps)))
  144. '()
  145. ls))))
  146. (compose (match-lambda
  147. (() ".")
  148. (ls (string-join ls "/")))
  149. remove..
  150. (cut remove (cut member <> '("" ".")) <>)
  151. (cut string-split <> #\/))))
  152. (define (find-files* base-dir . args)
  153. "Similar to 'find-files', but with BASE-DIR stripped and result
  154. canonicalized."
  155. (map canonicalize-relative-path
  156. (with-directory-excursion base-dir
  157. (apply find-files "./" args))))
  158. ;;; FIXME: should be moved to (guix build utils)
  159. (define (file-sans-extension file) ;TODO: factorize
  160. "Return the substring of FILE without its extension, if any."
  161. (let ((dot (string-rindex file #\.)))
  162. (if dot
  163. (substring file 0 dot)
  164. file)))
  165. (define (relative-path->clojure-lib-string path)
  166. "Convert PATH to a clojure library string."
  167. (string-map (match-lambda
  168. (#\/ #\.)
  169. (#\_ #\-)
  170. (chr chr))
  171. (file-sans-extension path)))
  172. (define* (find-clojure-libs base-dir
  173. #:key (clojure-regex %clojure-regex))
  174. "Return the list of clojure libraries found under BASE-DIR.
  175. CLOJURE-REGEX can be compiled or uncompiled."
  176. (map (compose string->symbol
  177. relative-path->clojure-lib-string)
  178. (find-files* base-dir clojure-regex)))
  179. (define (compiled-from? class lib)
  180. "Given class file CLASS and clojure library symbol LIB, decide if CLASS
  181. results from compiling LIB."
  182. (string-prefix? (symbol->string lib)
  183. (relative-path->clojure-lib-string class)))
  184. (define* (include-list\exclude-list include-list exclude-list
  185. #:key all-list)
  186. "Given INCLUDE-LIST and EXCLUDE-LIST, replace all occurrences of #:all by
  187. slicing ALL-LIST into them and compute their list difference."
  188. (define (replace-#:all ls all-ls)
  189. (append-map (match-lambda
  190. (#:all all-ls)
  191. (x (list x)))
  192. ls))
  193. (let ((include-list* (replace-#:all include-list all-list))
  194. (exclude-list* (replace-#:all exclude-list all-list)))
  195. (lset-difference equal? include-list* exclude-list*)))
  196. (define (eval-with-clojure expr extra-paths)
  197. "Evaluate EXPR with clojure.
  198. EXPR must be a s-expression writable by guile and readable by clojure.
  199. For examples, '(require '[clojure.string]) will not work,
  200. because the guile writer converts brackets to parentheses.
  201. EXTRA-PATHS is a list of paths which will be appended to $CLASSPATH."
  202. (let* ((classpath (getenv "CLASSPATH"))
  203. (classpath* (string-join (cons classpath extra-paths) ":")))
  204. (invoke "java"
  205. "-classpath" classpath*
  206. "clojure.main"
  207. "--eval" (object->string expr))))
  208. (define* (create-jar output-jar dir-files-alist
  209. #:key
  210. (verbose? #t)
  211. (compress? #f)
  212. (main-class %main-class))
  213. "Given DIR-FILES-ALIST, an alist of the form: ((DIR . FILES) ...)
  214. Create jar named OUTPUT-JAR from FILES with DIR stripped."
  215. (let ((grouped-options (string-append "c"
  216. (if verbose? "v" "")
  217. "f"
  218. (if compress? "" "0")
  219. (if main-class "e" ""))))
  220. (apply invoke `("jar"
  221. ,grouped-options
  222. ,output-jar
  223. ,@(if main-class (list (symbol->string main-class)) '())
  224. ,@(append-map (match-lambda
  225. ((dir . files)
  226. (append-map (lambda (file)
  227. `("-C" ,dir ,file))
  228. files)))
  229. dir-files-alist)))))