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.
-
#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
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments
8 9 10 |
# File 'lib/gemx.rb', line 8 def arguments @arguments 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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/gemx.rb', line 66 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('-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 end opt_parse.parse!(args) if args.first && args.first.start_with?('-') abort(opt_parse.help) if args.empty? .executable = args.shift .gem_name ||= .executable .arguments = args end |
.run!(argv) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/gemx.rb', line 96 def self.run!(argv) parse!(argv).tap do || .install_if_needed .load! end end |
Instance Method Details
#activate! ⇒ Object
19 20 21 22 |
# File 'lib/gemx.rb', line 19 def activate! gem(gem_name, *requirements) Gem.finish_resolve end |
#dependency_to_s ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/gemx.rb', line 24 def dependency_to_s if requirements.none? gem_name else "#{gem_name} (#{requirements})" end end |
#install_if_needed ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/gemx.rb', line 11 def install_if_needed activate! rescue Gem::MissingSpecError warn "#{dependency_to_s} not available locally" if verbose install activate! end |
#load! ⇒ Object
32 33 34 35 36 37 38 39 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 |
# File 'lib/gemx.rb', line 32 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 |