build-system: Rewrite using gexps.
[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 ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (guix build-system minify)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix packages)
24 #:use-module (guix gexp)
25 #:use-module (guix monads)
26 #:use-module (guix search-paths)
27 #:use-module (guix build-system)
28 #:use-module (guix build-system gnu)
29 #:use-module (ice-9 match)
30 #:use-module (srfi srfi-26)
31 #:export (%minify-build-system-modules
32 minify-build
33 minify-build-system))
34
35 ;; Commentary:
36 ;;
37 ;; Standard minification procedure for JavaScript files.
38 ;;
39 ;; Code:
40
41 (define %minify-build-system-modules
42 ;; Build-side modules imported by default.
43 `((guix build minify-build-system)
44 ,@%gnu-build-system-modules))
45
46 (define (default-uglify-js)
47 "Return the default package to minify JavaScript source files."
48 ;; Lazily resolve the binding to avoid a circular dependency.
49 (let ((mod (resolve-interface '(gnu packages lisp-xyz))))
50 (module-ref mod 'uglify-js)))
51
52 (define* (lower name
53 #:key source inputs native-inputs outputs system
54 (uglify-js (default-uglify-js))
55 #:allow-other-keys
56 #:rest arguments)
57 "Return a bag for NAME."
58 (define private-keywords
59 '(#:target #:inputs #:native-inputs))
60
61 (bag
62 (name name)
63 (system system)
64 (host-inputs `(,@(if source
65 `(("source" ,source))
66 '())
67 ,@inputs
68 ,@(standard-packages)))
69 (build-inputs `(("uglify-js" ,uglify-js)
70 ,@native-inputs))
71 (outputs outputs)
72 (build minify-build)
73 (arguments (strip-keyword-arguments private-keywords arguments))))
74
75 (define* (minify-build name inputs
76 #:key
77 source
78 (javascript-files #f)
79 (phases '(@ (guix build minify-build-system)
80 %standard-phases))
81 (outputs '("out"))
82 (system (%current-system))
83 search-paths
84 (guile #f)
85 (imported-modules %minify-build-system-modules)
86 (modules '((guix build minify-build-system)
87 (guix build utils))))
88 "Build SOURCE with INPUTS."
89 (define builder
90 (with-imported-modules imported-modules
91 #~(begin
92 (use-modules #$@modules)
93 (minify-build #:name #$name
94 #:source #+source
95 #:javascript-files #$javascript-files
96 #:phases #$phases
97 #:outputs #$(outputs->gexp outputs)
98 #:search-paths '#$(map search-path-specification->sexp
99 search-paths)
100 #:inputs #$(input-tuples->gexp inputs)))))
101
102 (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
103 system #:graft? #f)))
104 (gexp->derivation name builder
105 #:system system
106 #:guile-for-build guile)))
107
108 (define minify-build-system
109 (build-system
110 (name 'minify)
111 (description "The trivial JavaScript minification build system")
112 (lower lower)))
113
114 ;;; minify.scm ends here