multi_ruby_runner 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 292bf31accd916a0752d0ec0092c16b409141735
4
+ data.tar.gz: b79c7dccb5560b865beb18e8ca516d421aaee77d
5
+ SHA512:
6
+ metadata.gz: d69b5cc1d575c513b435320672eb52a07d0caec396f536593b9ac9187dc83cea08580596e5b277b4996ad884628d8b7d272d7d8845f5a92cd992c21f686ec177
7
+ data.tar.gz: 27e0a0134144d19c3018cf3e11f61d39ad8df788d55e738369281c42d034a7eaeb1df5932704cc56f3e0e47770599762823fe51a7f5182bd90ee18a04ef4ce94
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .ruby-gemset
6
+ .ruby-version
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+
3
+ script: "bundle exec rake test"
4
+
5
+ rvm:
6
+ - 2.3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 1.0.0
2
+
3
+ Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_ruby_runner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Jo Hund
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Multi Ruby Runner
2
+
3
+ This gem lets you call Ruby code in one Ruby environment from Ruby code in another environment.
4
+
5
+ It makes use of Ruby version managers to switch the Ruby runtime environment. Typically you will write a ruby script that executes the code you want and call that script via MultiRubyRunner in the desired Ruby runtime environment.
6
+
7
+ Examples:
8
+
9
+ * Call JRuby code from MRI 2.3.0
10
+ * Call MRI 1.9.3 code from MRI 2.2.1
11
+
12
+ ## Requirements
13
+
14
+ ### Operating system
15
+
16
+ * Linux: Not tested, but should work.
17
+ * OS X: Tested and works.
18
+ * Windows: I don’t think it works, and I have no plans of supporting this scenario.
19
+
20
+ ### Ruby version manager
21
+
22
+ * rbenv: Works
23
+ * RVM: Works
24
+ * None: You really should use a Ruby version manager :-)
25
+
26
+ ### Ruby versions
27
+
28
+ Caller: The Ruby code that invokes other ruby code.
29
+ Callee: The Ruby code being invoked.
30
+
31
+ * MRI: Works for caller and callee.
32
+ * JRuby: Works for callee. May not work for caller as we use `fork` in a non-blocking use case.
33
+ * Rubinius: Not tested, however it should work.
34
+
35
+ ### Bundler
36
+
37
+ We have Bundler baked into the Gem via `Bundler.with_clean_env`. This is not necessary for overall functionality. I assume you’re using Bundler.
38
+
39
+ ## Installation
40
+
41
+ Add this line to your application's Gemfile:
42
+
43
+ gem 'multi_ruby_runner'
44
+
45
+ And then execute:
46
+
47
+ $ bundle
48
+
49
+ Or install it yourself as:
50
+
51
+ $ gem install multi_ruby_runner
52
+
53
+ ## Usage
54
+
55
+ ### Blocking (without fork)
56
+
57
+ If you want to execute a Ruby script and wait for its result via output to STDOUT, use this snippet:
58
+
59
+ mrr = MultiRubyRunner.new
60
+ stdout = mrr.execute_command_in_directory(
61
+ "./bin/ruby-script-to-execute argument1 argument2",
62
+ "/path/to/folder/that/sets/ruby/env"
63
+ )
64
+
65
+ Note that we’re passing `argument1` and `argument2` as positional arguments to the callee Ruby script.
66
+
67
+ ### Non blocking (with fork)
68
+
69
+ If you just want to start a process and return back to the caller right away, you can set the `:fork` option to true. In that case you will get the child process’ PID as return value. This is useful if you want to start a service and communicate with it e.g., via `Sockets`.
70
+
71
+ mrr = MultiRubyRunner.new
72
+ child_pid = mrr.execute_command_in_directory(
73
+ "./bin/ruby-script-to-execute argument1 argument2",
74
+ "/path/to/folder/that/sets/ruby/env"
75
+ )
76
+
77
+ You can communicate with the child process via pipes or sockets.
78
+
79
+ ### Ruby scripts to call
80
+
81
+ Here is an example Ruby script that can be called via MultiRubyRunner:
82
+
83
+ #!/usr/bin/env ruby
84
+
85
+ # This is an example callee script for MultiRubyRunner
86
+
87
+ require_relative '../lib/path/to/your/ruby/code'
88
+
89
+ # Check for arguments
90
+ arg1 = ARGV[0]
91
+ arg2 = ARGV[1]
92
+
93
+ # Puts will return some text to STDOUT
94
+ puts "The current time is #{ Time.now }"
95
+
96
+ Please note that even if you use JRuby, you keep `ruby` in the shebang. Your Ruby version manager will make sure that the code will be executed by JRuby.
97
+
98
+ ## Additional resources
99
+
100
+ I found Jesse Storimer’s books `Working With Unix Processes` and `Working With TCP Sockets` very helpful.
101
+
102
+ ## Contributing
103
+
104
+ 1. Fork it ( https://github.com/jhund/multi_ruby_runner/fork )
105
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
106
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
107
+ 4. Push to the branch (`git push origin my-new-feature`)
108
+ 5. Create a new Pull Request
109
+
110
+ ### Resources
111
+
112
+ * [Source code (github)](https://github.com/jhund/multi_ruby_runner)
113
+ * [Issues](https://github.com/jhund/multi_ruby_runner/issues)
114
+ * [Rubygems.org](http://rubygems.org/gems/multi_ruby_runner)
115
+
116
+ ### License
117
+
118
+ [MIT licensed](https://github.com/jhund/multi_ruby_runner/blob/master/LICENSE.txt).
119
+
120
+ ### Copyright
121
+
122
+ Copyright (c) 2016 Jo Hund. See [(MIT) LICENSE](https://github.com/jhund/multi_ruby_runner/blob/master/LICENSE.txt) for details.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require 'rake/testtask'
10
+ Rake::TestTask.new do |test|
11
+ test.libs << 'spec'
12
+ test.pattern = 'spec/**/*_spec.rb'
13
+ test.verbose = true
14
+ end
data/doc/meta.md ADDED
@@ -0,0 +1,19 @@
1
+ # Gem meta info
2
+
3
+ ## Release a new version
4
+
5
+ * Make and commit code changes.
6
+ * Update `CHANGELOG.md`.
7
+ * Update the gem version in `lib/multi_ruby_runner/version.rb`.
8
+ * Commit version bump and changelog with message ‘Bumped version to x.y.z’.
9
+ * Run `rake release`. This will perform the following steps:
10
+ * Build a gem package to e.g. pkg/multi_ruby_runner-1.0.1.gem.
11
+ * Push the `.gem` package to `Rubygems.org`
12
+ * Add and push a tag like “v1.0.1” to git.
13
+ * Push commits to the git remote.
14
+
15
+ ## Run tests
16
+
17
+ rake test
18
+
19
+ Also every push to github will trigger a test run at travis CI.
@@ -0,0 +1,3 @@
1
+ class MultiRubyRunner
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ class MultiRubyRunner
2
+ class VersionManager
3
+
4
+ # Represents a ruby environment without a version manager.
5
+ # In this case MultiRubyRunner won't work and will raise errors instead.
6
+ class None < VersionManager
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ class MultiRubyRunner
2
+ class VersionManager
3
+
4
+ # Represents the [rbenv](https://github.com/rbenv/rbenv) ruby version manager.
5
+ class Rbenv < VersionManager
6
+
7
+ # Returns the string to be executed in shell to activate self in a shell.
8
+ # @return [String]
9
+ def activation_string
10
+ %(eval "$(rbenv init -)")
11
+ end
12
+
13
+ # This is what `rbenv init -` returns:
14
+
15
+ # export PATH="/home/deploy/.rbenv/shims:${PATH}"
16
+ # export RBENV_SHELL=bash
17
+ # source '/home/deploy/.rbenv/libexec/../completions/rbenv.bash'
18
+ # command rbenv rehash 2>/dev/null
19
+ # rbenv() {
20
+ # local command
21
+ # command="$1"
22
+ # if [ "$#" -gt 0 ]; then
23
+ # shift
24
+ # fi
25
+
26
+ # case "$command" in
27
+ # rehash|shell)
28
+ # eval "$(rbenv "sh-$command" "$@")";;
29
+ # *)
30
+ # command rbenv "$command" "$@";;
31
+ # esac
32
+ # }
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ class MultiRubyRunner
2
+ class VersionManager
3
+
4
+ # Represents the [rvm](https://rvm.io/) ruby version manager.
5
+ class Rvm < VersionManager
6
+
7
+ # Returns the string to be executed in shell to activate self in a shell.
8
+ # @return [String]
9
+ def activation_string
10
+ # Given the @ruby_executable_path, we can compute the rvm script path
11
+ # that needs to be sourced:
12
+ # /Users/uname/.rvm/rubies/ruby-2.2.3/bin/ruby
13
+ # ~/.rvm/scripts/rvm
14
+ script_path = @ruby_executable_path.match(
15
+ /^.+(#{ Regexp.escape("/.rvm/") })(?=rubies)/
16
+ )[0]
17
+ if script_path.nil?
18
+ raise RuntimeError.new("Could not detect rvm script path! (#{ @ruby_executable_path.inspect }")
19
+ end
20
+ script_path << 'scripts/rvm'
21
+ # Rvm requires sourcing of script_path:
22
+ "source #{ script_path }"
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ class MultiRubyRunner
2
+
3
+ # Abstract class for ruby version managers like rbenv and rvm (or none)
4
+ class VersionManager
5
+
6
+ # Detects if and which ruby version manager is present.
7
+ def self.detect
8
+ which_ruby = `which ruby`
9
+ case which_ruby
10
+ when /\/\.rbenv\//
11
+ Rbenv.new(which_ruby)
12
+ when /\/\.rvm\//
13
+ Rvm.new(which_ruby)
14
+ else
15
+ None.new(which_ruby)
16
+ end
17
+ end
18
+
19
+ # Instantiates a new VersionManager.
20
+ # @param ruby_path [String] path to ruby executable, as returned by `which ruby`
21
+ def initialize(ruby_executable_path)
22
+ @ruby_executable_path = ruby_executable_path
23
+ end
24
+
25
+ def activation_string
26
+ raise "Implement #activation_string in subclasses!"
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,63 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "multi_ruby_runner/version"
4
+ require 'multi_ruby_runner'
5
+ require 'multi_ruby_runner/version_manager'
6
+ require 'multi_ruby_runner/version_manager/none'
7
+ require 'multi_ruby_runner/version_manager/rbenv'
8
+ require 'multi_ruby_runner/version_manager/rvm'
9
+
10
+ # Allows calling of Ruby code in various Ruby environments.
11
+ class MultiRubyRunner
12
+
13
+ def initialize
14
+ end
15
+
16
+ # Executes a blocking command in a directory.
17
+ # Command will be executed in the ruby environment specified in .ruby-version
18
+ # file in the directory.
19
+ # Example command:
20
+ # `/bin/bash -c 'source /Users/johund/.rvm/scripts/rvm; cd /Users/johund/development/vgr-table-export; ruby -v'`
21
+ # @param command_string [String] the command to run in directory
22
+ # @param directory [String] the dir containing the ".ruby-version" file and the ruby project to run
23
+ # @param options [Hash, optional]
24
+ # @option options [String, optional] shell_invocation what shell to use, defaults to bash
25
+ # @option options [Boolean, optional] blocking, defaults to true.
26
+ # @return [String] STDOUT output
27
+ def execute_command_in_directory(command_string, directory, options = {})
28
+ shell_path = ENV['SHELL'] || '/bin/bash'
29
+ options = {
30
+ blocking: true,
31
+ shell_invocation: "#{ shell_path } -c",
32
+ }.merge(options)
33
+ shell_command_string = [
34
+ ruby_version_manager.activation_string, # e.g., "source ~/.rvm/scripts/rvm"
35
+ "cd #{ directory }",
36
+ command_string
37
+ ].join('; ')
38
+ entire_command = "#{ options[:shell_invocation] } '#{ shell_command_string }'"
39
+
40
+ # Bundler.with_clean_env avoids spilling over of any bundler environment
41
+ # e.g., BUNDLE_BIN_PATH, BUNDLE_GEMFILE, and RUBYOPT
42
+ if options[:blocking]
43
+ # Wait for command to complete
44
+ Bundler.with_clean_env do
45
+ `#{ entire_command }`
46
+ end
47
+ else
48
+ # Fork new process, execute command there and return immediately to caller.
49
+ fork do
50
+ Bundler.with_clean_env do
51
+ `#{ entire_command }`
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ # Returns the ruby version manager used
58
+ # @return [MultiRubyRunner::VersionManager] 'rbenv', 'rvm', 'none'
59
+ def ruby_version_manager
60
+ VersionManager.detect
61
+ end
62
+
63
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_ruby_runner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_ruby_runner"
8
+ spec.version = MultiRubyRunner::VERSION
9
+ spec.authors = ["Jo Hund"]
10
+ spec.email = ["jhund@clearcove.ca"]
11
+ spec.summary = %q{Execute Ruby code in different Ruby environments.}
12
+ spec.description = %q{This gem lets you for example call JRuby code from MRI. It relies on rbenv or RVM to manage the Ruby runtime environment.}
13
+ spec.homepage = "https://github.com/jhund/multi_ruby_runner"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ class MultiRubyRunner
6
+ describe VersionManager do
7
+
8
+ it "responds to .detect" do
9
+ VersionManager.respond_to?(:detect).must_equal(true)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require_relative './spec_helper'
4
+
5
+ describe MultiRubyRunner do
6
+
7
+ it "responds to #execute_command_in_directory" do
8
+ mrr = MultiRubyRunner.new
9
+ mrr.respond_to?(:execute_command_in_directory).must_equal(true)
10
+ end
11
+
12
+ it "responds to #ruby_version_manager" do
13
+ mrr = MultiRubyRunner.new
14
+ mrr.respond_to?(:ruby_version_manager).must_equal(true)
15
+ end
16
+
17
+ end
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'bundler/setup'
4
+ require 'minitest/autorun'
5
+ require 'multi_ruby_runner'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_ruby_runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jo Hund
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This gem lets you for example call JRuby code from MRI. It relies on
56
+ rbenv or RVM to manage the Ruby runtime environment.
57
+ email:
58
+ - jhund@clearcove.ca
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - CHANGELOG.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - doc/meta.md
71
+ - lib/multi_ruby_runner.rb
72
+ - lib/multi_ruby_runner/version.rb
73
+ - lib/multi_ruby_runner/version_manager.rb
74
+ - lib/multi_ruby_runner/version_manager/none.rb
75
+ - lib/multi_ruby_runner/version_manager/rbenv.rb
76
+ - lib/multi_ruby_runner/version_manager/rvm.rb
77
+ - multi_ruby_runner.gemspec
78
+ - spec/multi_ruby_runner/version_manager_spec.rb
79
+ - spec/multi_ruby_runner_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/jhund/multi_ruby_runner
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Execute Ruby code in different Ruby environments.
105
+ test_files:
106
+ - spec/multi_ruby_runner/version_manager_spec.rb
107
+ - spec/multi_ruby_runner_spec.rb
108
+ - spec/spec_helper.rb