jar-dependencies 0.3.9 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,41 +4,39 @@ Gem::Specification.new do |s|
4
4
  s.name = 'jar-dependencies'
5
5
 
6
6
  path = File.expand_path('lib/jars/version.rb', File.dirname(__FILE__))
7
- s.version = File.read(path).match( /\s*VERSION\s*=\s*['"](.*)['"]/ )[1]
8
-
7
+ s.version = File.read(path).match(/\s*VERSION\s*=\s*['"](.*)['"]/)[1]
8
+
9
9
  s.author = 'christian meier'
10
- s.email = [ 'mkristian@web.de' ]
10
+ s.email = ['mkristian@web.de']
11
11
  s.summary = 'manage jar dependencies for gems'
12
12
  s.homepage = 'https://github.com/mkristian/jar-dependencies'
13
13
 
14
- s.bindir = "bin"
15
- LOCK_JARS = 'lock_jars'
16
- s.executables = [LOCK_JARS]
14
+ s.bindir = 'bin'
15
+ s.executables = [lock_jars = 'lock_jars'.freeze]
17
16
 
18
17
  s.license = 'MIT'
19
18
 
20
- s.files = `git ls-files`.split($/).select do |file|
19
+ s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR).select do |file|
21
20
  file =~ /^lib\// ||
22
- [ 'Mavenfile', 'Rakefile' ].include?(file) ||
23
- [ 'Readme.md', 'jar-dependencies.gemspec', 'MIT-LICENSE' ].include?(file)
21
+ %w[Mavenfile Rakefile].include?(file) ||
22
+ ['Readme.md', 'jar-dependencies.gemspec', 'MIT-LICENSE'].include?(file)
24
23
  end
25
24
 
26
25
  s.description = 'manage jar dependencies for gems and keep track which jar was already loaded using maven artifact coordinates. it warns on version conflicts and loads only ONE jar assuming the first one is compatible to the second one otherwise your project needs to lock down the right version by providing a Jars.lock file.'
27
26
 
28
- s.add_development_dependency 'minitest', '~> 5.3'
27
+ s.add_development_dependency 'minitest', '~> 5.10.0'
29
28
  s.add_development_dependency 'rake', '~> 10.2'
30
- RUBY_MAVEN_VERSION = '~> 3.3.11'
31
- s.add_development_dependency 'ruby-maven', RUBY_MAVEN_VERSION
29
+ s.add_development_dependency 'ruby-maven', ruby_maven_version = '~> 3.3.11'.freeze
32
30
 
33
31
  s.post_install_message = <<EOF
34
32
 
35
- if you want to use the executable #{LOCK_JARS} then install ruby-maven gem before using #{LOCK_JARS}
33
+ if you want to use the executable #{lock_jars} then install ruby-maven gem before using #{lock_jars}
36
34
 
37
- $ gem install ruby-maven -v '#{RUBY_MAVEN_VERSION}'
35
+ $ gem install ruby-maven -v '#{ruby_maven_version}'
38
36
 
39
37
  or add it as a development dependency to your Gemfile
40
38
 
41
- gem 'ruby-maven', '#{RUBY_MAVEN_VERSION}'
39
+ gem 'ruby-maven', '#{ruby_maven_version}'
42
40
 
43
41
  EOF
44
42
  end
@@ -18,7 +18,7 @@
18
18
  # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
19
  # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
20
  #
21
- require 'jars/maven_settings'
21
+
22
22
  module Jars
23
23
  unless defined? Jars::SKIP_LOCK
24
24
  MAVEN_SETTINGS = 'JARS_MAVEN_SETTINGS'.freeze
@@ -45,45 +45,48 @@ module Jars
45
45
  DEBUG = 'JARS_DEBUG'.freeze
46
46
  # vendor jars inside gem when installing gem
47
47
  VENDOR = 'JARS_VENDOR'.freeze
48
+ # string used when the version is unknown
49
+ UNKNOWN = 'unknown'
48
50
  end
49
51
 
50
- class << self
52
+ autoload :MavenSettings, 'jars/maven_settings'
51
53
 
52
- def lock_down( debug = false, verbose = false, options = {} )
53
- ENV[ SKIP_LOCK ] = 'true'
54
+ class << self
55
+ def lock_down(debug = false, verbose = false, options = {})
56
+ ENV[SKIP_LOCK] = 'true'
54
57
  require 'jars/lock_down' # do this lazy to keep things clean
55
- Jars::LockDown.new( debug, verbose ).lock_down( options )
58
+ Jars::LockDown.new(debug, verbose).lock_down(options)
56
59
  ensure
57
- ENV[ SKIP_LOCK ] = nil
60
+ ENV[SKIP_LOCK] = nil
58
61
  end
59
62
 
60
63
  if defined? JRUBY_VERSION
61
- def to_prop( key )
62
- key = key.gsub( '_', '.' )
63
- ENV_JAVA[ ( key.downcase!; key ) ] ||
64
- ENV[ ( key.gsub!( '.', '_' ); key.upcase!; key ) ]
64
+ def to_prop(key)
65
+ key = key.tr('_', '.')
66
+ ENV_JAVA[(key.downcase!; key)] ||
67
+ ENV[(key.tr!('.', '_'); key.upcase!; key)]
65
68
  end
66
69
  else
67
- def to_prop( key )
68
- ENV[ key.gsub( '.', '_' ).upcase ]
70
+ def to_prop(key)
71
+ ENV[key.tr('.', '_').upcase]
69
72
  end
70
73
  end
71
74
 
72
- def to_boolean( key )
73
- return nil if ( prop = to_prop( key ) ).nil?
75
+ def to_boolean(key)
76
+ return nil if (prop = to_prop(key)).nil?
74
77
  prop.empty? || prop.eql?('true')
75
78
  end
76
79
 
77
80
  def skip?
78
- to_boolean( SKIP )
81
+ to_boolean(SKIP)
79
82
  end
80
83
 
81
84
  def require?
82
85
  @require = nil unless instance_variable_defined?(:@require)
83
86
  if @require.nil?
84
- if ( require = to_boolean( REQUIRE ) ).nil?
85
- no_require = to_boolean( NO_REQUIRE )
86
- @require = no_require.nil? ? true : ! no_require
87
+ if (require = to_boolean(REQUIRE)).nil?
88
+ no_require = to_boolean(NO_REQUIRE)
89
+ @require = no_require.nil? ? true : !no_require
87
90
  else
88
91
  @require = require
89
92
  end
@@ -93,26 +96,28 @@ module Jars
93
96
  attr_writer :require
94
97
 
95
98
  def quiet?
96
- ( @silent ||= false ) || to_boolean( QUIET )
99
+ (@silent ||= false) || to_boolean(QUIET)
97
100
  end
98
101
 
99
102
  def jarfile
100
- ENV[ 'JARFILE' ] || ENV_JAVA[ 'jarfile' ] || ENV[ 'JBUNDLER_JARFILE' ] || ENV_JAVA[ 'jbundler.jarfile' ] || 'Jarfile'
103
+ ENV['JARFILE'] || ENV_JAVA['jarfile'] || ENV['JBUNDLER_JARFILE'] || ENV_JAVA['jbundler.jarfile'] || 'Jarfile'
101
104
  end
102
105
 
103
106
  # @deprecated
104
- def no_require?; ! require? end
107
+ def no_require?
108
+ !require?
109
+ end
105
110
 
106
111
  def verbose?
107
- to_boolean( VERBOSE )
112
+ to_boolean(VERBOSE)
108
113
  end
109
114
 
110
115
  def debug?
111
- to_boolean( DEBUG )
116
+ to_boolean(DEBUG)
112
117
  end
113
118
 
114
119
  def vendor?
115
- to_boolean( VENDOR )
120
+ to_boolean(VENDOR)
116
121
  end
117
122
 
118
123
  def no_more_warnings
@@ -124,28 +129,30 @@ module Jars
124
129
  end
125
130
 
126
131
  def skip_lock?
127
- to_prop( SKIP_LOCK ) || false
132
+ to_prop(SKIP_LOCK) || false
128
133
  end
129
134
 
130
135
  def lock
131
- to_prop( LOCK ) || 'Jars.lock'
136
+ to_prop(LOCK) || 'Jars.lock'
132
137
  end
133
138
 
134
139
  def jars_lock_from_class_loader
135
- if to_prop( LOCK ).nil? && defined?(JRUBY_VERSION)
136
- JRuby.runtime.jruby_class_loader.get_resources( 'Jars.lock' ).collect do |url|
137
- url.to_s
140
+ if to_prop(LOCK).nil? && defined?(JRUBY_VERSION)
141
+ if JRuby::Util.respond_to?(:class_loader_resources)
142
+ JRuby::Util.class_loader_resources('Jars.lock')
143
+ else; require 'jruby'
144
+ JRuby.runtime.jruby_class_loader.get_resources('Jars.lock').collect(&:to_s)
138
145
  end
139
146
  end
140
147
  end
141
148
 
142
- def lock_path( basedir = nil )
143
- deps = self.lock
144
- return deps if File.exists?( deps )
149
+ def lock_path(basedir = nil)
150
+ deps = lock
151
+ return deps if File.exist?(deps)
145
152
  basedir ||= '.'
146
- [ '.', 'jars', 'vendor/jars' ].each do |dir|
147
- file = File.join( basedir, dir, self.lock )
148
- return file if File.exists?( file )
153
+ ['.', 'jars', 'vendor/jars'].each do |dir|
154
+ file = File.join(basedir, dir, lock)
155
+ return file if File.exist?(file)
149
156
  end
150
157
  nil
151
158
  end
@@ -153,7 +160,7 @@ module Jars
153
160
  def reset
154
161
  instance_variables.each { |var| instance_variable_set(var, nil) }
155
162
  Jars::MavenSettings.reset
156
- ( @@jars ||= {} ).clear
163
+ (@@jars ||= {}).clear
157
164
  end
158
165
 
159
166
  def maven_local_settings
@@ -177,29 +184,28 @@ module Jars
177
184
  detect_local_repository(maven_local_settings) ||
178
185
  detect_local_repository(maven_user_settings) ||
179
186
  detect_local_repository(maven_global_settings) ||
180
- File.join( user_home, '.m2', 'repository' )
187
+ File.join(user_home, '.m2', 'repository')
181
188
  end
182
189
 
183
190
  def home
184
- @_jars_home_ ||= absolute(to_prop(HOME)) || local_maven_repo
191
+ absolute(to_prop(HOME)) || local_maven_repo
185
192
  end
186
193
 
187
- def require_jars_lock!( scope = :runtime )
194
+ def require_jars_lock!(scope = :runtime)
188
195
  urls = jars_lock_from_class_loader
189
- if urls and urls.size > 0
196
+ if urls && !urls.empty?
190
197
  @@jars_lock = true
191
198
  # funny error during spec where it tries to load it again
192
199
  # and finds it as gem instead of the LOAD_PATH
193
200
  require 'jars/classpath' unless defined? Jars::Classpath
194
201
  done = []
195
- while done != urls do
202
+ while done != urls
196
203
  urls.each do |url|
197
- unless done.member?( url )
198
- Jars.debug { "--- load jars from uri #{url}" }
199
- classpath = Jars::Classpath.new( nil, "uri:#{url}" )
200
- classpath.require( scope )
201
- done << url
202
- end
204
+ next if done.member?(url)
205
+ Jars.debug { "--- load jars from uri #{url}" }
206
+ classpath = Jars::Classpath.new(nil, "uri:#{url}")
207
+ classpath.require(scope)
208
+ done << url
203
209
  end
204
210
  urls = jars_lock_from_class_loader
205
211
  end
@@ -210,25 +216,25 @@ module Jars
210
216
  # funny error during spec where it tries to load it again
211
217
  # and finds it as gem instead of the LOAD_PATH
212
218
  require 'jars/classpath' unless defined? Jars::Classpath
213
- classpath = Jars::Classpath.new( nil, jars_lock )
214
- classpath.require( scope )
219
+ classpath = Jars::Classpath.new(nil, jars_lock)
220
+ classpath.require(scope)
215
221
  no_more_warnings
216
222
  end
217
- Jars.debug {
223
+ Jars.debug do
218
224
  @@jars ||= {}
219
- loaded = @@jars.collect{ |k,v| "#{k}:#{v}" }
225
+ loaded = @@jars.collect { |k, v| "#{k}:#{v}" }
220
226
  "--- loaded jars ---\n\t#{loaded.join("\n\t")}"
221
- }
227
+ end
222
228
  end
223
229
 
224
- def setup( options = nil )
230
+ def setup(options = nil)
225
231
  case options
226
232
  when Symbol
227
- require_jars_lock!( options )
233
+ require_jars_lock!(options)
228
234
  when Hash
229
235
  @_jars_home = options[:jars_home]
230
236
  @_jars_lock = options[:jars_lock]
231
- require_jars_lock!( options[:scope] || :runtime )
237
+ require_jars_lock!(options[:scope] || :runtime)
232
238
  else
233
239
  require_jars_lock!
234
240
  end
@@ -242,35 +248,41 @@ module Jars
242
248
  end
243
249
  end
244
250
 
245
- def mark_as_required( group_id, artifact_id, *classifier_version )
246
- require_jar_with_block( group_id, artifact_id, *classifier_version ) do
251
+ def mark_as_required(group_id, artifact_id, *classifier_version)
252
+ require_jar_with_block(group_id, artifact_id, *classifier_version) do
247
253
  end
248
254
  end
249
255
 
250
- def require_jar( group_id, artifact_id, *classifier_version )
256
+ def require_jar(group_id, artifact_id, *classifier_version, &block)
251
257
  require_jars_lock unless skip_lock?
252
- require_jar_with_block( group_id, artifact_id, *classifier_version ) do |gid, aid, version, classifier|
253
- do_require( gid, aid, version, classifier )
258
+ if classifier_version.empty? && block_given?
259
+ classifier_version = [block.call].compact
260
+ if classifier_version.empty?
261
+ return mark_as_required(group_id, artifact_id, UNKNOWN) || false
262
+ end
263
+ end
264
+ require_jar_with_block(group_id, artifact_id, *classifier_version) do |gid, aid, version, classifier|
265
+ do_require(gid, aid, version, classifier)
254
266
  end
255
267
  end
256
268
 
257
- def warn(msg = nil, &block)
258
- Kernel.warn(msg || block.call) unless quiet? and not verbose?
269
+ def warn(msg = nil)
270
+ Kernel.warn(msg || yield) unless quiet? && !verbose?
259
271
  end
260
272
 
261
- def debug(msg = nil, &block)
262
- Kernel.warn(msg || block.call) if verbose?
273
+ def debug(msg = nil)
274
+ Kernel.warn(msg || yield) if verbose?
263
275
  end
264
276
 
265
- def absolute( file )
266
- File.expand_path( file ) if file
277
+ def absolute(file)
278
+ File.expand_path(file) if file
267
279
  end
268
280
 
269
281
  def user_home
270
- ENV[ 'HOME' ] || begin
282
+ ENV['HOME'] || begin
271
283
  user_home = Dir.home if Dir.respond_to?(:home)
272
284
  unless user_home
273
- user_home = ENV_JAVA[ 'user.home' ] if Object.const_defined?(:ENV_JAVA)
285
+ user_home = ENV_JAVA['user.home'] if Object.const_defined?(:ENV_JAVA)
274
286
  end
275
287
  user_home
276
288
  end
@@ -278,22 +290,22 @@ module Jars
278
290
 
279
291
  private
280
292
 
281
- def require_jar_with_block( group_id, artifact_id, *classifier_version )
282
- version = classifier_version[ -1 ]
283
- classifier = classifier_version[ -2 ]
293
+ def require_jar_with_block(group_id, artifact_id, *classifier_version)
294
+ version = classifier_version[-1]
295
+ classifier = classifier_version[-2]
284
296
 
285
297
  @@jars ||= {}
286
298
  coordinate = "#{group_id}:#{artifact_id}"
287
299
  coordinate << ":#{classifier}" if classifier
288
300
  if @@jars.key? coordinate
289
- if @@jars[ coordinate ] == version
301
+ if @@jars[coordinate] == version
290
302
  false
291
303
  else
292
- @@jars[ coordinate ] # version of already registered jar
304
+ @@jars[coordinate] # version of already registered jar
293
305
  end
294
306
  else
295
307
  yield group_id, artifact_id, version, classifier
296
- @@jars[ coordinate ] = version
308
+ @@jars[coordinate] = version
297
309
  return true
298
310
  end
299
311
  end
@@ -301,60 +313,57 @@ module Jars
301
313
  def detect_local_repository(settings)
302
314
  return nil unless settings
303
315
 
304
- doc = File.read( settings )
305
- # TODO filter out xml comments
306
- local_repo = doc.sub( /<\/localRepository>.*/m, '' ).sub( /.*<localRepository>/m, '' )
316
+ doc = File.read(settings)
317
+ # TODO: filter out xml comments
318
+ local_repo = doc.sub(/<\/localRepository>.*/m, '').sub(/.*<localRepository>/m, '')
307
319
  # replace maven like system properties embedded into the string
308
- local_repo.gsub!( /\$\{[a-zA-Z.]+\}/ ) do |a|
309
- ENV_JAVA[ a[2..-2] ] || a
310
- end
311
- if local_repo.empty? or not File.exists?( local_repo )
312
- local_repo = nil
320
+ local_repo.gsub!(/\$\{[a-zA-Z.]+\}/) do |a|
321
+ ENV_JAVA[a[2..-2]] || a
313
322
  end
323
+ local_repo = nil if local_repo.empty? || !File.exist?(local_repo)
314
324
  local_repo
315
325
  rescue
316
326
  Jars.warn { "error reading or parsing #{settings}" }
317
327
  nil
318
328
  end
319
329
 
320
- def to_jar( group_id, artifact_id, version, classifier = nil )
321
- file = String.new("#{group_id.gsub( '.', '/' )}/#{artifact_id}/#{version}/#{artifact_id}-#{version}")
330
+ def to_jar(group_id, artifact_id, version, classifier = nil)
331
+ file = String.new("#{group_id.tr('.', '/')}/#{artifact_id}/#{version}/#{artifact_id}-#{version}")
322
332
  file << "-#{classifier}" if classifier
323
333
  file << '.jar'
324
334
  file
325
335
  end
326
336
 
327
- def do_require( *args )
328
- jar = to_jar( *args )
329
- local = File.join( Dir.pwd, 'jars', jar )
330
- vendor = File.join( Dir.pwd, 'vendor', 'jars', jar )
331
- file = File.join( home, jar )
337
+ def do_require(*args)
338
+ jar = to_jar(*args)
339
+ local = File.join(Dir.pwd, 'jars', jar)
340
+ vendor = File.join(Dir.pwd, 'vendor', 'jars', jar)
341
+ file = File.join(home, jar)
332
342
  # use jar from local repository if exists
333
- if File.exists?( file )
343
+ if File.exist?(file)
334
344
  require file
335
345
  # use jar from PWD/jars if exists
336
- elsif File.exists?( local )
346
+ elsif File.exist?(local)
337
347
  require local
338
348
  # use jar from PWD/vendor/jars if exists
339
- elsif File.exists?( vendor )
349
+ elsif File.exist?(vendor)
340
350
  require vendor
341
351
  else
342
352
  # otherwise try to find it on the load path
343
353
  require jar
344
354
  end
345
355
  rescue LoadError => e
346
- raise "\n\n\tyou might need to reinstall the gem which depends on the missing jar or in case there is Jars.lock then resolve the jars with `lock_jars` command\n\n" + e.message + " (LoadError)"
356
+ raise "\n\n\tyou might need to reinstall the gem which depends on the missing jar or in case there is Jars.lock then resolve the jars with `lock_jars` command\n\n" + e.message + ' (LoadError)'
347
357
  end
348
-
349
358
  end # class << self
350
-
351
359
  end
352
360
 
353
- def require_jar( *args )
361
+ def require_jar(*args, &block)
354
362
  return nil unless Jars.require?
355
- result = Jars.require_jar( *args )
363
+ result = Jars.require_jar(*args, &block)
356
364
  if result.is_a? String
357
- Jars.warn { "--- jar coordinate #{args[0..-2].join( ':' )} already loaded with version #{result} - omit version #{args[-1]}" }
365
+ args << (block.call || Jars::UNKNOWN) if args.size == 2 && block_given?
366
+ Jars.warn { "--- jar coordinate #{args[0..-2].join(':')} already loaded with version #{result} - omit version #{args[-1]}" }
358
367
  Jars.debug { " try to load from #{caller.join("\n\t")}" }
359
368
  return false
360
369
  end
@@ -1,22 +1,22 @@
1
1
  # this file is maven DSL
2
2
 
3
- ( 0..10000 ).each do |i|
4
- coord = ENV_JAVA[ "jars.#{i}" ]
3
+ (0..10_000).each do |i|
4
+ coord = ENV_JAVA["jars.#{i}"]
5
5
  break unless coord
6
- artifact = Maven::Tools::Artifact.from_coordinate( coord )
6
+ artifact = Maven::Tools::Artifact.from_coordinate(coord)
7
7
  exclusions = []
8
- ( 0..10000 ).each do |j|
9
- exclusion = ENV_JAVA[ "jars.#{i}.exclusions.#{j}" ]
8
+ (0..10_000).each do |j|
9
+ exclusion = ENV_JAVA["jars.#{i}.exclusions.#{j}"]
10
10
  break unless exclusion
11
11
  exclusions << exclusion
12
12
  end
13
- scope = ENV_JAVA[ "jars.#{i}.scope" ]
13
+ scope = ENV_JAVA["jars.#{i}.scope"]
14
14
  artifact.scope = scope if scope
15
- classifier = ENV_JAVA[ "jars.#{i}.classifier" ]
15
+ classifier = ENV_JAVA["jars.#{i}.classifier"]
16
16
  artifact.classifier = classifier if classifier
17
17
 
18
18
  # declare the artifact inside the POM
19
- dependency_artifact( artifact ) do
19
+ dependency_artifact(artifact) do
20
20
  exclusions.each do |ex|
21
21
  exclusion ex
22
22
  end