Skip to content

Commit

Permalink
Adding and removing sources implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
copiousfreetime committed Sep 21, 2008
1 parent 0d8c581 commit 4893c7f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 23 deletions.
3 changes: 2 additions & 1 deletion data/stickler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ sources:
- http://gems.rubyforge.org

# default configuration values that exist in Gem::ConfigFile
# the configurations reply to the Gems subysystem, not to Stickler
bulk_threshold: 1000
verbose: true
verbose: false
update_sources: true
backtrace: false
15 changes: 12 additions & 3 deletions lib/stickler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,14 @@ def self.params_to_hash( params )
txt

argument( 'source_uri' ) { description "the source uri to add" }

mixin :option_directory
run { puts "Add source not implemented" }

run {
p = Stickler.params_to_hash( params )
repo = Stickler::Repository.new( p )
repo.add_source( p['source_uri'] )
}
end
end

Expand All @@ -170,7 +176,6 @@ def self.params_to_hash( params )

mixin :option_directory
argument( 'gem_name' ) { description "The gem to remove" }
run { puts "Remove gem not implemented" }
end

mode( :source ) do
Expand All @@ -186,7 +191,11 @@ def self.params_to_hash( params )
mixin :option_directory
argument( 'source_uri' ) { description "The source to remove" }

run { puts "Remove source not implemented" }
run {
p = Stickler.params_to_hash( params )
repo = Stickler::Repository.new( p )
repo.remove_source( p['source_uri'] )
}
end

end
Expand Down
93 changes: 74 additions & 19 deletions lib/stickler/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'stickler'
require 'fileutils'
require 'stickler/configuration'
require 'rubygems/source_info_cache'

module Stickler
#
Expand Down Expand Up @@ -41,15 +42,21 @@ def self.other_dir_names
end

def initialize( opts )

@directory = File.expand_path( opts['directory'] )
enhance_logging( opts ) if File.directory?( log_dir )
@overwrite = opts['force']

# this must be loaded early so it overrites the global Gem.configuration
@configuration_loaded = false
load_configuration if File.exist?( config_file )

end

def configuration_loaded?
@configuration_loaded
end

#
# should existing items be overwritten
#
Expand Down Expand Up @@ -88,6 +95,9 @@ def load_configuration
begin
@configuration = Configuration.new( config_file )
::Gem.configuration = @configuration
::Gem.sources.replace( @configuration.sources )
ENV['GEMCACHE'] = source_cache_dir
@configuration_loaded = true
rescue => e
logger.error "Failure to load configuration #{e}"
exit 1
Expand Down Expand Up @@ -131,6 +141,13 @@ def specification_dir
@specification_dir ||= File.join( directory, 'specifications' )
end

#
# The cache dir used by rubygems for this repository
#
def source_cache_dir
@source_cache_dir ||= File.join( directory, 'source_cache' )
end

#
# The Distribution directory
#
Expand Down Expand Up @@ -232,43 +249,81 @@ def setup
#
def info
return unless valid?
Stickler.tee "Configuration settings"
Stickler.tee "----------------------"
Stickler.tee "Stickler Information"
Stickler.tee "===================="
Stickler.tee ""
keys = configuration.keys
max_width = keys.collect { |k| k.length }.max

Stickler.tee " #{"downstream_source".rjust( max_width )} : #{configuration['downstream_source']}"
Stickler.tee " #{"sources".rjust( max_width )} : #{configuration.sources.first}"
Stickler.tee " Upstream Sources"
Stickler.tee " ----------------"
Stickler.tee ""

configuration.sources[1..-1].each do |source|
Stickler.tee " #{"".rjust( max_width )} #{source}"
max_width = configuration.sources.collect { |k| k.length }.max
configuration.sources.each do |url|
Stickler.tee " #{url.rjust( max_width )} : #{source_cache.cache_data[url].source_index.size} available"
end

Stickler.tee ""

keys = keys.sort - %w[ downstream_source sources ]
keys = configuration.keys
max_width = keys.collect { |k| k.length }.max

keys = keys.sort - %w[ sources ]

Stickler.tee " Configuration variables"
Stickler.tee " -----------------------"
Stickler.tee ""

keys.each do |key|
Stickler.tee " #{key.rjust( max_width )} : #{configuration[ key ]}"
end
end

def sources
unless @sources
s = {}
configuration.sources.each do |upstream_uri|
src = Source.new( upstream_uri )
@sources << Sources.new( upstream_uri )
end
end
return @sources
def source_cache
load_configuration unless configuration_loaded?
@source_cache ||= ::Gem::SourceInfoCache.cache
end

#
# Add a source to the repository
#
def add_source( source_uri )
sources << Source.new( source_uri )
load_configuration unless configuration_loaded?
begin
uri = ::URI.parse source_uri
::Gem::SpecFetcher.fetcher.load_specs uri, 'specs'
configuration.sources << source_uri
configuration.write
Gem.sources.replace configuration.sources
Stickler.tee "#{source_uri} added to sources"
rescue ::URI::Error
Stickler.tee "Error : #{source_uri} is not a URI"
rescue ::Gem::RemoteFetcher::FetchError => e
Stickler.tee "Error fetching #{source_uri}"
Stickler.tee "\t#{e.message}"
end
end

#
# Remove a source from the repository
#
def remove_source( source_uri )
load_configuation unless configuration_loaded?
begin
uri = ::URI.parse source_uri
if configuration.sources.delete( source_uri ) then
configuration.write
Gem.sources.replace configuration.sources
Stickler.tee "#{source_uri} removed from sources"
else
Stickler.tee "#{source_uri} is not one of your sources"
Stickler.tee "Your sources are:"
configuration.sources.each do |src|
Stickler.tee " #{src}"
end
end
rescue ::URI::Error
Stickler.tee "Error : #{source_uri} is not a URI"
end
end
end
end
7 changes: 7 additions & 0 deletions spec/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@
@repo.configuration.should == Gem.configuration
end

it "sets the global sources list for Gem" do
Stickler.silent {
@repo.add_source( "http://gems.collectiveintellect.com" )
}
Gem.sources.should == @repo.configuration.sources
end

end

0 comments on commit 4893c7f

Please sign in to comment.