rkh-bundler 1.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/.gitignore +22 -0
  2. data/.travis.yml +42 -0
  3. data/CHANGELOG.md +1105 -0
  4. data/ISSUES.md +67 -0
  5. data/LICENSE +23 -0
  6. data/README.md +31 -0
  7. data/Rakefile +208 -0
  8. data/UPGRADING.md +103 -0
  9. data/bin/bundle +31 -0
  10. data/bundler.gemspec +31 -0
  11. data/lib/bundler.rb +353 -0
  12. data/lib/bundler/capistrano.rb +11 -0
  13. data/lib/bundler/cli.rb +693 -0
  14. data/lib/bundler/definition.rb +568 -0
  15. data/lib/bundler/dep_proxy.rb +43 -0
  16. data/lib/bundler/dependency.rb +134 -0
  17. data/lib/bundler/deployment.rb +58 -0
  18. data/lib/bundler/dsl.rb +256 -0
  19. data/lib/bundler/endpoint_specification.rb +78 -0
  20. data/lib/bundler/environment.rb +47 -0
  21. data/lib/bundler/fetcher.rb +225 -0
  22. data/lib/bundler/gem_helper.rb +162 -0
  23. data/lib/bundler/gem_helpers.rb +23 -0
  24. data/lib/bundler/gem_installer.rb +9 -0
  25. data/lib/bundler/gem_tasks.rb +2 -0
  26. data/lib/bundler/graph.rb +148 -0
  27. data/lib/bundler/index.rb +187 -0
  28. data/lib/bundler/installer.rb +190 -0
  29. data/lib/bundler/lazy_specification.rb +79 -0
  30. data/lib/bundler/lockfile_parser.rb +127 -0
  31. data/lib/bundler/match_platform.rb +13 -0
  32. data/lib/bundler/psyched_yaml.rb +15 -0
  33. data/lib/bundler/remote_specification.rb +57 -0
  34. data/lib/bundler/resolver.rb +486 -0
  35. data/lib/bundler/ruby_version.rb +94 -0
  36. data/lib/bundler/rubygems_ext.rb +153 -0
  37. data/lib/bundler/rubygems_integration.rb +394 -0
  38. data/lib/bundler/runtime.rb +233 -0
  39. data/lib/bundler/settings.rb +128 -0
  40. data/lib/bundler/setup.rb +23 -0
  41. data/lib/bundler/shared_helpers.rb +71 -0
  42. data/lib/bundler/source.rb +869 -0
  43. data/lib/bundler/spec_set.rb +137 -0
  44. data/lib/bundler/templates/Executable +16 -0
  45. data/lib/bundler/templates/Executable.standalone +12 -0
  46. data/lib/bundler/templates/Gemfile +4 -0
  47. data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
  48. data/lib/bundler/templates/newgem/LICENSE.tt +22 -0
  49. data/lib/bundler/templates/newgem/README.md.tt +29 -0
  50. data/lib/bundler/templates/newgem/Rakefile.tt +2 -0
  51. data/lib/bundler/templates/newgem/bin/newgem.tt +3 -0
  52. data/lib/bundler/templates/newgem/gitignore.tt +17 -0
  53. data/lib/bundler/templates/newgem/lib/newgem.rb.tt +9 -0
  54. data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
  55. data/lib/bundler/templates/newgem/newgem.gemspec.tt +17 -0
  56. data/lib/bundler/ui.rb +88 -0
  57. data/lib/bundler/vendor/net/http/faster.rb +27 -0
  58. data/lib/bundler/vendor/net/http/persistent.rb +468 -0
  59. data/lib/bundler/vendor/thor.rb +358 -0
  60. data/lib/bundler/vendor/thor/actions.rb +314 -0
  61. data/lib/bundler/vendor/thor/actions/create_file.rb +105 -0
  62. data/lib/bundler/vendor/thor/actions/create_link.rb +57 -0
  63. data/lib/bundler/vendor/thor/actions/directory.rb +93 -0
  64. data/lib/bundler/vendor/thor/actions/empty_directory.rb +134 -0
  65. data/lib/bundler/vendor/thor/actions/file_manipulation.rb +270 -0
  66. data/lib/bundler/vendor/thor/actions/inject_into_file.rb +109 -0
  67. data/lib/bundler/vendor/thor/base.rb +576 -0
  68. data/lib/bundler/vendor/thor/core_ext/file_binary_read.rb +9 -0
  69. data/lib/bundler/vendor/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  70. data/lib/bundler/vendor/thor/core_ext/ordered_hash.rb +100 -0
  71. data/lib/bundler/vendor/thor/error.rb +30 -0
  72. data/lib/bundler/vendor/thor/group.rb +273 -0
  73. data/lib/bundler/vendor/thor/invocation.rb +168 -0
  74. data/lib/bundler/vendor/thor/parser.rb +4 -0
  75. data/lib/bundler/vendor/thor/parser/argument.rb +67 -0
  76. data/lib/bundler/vendor/thor/parser/arguments.rb +161 -0
  77. data/lib/bundler/vendor/thor/parser/option.rb +120 -0
  78. data/lib/bundler/vendor/thor/parser/options.rb +172 -0
  79. data/lib/bundler/vendor/thor/rake_compat.rb +66 -0
  80. data/lib/bundler/vendor/thor/runner.rb +309 -0
  81. data/lib/bundler/vendor/thor/shell.rb +88 -0
  82. data/lib/bundler/vendor/thor/shell/basic.rb +302 -0
  83. data/lib/bundler/vendor/thor/shell/color.rb +108 -0
  84. data/lib/bundler/vendor/thor/shell/html.rb +121 -0
  85. data/lib/bundler/vendor/thor/task.rb +113 -0
  86. data/lib/bundler/vendor/thor/util.rb +229 -0
  87. data/lib/bundler/vendor/thor/version.rb +3 -0
  88. data/lib/bundler/vendored_thor.rb +7 -0
  89. data/lib/bundler/version.rb +6 -0
  90. data/lib/bundler/vlad.rb +11 -0
  91. data/man/bundle-config.ronn +130 -0
  92. data/man/bundle-exec.ronn +111 -0
  93. data/man/bundle-install.ronn +335 -0
  94. data/man/bundle-package.ronn +59 -0
  95. data/man/bundle-update.ronn +176 -0
  96. data/man/bundle.ronn +83 -0
  97. data/man/gemfile.5.ronn +324 -0
  98. data/man/index.txt +6 -0
  99. data/spec/bundler/dsl_spec.rb +48 -0
  100. data/spec/bundler/gem_helper_spec.rb +174 -0
  101. data/spec/bundler/source_spec.rb +25 -0
  102. data/spec/cache/gems_spec.rb +239 -0
  103. data/spec/cache/git_spec.rb +124 -0
  104. data/spec/cache/path_spec.rb +103 -0
  105. data/spec/cache/platform_spec.rb +57 -0
  106. data/spec/install/deploy_spec.rb +211 -0
  107. data/spec/install/gems/c_ext_spec.rb +48 -0
  108. data/spec/install/gems/dependency_api_spec.rb +402 -0
  109. data/spec/install/gems/env_spec.rb +107 -0
  110. data/spec/install/gems/flex_spec.rb +313 -0
  111. data/spec/install/gems/groups_spec.rb +268 -0
  112. data/spec/install/gems/packed_spec.rb +84 -0
  113. data/spec/install/gems/platform_spec.rb +208 -0
  114. data/spec/install/gems/post_install_spec.rb +47 -0
  115. data/spec/install/gems/resolving_spec.rb +72 -0
  116. data/spec/install/gems/simple_case_spec.rb +814 -0
  117. data/spec/install/gems/standalone_spec.rb +260 -0
  118. data/spec/install/gems/sudo_spec.rb +74 -0
  119. data/spec/install/gems/win32_spec.rb +26 -0
  120. data/spec/install/gemspec_spec.rb +170 -0
  121. data/spec/install/git_spec.rb +796 -0
  122. data/spec/install/invalid_spec.rb +35 -0
  123. data/spec/install/path_spec.rb +405 -0
  124. data/spec/install/upgrade_spec.rb +26 -0
  125. data/spec/lock/git_spec.rb +35 -0
  126. data/spec/lock/lockfile_spec.rb +809 -0
  127. data/spec/other/check_spec.rb +265 -0
  128. data/spec/other/clean_spec.rb +492 -0
  129. data/spec/other/config_spec.rb +138 -0
  130. data/spec/other/console_spec.rb +54 -0
  131. data/spec/other/exec_spec.rb +229 -0
  132. data/spec/other/ext_spec.rb +37 -0
  133. data/spec/other/help_spec.rb +39 -0
  134. data/spec/other/init_spec.rb +40 -0
  135. data/spec/other/newgem_spec.rb +87 -0
  136. data/spec/other/open_spec.rb +35 -0
  137. data/spec/other/outdated_spec.rb +93 -0
  138. data/spec/other/platform_spec.rb +881 -0
  139. data/spec/other/show_spec.rb +88 -0
  140. data/spec/quality_spec.rb +62 -0
  141. data/spec/realworld/edgecases_spec.rb +177 -0
  142. data/spec/resolver/basic_spec.rb +20 -0
  143. data/spec/resolver/platform_spec.rb +82 -0
  144. data/spec/runtime/executable_spec.rb +120 -0
  145. data/spec/runtime/load_spec.rb +107 -0
  146. data/spec/runtime/platform_spec.rb +90 -0
  147. data/spec/runtime/require_spec.rb +261 -0
  148. data/spec/runtime/setup_spec.rb +755 -0
  149. data/spec/runtime/with_clean_env_spec.rb +80 -0
  150. data/spec/spec_helper.rb +98 -0
  151. data/spec/support/artifice/endopint_marshal_fail_basic_authentication.rb +13 -0
  152. data/spec/support/artifice/endpoint.rb +54 -0
  153. data/spec/support/artifice/endpoint_500.rb +37 -0
  154. data/spec/support/artifice/endpoint_api_missing.rb +16 -0
  155. data/spec/support/artifice/endpoint_basic_authentication.rb +13 -0
  156. data/spec/support/artifice/endpoint_extra.rb +27 -0
  157. data/spec/support/artifice/endpoint_extra_missing.rb +15 -0
  158. data/spec/support/artifice/endpoint_fallback.rb +18 -0
  159. data/spec/support/artifice/endpoint_marshal_fail.rb +11 -0
  160. data/spec/support/artifice/endpoint_redirect.rb +15 -0
  161. data/spec/support/builders.rb +604 -0
  162. data/spec/support/fakeweb/rack-1.0.0.marshal +2 -0
  163. data/spec/support/fakeweb/windows.rb +23 -0
  164. data/spec/support/helpers.rb +317 -0
  165. data/spec/support/indexes.rb +112 -0
  166. data/spec/support/matchers.rb +77 -0
  167. data/spec/support/path.rb +73 -0
  168. data/spec/support/platforms.rb +86 -0
  169. data/spec/support/ruby_ext.rb +20 -0
  170. data/spec/support/rubygems_ext.rb +37 -0
  171. data/spec/support/rubygems_hax/platform.rb +22 -0
  172. data/spec/support/sudo.rb +21 -0
  173. data/spec/update/gems_spec.rb +134 -0
  174. data/spec/update/git_spec.rb +196 -0
  175. data/spec/update/source_spec.rb +51 -0
  176. metadata +338 -0
