vendor/gems/suitcase/lib/suitcase/zipper.rb in auser-poolparty-1.2.10 vs vendor/gems/suitcase/lib/suitcase/zipper.rb in auser-poolparty-1.2.11

- old
+ new

@@ -59,41 +59,41 @@ add(f, "#{namespace.empty? ? "" : "#{namespace}/"}#{::File.basename(::File.dirname(f))}") end end end end - - def self.gems(gem_list, gem_location) + + # TODO: MOVE + def self.gems(gem_list, o={}) require 'rubygems/dependency_installer' gem_list = [gem_list] unless gem_list.is_a?(Array) - ensure_location_exists gem_location + + gem_list.each do |g| + add find_gem(g, o), "gems" + end + end + + # Find the gem named named + # First, search in the :search_paths passed in via the options + # If it's not found there, then search in the locally installed gems + # and finally search online if the gem isn't available + def self.find_gem(named, o={}) + require 'rubygems/dependency_installer' - cache_dir = "#{gem_location}/cache" - ::FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir - - locally_installed_gems = Gem::SourceIndex.from_installed_gems.map {|n,s| s.name } + # Look for the gem in a path passed + found_path = search_path_for_gem_in_paths(named, o[:search_paths]) if o[:search_paths] + return found_path if found_path - locally_installable_gems = gem_list & locally_installed_gems - remotely_installable = gem_list - locally_installable_gems + found_path = find_gem_in_locally_installed_gems(named) + return found_path if found_path - # First, add the locally installed gems - locally_installable_gems.each do |spec| - spec = Gem::SourceIndex.from_installed_gems.find_name(spec).last#.sort_by {|a,b| a.version <=> b.version }.last - f = Gem.path.map {|gemdir| f = File.join(gemdir, 'cache', "#{spec.full_name}.gem"); f if File.exists?(f) }.compact.first - add(f, "gems") - end + gem_location = o[:temp_path] || "/tmp/gems" + ensure_location_exists(gem_location) - remotely_installable.each do |g| - di = Gem::DependencyInstaller.new - spec, url = di.find_spec_by_name_and_version(g).first - f = begin - Gem::RemoteFetcher.fetcher.download spec, "http://gems.github.com", gem_location - rescue Exception => e - Gem::RemoteFetcher.fetcher.download spec, url, gem_location - end - add(f, "gems") - end + found_path = find_gem_remotely_by_name_and_download_to(named, gem_location) + return found_path if found_path + nil end def self.packages(package_list, package_location="#{Dir.pwd}/packages") ensure_location_exists package_location package_list.each do |package| @@ -112,8 +112,56 @@ end def self.add_content_as(content="", filename="", namespace="files") items.merge!({"__p__string#{filename}_#{namespace}".to_sym => {:name => ::File.basename(filename), :content => content, :namespace => namespace}}) end + + def self.reset! + @items = nil + end + + private + # Search for the gem in a given path + def self.search_path_for_gem_in_paths(named, dirs) + fi = dirs.map do |d| + q = Dir["#{d}/*"].entries.detect {|fi|fi =~ /#{named}/} + q ? q : nil + end.compact.first + end + + # Search in the locally installed gems + def self.find_gem_in_locally_installed_gems(named) + locally_installed_gems_list = locally_installed_gems.map {|n,s| s.name } + + locally_installed_gems_list.detect do |g| + if g == named + spec = locally_installed_gems.find_name(g).last + if f = Dir[File.join(Gem.dir, 'cache', "#{spec.full_name}.gem")].first + return f + end + end + end + nil + end + + def self.locally_installed_gems + @locally_installed_gems ||= Gem::SourceIndex.from_installed_gems + end + + # Find the gem from online, first trying gems.github.com + # and then from rubyforge + def self.find_gem_remotely_by_name_and_download_to(named, to, o={}) + # Look for the gems from remote + di = Gem::DependencyInstaller.new + spec, url = di.find_spec_by_name_and_version(named).first + f = begin + Gem::RemoteFetcher.fetcher.download spec, "http://gems.github.com", to + rescue Exception => e + Gem::RemoteFetcher.fetcher.download spec, url, to + end + return f if f + nil + end + end end \ No newline at end of file