Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ plugins:
- rubocop-performance
- rubocop-minitest

inherit_mode:
merge:
- Exclude

AllCops:
TargetRubyVersion: 2.6
NewCops: enable
Exclude:
- target/**/*
- vendor/**/*
- tmp/**/*

Metrics:
Enabled: false
Expand Down
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source 'https://gem.coop'

gemspec

group :development do
group :development do
gem 'rake', require: false
gem 'ruby-debug', '~> 0.11', require: false

Expand All @@ -13,3 +13,8 @@ group :development do
gem 'rubocop-performance', require: false
gem 'rubocop-rake', require: false
end

group :test do
gem 'minitest', '~> 5.27'
gem 'minitest-mock', '~> 5.27'
end
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ task :download_jars do

puts " downloading #{filename}..."
URI.open(info[:url]) do |remote| # rubocop:disable Security/Open
File.open(target, 'wb') { |f| f.write(remote.read) }
File.binwrite(target, remote.read)
end
verify_checksum(target, info[:sha1])
puts " saved: #{target}"
Expand All @@ -60,7 +60,7 @@ def verify_checksum(path, expected_sha1)
return if actual == expected_sha1

File.delete(path)
raise "SHA-1 mismatch for #{path}:\n" \
" expected: #{expected_sha1}\n" \
" actual: #{actual}"
raise "SHA-1 mismatch for #{path}:\n " \
"expected: #{expected_sha1}\n " \
"actual: #{actual}"
end
13 changes: 8 additions & 5 deletions jar-dependencies.gemspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# frozen_string_literal: true

require_relative 'lib/jars/version'
require_relative 'lib/jars/mima/version'
File.expand_path('lib', File.dirname(__FILE__)).tap do |lib|
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
end

require 'jars/version'
require 'jars/mima/version'

Gem::Specification.new do |s|
s.name = 'jar-dependencies'

s.version = Jars::VERSION
s.platform = 'java'

s.author = 'christian meier'
s.email = ['mkristian@web.de']
Expand All @@ -20,7 +25,7 @@ Gem::Specification.new do |s|

s.files = Dir['{lib}/**/*'] + %w[Mavenfile Rakefile Readme.md jar-dependencies.gemspec MIT-LICENSE]
# explicitly require the jars
s.files += Jars::Mima::JARS.each_key.map {File.join(Jars::Mima::MIMA_DIR, _1)}
s.files += Jars::Mima::JARS.each_key.map { File.join(Jars::Mima::MIMA_DIR, _1) }

s.description = <<~TEXT
manage jar dependencies for gems and keep track which jar was already
Expand All @@ -32,7 +37,5 @@ Gem::Specification.new do |s|

s.required_ruby_version = '>= 2.6'

s.add_development_dependency 'minitest', '~> 5.10'

s.metadata['rubygems_mfa_required'] = 'true'
end
47 changes: 34 additions & 13 deletions lib/jar_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ module Jars
autoload :MavenSettings, 'jars/maven_settings'
autoload :Mima, 'jars/mima'

@jars_lock = false
@jars = {}
@jars_lock = false
@jars_locked = nil

class JarLoadError < LoadError; end

Expand Down Expand Up @@ -119,10 +120,15 @@ def quiet?
@quiet
end

# @deprecated
def no_more_warnings
@quiet = true
end

def locked_jar?(coordinate)
@jars_locked&.include?(coordinate)
end

def jarfile
ENV['JARFILE'] || ENV_JAVA['jarfile'] || ENV['JBUNDLER_JARFILE'] || ENV_JAVA['jbundler.jarfile'] || 'Jarfile'
end
Expand Down Expand Up @@ -177,6 +183,7 @@ def reset
end
Jars::MavenSettings.reset
@jars = {}
@jars_locked = nil
end

def maven_local_settings
Expand Down Expand Up @@ -209,6 +216,7 @@ def home

def require_jars_lock!(scope = :runtime)
urls = jars_lock_from_class_loader if to_prop(LOCK).nil?
pre_lock_jars = @jars.keys.dup
if urls && !urls.empty?
@jars_lock = true

Expand All @@ -223,15 +231,19 @@ def require_jars_lock!(scope = :runtime)
done << url
end
end
no_more_warnings
elsif (jars_lock = Jars.lock_path)
Jars.debug { "--- load jars from #{jars_lock}" }
@jars_lock = jars_lock

classpath = Jars::Classpath.new(nil, jars_lock)
classpath.require(scope)
no_more_warnings
end

return unless @jars_lock

@jars_locked = (@jars.keys - pre_lock_jars).freeze
return if @jars_locked.empty?

Jars.debug do
loaded = @jars.collect { |k, v| "#{k}:#{v}" }
"--- loaded jars ---\n\t#{loaded.join("\n\t")}"
Expand Down Expand Up @@ -275,6 +287,13 @@ def require_jar(group_id, artifact_id, *classifier_version)
end
end

