gnu: emacs-org: Update to 9.4.
[jackhill/guix/guix.git] / guix / build-system / minify.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2018 Ricardo Wurmus <rekado@elephly.net>
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
19 (define-module (guix build-system minify)
20 #:use-module (guix store)
21 #:use-module (guix utils)
22 #:use-module (guix packages)
23 #:use-module (guix derivations)
24 #:use-module (guix search-paths)
25 #:use-module (guix build-system)
26 #:use-module (guix build-system gnu)
27 #:use-module (ice-9 match)
28 #:use-module (srfi srfi-26)
29 #:export (%minify-build-system-modules
30 minify-build
31 minify-build-system))
32
33 ;; Commentary:
34 ;;
35 ;; Standard minification procedure for JavaScript files.
36 ;;
37 ;; Code:
38
39 (define %minify-build-system-modules
40 ;; Build-side modules imported by default.
41 `((guix build minify-build-system)
42 ,@%gnu-build-system-modules))
43
44 (define (default-uglify-js)
45 "Return the default package to minify JavaScript source files."
46 ;; Lazily resolve the binding to avoid a circular dependency.
47 (let ((mod (resolve-interface '(gnu packages lisp-xyz))))
48 (module-ref mod 'uglify-js)))
49
50 (define* (lower name
51 #:key source inputs native-inputs outputs system
52 (uglify-js (default-uglify-js))
53 #:allow-other-keys
54 #:rest arguments)
55 "Return a bag for NAME."
56 (define private-keywords
57 '(#:source #:target #:inputs #:native-inputs))
58
59 (bag
60 (name name)
61 (system system)
62 (host-inputs `(,@(if source
63 `(("source" ,source))
64 '())
65 ,@inputs
66 ,@(standard-packages)))
67 (build-inputs `(("uglify-js" ,uglify-js)
68 ,@native-inputs))
69 (outputs outputs)
70 (build minify-build)
71 (arguments (strip-keyword-arguments private-keywords arguments))))
72
73 (define* (minify-build store name inputs
74 #:key
75 (javascript-files #f)
76 (phases '(@ (guix build minify-build-system)
77 %standard-phases))
78 (outputs '("out"))
79 (system (%current-system))
80 search-paths
81 (guile #f)
82 (imported-modules %minify-build-system-modules)
83 (modules '((guix build minify-build-system)
84 (guix build utils))))
85 "Build SOURCE with INPUTS."
86 (define builder
87 `(begin
88 (use-modules ,@modules)
89 (minify-build #:name ,name
90 #:source ,(match (assoc-ref inputs "source")
91 (((? derivation? source))
92 (derivation->output-path source))
93 ((source)
94 source)
95 (source
96 source))
97 #:javascript-files ,javascript-files
98 #:phases ,phases
99 #:outputs %outputs
100 #:search-paths ',(map search-path-specification->sexp
101 search-paths)
102 #:inputs %build-inputs)))
103
104 (define guile-for-build
105 (match guile
106 ((? package?)
107 (package-derivation store guile system #:graft? #f))
108 (#f ; the default
109 (let* ((distro (resolve-interface '(gnu packages commencement)))
110 (guile (module-ref distro 'guile-final)))
111 (package-derivation store guile system #:graft? #f)))))
112
113 (build-expression->derivation store name builder
114 #:inputs inputs
115 #:system system
116 #:modules imported-modules
117 #:outputs outputs
118 #:guile-for-build guile-for-build))
119
120 (define minify-build-system
121 (build-system
122 (name 'minify)
123 (description "The trivial JavaScript minification build system")
124 (lower lower)))
125
126 ;;; minify.scm ends here