OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / lib / rubygems / local_remote_options.rb
1 #--
2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3 # All rights reserved.
4 # See LICENSE.txt for permissions.
5 #++
6
7 require 'uri'
8 require 'rubygems'
9
10 ##
11 # Mixin methods for local and remote Gem::Command options.
12
13 module Gem::LocalRemoteOptions
14
15   ##
16   # Allows OptionParser to handle HTTP URIs.
17
18   def accept_uri_http
19     OptionParser.accept URI::HTTP do |value|
20       begin
21         uri = URI.parse value
22       rescue URI::InvalidURIError
23         raise OptionParser::InvalidArgument, value
24       end
25
26       raise OptionParser::InvalidArgument, value unless uri.scheme == 'http'
27
28       value
29     end
30   end
31
32   ##
33   # Add local/remote options to the command line parser.
34
35   def add_local_remote_options
36     add_option(:"Local/Remote", '-l', '--local',
37                'Restrict operations to the LOCAL domain') do |value, options|
38       options[:domain] = :local
39     end
40
41     add_option(:"Local/Remote", '-r', '--remote',
42       'Restrict operations to the REMOTE domain') do |value, options|
43       options[:domain] = :remote
44     end
45
46     add_option(:"Local/Remote", '-b', '--both',
47                'Allow LOCAL and REMOTE operations') do |value, options|
48       options[:domain] = :both
49     end
50
51     add_bulk_threshold_option
52     add_source_option
53     add_proxy_option
54     add_update_sources_option
55   end
56
57   ##
58   # Add the --bulk-threshold option
59
60   def add_bulk_threshold_option
61     add_option(:"Local/Remote", '-B', '--bulk-threshold COUNT',
62                "Threshold for switching to bulk",
63                "synchronization (default #{Gem.configuration.bulk_threshold})") do
64       |value, options|
65       Gem.configuration.bulk_threshold = value.to_i
66     end
67   end
68
69   ##
70   # Add the --http-proxy option
71
72   def add_proxy_option
73     accept_uri_http
74
75     add_option(:"Local/Remote", '-p', '--[no-]http-proxy [URL]', URI::HTTP,
76                'Use HTTP proxy for remote operations') do |value, options|
77       options[:http_proxy] = (value == false) ? :no_proxy : value
78       Gem.configuration[:http_proxy] = options[:http_proxy]
79     end
80   end
81
82   ##
83   # Add the --source option
84
85   def add_source_option
86     accept_uri_http
87
88     add_option(:"Local/Remote", '--source URL', URI::HTTP,
89                'Use URL as the remote source for gems') do |source, options|
90       source << '/' if source !~ /\/\z/
91
92       if options[:added_source] then
93         Gem.sources << source
94       else
95         options[:added_source] = true
96         Gem.sources.replace [source]
97       end
98     end
99   end
100
101   ##
102   # Add the --update-source option
103
104   def add_update_sources_option
105
106     add_option(:"Local/Remote", '-u', '--[no-]update-sources',
107                'Update local source cache') do |value, options|
108       Gem.configuration.update_sources = value
109     end
110   end
111
112   ##
113   # Is fetching of local and remote information enabled?
114
115   def both?
116     options[:domain] == :both
117   end
118
119   ##
120   # Is local fetching enabled?
121
122   def local?
123     options[:domain] == :local || options[:domain] == :both
124   end
125
126   ##
127   # Is remote fetching enabled?
128
129   def remote?
130     options[:domain] == :remote || options[:domain] == :both
131   end
132
133 end
134