Class: Noexec

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems-bundler/noexec.rb

Constant Summary collapse

DEBUG =
ENV['NOEXEC_DEBUG']
CURRENT =
Dir.pwd

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin) ⇒ Noexec

Returns a new instance of Noexec.



9
10
11
12
13
14
# File 'lib/rubygems-bundler/noexec.rb', line 9

def initialize(bin)
  log "Bin used: #{bin}"
  bin = bin.split(/ /)
  @bin = File.basename(bin[1]||bin[0])
  log "Bin calculated: #{@bin}"
end

Instance Attribute Details

#binObject (readonly)

Returns the value of attribute bin.



7
8
9
# File 'lib/rubygems-bundler/noexec.rb', line 7

def bin
  @bin
end

#gemfileObject (readonly)

Returns the value of attribute gemfile.



7
8
9
# File 'lib/rubygems-bundler/noexec.rb', line 7

def gemfile
  @gemfile
end

#rubygems_specsObject (readonly)

Returns the value of attribute rubygems_specs.



7
8
9
# File 'lib/rubygems-bundler/noexec.rb', line 7

def rubygems_specs
  @rubygems_specs
end

Instance Method Details

#candidate?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
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
65
66
67
# File 'lib/rubygems-bundler/noexec.rb', line 27

def candidate?
  log "Examining #{gemfile}"
  config_file = File.expand_path('../.noexec.yaml', gemfile)
  if File.exist?(config_file)
    log "Using config file at #{config_file}"
    require "yaml"
    config = YAML::load_file(config_file)
    unless config['include'].nil? ^ config['exclude'].nil?
      raise "You cannot have both an include and exclude section in your #{config_file.inspect}"
    end
    if config['include'] && config['include'].include?(bin)
      log "Binary included by config"
      return true
    elsif config['exclude'] && config['exclude'].include?(bin)
      log "Binary excluded by config"
      return false
    end
    log "Config based matching didn't find it, resorting to Gemfile lookup"
  end
  runtime = Bundler.load
  log2(){ "runtime specs: #{runtime.specs.map{|g| "#{g.name}-#{g.version}"}*" "}" }
  if rubygems_spec # that single gem
    missing_spec = runtime.
      instance_variable_get(:@definition).
      missing_specs.
      detect{|spec| spec.name == rubygems_spec.name}
    if missing_spec
      puts "\e[31mCould not find proper version of #{missing_spec.to_s} in any of the sources\e[0m"
      puts "\e[33mRun `bundle install` to install missing gems.\e[0m"
      exit Bundler::GemNotFound.new.status_code
    end
  end
  if runtime.specs.detect{ |spec| spec.executables.include?(bin) }
    return true
  end
  Bundler.unload!(rubygems_specs)
  false
rescue Bundler::BundlerError, Bundler::GemfileError => e
  warn "Ignoring candidate #{gemfile}:\n#{e}" if Noexec::DEBUG
  false
end

#checkObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rubygems-bundler/noexec.rb', line 108

def check
  if %w(bundle rubygems-bundler-uninstaller).include?(bin)
    log "Noexec - skipped binary: #{bin}"

  elsif ENV['NOEXEC_EXCLUDE'] && ENV['NOEXEC_EXCLUDE'].split(/ /).include?(bin)
    log "Noexec - ENV skipped binary: #{bin}"

  elsif ENV['BUNDLE_GEMFILE'] && ENV['BUNDLE_BIN_PATH'] && ENV['RUBYOPT']
    log "Noexec - already in 'bundle exec'"

  elsif %w(0 skip).include?( ENV['NOEXEC'] ) || ENV.key?('NOEXEC_DISABLE')
    #TODO: deprecated in 1.1.0, to be removed later -- 2012.09.05
    $stderr.puts "Warning, 'NOEXEC' environment variable is deprecated, switch to 'NOEXEC_DISABLE=1'." if ENV.key?('NOEXEC')
    log "Noexec - disabled with environment variable"

  else
    setup

  end
end

#log(msg) ⇒ Object



16
17
18
# File 'lib/rubygems-bundler/noexec.rb', line 16

def log(msg)
  puts msg if Noexec::DEBUG
end

#log2(msg = nil) ⇒ Object



20
21
22
23
24
25
# File 'lib/rubygems-bundler/noexec.rb', line 20

def log2(msg=nil)
  if Noexec::DEBUG == "2"
    msg=yield if msg.nil? && block_given?
    puts msg
  end
end

#old_specsObject



69
70
71
72
73
74
# File 'lib/rubygems-bundler/noexec.rb', line 69

def old_specs
  if Hash === rubygems_specs
  then @rubygems_specs.values
  else @rubygems_specs
  end
end

#rubygems_specObject



76
77
78
# File 'lib/rubygems-bundler/noexec.rb', line 76

def rubygems_spec
  @rubygems_spec ||= old_specs.detect{|spec| spec.executables.include?(bin) }
end

#setupObject



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
# File 'lib/rubygems-bundler/noexec.rb', line 80

def setup
  puts "Noexec - starting check" if Noexec::DEBUG
  require "bundler-unload"

  @rubygems_specs = Bundler.rubygems.plain_specs # save it for unloading and checking binary
  log2(){ "rubygems_specs: #{rubygems_specs.map{|g| "#{g.name}-#{g.version}"}*" "}" }

  @gemfile = ENV['BUNDLE_GEMFILE'] || File.join(Noexec::CURRENT, "Gemfile")
  initial_env_gemfile = ENV['BUNDLE_GEMFILE']

  while true
    ENV['BUNDLE_GEMFILE'] = gemfile
    if File.file?(gemfile) && candidate?
      log "Keeping #{gemfile} loaded"
      Bundler.setup
      return
    end
    new_gemfile = File.expand_path("../../Gemfile", gemfile)
    break if new_gemfile == gemfile
    @gemfile = new_gemfile
  end
  log "No valid Gemfile found, moving on"
  ENV['BUNDLE_GEMFILE'] = initial_env_gemfile
rescue LoadError
  warn "bundler not being used, unable to load" if Noexec::DEBUG
  ENV['BUNDLE_GEMFILE'] = initial_env_gemfile
end