@@ -0,0 +1,78 @@
1
+ require 'uri'
2
+
3
+ module Bundler
4
+ # used for Creating Specifications from the Gemcutter Endpoint
5
+ class EndpointSpecification < Gem::Specification
6
+ include MatchPlatform
7
+
8
+ attr_reader :name, :version, :platform, :dependencies
9
+ attr_accessor :source
10
+
11
+ def initialize(name, version, platform, dependencies)
12
+ @name = name
13
+ @version = version
14
+ @platform = platform
15
+ @dependencies = dependencies
16
+ end
17
+
18
+ def fetch_platform
19
+ @plaftorm
20
+ end
21
+
22
+ # needed for standalone, load required_paths from local gemspec
23
+ # after the gem in installed
24
+ def require_paths
25
+ if @remote_specification
26
+ @remote_specification.require_paths
27
+ elsif _local_specification
28
+ _local_specification.require_paths
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ # needed for binstubs
35
+ def executables
36
+ if @remote_specification
37
+ @remote_specification.executables
38
+ elsif _local_specification
39
+ _local_specification.executables
40
+ else
41
+ super
42
+ end
43
+ end
44
+
45
+ # needed for bundle clean
46
+ def bindir
47
+ if @remote_specification
48
+ @remote_specification.bindir
49
+ elsif _local_specification
50
+ _local_specification.bindir
51
+ else
52
+ super
53
+ end
54
+ end
55
+
56
+ # needed for post_install_messages during install
57
+ def post_install_message
58
+ if @remote_specification
59
+ @remote_specification.post_install_message
60
+ elsif _local_specification
61
+ _local_specification.post_install_message
62
+ end
63
+ end
64
+
65
+ def _local_specification
66
+ eval(File.read(local_specification_path)) if @loaded_from && File.exists?(local_specification_path)
67
+ end
68
+
69
+ def __swap__(spec)
70
+ @remote_specification = spec
71
+ end
72
+
73
+ private
74
+ def local_specification_path
75
+ "#{base_dir}/specifications/#{full_name}.gemspec"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,47 @@
1
+ module Bundler
2
+ class Environment
3
+ attr_reader :root
4
+
5
+ def initialize(root, definition)
6
+ @root = root
7
+ @definition = definition
8
+
9
+ env_file = Bundler.app_config_path.join('environment.rb')
10
+ env_file.rmtree if env_file.exist?
11
+ end
12
+
13
+ def inspect
14
+ @definition.to_lock.inspect
15
+ end
16
+
17
+ # TODO: Remove this method. It's used in cli.rb still
18
+ def index
19
+ @definition.index
20
+ end
21
+
22
+ def requested_specs
23
+ @definition.requested_specs
24
+ end
25
+
26
+ def specs
27
+ @definition.specs
28
+ end
29
+
30
+ def dependencies
31
+ @definition.dependencies
32
+ end
33
+
34
+ def current_dependencies
35
+ @definition.current_dependencies
36
+ end
37
+
38
+ def lock
39
+ @definition.lock(Bundler.default_lockfile)
40
+ end
41
+
42
+ def update(*gems)
43
+ # Nothing
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,225 @@
1
+ require 'uri'
2
+ require 'net/http/persistent'
3
+
4
+ module Bundler
5
+ # Handles all the fetching with the rubygems server
6
+ class Fetcher
7
+ REDIRECT_LIMIT = 5
8
+
9
+ attr_reader :has_api
10
+
11
+ class << self
12
+ attr_accessor :disable_endpoint
13
+
14
+ @@spec_fetch_map ||= {}
15
+
16
+ def fetch(spec)
17
+ spec, uri = @@spec_fetch_map[spec.full_name]
18
+ if spec
19
+ path = download_gem_from_uri(spec, uri)
20
+ s = Bundler.rubygems.spec_from_gem(path)
21
+ spec.__swap__(s)
22
+ end
23
+ end
24
+
25
+ def download_gem_from_uri(spec, uri)
26
+ spec.fetch_platform
27
+
28
+ download_path = Bundler.requires_sudo? ? Bundler.tmp : Bundler.rubygems.gem_dir
29
+ gem_path = "#{Bundler.rubygems.gem_dir}/cache/#{spec.full_name}.gem"
30
+
31
+ FileUtils.mkdir_p("#{download_path}/cache")
32
+ Bundler.rubygems.download_gem(spec, uri, download_path)
33
+
34
+ if Bundler.requires_sudo?
35
+ Bundler.mkdir_p "#{Bundler.rubygems.gem_dir}/cache"
36
+ Bundler.sudo "mv #{Bundler.tmp}/cache/#{spec.full_name}.gem #{gem_path}"
37
+ end
38
+
39
+ gem_path
40
+ end
41
+ end
42
+
43
+ def initialize(remote_uri)
44
+ @remote_uri = remote_uri
45
+ @has_api = true # will be set to false if the rubygems index is ever fetched
46
+ @@connection ||= Net::HTTP::Persistent.new nil, :ENV
47
+ end
48
+
49
+ # fetch a gem specification
50
+ def fetch_spec(spec)
51
+ spec = spec - [nil, 'ruby', '']
52
+ spec_file_name = "#{spec.join '-'}.gemspec.rz"
53
+
54
+ uri = URI.parse("#{@remote_uri}#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}")
55
+
56
+ spec_rz = (uri.scheme == "file") ? Gem.read_binary(uri.path) : fetch(uri)
57
+ Marshal.load Gem.inflate(spec_rz)
58
+ end
59
+
60
+ # return the specs in the bundler format as an index
61
+ def specs(gem_names, source)
62
+ index = Index.new
63
+
64
+ if !gem_names || @remote_uri.scheme == "file" || Bundler::Fetcher.disable_endpoint
65
+ Bundler.ui.info "Fetching source index from #{strip_user_pass_from_uri(@remote_uri)}"
66
+ specs = fetch_all_remote_specs
67
+ else
68
+ Bundler.ui.info "Fetching gem metadata from #{strip_user_pass_from_uri(@remote_uri)}", Bundler.ui.debug?
69
+ begin
70
+ specs = fetch_remote_specs(gem_names)
71
+ # fall back to the legacy index in the following cases
72
+ # 1. Gemcutter Endpoint doesn't return a 200
73
+ # 2. Marshal blob doesn't load properly
74
+ # 3. One of the YAML gemspecs has the Syck::DefaultKey problem
75
+ rescue HTTPError, TypeError => e
76
+ # new line now that the dots are over
77
+ Bundler.ui.info "" unless Bundler.ui.debug?
78
+
79
+ if @remote_uri.to_s.include?("rubygems.org")
80
+ Bundler.ui.info "Error #{e.class} during request to dependency API"
81
+ end
82
+ Bundler.ui.debug e.message
83
+ Bundler.ui.debug e.backtrace
84
+
85
+ Bundler.ui.info "Fetching full source index from #{strip_user_pass_from_uri(@remote_uri)}"
86
+ specs = fetch_all_remote_specs
87
+ else
88
+ # new line now that the dots are over
89
+ Bundler.ui.info "" unless Bundler.ui.debug?
90
+ end
91
+ end
92
+
93
+ specs[@remote_uri].each do |name, version, platform, dependencies|
94
+ next if name == 'bundler'
95
+ spec = nil
96
+ if dependencies
97
+ spec = EndpointSpecification.new(name, version, platform, dependencies)
98
+ else
99
+ spec = RemoteSpecification.new(name, version, platform, self)
100
+ end
101
+ spec.source = source
102
+ @@spec_fetch_map[spec.full_name] = [spec, @remote_uri]
103
+ index << spec
104
+ end
105
+
106
+ index
107
+ end
108
+
109
+ # fetch index
110
+ def fetch_remote_specs(gem_names, full_dependency_list = [], last_spec_list = [])
111
+ query_list = gem_names - full_dependency_list
112
+
113
+ # only display the message on the first run
114
+ if Bundler.ui.debug?
115
+ Bundler.ui.debug "Query List: #{query_list.inspect}"
116
+ else
117
+ Bundler.ui.info ".", false
118
+ end
119
+
120
+ return {@remote_uri => last_spec_list} if query_list.empty?
121
+
122
+ spec_list, deps_list = fetch_dependency_remote_specs(query_list)
123
+ returned_gems = spec_list.map {|spec| spec.first }.uniq
124
+
125
+ fetch_remote_specs(deps_list, full_dependency_list + returned_gems, spec_list + last_spec_list)
126
+ end
127
+
128
+ private
129
+
130
+ def fetch(uri, counter = 0)
131
+ raise HTTPError, "Too many redirects" if counter >= REDIRECT_LIMIT
132
+
133
+ begin
134
+ Bundler.ui.debug "Fetching from: #{uri}"
135
+ response = @@connection.request(uri)
136
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
137
+ SocketError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
138
+ Net::HTTP::Persistent::Error, Net::ProtocolError => e
139
+ raise HTTPError, "Network error while fetching #{uri}"
140
+ end
141
+
142
+ case response
143
+ when Net::HTTPRedirection
144
+ Bundler.ui.debug("HTTP Redirection")
145
+ new_uri = URI.parse(response["location"])
146
+ new_uri.user = uri.user
147
+ new_uri.password = uri.password
148
+ fetch(new_uri, counter + 1)
149
+ when Net::HTTPSuccess
150
+ Bundler.ui.debug("HTTP Success")
151
+ response.body
152
+ else
153
+ Bundler.ui.debug("HTTP Error")
154
+ raise HTTPError
155
+ end
156
+ end
157
+
158
+ # fetch from Gemcutter Dependency Endpoint API
159
+ def fetch_dependency_remote_specs(gem_names)
160
+ Bundler.ui.debug "Query Gemcutter Dependency Endpoint API: #{gem_names.join(' ')}"
161
+ encoded_gem_names = URI.encode(gem_names.join(","))
162
+ uri = URI.parse("#{@remote_uri}api/v1/dependencies?gems=#{encoded_gem_names}")
163
+ marshalled_deps = fetch(uri)
164
+ gem_list = Marshal.load(marshalled_deps)
165
+ deps_list = []
166
+
167
+ spec_list = gem_list.map do |s|
168
+ dependencies = s[:dependencies].map do |d|
169
+ begin
170
+ name, requirement = d
171
+ dep = Gem::Dependency.new(name, requirement.split(", "))
172
+ rescue ArgumentError => e
173
+ if e.message.include?('Illformed requirement ["#<YAML::Syck::DefaultKey')
174
+ puts # we shouldn't print the error message on the "fetching info" status line
175
+ raise GemspecError, %{Unfortunately, the gem #{s[:name]} (#{s[:number]}) } +
176
+ %{has an invalid gemspec. As a result, Bundler cannot install this Gemfile. } +
177
+ %{Please ask the gem author to yank the bad version to fix this issue. For } +
178
+ %{more information, see http://bit.ly/syck-defaultkey. For a temporary } +
179
+ %{workaround try using the --full-index option.}
180
+ else
181
+ raise e
182
+ end
183
+ end
184
+
185
+ deps_list << dep.name
186
+
187
+ dep
188
+ end
189
+
190
+ [s[:name], Gem::Version.new(s[:number]), s[:platform], dependencies]
191
+ end
192
+
193
+ [spec_list, deps_list.uniq]
194
+ end
195
+
196
+ # fetch from modern index: specs.4.8.gz
197
+ def fetch_all_remote_specs
198
+ @has_api = false
199
+ Gem.sources = ["#{@remote_uri}"]
200
+ spec_list = Hash.new { |h,k| h[k] = [] }
201
+ begin
202
+ # Fetch all specs, minus prerelease specs
203
+ spec_list = Gem::SpecFetcher.new.list(true, false)
204
+ # Then fetch the prerelease specs
205
+ begin
206
+ Gem::SpecFetcher.new.list(false, true).each {|k, v| spec_list[k] += v }
207
+ rescue Gem::RemoteFetcher::FetchError
208
+ Bundler.ui.debug "Could not fetch prerelease specs from #{strip_user_pass_from_uri(@remote_uri)}"
209
+ end
210
+ rescue Gem::RemoteFetcher::FetchError
211
+ raise Bundler::HTTPError, "Could not reach #{strip_user_pass_from_uri(@remote_uri)}"
212
+ end
213
+
214
+ return spec_list
215
+ end
216
+
217
+ def strip_user_pass_from_uri(uri)
218
+ uri_dup = uri.dup
219
+ uri_dup.user = "****" if uri_dup.user
220
+ uri_dup.password = "****" if uri_dup.password
221
+
222
+ uri_dup
223
+ end
224
+ end
225
+ end
@@ -0,0 +1,162 @@
1
+ $:.unshift File.expand_path('../vendor', __FILE__)
2
+ require 'thor'
3
+ require 'bundler'
4
+
5
+ module Bundler
6
+ class GemHelper
7
+ include Rake::DSL if defined? Rake::DSL
8
+
9
+ class << self
10
+ # set when install'd.
11
+ attr_accessor :instance
12
+
13
+ def install_tasks(opts = {})
14
+ new(opts[:dir], opts[:name]).install
15
+ end
16
+
17
+ def gemspec(&block)
18
+ gemspec = instance.gemspec
19
+ block.call(gemspec) if block
20
+ gemspec
21
+ end
22
+ end
23
+
24
+ attr_reader :spec_path, :base, :gemspec
25
+
26
+ def initialize(base = nil, name = nil)
27
+ Bundler.ui = UI::Shell.new(Thor::Base.shell.new)
28
+ @base = (base ||= Dir.pwd)
29
+ gemspecs = name ? [File.join(base, "#{name}.gemspec")] : Dir[File.join(base, "{,*}.gemspec")]
30
+ raise "Unable to determine name from existing gemspec. Use :name => 'gemname' in #install_tasks to manually set it." unless gemspecs.size == 1
31
+ @spec_path = gemspecs.first
32
+ @gemspec = Bundler.load_gemspec(@spec_path)
33
+ end
34
+
35
+ def install
36
+ desc "Build #{name}-#{version}.gem into the pkg directory"
37
+ task 'build' do
38
+ build_gem
39
+ end
40
+
41
+ desc "Build and install #{name}-#{version}.gem into system gems"
42
+ task 'install' do
43
+ install_gem
44
+ end
45
+
46
+ desc "Create tag #{version_tag} and build and push #{name}-#{version}.gem to Rubygems"
47
+ task 'release' do
48
+ release_gem
49
+ end
50
+
51
+ GemHelper.instance = self
52
+ end
53
+
54
+ def build_gem
55
+ file_name = nil
56
+ sh("gem build -V '#{spec_path}'") { |out, code|
57
+ file_name = File.basename(built_gem_path)
58
+ FileUtils.mkdir_p(File.join(base, 'pkg'))
59
+ FileUtils.mv(built_gem_path, 'pkg')
60
+ Bundler.ui.confirm "#{name} #{version} built to pkg/#{file_name}"
61
+ }
62
+ File.join(base, 'pkg', file_name)
63
+ end
64
+
65
+ def install_gem
66
+ built_gem_path = build_gem
67
+ out, _ = sh_with_code("gem install '#{built_gem_path}'")
68
+ raise "Couldn't install gem, run `gem install #{built_gem_path}' for more detailed output" unless out[/Successfully installed/]
69
+ Bundler.ui.confirm "#{name} (#{version}) installed"
70
+ end
71
+
72
+ def release_gem
73
+ guard_clean
74
+ guard_already_tagged
75
+ built_gem_path = build_gem
76
+ tag_version {
77
+ git_push
78
+ rubygem_push(built_gem_path)
79
+ }
80
+ end
81
+
82
+ protected
83
+ def rubygem_push(path)
84
+ if Pathname.new("~/.gem/credentials").expand_path.exist?
85
+ sh("gem push '#{path}'")
86
+ Bundler.ui.confirm "Pushed #{name} #{version} to rubygems.org"
87
+ else
88
+ raise "Your rubygems.org credentials aren't set. Run `gem push` to set them."
89
+ end
90
+ end
91
+
92
+ def built_gem_path
93
+ Dir[File.join(base, "#{name}-*.gem")].sort_by{|f| File.mtime(f)}.last
94
+ end
95
+
96
+ def git_push
97
+ perform_git_push
98
+ perform_git_push ' --tags'
99
+ Bundler.ui.confirm "Pushed git commits and tags"
100
+ end
101
+
102
+ def perform_git_push(options = '')
103
+ cmd = "git push #{options}"
104
+ out, code = sh_with_code(cmd)
105
+ raise "Couldn't git push. `#{cmd}' failed with the following output:\n\n#{out}\n" unless code == 0
106
+ end
107
+
108
+ def guard_already_tagged
109
+ if sh('git tag').split(/\n/).include?(version_tag)
110
+ raise("This tag has already been committed to the repo.")
111
+ end
112
+ end
113
+
114
+ def guard_clean
115
+ clean? or raise("There are files that need to be committed first.")
116
+ end
117
+
118
+ def clean?
119
+ sh_with_code("git diff --exit-code")[1] == 0
120
+ end
121
+
122
+ def tag_version
123
+ sh "git tag -a -m \"Version #{version}\" #{version_tag}"
124
+ Bundler.ui.confirm "Tagged #{version_tag}"
125
+ yield if block_given?
126
+ rescue
127
+ Bundler.ui.error "Untagged #{version_tag} due to error"
128
+ sh_with_code "git tag -d #{version_tag}"
129
+ raise
130
+ end
131
+
132
+ def version
133
+ gemspec.version
134
+ end
135
+
136
+ def version_tag
137
+ "v#{version}"
138
+ end
139
+
140
+ def name
141
+ gemspec.name
142
+ end
143
+
144
+ def sh(cmd, &block)
145
+ out, code = sh_with_code(cmd, &block)
146
+ code == 0 ? out : raise(out.empty? ? "Running `#{cmd}' failed. Run this command directly for more detailed output." : out)
147
+ end
148
+
149
+ def sh_with_code(cmd, &block)
150
+ cmd << " 2>&1"
151
+ outbuf = ''
152
+ Bundler.ui.debug(cmd)
153
+ Dir.chdir(base) {
154
+ outbuf = `#{cmd}`
155
+ if $? == 0
156
+ block.call(outbuf) if block
157
+ end
158
+ }
159
+ [outbuf, $?]
160
+ end
161
+ end
162
+ end