From 614fedc2b359f123dfdf4e31eee30e7ce47e1bd2 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 7 Mar 2015 18:39:52 -0500 Subject: [PATCH] build: ruby: Patch executables to set necessary gem load path. * guix/build/ruby-build-system.scm (gem-directory): New procedure. (install): Deduplicate gem directory code. (patch-executables): New procedure. (%standard-phases): Add 'patch-executables' phase. --- guix/build/ruby-build-system.scm | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/guix/build/ruby-build-system.scm b/guix/build/ruby-build-system.scm index a143df4..85ad4d7 100644 --- a/guix/build/ruby-build-system.scm +++ b/guix/build/ruby-build-system.scm @@ -32,6 +32,16 @@ ;; ;; Code: +(define (gem-directory inputs) + "Return the gem installation directory for the version of Ruby within +INPUTS." + ;; Leave off the patch version number in the directory name. + (string-append "/lib/ruby/gems" + (match:substring (string-match "ruby-(.*)\\.[0-9]$" + (assoc-ref inputs "ruby")) + 1) + ".0")) + (define (first-matching-file pattern) "Return the first file name that matches PATTERN in the current working directory." @@ -57,12 +67,8 @@ directory." #t)) (define* (install #:key source inputs outputs #:allow-other-keys) - (let* ((ruby-version - (match:substring (string-match "ruby-(.*)\\.[0-9]$" - (assoc-ref inputs "ruby")) - 1)) - (out (assoc-ref outputs "out")) - (gem-home (string-append out "/lib/ruby/gems/" ruby-version ".0"))) + (let* ((out (assoc-ref outputs "out")) + (gem-home (string-append out (gem-directory inputs)))) (setenv "GEM_HOME" gem-home) (mkdir-p gem-home) (zero? (system* "gem" "install" "--local" @@ -70,12 +76,32 @@ directory." ;; Executables should go into /bin, not /lib/ruby/gems. "--bindir" (string-append out "/bin"))))) +(define* (patch-executables #:key inputs outputs #:allow-other-keys) + (let* ((out (assoc-ref outputs "out")) + (gem-dir (gem-directory inputs)) + ;; Ruby code to add all input gems to the load path. + (ruby-snippet + (string-concatenate + (filter-map (match-lambda + ((_ . input) + (let ((path (string-append input gem-dir))) + (and (file-exists? path) + (string-append "Gem.path.unshift '" + path "'\n"))))) + inputs)))) + ;; Insert code snippet before the built gem is loaded. + (substitute* (find-files (string-append out "/bin") ".*") + (("(gem '.*', version\n)" gem-load-line) + (string-append ruby-snippet gem-load-line))) + #t)) + (define %standard-phases (modify-phases gnu:%standard-phases (delete configure) (add-after unpack gitify gitify) (replace build build) (replace install install) + (add-after install patch-executables patch-executables) (replace check check))) (define* (ruby-build #:key inputs (phases %standard-phases) -- 2.1.4