Merge branch 'core-updates'.
[jackhill/guix/guix.git] / guix / build / ruby-build-system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
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 ruby-build-system)
20 #:use-module ((guix build gnu-build-system) #:prefix gnu:)
21 #:use-module (guix build utils)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 regex)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-26)
26 #:export (%standard-phases
27 ruby-build))
28
29 ;; Commentary:
30 ;;
31 ;; Builder-side code of the standard Ruby package build procedure.
32 ;;
33 ;; Code:
34
35 (define (first-matching-file pattern)
36 "Return the first file name that matches PATTERN in the current working
37 directory."
38 (match (find-files "." pattern)
39 ((file-name . _) file-name)
40 (() (error "No files matching pattern: " pattern))))
41
42 ;; Most gemspecs assume that builds are taking place within a git repository
43 ;; by include calls to 'git ls-files'. In order for these gemspecs to work
44 ;; as-is, every file in the source tree is added to the staging area.
45 (define gitify
46 (lambda _
47 (and (zero? (system* "git" "init"))
48 (zero? (system* "git" "add" ".")))))
49
50 (define build
51 (lambda _
52 (zero? (system* "gem" "build" (first-matching-file "\\.gemspec$")))))
53
54 (define* (check #:key tests? test-target #:allow-other-keys)
55 (if tests?
56 (zero? (system* "rake" test-target))
57 #t))
58
59 (define* (install #:key source inputs outputs #:allow-other-keys)
60 (let* ((ruby-version
61 (match:substring (string-match "ruby-(.*)$"
62 (assoc-ref inputs "ruby"))
63 1))
64 (out (assoc-ref outputs "out"))
65 (gem-home (string-append out "/lib/ruby/gems/" ruby-version)))
66 (setenv "GEM_HOME" gem-home)
67 (mkdir-p gem-home)
68 (zero? (system* "gem" "install" "--local"
69 (first-matching-file "\\.gem$")
70 ;; Executables should go into /bin, not /lib/ruby/gems.
71 "--bindir" (string-append out "/bin")))))
72
73 (define %standard-phases
74 (modify-phases gnu:%standard-phases
75 (delete configure)
76 (add-after unpack gitify gitify)
77 (replace build build)
78 (replace install install)
79 (replace check check)))
80
81 (define* (ruby-build #:key inputs (phases %standard-phases)
82 #:allow-other-keys #:rest args)
83 (apply gnu:gnu-build #:inputs inputs #:phases phases args))