Class: GemX::X
- Inherits:
-
Struct
- Object
- Struct
- GemX::X
- Defined in:
- lib/gemx.rb,
lib/gemx.rb
Overview
The eXecutable part of this gem
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#conservative ⇒ Object
Returns the value of attribute conservative.
-
#executable ⇒ Object
Returns the value of attribute executable.
-
#gem_name ⇒ Object
Returns the value of attribute gem_name.
-
#requirements ⇒ Object
Returns the value of attribute requirements.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #activate! ⇒ Object
- #dependency_to_s ⇒ Object
- #install_if_needed ⇒ Object
- #load! ⇒ Object
- #run! ⇒ Object
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments
8 9 10 |
# File 'lib/gemx.rb', line 8 def arguments @arguments end |
#conservative ⇒ Object
Returns the value of attribute conservative
8 9 10 |
# File 'lib/gemx.rb', line 8 def conservative @conservative end |
#executable ⇒ Object
Returns the value of attribute executable
8 9 10 |
# File 'lib/gemx.rb', line 8 def executable @executable end |
#gem_name ⇒ Object
Returns the value of attribute gem_name
8 9 10 |
# File 'lib/gemx.rb', line 8 def gem_name @gem_name end |
#requirements ⇒ Object
Returns the value of attribute requirements
8 9 10 |
# File 'lib/gemx.rb', line 8 def requirements @requirements end |
#verbose ⇒ Object
Returns the value of attribute verbose
8 9 10 |
# File 'lib/gemx.rb', line 8 def verbose @verbose end |
Class Method Details
.parse!(args) ⇒ Object
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 |
# File 'lib/gemx.rb', line 74 def self.parse!(args) = new .requirements = Gem::Requirement.new opt_parse = OptionParser.new do |opts| opts.program_name = 'gemx' opts.version = VERSION opts. = 'Usage: gemx [options --] command' opts.on_tail('-v', '--[no-]verbose', 'Run verbosely') do |v| .verbose = v end opts.on('-g', '--gem=GEM', 'Run the executable from the given gem') do |g| .gem_name = g end opts.on('-r', '--requirement REQ', 'Run the gem with the given requirement') do |r| .requirements.concat [r] end opts.on('--pre', 'Allow resolving pre-release versions of the gem') do |_r| .requirements.concat ['>= 0.a'] end opts.on('-c', '--[no-]conservative', 'Prefer the most recent installed version, ' \ 'rather than the latest version overall') do |c| .conservative = c end end opt_parse.order!(args) do |v| # put the non-option back at the front of the list of arguments args.unshift(v) # stop parsing once we hit the first non-option, # so you can call `gemx rails --version` and it prints the rails # version rather than gemx's break end abort(opt_parse.help) if args.empty? .executable = args.shift .gem_name ||= .executable .arguments = args .requirements.requirements.tap(&:uniq).delete(['>=', Gem::Version.new('0')]) end |
.run!(argv) ⇒ Object
128 129 130 |
# File 'lib/gemx.rb', line 128 def self.run!(argv) parse!(argv).run! end |
Instance Method Details
#activate! ⇒ Object
27 28 29 30 |
# File 'lib/gemx.rb', line 27 def activate! gem(gem_name, *requirements) Gem.finish_resolve end |
#dependency_to_s ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/gemx.rb', line 32 def dependency_to_s if requirements.none? gem_name else "#{gem_name} (#{requirements})" end end |
#install_if_needed ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/gemx.rb', line 19 def install_if_needed activate! rescue Gem::MissingSpecError warn "#{dependency_to_s} not available locally" if verbose install activate! end |
#load! ⇒ Object
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 |
# File 'lib/gemx.rb', line 40 def load! argv = ARGV.clone ARGV.replace arguments exe = executable contains_executable = Gem.loaded_specs.values.select do |spec| spec.executables.include?(executable) end if contains_executable.any? { |s| s.name == executable } contains_executable.select! { |s| s.name == executable } end if contains_executable.empty? if (spec = Gem.loaded_specs[executable]) && (exe = spec.executable) contains_executable << spec else abort "Failed to load executable #{executable}," \ " are you sure the gem #{gem_name} contains it?" end end if contains_executable.size > 1 abort "Ambiguous which gem `#{executable}` should come from: " \ "the options are #{contains_executable.map(&:name)}, " \ 'specify one via `-g`' end load Gem.activate_bin_path(contains_executable.first.name, exe, '>= 0.a') ensure ARGV.replace argv end |
#run! ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/gemx.rb', line 132 def run! print_command if verbose if conservative install_if_needed else install activate! end load! end |