1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
| | ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;; Copyright © 2016 Jelle Licht <jlicht@fsfe.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (guix build node-build-system)
#:use-module ((guix build gnu-build-system) #:prefix gnu:)
#:use-module (guix build json)
#:use-module (guix build union)
#:use-module (guix build utils)
#:use-module (ice-9 match)
#:use-module (ice-9 popen)
#:use-module (ice-9 regex)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:export (%standard-phases
node-build))
;; Commentary:
;;
;; Builder-side code of the standard Node/npm package build procedure.
;;
;; Code:
(define* (read-package-data #:key (filename "package.json"))
(call-with-input-file filename
(lambda (port)
(read-json port))))
(define* (build #:key inputs #:allow-other-keys)
(define (build-from-package-json? package-file)
(let* ((package-data (read-package-data #:filename package-file))
(scripts (assoc-ref package-data "scripts")))
(assoc-ref scripts "build")))
"Build a new node module using the appropriate build system."
;; XXX: Develop a more robust heuristic, allow override
(cond ((file-exists? "gulpfile.js")
(invoke "gulp"))
((file-exists? "gruntfile.js")
(invoke "grunt"))
((file-exists? "Makefile")
(invoke "make"))
((and (file-exists? "package.json")
(build-from-package-json? "package.json"))
(invoke "npm" "run" "build")))
#t)
(define* (link-npm-dependencies #:key inputs #:allow-other-keys)
(define (inputs->node-inputs inputs)
"Filter the directory part from INPUTS."
(filter (lambda (input)
(match input
((name . _) (node-package? name))))
inputs))
(define (inputs->directories inputs)
"Extract the directory part from INPUTS."
(match inputs
(((names . directories) ...)
directories)))
(define (make-node-path root)
(string-append root "/lib/node_modules/"))
(let ((input-node-directories (inputs->directories
(inputs->node-inputs inputs))))
(union-build "node_modules"
(map make-node-path input-node-directories))
#t))
(define configure link-npm-dependencies)
(define* (check #:key tests? #:allow-other-keys)
"Run 'npm test' if TESTS?"
(if tests?
;; Should only be enabled once we know that there are tests
(invoke "npm" "test"))
#t)
(define (node-package? name)
"Check if NAME correspond to the name of an Node package."
(string-prefix? "node-" name))
(define* (install #:key outputs inputs #:allow-other-keys)
"Install the node module to the output store item. The module itself is
installed in a subdirectory of @file{node_modules} and its runtime dependencies
as defined by @file{package.json} are symlinked into a @file{node_modules}
subdirectory of the module's directory. Additionally, binaries are installed in
the @file{bin} directory."
(let* ((out (assoc-ref outputs "out"))
(target (string-append out "/lib"))
(binaries (string-append out "/bin"))
(data (read-package-data))
(modulename (assoc-ref data "name"))
(binary-configuration (match (assoc-ref data "bin")
(('@ configuration ...) configuration)
((? string? configuration) configuration)
(#f #f)))
(dependencies (match (assoc-ref data "dependencies")
(('@ deps ...) deps)
(#f #f))))
(mkdir-p target)
(copy-recursively "." (string-append target "/node_modules/" modulename))
;; Remove references to dependencies
(delete-file-recursively
(string-append target "/node_modules/" modulename "/node_modules"))
(cond
((string? binary-configuration)
(begin
(mkdir-p binaries)
(symlink (string-append target "/node_modules/" modulename "/"
binary-configuration)
(string-append binaries "/" modulename))))
((list? binary-configuration)
(for-each
(lambda (conf)
(match conf
((key . value)
(begin
(mkdir-p (dirname (string-append binaries "/" key)))
(symlink (string-append target "/node_modules/" modulename "/"
value)
(string-append binaries "/" key))))))
binary-configuration)))
(when dependencies
(mkdir-p
(string-append target "/node_modules/" modulename "/node_modules"))
(for-each
(lambda (dependency)
(let ((dependency (car dependency)))
(symlink
(string-append (assoc-ref inputs (string-append "node-" dependency))
"/lib/node_modules/" dependency)
(string-append target "/node_modules/" modulename
"/node_modules/" dependency))))
dependencies))
#t))
(define %standard-phases
(modify-phases gnu:%standard-phases
(replace 'configure configure)
(replace 'build build)
(replace 'install install)
(delete 'check)
(add-after 'install 'check check)
(delete 'strip)))
(define* (node-build #:key inputs (phases %standard-phases)
#:allow-other-keys #:rest args)
(apply gnu:gnu-build #:inputs inputs #:phases phases args))
|