Skip to content

Commit

Permalink
Removed more path-related concepts from Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlhuda committed Jan 4, 2010
1 parent 028d412 commit 75feca1
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 107 deletions.
97 changes: 67 additions & 30 deletions lib/bundler/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,69 @@ module Bundler
class InvalidRepository < StandardError ; end

class Bundle
attr_reader :path, :environment
attr_reader :gemfile, :environment

def initialize(env)
path = env.gem_path
bindir = env.bindir
def self.load(gemfile = nil)
gemfile = Pathname.new(gemfile || default_gemfile).expand_path

FileUtils.mkdir_p(path)
unless gemfile.file?
raise ManifestFileNotFound, "Manifest file not found: #{gemfile.to_s.inspect}"
end

new(gemfile)
end

def self.default_gemfile
current = Pathname.new(Dir.pwd)

until current.root?
filename = current.join("Gemfile")
return filename if filename.exist?
current = current.parent
end

raise DefaultManifestNotFound
end

def self.default_gem_path(root)
root.join("vendor/gems/#{Gem.ruby_engine}/#{Gem::ConfigMap[:ruby_version]}")
end

# TODO: passing in the filename is not good
def initialize(gemfile)
@gemfile = gemfile
@environment = Environment.new(self)
Dsl.evaluate(gemfile, self, @environment)

@environment = env
@path = Pathname.new(path)
@bindir = Pathname.new(bindir)
# path = env.gem_path

@cache_path = @path.join('cache')
@cache = GemDirectorySource.new(:location => @cache_path)
FileUtils.mkdir_p(gem_path)

@cache_path = gem_path.join('cache')
@cache = GemDirectorySource.new(self, :location => @cache_path)

@specs_path = gem_path.join('specifications')
@gems_path = gem_path.join('gems')
end

@specs_path = @path.join('specifications')
@gems_path = @path.join('gems')
def root
gemfile.parent
end

def gem_path
@gem_path ||= self.class.default_gem_path(root)
end

def gem_path=(path)
@gem_path = (path.relative? ? root.join(path) : path).expand_path
end

def bindir
@bindir ||= root.join("bin")
end

def bindir=(path)
@bindir = (path.relative? ? root.join(path) : path).expand_path
end

def install(options = {})
Expand All @@ -33,7 +79,6 @@ def install(options = {})

# TODO: clean this up
sources.each do |s|
s.bundle = self
s.local = options[:cached]
end

Expand Down Expand Up @@ -88,7 +133,6 @@ def prune(options = {})
dependencies, sources = @environment.gem_dependencies, @environment.sources

sources.each do |s|
s.bundle = self
s.local = true
end

Expand All @@ -115,11 +159,6 @@ def gems
source_index.gems.values
end

# TODO: Refactor this
def gem_path
@environment.gem_path
end

def source_index
index = Gem::SourceIndex.from_gems_in(@specs_path)
index.each { |n, spec| spec.loaded_from = @specs_path.join("#{spec.full_name}.gemspec") }
Expand All @@ -131,13 +170,11 @@ def download_path_for(type)
end

def setup_environment
gem_path = @environment.gem_path

unless @environment.system_gems
ENV["GEM_HOME"] = gem_path
ENV["GEM_PATH"] = gem_path
end
ENV["PATH"] = "#{@environment.bindir}:#{ENV["PATH"]}"
ENV["PATH"] = "#{bindir}:#{ENV["PATH"]}"
ENV["RUBYOPT"] = "-r#{gem_path}/environment #{ENV["RUBYOPT"]}"
end

Expand Down Expand Up @@ -174,8 +211,8 @@ def generate_bins(bundle, options)
bundle.each do |spec|
next if spec.no_bundle?
# HAX -- Generate the bin
bin_dir = @bindir
path = @path
bin_dir = bindir
path = gem_path
gems_path = @gems_path
installer = Gem::Installer.allocate
installer.instance_eval do
Expand All @@ -201,11 +238,11 @@ def expand_gemfile(spec, options)
end

installer = Gem::Installer.new(gemfile, options.merge(
:install_dir => @path,
:install_dir => gem_path,
:ignore_dependencies => true,
:env_shebang => true,
:wrappers => true,
:bin_dir => @bindir
:bin_dir => bindir
))
installer.install
rescue Gem::InstallError
Expand Down Expand Up @@ -245,7 +282,7 @@ def cleanup(valid, options)
spec.executables.each do |bin|
next if valid_executables.include?(bin)
Bundler.logger.info "Deleting bin file: #{bin}"
FileUtils.rm_rf(@bindir.join(bin))
FileUtils.rm_rf(bindir.join(bin))
end
end
end
Expand All @@ -262,17 +299,17 @@ def expand(options)
end

def configure(specs, options)
FileUtils.mkdir_p(path)
FileUtils.mkdir_p(gem_path)

File.open(path.join("environment.rb"), "w") do |file|
File.open(gem_path.join("environment.rb"), "w") do |file|
file.puts @environment.environment_rb(specs, options)
end

generate_environment_picker
end

def generate_environment_picker
FileUtils.cp("#{File.dirname(__FILE__)}/templates/environment_picker.erb", path.join("../../environment.rb"))
FileUtils.cp("#{File.dirname(__FILE__)}/templates/environment_picker.erb", gem_path.join("../../environment.rb"))
end

def require_code(file, dep)
Expand Down
3 changes: 1 addition & 2 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def self.run(command, options = {})

def initialize(options)
@options = options
environment = Dsl.load_gemfile(@options[:manifest])
@bundle = Bundle.new(environment)
@bundle = Bundle.load(@options[:manifest])
end

