Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed more path-related concepts from Environment

  • Loading branch information...
commit 75feca1491d90b036342c9cbcb252aa7558421fa 1 parent 028d412
Carlhuda authored
View
97 lib/bundler/bundle.rb
@@ -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 = {})
@@ -33,7 +79,6 @@ def install(options = {})
# TODO: clean this up
sources.each do |s|
- s.bundle = self
s.local = options[:cached]
end
@@ -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
@@ -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") }
@@ -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
@@ -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
@@ -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
@@ -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
@@ -262,9 +299,9 @@ 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
@@ -272,7 +309,7 @@ def configure(specs, options)
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)
View
3  lib/bundler/cli.rb
@@ -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
View
46 lib/bundler/dsl.rb
@@ -4,36 +4,14 @@ 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 = {}
@@ -41,15 +19,11 @@ def initialize(environment)
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
@@ -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
@@ -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?
@@ -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?
@@ -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]
View
39 lib/bundler/environment.rb
@@ -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 = []
@@ -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, '-')
@@ -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)
@@ -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
@@ -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
View
31 lib/bundler/source.rb
@@ -4,9 +4,12 @@ class GitSourceError < StandardError ; end
# Represents a source of rubygems. Initially, this is only gem repositories, but
# eventually, this will be git, svn, HTTP
class Source
- attr_accessor :bundle, :local
+ attr_accessor :local
+ attr_reader :bundle
- def initialize(options) ; end
+ def initialize(bundle, options)
+ @bundle = bundle
+ end
private
@@ -23,7 +26,8 @@ def process_source_gems(gems)
class GemSource < Source
attr_reader :uri
- def initialize(options)
+ def initialize(bundle, options)
+ super
@uri = options[:uri]
@uri = URI.parse(@uri) unless @uri.is_a?(URI)
raise ArgumentError, "The source must be an absolute URI" unless @uri.absolute?
@@ -50,7 +54,7 @@ class RubygemsRetardation < StandardError; end
def download(spec)
Bundler.logger.info "Downloading #{spec.full_name}.gem"
- destination = bundle.path
+ destination = bundle.gem_path
unless destination.writable?
raise RubygemsRetardation, "destination: #{destination} is not writable"
@@ -99,10 +103,15 @@ def fetch_prerelease_specs
class SystemGemSource < Source
def self.instance
- @instance ||= new({})
+ @instance
+ end
+
+ def self.new(*args)
+ @instance ||= super
end
- def initialize(options)
+ def initialize(bundle, options = {})
+ super
@source = Gem::SourceIndex.from_installed_gems
end
@@ -135,7 +144,8 @@ def download(spec)
class GemDirectorySource < Source
attr_reader :location
- def initialize(options)
+ def initialize(bundle, options)
+ super
@location = options[:location]
end
@@ -177,7 +187,8 @@ def fetch_specs
class DirectorySource < Source
attr_reader :location, :specs, :required_specs
- def initialize(options)
+ def initialize(bundle, options)
+ super
if options[:location]
@location = Pathname.new(options[:location]).expand_path
end
@@ -289,7 +300,7 @@ def download(spec)
class GitSource < DirectorySource
attr_reader :ref, :uri, :branch
- def initialize(options)
+ def initialize(bundle, options)
super
@uri = options[:uri]
@branch = options[:branch] || 'master'
@@ -298,7 +309,7 @@ def initialize(options)
def location
# TMP HAX to get the *.gemspec reading to work
- bundle.path.join('dirs', File.basename(@uri, '.git'))
+ bundle.gem_path.join('dirs', File.basename(@uri, '.git'))
end
def gems
View
3  spec/support/helpers.rb
@@ -40,8 +40,7 @@ def build_manifest(*args)
FileUtils.mkdir_p(path.dirname)
Dir.chdir(path.dirname) do
build_manifest_file(path, str)
- env = Bundler::Dsl.load_gemfile(path)
- Bundler::Bundle.new(env)
+ Bundler::Bundle.load(path)
end
end
View
2  spec/support/path_utils.rb
@@ -15,7 +15,7 @@ def bundled_app(*path)
end
def bundled_path
- @bundled_path ||= Bundler::Environment.default_gem_path(tmp_path.join('bundled_app'))
+ @bundled_path ||= Bundler::Bundle.default_gem_path(tmp_path.join('bundled_app'))
end
def tmp_gem_path(*path)
Please sign in to comment.
Something went wrong with that request. Please try again.