@@ -18,6 +18,14 @@ def initialize
1818 options [ :add ] = value
1919 end
2020
21+ add_option "--append SOURCE_URI" , "Append source (can be used multiple times)" do |value , options |
22+ options [ :append ] = value
23+ end
24+
25+ add_option "-p" , "--prepend SOURCE_URI" , "Prepend source (can be used multiple times)" do |value , options |
26+ options [ :prepend ] = value
27+ end
28+
2129 add_option "-l" , "--list" , "List sources" do |value , options |
2230 options [ :list ] = value
2331 end
@@ -26,8 +34,7 @@ def initialize
2634 options [ :remove ] = value
2735 end
2836
29- add_option "-c" , "--clear-all" ,
30- "Remove all sources (clear the cache)" do |value , options |
37+ add_option "-c" , "--clear-all" , "Remove all sources (clear the cache)" do |value , options |
3138 options [ :clear_all ] = value
3239 end
3340
@@ -68,6 +75,60 @@ def add_source(source_uri) # :nodoc:
6875 end
6976 end
7077
78+ def append_source ( source_uri ) # :nodoc:
79+ check_rubygems_https source_uri
80+
81+ source = Gem ::Source . new source_uri
82+
83+ check_typo_squatting ( source )
84+
85+ begin
86+ source . load_specs :released
87+ was_present = Gem . sources . include? ( source )
88+ Gem . sources . append source
89+ Gem . configuration . write
90+
91+ if was_present
92+ say "#{ source_uri } moved to end of sources"
93+ else
94+ say "#{ source_uri } added to sources"
95+ end
96+ rescue Gem ::URI ::Error , ArgumentError
97+ say "#{ source_uri } is not a URI"
98+ terminate_interaction 1
99+ rescue Gem ::RemoteFetcher ::FetchError => e
100+ say "Error fetching #{ Gem ::Uri . redact ( source . uri ) } :\n \t #{ e . message } "
101+ terminate_interaction 1
102+ end
103+ end
104+
105+ def prepend_source ( source_uri ) # :nodoc:
106+ check_rubygems_https source_uri
107+
108+ source = Gem ::Source . new source_uri
109+
110+ check_typo_squatting ( source )
111+
112+ begin
113+ source . load_specs :released
114+ was_present = Gem . sources . include? ( source )
115+ Gem . sources . prepend source
116+ Gem . configuration . write
117+
118+ if was_present
119+ say "#{ source_uri } moved to top of sources"
120+ else
121+ say "#{ source_uri } added to sources"
122+ end
123+ rescue Gem ::URI ::Error , ArgumentError
124+ say "#{ source_uri } is not a URI"
125+ terminate_interaction 1
126+ rescue Gem ::RemoteFetcher ::FetchError => e
127+ say "Error fetching #{ Gem ::Uri . redact ( source . uri ) } :\n \t #{ e . message } "
128+ terminate_interaction 1
129+ end
130+ end
131+
71132 def check_typo_squatting ( source )
72133 if source . typo_squatting? ( "rubygems.org" )
73134 question = <<-QUESTION . chomp
@@ -147,14 +208,20 @@ def description # :nodoc:
147208of them in your list. https://rubygems.org is recommended as it brings the
148209protections of an SSL connection to gem downloads.
149210
150- To add a source use the --add argument:
211+ To add a private gem source use the --prepend argument to insert it before
212+ the default source. This is usually the best place for private gem sources:
151213
152- $ gem sources --add https://my.private.source
214+ $ gem sources --prepend https://my.private.source
153215 https://my.private.source added to sources
154216
155217RubyGems will check to see if gems can be installed from the source given
156218before it is added.
157219
220+ To add or move a source after all other sources, use --append:
221+
222+ $ gem sources --append https://rubygems.org
223+ https://rubygems.org moved to end of sources
224+
158225To remove a source use the --remove argument:
159226
160227 $ gem sources --remove https://my.private.source/
@@ -182,6 +249,8 @@ def list # :nodoc:
182249
183250 def list? # :nodoc:
184251 !( options [ :add ] ||
252+ options [ :prepend ] ||
253+ options [ :append ] ||
185254 options [ :clear_all ] ||
186255 options [ :remove ] ||
187256 options [ :update ] )
@@ -190,11 +259,13 @@ def list? # :nodoc:
190259 def execute
191260 clear_all if options [ :clear_all ]
192261
193- source_uri = options [ :add ]
194- add_source source_uri if source_uri
262+ add_source options [ :add ] if options [ :add ]
263+
264+ prepend_source options [ :prepend ] if options [ :prepend ]
265+
266+ append_source options [ :append ] if options [ :append ]
195267
196- source_uri = options [ :remove ]
197- remove_source source_uri if source_uri
268+ remove_source options [ :remove ] if options [ :remove ]
198269
199270 update if options [ :update ]
200271
0 commit comments