@@ -18,6 +18,14 @@ def initialize
18
18
options [ :add ] = value
19
19
end
20
20
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
+
21
29
add_option "-l" , "--list" , "List sources" do |value , options |
22
30
options [ :list ] = value
23
31
end
@@ -26,8 +34,7 @@ def initialize
26
34
options [ :remove ] = value
27
35
end
28
36
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 |
31
38
options [ :clear_all ] = value
32
39
end
33
40
@@ -68,6 +75,60 @@ def add_source(source_uri) # :nodoc:
68
75
end
69
76
end
70
77
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
+
71
132
def check_typo_squatting ( source )
72
133
if source . typo_squatting? ( "rubygems.org" )
73
134
question = <<-QUESTION . chomp
@@ -147,14 +208,20 @@ def description # :nodoc:
147
208
of them in your list. https://rubygems.org is recommended as it brings the
148
209
protections of an SSL connection to gem downloads.
149
210
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:
151
213
152
- $ gem sources --add https://my.private.source
214
+ $ gem sources --prepend https://my.private.source
153
215
https://my.private.source added to sources
154
216
155
217
RubyGems will check to see if gems can be installed from the source given
156
218
before it is added.
157
219
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
+
158
225
To remove a source use the --remove argument:
159
226
160
227
$ gem sources --remove https://my.private.source/
@@ -182,6 +249,8 @@ def list # :nodoc:
182
249
183
250
def list? # :nodoc:
184
251
!( options [ :add ] ||
252
+ options [ :prepend ] ||
253
+ options [ :append ] ||
185
254
options [ :clear_all ] ||
186
255
options [ :remove ] ||
187
256
options [ :update ] )
@@ -190,11 +259,13 @@ def list? # :nodoc:
190
259
def execute
191
260
clear_all if options [ :clear_all ]
192
261
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 ]
195
267
196
- source_uri = options [ :remove ]
197
- remove_source source_uri if source_uri
268
+ remove_source options [ :remove ] if options [ :remove ]
198
269
199
270
update if options [ :update ]
200
271
0 commit comments