Skip to content

Commit

Permalink
use include/exclude lists for detection
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Mar 28, 2012
1 parent abc04d6 commit 24b17fa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ Then, in your .profile (or somewhere you can set env variables)

And you're done!

## Configuration

Though you can let noexec do it's own thing and rely on looking up your binary via your Gemfile, you can also specify which binaries you want included or excluded. Create a .noexec.yaml file along side any Gemfiles you want to use. Then, to enable (or disable) the usage of your particular binary into your bundle, add an include or exclude section. For example:

### .noexec.yaml

exclude: [rake]

Or,

include: [irb, ruby]

## How does this work?

It adds a script to every execution of ruby via the RUBYOPT environment variable. Then, when you run ruby, it takes a look at your working directory, and every directory above it until it can find a `Gemfile`. If the executable you're running is present in your Gemfile, it switches to using that `Gemfile` instead (via `Bundle.setup`).
Expand Down
31 changes: 25 additions & 6 deletions lib/noexec/auto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,46 @@ module Noexec

extend self

def log(msg)
puts msg if DEBUG
end

def candidate?(gemfile, bin)
config_file = File.expand_path('../.noexec.yaml', gemfile)
log "Considering #{config_file.inspect}"
if File.exist?(config_file)
log "Using config file at #{config_file}"
config = YAML::load_file(config_file)
raise "You cannot have both an include and exclude section in your #{config_file.inspect}" unless config['include'].nil? ^ config['exclude'].nil?
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
ENV['BUNDLE_GEMFILE'] = gemfile
Bundler.load.specs.each do |spec|
next if spec.name == 'bundler'
return spec if %w(ruby irb).include?(bin) || spec.executables.include?(bin)
return true if %w(ruby irb).include?(bin) || spec.executables.include?(bin)
end
nil
false
ensure
Bundler.reset!
end

def setup
puts "Noexec" if DEBUG
log "Noexec"
return if File.basename($0) == 'bundle'
return if ENV['BUNDLE_GEMFILE']
gemfile = File.join(CURRENT, "Gemfile")
while true
if File.exist?(gemfile)
puts "Examining #{gemfile}" if DEBUG
log "Examining #{gemfile}"
if Noexec.candidate?(gemfile, File.basename($0))
puts "Using #{gemfile}" if DEBUG
log "Using #{gemfile}"
ENV['BUNDLE_GEMFILE'] = gemfile
Bundler.setup
return
Expand All @@ -47,7 +66,7 @@ def setup
break if new_gemfile == gemfile
gemfile = new_gemfile
end
puts "No valid Gemfile found, moving on" if DEBUG
log "No valid Gemfile found, moving on"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/noexec/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Noexec
VERSION = "0.0.4"
VERSION = "0.1.0"
end

0 comments on commit 24b17fa

Please sign in to comment.