unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 6e11d1c142d17c799171626dc40e414c39ab715f 7976 bytes (raw)
name: guix/build/node-build-system.scm 	 # note: path name is non-authoritative(*)

  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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;; Copyright © 2016 Jelle Licht <jlicht@fsfe.org>
;;; Copyright © 2020 Giacomo Leidi <goodoldpaul@autistici.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 (guix glob)
  #: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)))
         (file-list (match (assoc-ref data "files")
                      (() #f)
                      ((? list? pattern-list) pattern-list)
                      (#f #f)))
         (patterns
          (when file-list
            (map (lambda (pattern)
                   (string->compiled-sglob pattern))
                 (append file-list
                         '("package.json"
                           ;; These files get installed no
                           ;; matter the case or extension.
                           "[rR][eE][aA][dD][mM][eE]*"
                           "[cC][hH][aA][nN][gG][eE][sS]*"
                           "[cC][hH][aA][nN][gG][eE][lL][oO][gG]*"
                           "[hH][iI][sS][tT][oO][rR][yY]*"
                           "[nN][oO][tT][iI][cC][eE]*")))))
         (main (match (assoc-ref data "main")
                 ("" #f)
                 ((? string? main-module) main-module)
                 (#f #f)))
         (install-dir (string-append target "/node_modules/" modulename))
         (install-files (lambda (files)
                          (for-each (lambda (file)
                                      (install-file
                                       file
                                       (string-append install-dir "/"
                                                      (dirname file))))
                                    files))))
    (mkdir-p target)
    (if file-list
        (install-files
         (find-files "." (lambda (file stat)
                           (any (lambda (pattern)
                                  (glob-match? pattern
                                               (string-drop file 2)))
                                patterns))))
        (begin
          (copy-recursively "." install-dir)
          ;; Remove references to dependencies
          (delete-file-recursively
           (string-append install-dir "/node_modules"))))
    (if (and main
             (not (file-exists?
                   (string-append
                    install-dir "/" (dirname main)))))
        (install-files (list main)))
    (cond
      ((string? binary-configuration)
       (begin
         (mkdir-p binaries)
         (symlink (string-append install-dir "/" 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 install-dir "/" value)
                         (string-append binaries "/" key))))))
        binary-configuration)))
    (when dependencies
      (mkdir-p
        (string-append install-dir "/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 install-dir "/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))

debug log:

solving 6e11d1c142 ...
found 6e11d1c142 in https://yhetil.org/guix-patches/7bea951c-c8c8-cca9-4bfe-8d8f5c83e2ab@autistici.org/
found 7799f03595 in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 7799f035955473142d8cd82fd5667214f9cb9697	guix/build/node-build-system.scm

applying [1/1] https://yhetil.org/guix-patches/7bea951c-c8c8-cca9-4bfe-8d8f5c83e2ab@autistici.org/
diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index 7799f03595..6e11d1c142 100644

Checking patch guix/build/node-build-system.scm...
Applied patch guix/build/node-build-system.scm cleanly.

index at:
100644 6e11d1c142d17c799171626dc40e414c39ab715f	guix/build/node-build-system.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).