def bundle
Expand Down
46 changes: 10 additions & 36 deletions lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,26 @@ class InvalidKey < StandardError; end
class DefaultManifestNotFound < StandardError; end

class Dsl
def self.load_gemfile(file)
gemfile = Pathname.new(file || default_gemfile).expand_path

unless gemfile.file?
raise ManifestFileNotFound, "Manifest file not found: #{gemfile.to_s.inspect}"
end

evaluate(gemfile)
end

def self.default_gemfile
current = Pathname.new(Dir.pwd)

until current.root?
filename = current.join("Gemfile")
return filename if filename.exist?
current = current.parent
end

raise DefaultManifestNotFound
end

def self.evaluate(file)
environment = Environment.new(file)
builder = new(environment)
def self.evaluate(file, bundle, environment)
builder = new(bundle, environment)
builder.instance_eval(File.read(file.to_s), file.to_s, 1)
environment
end

def initialize(environment)
def initialize(bundle, environment)
@bundle = bundle
@environment = environment
@directory_sources = []
@git_sources = {}
@only, @except, @directory, @git = nil, nil, nil, nil
end

def bundle_path(path)
path = Pathname.new(path)
@environment.gem_path = (path.relative? ?
@environment.root.join(path) : path).expand_path
@bundle.gem_path = Pathname.new(path)
end

def bin_path(path)
path = Pathname.new(path)
@environment.bindir = (path.relative? ?
@environment.root.join(path) : path).expand_path
@bundle.bindir = Pathname.new(path)
end

def disable_rubygems
Expand All @@ -61,7 +35,7 @@ def disable_system_gems
end

def source(source)
source = GemSource.new(:uri => source)
source = GemSource.new(@bundle, :uri => source)
unless @environment.sources.include?(source)
@environment.add_source(source)
end
Expand All @@ -81,7 +55,7 @@ def except(*env)

def directory(path, options = {})
raise DirectorySourceError, "cannot nest calls to directory or git" if @directory || @git
@directory = DirectorySource.new(options.merge(:location => path))
@directory = DirectorySource.new(@bundle, options.merge(:location => path))
@directory_sources << @directory
@environment.add_priority_source(@directory)
retval = yield if block_given?
Expand All @@ -91,7 +65,7 @@ def directory(path, options = {})

def git(uri, options = {})
raise DirectorySourceError, "cannot nest calls to directory or git" if @directory || @git
@git = GitSource.new(options.merge(:uri => uri))
@git = GitSource.new(@bundle, options.merge(:uri => uri))
@git_sources[uri] = @git
@environment.add_priority_source(@git)
retval = yield if block_given?
Expand Down Expand Up @@ -123,7 +97,7 @@ def gem(name, *args)
dep = Dependency.new(name, options.merge(:version => version))

if options.key?(:bundle) && !options[:bundle]
dep.source = SystemGemSource.instance
dep.source = SystemGemSource.new(@bundle)
elsif @git || options[:git]
dep.source = _handle_git_option(name, version, options)
elsif @directory || options[:path]
Expand Down
39 changes: 13 additions & 26 deletions lib/bundler/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ class InvalidCacheArgument < StandardError; end
class SourceNotCached < StandardError; end

class Environment
attr_reader :filename, :dependencies
attr_reader :dependencies
attr_accessor :rubygems, :system_gems
attr_writer :gem_path, :bindir

def self.default_gem_path(root)
Pathname.new("#{root}/vendor/gems/#{Gem.ruby_engine}/#{Gem::ConfigMap[:ruby_version]}")
end

def initialize(filename)
@filename = filename
def initialize(bundle)
@bundle = bundle # TODO: remove this
@default_sources = default_sources
@sources = []
@priority_sources = []
Expand All @@ -23,10 +18,14 @@ def initialize(filename)
@system_gems = true
end

def filename
@bundle.gemfile
end

def environment_rb(specs, options)
load_paths = load_paths_for_specs(specs, options)
bindir = @bindir.relative_path_from(gem_path).to_s
filename = self.filename.relative_path_from(gem_path).to_s
bindir = @bundle.bindir.relative_path_from(@bundle.gem_path).to_s
filename = self.filename.relative_path_from(@bundle.gem_path).to_s

template = File.read(File.join(File.dirname(__FILE__), "templates", "environment.erb"))
erb = ERB.new(template, nil, '-')
Expand All @@ -37,20 +36,8 @@ def require_env(env = nil)
dependencies.each { |d| d.require_env(env) }
end

def root
filename.parent
end

def gem_path
@gem_path ||= self.class.default_gem_path(root)
end

def bindir
@bindir ||= root.join("bin")
end

def sources
@priority_sources + @sources + @default_sources + [SystemGemSource.instance]
@priority_sources + @sources + @default_sources + [SystemGemSource.new(@bundle)]
end

def add_source(source)
Expand All @@ -76,7 +63,7 @@ def gem_dependencies
private

def default_sources
[GemSource.new(:uri => "http://gems.rubyforge.org")]
[GemSource.new(@bundle, :uri => "http://gems.rubyforge.org")]
end

def repository
Expand All @@ -98,11 +85,11 @@ def load_paths_for_specs(specs, options)
end

def load_path_for(gem_path, path)
gem_path.join(path).relative_path_from(self.gem_path).to_s
gem_path.join(path).relative_path_from(@bundle.gem_path).to_s
end

def spec_file_for(spec)
spec.loaded_from.relative_path_from(self.gem_path).to_s
spec.loaded_from.relative_path_from(@bundle.gem_path).to_s
end
end
end
Loading

0 comments on commit 75feca1

Please sign in to comment.