Class: GemX::X

Inherits:
Struct
  • Object
show all
Defined in:
lib/gemx.rb,
lib/gemx.rb

Overview

The eXecutable part of this gem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments

Returns:

  • (Object)

    the current value of arguments



8
9
10
# File 'lib/gemx.rb', line 8

def arguments
  @arguments
end

#executableObject

Returns the value of attribute executable

Returns:

  • (Object)

    the current value of executable



8
9
10
# File 'lib/gemx.rb', line 8

def executable
  @executable
end

#gem_nameObject

Returns the value of attribute gem_name

Returns:

  • (Object)

    the current value of gem_name



8
9
10
# File 'lib/gemx.rb', line 8

def gem_name
  @gem_name
end

#requirementsObject

Returns the value of attribute requirements

Returns:

  • (Object)

    the current value of requirements



8
9
10
# File 'lib/gemx.rb', line 8

def requirements
  @requirements
end

#verboseObject

Returns the value of attribute verbose

Returns:

  • (Object)

    the current value of 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)
  options = new
  options.requirements = Gem::Requirement.new
  opt_parse = OptionParser.new do |opts|
    opts.program_name = 'gemx'
    opts.version = VERSION
    opts.banner = 'Usage: gemx [options --] command'

    opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
      options.verbose = v
    end

    opts.on('-g', '--gem=GEM',
            'Run the executable from the given gem') do |g|
      options.gem_name = g
    end

    opts.on('-r', '--requirement REQ',
            'Run the gem with the given requirement') do |r|
      options.requirements.concat [r]
    end
  end
  opt_parse.parse!(args) if args.first && args.first.start_with?('-')
  abort(opt_parse.help) if args.empty?
  options.executable = args.shift
  options.gem_name ||= options.executable
  options.arguments = args
  options
end

.run!(argv) ⇒ Object



96
97
98
99
100
101
# File 'lib/gemx.rb', line 96

def self.run!(argv)
  parse!(argv).tap do |options|
    options.install_if_needed
    options.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_sObject



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_neededObject



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