unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob b74e59383872713a0cf980edbe7d8a02d6870fca 6945 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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;; Copyright © 2016, 2020 Jelle Licht <jlicht@fsfe.org>
;;; Copyright © 2019, 2021 Timothy Sample <samplet@ngyro.com>
;;;
;;; 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 utils)
  #:use-module (guix build json)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:export (%standard-phases
            node-build))

;; Commentary:
;;
;; Builder-side code of the standard Node/NPM package install procedure.
;;
;; Code:

(define (set-home . _)
  (with-directory-excursion ".."
    (let loop ((i 0))
      (let ((dir (string-append "npm-home-" (number->string i))))
        (if (directory-exists? dir)
            (loop (1+ i))
            (begin
              (mkdir dir)
              (setenv "HOME" (string-append (getcwd) "/" dir))
              (format #t "set HOME to ~s~%" (getenv "HOME")))))))
  #t)

(define (module-name module)
  (let* ((package.json (string-append module "/package.json"))
         (package-meta (call-with-input-file package.json read-json)))
    (assoc-ref package-meta "name")))

(define (index-modules input-paths)
  (define (list-modules directory)
    (append-map (lambda (x)
                  (if (string-prefix? "@" x)
                      (list-modules (string-append directory "/" x))
                      (list (string-append directory "/" x))))
                (filter (lambda (x)
                          (not (member x '("." ".."))))
                        (or (scandir directory) '()))))
  (let ((index (make-hash-table (* 2 (length input-paths)))))
    (for-each (lambda (dir)
                (let ((nm (string-append dir "/lib/node_modules")))
                  (for-each (lambda (module)
                              (hash-set! index (module-name module) module))
                            (list-modules nm))))
              input-paths)
    index))

(define* (patch-dependencies #:key inputs #:allow-other-keys)

  (define index (index-modules (map cdr inputs)))

  (define (resolve-dependencies meta-alist meta-key)
    ;; Given:
    ;;  - The alist from "package.json", with the '@ unwrapped
    ;;  - A string key, like "dependencies"
    ;; Returns: an alist (without a wrapping '@) like the entry in
    ;; meta-alist for meta-key, but with dependencies supplied
    ;; by Guix packages mapped to the absolute store paths to use.
    (match (assoc-ref meta-alist meta-key)
      (#f
       '())
      (('@ . orig-deps)
       (fold (match-lambda*
               (((key . value) acc)
                (acons key (hash-ref index key value) acc)))
             '()
             orig-deps))))

  (with-atomic-file-replacement "package.json"
    (lambda (in out)
      ;; It is unsafe to rely on 'assoc-set!' to update an
      ;; existing assosciation list variable:
      ;; see 'info "(guile)Adding or Setting Alist Entries"'.
      (let* ((package-meta (read-json in))
             (alist (match package-meta
                      ((@ . alist) alist)))
             (alist
              (assoc-set!
               alist "dependencies"
               (append
                '(@)
                (resolve-dependencies alist "dependencies")
                (resolve-dependencies alist "peerDependencies"))))
             (alist
              (assoc-set!
               alist "devDependencies"
               (append
                '(@)
                (resolve-dependencies alist "devDependencies"))))
             (package-meta (cons '@ alist)))
        (write-json package-meta out))))
  #t)

(define* (delete-lockfiles #:key inputs #:allow-other-keys)
  "Delete 'package-lock.json', 'yarn.lock', and 'npm-shrinkwrap.json', if they
exist."
  (for-each (lambda (pth)
              (when (file-exists? pth)
                (delete-file pth)))
            '("package-lock.json"
              "yarn.lock"
              "npm-shrinkwrap.json"))
  #t)

(define* (configure #:key outputs inputs #:allow-other-keys)
  (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
    (invoke npm "--offline" "--ignore-scripts" "install")
    #t))

(define* (build #:key inputs #:allow-other-keys)
  (let ((package-meta (call-with-input-file "package.json" read-json)))
    (if (and=> (assoc-ref package-meta "scripts")
               (lambda (scripts)
                 (assoc-ref scripts "build")))
        (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
          (invoke npm "run" "build"))
        (format #t "there is no build script to run~%"))
    #t))

(define* (check #:key tests? inputs #:allow-other-keys)
  "Run 'npm test' if TESTS?"
  (if tests?
      (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
        (invoke npm "test"))
      (format #t "test suite not run~%"))
  #t)

(define* (repack #:key inputs #:allow-other-keys)
  (invoke "tar"
          ;; Add options suggested by https://reproducible-builds.org/docs/archives/
          "--sort=name"
          (string-append "--mtime=@" (getenv "SOURCE_DATE_EPOCH"))
          "--owner=0"
          "--group=0"
          "--numeric-owner"
          "-czf" "../package.tgz" ".")
  #t)

(define* (install #:key outputs inputs #:allow-other-keys)
  "Install the node module to the output store item."
  (let ((out (assoc-ref outputs "out"))
        (npm (string-append (assoc-ref inputs "node") "/bin/npm")))
    (invoke npm "--prefix" out
            "--global"
            "--offline"
            "--loglevel" "info"
            "--production"
            "install" "../package.tgz")
    #t))

(define %standard-phases
  (modify-phases gnu:%standard-phases
    (add-after 'unpack 'set-home set-home)
    (add-before 'configure 'patch-dependencies patch-dependencies)
    (add-after 'patch-dependencies 'delete-lockfiles delete-lockfiles)
    (replace 'configure configure)
    (replace 'build build)
    (replace 'check check)
    (add-before 'install 'repack repack)
    (replace 'install install)))

(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 b74e593838 ...
found b74e593838 in https://yhetil.org/guix-patches/20211217020325.520821-7-philip@philipmcgrath.com/ ||
	https://yhetil.org/guix-patches/20211213060107.129223-7-philip@philipmcgrath.com/
found dcaa719f40 in https://yhetil.org/guix-patches/20211230074456.33433-2-philip@philipmcgrath.com/ ||
	https://yhetil.org/guix-patches/20211230073919.30327-2-philip@philipmcgrath.com/ ||
	https://yhetil.org/guix-patches/20211217020325.520821-6-philip@philipmcgrath.com/ ||
	https://yhetil.org/guix-patches/20211213060107.129223-6-philip@philipmcgrath.com/
found 70a367618e in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 70a367618eac25fac665d5abeed5c1d5328ba234	guix/build/node-build-system.scm

applying [1/2] https://yhetil.org/guix-patches/20211230074456.33433-2-philip@philipmcgrath.com/
diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index 70a367618e..dcaa719f40 100644

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

skipping https://yhetil.org/guix-patches/20211230073919.30327-2-philip@philipmcgrath.com/ for dcaa719f40
skipping https://yhetil.org/guix-patches/20211217020325.520821-6-philip@philipmcgrath.com/ for dcaa719f40
skipping https://yhetil.org/guix-patches/20211213060107.129223-6-philip@philipmcgrath.com/ for dcaa719f40
index at:
100644 dcaa719f4045a2bdc67a2e1cbb8f8cbd676fbdcc	guix/build/node-build-system.scm

applying [2/2] https://yhetil.org/guix-patches/20211217020325.520821-7-philip@philipmcgrath.com/
diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index dcaa719f40..b74e593838 100644

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

skipping https://yhetil.org/guix-patches/20211213060107.129223-7-philip@philipmcgrath.com/ for b74e593838
index at:
100644 b74e59383872713a0cf980edbe7d8a02d6870fca	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).