unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] build: ruby: Patch executables to set necessary gem load path.
@ 2015-03-08  0:00 David Thompson
  2015-03-08  0:45 ` David Thompson
  2015-03-09 22:18 ` Ludovic Courtès
  0 siblings, 2 replies; 4+ messages in thread
From: David Thompson @ 2015-03-08  0:00 UTC (permalink / raw)
  To: guix-devel

[-- Attachment #1: Type: text/plain, Size: 872 bytes --]

This patch addresses the issue of how to ensure that Ruby executables
are able to load all of the additional Ruby libraries that they need in
order to work: with a new 'patch-executables' build phase.  Instead of
using wrap-program, I instead take advantage of the wrappers that the
'gem' command already creates for Ruby executables.  There's guaranteed
to be a line in which the executable's host gem is loaded that looks
like `gem 'foo', version`.  I simply insert a Ruby code snippet above it
that adds all of the necessary gems to the 'Gem.path' array.

Users of Ruby programs must still apply the $GEM_PATH suggested by 'guix
package --search-paths' in order for the gem that the executable belongs
to (the gem that they explicitly installed) to be found.  I think this
is reasonable and much like how you must set the proper load paths for
Guile programs to work.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-build-ruby-Patch-executables-to-set-necessary-gem-lo.patch --]
[-- Type: text/x-diff, Size: 3381 bytes --]

From 614fedc2b359f123dfdf4e31eee30e7ce47e1bd2 Mon Sep 17 00:00:00 2001
From: David Thompson <dthompson2@worcester.edu>
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


[-- Attachment #3: Type: text/plain, Size: 136 bytes --]


-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] build: ruby: Patch executables to set necessary gem load path.
  2015-03-08  0:00 [PATCH] build: ruby: Patch executables to set necessary gem load path David Thompson
@ 2015-03-08  0:45 ` David Thompson
  2015-03-09 22:19   ` Ludovic Courtès
  2015-03-09 22:18 ` Ludovic Courtès
  1 sibling, 1 reply; 4+ messages in thread
From: David Thompson @ 2015-03-08  0:45 UTC (permalink / raw)
  To: guix-devel

David Thompson <dthompson2@worcester.edu> writes:

> This patch addresses the issue of how to ensure that Ruby executables
> are able to load all of the additional Ruby libraries that they need in
> order to work: with a new 'patch-executables' build phase.  Instead of
> using wrap-program, I instead take advantage of the wrappers that the
> 'gem' command already creates for Ruby executables.  There's guaranteed
> to be a line in which the executable's host gem is loaded that looks
> like `gem 'foo', version`.  I simply insert a Ruby code snippet above it
> that adds all of the necessary gems to the 'Gem.path' array.
>
> Users of Ruby programs must still apply the $GEM_PATH suggested by 'guix
> package --search-paths' in order for the gem that the executable belongs
> to (the gem that they explicitly installed) to be found.  I think this
> is reasonable and much like how you must set the proper load paths for
> Guile programs to work.

I can see that this won't be enough for any application whose Ruby gem
dependency tree is deeper than 1.  I suppose I could use the transitive
inputs of the package, which would get everything, but that feels a bit
ugly.  More thought needed...

-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] build: ruby: Patch executables to set necessary gem load path.
  2015-03-08  0:00 [PATCH] build: ruby: Patch executables to set necessary gem load path David Thompson
  2015-03-08  0:45 ` David Thompson
@ 2015-03-09 22:18 ` Ludovic Courtès
  1 sibling, 0 replies; 4+ messages in thread
From: Ludovic Courtès @ 2015-03-09 22:18 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson <dthompson2@worcester.edu> skribis:

> This patch addresses the issue of how to ensure that Ruby executables
> are able to load all of the additional Ruby libraries that they need in
> order to work: with a new 'patch-executables' build phase.  Instead of
> using wrap-program, I instead take advantage of the wrappers that the
> 'gem' command already creates for Ruby executables.  There's guaranteed
> to be a line in which the executable's host gem is loaded that looks
> like `gem 'foo', version`.  I simply insert a Ruby code snippet above it
> that adds all of the necessary gems to the 'Gem.path' array.

OK.

> Users of Ruby programs must still apply the $GEM_PATH suggested by 'guix
> package --search-paths' in order for the gem that the executable belongs
> to (the gem that they explicitly installed) to be found.  I think this
> is reasonable and much like how you must set the proper load paths for
> Guile programs to work.

Makes sense.

> From 614fedc2b359f123dfdf4e31eee30e7ce47e1bd2 Mon Sep 17 00:00:00 2001
> From: David Thompson <dthompson2@worcester.edu>
> 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.

[...]

> +(define* (patch-executables #:key inputs outputs #:allow-other-keys)

Please add a docstring, with the rationale you gave in this message as
part of the docstring or in a comment.

OK to push with this change, thank you!

Ludo’.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] build: ruby: Patch executables to set necessary gem load path.
  2015-03-08  0:45 ` David Thompson
@ 2015-03-09 22:19   ` Ludovic Courtès
  0 siblings, 0 replies; 4+ messages in thread
From: Ludovic Courtès @ 2015-03-09 22:19 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson <dthompson2@worcester.edu> skribis:

> David Thompson <dthompson2@worcester.edu> writes:
>
>> This patch addresses the issue of how to ensure that Ruby executables
>> are able to load all of the additional Ruby libraries that they need in
>> order to work: with a new 'patch-executables' build phase.  Instead of
>> using wrap-program, I instead take advantage of the wrappers that the
>> 'gem' command already creates for Ruby executables.  There's guaranteed
>> to be a line in which the executable's host gem is loaded that looks
>> like `gem 'foo', version`.  I simply insert a Ruby code snippet above it
>> that adds all of the necessary gems to the 'Gem.path' array.
>>
>> Users of Ruby programs must still apply the $GEM_PATH suggested by 'guix
>> package --search-paths' in order for the gem that the executable belongs
>> to (the gem that they explicitly installed) to be found.  I think this
>> is reasonable and much like how you must set the proper load paths for
>> Guile programs to work.
>
> I can see that this won't be enough for any application whose Ruby gem
> dependency tree is deeper than 1.  I suppose I could use the transitive
> inputs of the package, which would get everything, but that feels a bit
> ugly.  More thought needed...

I guess it could recurse into the DAG, possibly with
‘bag-transitive-inputs’ or some such?

Ludo’.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-03-09 22:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-08  0:00 [PATCH] build: ruby: Patch executables to set necessary gem load path David Thompson
2015-03-08  0:45 ` David Thompson
2015-03-09 22:19   ` Ludovic Courtès
2015-03-09 22:18 ` Ludovic Courtès

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).