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

#conservativeObject

Returns the value of attribute conservative

Returns:

  • (Object)

    the current value of conservative



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

def conservative
  @conservative
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



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)
  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_tail('-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

    opts.on('--pre',
            'Allow resolving pre-release versions of the gem') do |_r|
      options.requirements.concat ['>= 0.a']
    end

    opts.on('-c', '--[no-]conservative',
            'Prefer the most recent installed version, ' \
            'rather than the latest version overall') do |c|
      options.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?

  options.executable = args.shift
  options.gem_name ||= options.executable
  options.arguments = args
  options.requirements.requirements.tap(&:uniq).delete(['>=', Gem::Version.new('0')])

  options
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_sObject



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_neededObject



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