def info(msg = nil, newline: true)
return if quiet?

msg = yield if msg.nil? && block_given?
Kernel.print("#{msg}#{"\n" if newline}")
end

def warn(msg = nil)
return if quiet? && !debug?

Expand Down Expand Up @@ -306,9 +325,8 @@ def require_jar_with_block(group_id, artifact_id, *classifier_version)
version = classifier_version[-1]
classifier = classifier_version[-2]

coordinate = +"#{group_id}:#{artifact_id}"
coordinate << ":#{classifier}" if classifier
if @jars.key? coordinate
coordinate = "#{group_id}:#{artifact_id}#{":#{classifier}" if classifier}"
if @jars.key?(coordinate)
if @jars[coordinate] == version
false
else
Expand Down Expand Up @@ -346,8 +364,8 @@ def to_jar(group_id, artifact_id, version, classifier = nil)
file
end

def do_require(*args)
jar = to_jar(*args)
def do_require(group_id, artifact_id, version, classifier)
jar = to_jar(group_id, artifact_id, version, classifier)
# use jar from PWD/vendor/jars if exists
if File.exist?(vendor = File.join(Dir.pwd, 'vendor', 'jars', jar))
require vendor
Expand All @@ -373,15 +391,18 @@ def require_jar(*args, &block)
return unless Jars.require?

result = Jars.require_jar(*args, &block)
if result.is_a? String
if result.is_a?(String)
args << (yield || Jars::UNKNOWN) if args.size == 2 && block
Jars.warn do
"jar conflict: #{args[0..-2].join(':')} already loaded with version #{result}; " \
"skipping requested version #{args[-1]}"
coordinate = args[0..-2].join(':')
unless Jars.locked_jar?(coordinate)
Jars.warn do
"jar conflict: #{coordinate} already loaded with version #{result}; " \
"skipping requested version #{args[-1]}"
end
end
Jars.debug("\n\t#{caller.join("\n\t")}") if Jars.debug?
return false
end
Jars.debug { "jar registration: #{args.inspect}; loaded=#{result == true}" }
Jars.debug { "jar registration: #{args.inspect}; loaded=#{result}" }
result
end
51 changes: 21 additions & 30 deletions lib/jars/gemspec_artifacts.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
# frozen_string_literal: true

module Jars
class MavenVersion < String
module MavenVersion
class << self
def new(*args)
if args.empty? || (args.size == 1 && args[0].nil?)
nil
else
low, high = convert(args[0])
low, high = convert(args[1], low, high) if /[=~><]/.match?(args[1])
if low == high
low
else
super("#{low || '[0'},#{high || ')'}")
end
end
def resolve(*args)
return nil if args.empty? || (args.size == 1 && args[0].nil?)

low, high = convert(args[0])
low, high = convert(args[1], low, high) if /[=~><]/.match?(args[1])
(low == high) ? low : "#{low || '[0'},#{high || ')'}"
end

private
Expand Down Expand Up @@ -94,14 +88,19 @@ class Artifact

ALLOWED_TYPES = %w[jar pom].freeze

def initialize(options, *args)
@type, @group_id, @artifact_id, @classifier, @version, @exclusions = *args
def initialize(type, group_id, artifact_id, classifier, version, exclusions, **options)
@type = type
@group_id = group_id
@artifact_id = artifact_id
@classifier = classifier
@version = version
@exclusions = exclusions
options.each do |k, v|
instance_variable_set(:"@#{k}", v)
end
end

def self.new(line)
def self.parse(line)
line = line.strip
index = line.index(/\s/)
return nil if index.nil?
Expand Down Expand Up @@ -151,7 +150,7 @@ def self.new(line)
warn line
return nil
end
super(options, type, group_id, artifact_id, classifier, version, exclusions)
new(type, group_id, artifact_id, classifier, version, exclusions, **options)
end

def to_s
Expand All @@ -169,18 +168,11 @@ def to_gacv
args.join(':')
end

def to_coord_no_classifier
args = [@group_id, @artifact_id]
args << @type
args << MavenVersion.new(@version)
args.join(':')
end

def to_coord
def to_coord(classifier: true)
args = [@group_id, @artifact_id]
args << @classifier if @classifier
args << @classifier if classifier && @classifier
args << @type
args << MavenVersion.new(@version)
args << MavenVersion.resolve(@version)
args.join(':')
end

Expand All @@ -197,9 +189,8 @@ def initialize(spec)
@artifacts = []
spec.requirements.each do |req|
req.split("\n").each do |line|
if (a = Artifact.new(line))
@artifacts << a
end
artifact = Artifact.parse(line)
@artifacts << artifact if artifact
end
end
@artifacts.freeze
Expand Down
Loading