lock_jar 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +16 -0
  3. data/.rubocop.yml +28 -0
  4. data/.travis.yml +12 -1
  5. data/Gemfile +6 -3
  6. data/Guardfile +2 -3
  7. data/README.md +17 -16
  8. data/Rakefile +11 -7
  9. data/lib/lock_jar/buildr.rb +95 -89
  10. data/lib/lock_jar/bundler.rb +85 -84
  11. data/lib/lock_jar/class_loader.rb +19 -21
  12. data/lib/lock_jar/cli.rb +32 -25
  13. data/lib/lock_jar/domain/artifact.rb +39 -45
  14. data/lib/lock_jar/domain/dsl.rb +50 -79
  15. data/lib/lock_jar/domain/dsl_merger.rb +76 -0
  16. data/lib/lock_jar/domain/gem_dsl.rb +10 -12
  17. data/lib/lock_jar/domain/jarfile_dsl.rb +6 -18
  18. data/lib/lock_jar/domain/lockfile.rb +17 -24
  19. data/lib/lock_jar/logging.rb +4 -3
  20. data/lib/lock_jar/maven.rb +29 -29
  21. data/lib/lock_jar/registry.rb +52 -60
  22. data/lib/lock_jar/resolver.rb +17 -20
  23. data/lib/lock_jar/runtime/install.rb +28 -0
  24. data/lib/lock_jar/runtime/list.rb +55 -0
  25. data/lib/lock_jar/runtime/load.rb +54 -0
  26. data/lib/lock_jar/runtime/lock.rb +152 -0
  27. data/lib/lock_jar/runtime.rb +30 -302
  28. data/lib/lock_jar/version.rb +2 -1
  29. data/lib/lock_jar.rb +137 -105
  30. data/lock_jar.gemspec +7 -4
  31. data/spec/fixtures/jarfile_gem/Gemfile +4 -0
  32. data/spec/fixtures/jarfile_gem/Jarfile +1 -0
  33. data/spec/fixtures/jarfile_gem/jarfile_gem.gemspec +23 -0
  34. data/spec/fixtures/jarfile_gem/lib/jarfile_gem/version.rb +3 -0
  35. data/spec/fixtures/jarfile_gem/lib/jarfile_gem.rb +5 -0
  36. data/spec/lock_jar/bundler_spec.rb +27 -0
  37. data/spec/lock_jar/class_loader_spec.rb +34 -36
  38. data/spec/lock_jar/cli_spec.rb +39 -46
  39. data/spec/lock_jar/domain/dsl_merger_spec.rb +49 -0
  40. data/spec/lock_jar/domain/dsl_spec.rb +35 -37
  41. data/spec/lock_jar/domain/gem_dsl_spec.rb +18 -0
  42. data/spec/lock_jar/maven_spec.rb +9 -11
  43. data/spec/lock_jar/resolver_spec.rb +16 -17
  44. data/spec/lock_jar/runtime_spec.rb +17 -13
  45. data/spec/lock_jar_spec.rb +255 -195
  46. data/spec/spec_helper.rb +13 -8
  47. data/spec/support/helper.rb +13 -5
  48. data/spec/support/shared_examples/lockfile.rb +4 -6
  49. metadata +43 -19
  50. data/bundler/Gemfile +0 -21
  51. data/bundler/LICENSE.txt +0 -22
  52. data/bundler/README.md +0 -29
  53. data/bundler/Rakefile +0 -2
  54. data/bundler/lib/lock_jar_bundler/bundler.rb +0 -35
  55. data/bundler/lib/lock_jar_bundler/piggy_back.rb +0 -98
  56. data/bundler/lib/lock_jar_bundler/version.rb +0 -5
  57. data/bundler/lib/lock_jar_bundler.rb +0 -5
  58. data/bundler/lock_jar_bundler.gemspec +0 -24
  59. data/bundler/spec/Jarfile +0 -3
  60. data/bundler/spec/dummy_gem/Jarfile +0 -1
  61. data/bundler/spec/dummy_gem/dummy_gem.gemspec +0 -19
  62. data/bundler/spec/lock_jar_bundler_spec.rb +0 -49
  63. data/bundler/spec/spec_helper.rb +0 -88
  64. data/lib/lock_jar/domain/dsl_helper.rb +0 -84
  65. data/spec/lock_jar/domain/dsl_helper_spec.rb +0 -52
@@ -4,60 +4,102 @@ require 'lock_jar/domain/lockfile'
4
4
  require 'lock_jar/domain/dsl'
5
5
  require 'lock_jar/domain/gem_dsl'
6
6
  require 'lock_jar/domain/jarfile_dsl'
7
- require 'lock_jar/domain/dsl_helper'
7
+ require 'lock_jar/domain/dsl_merger'
8
8
 
9
9
  module LockJar
10
-
10
+ #
11
11
  class Bundler
12
-
13
12
  class << self
14
-
15
13
  attr_accessor :skip_lock
16
-
14
+
17
15
  def load(*groups)
18
- if groups && !groups.empty? && File.exists?( 'Jarfile.lock')
19
-
20
- lockfile = LockJar::Domain::Lockfile.read( 'Jarfile.lock' )
21
-
22
- # expand merged paths to include gem base path
23
- unless lockfile.merged.empty?
24
- lockfile.merged = LockJar::Bundler.expand_gem_paths( lockfile.merged )
25
- end
26
-
27
- LockJar.load( lockfile, groups )
28
-
29
- if ENV["DEBUG"]
30
- puts "[LockJar] Loaded Jars for #{groups.inspect}: #{LockJar::Registry.instance.loaded_jars.inspect}"
31
- end
16
+ return if groups.empty? || !File.exist?('Jarfile.lock')
17
+
18
+ lockfile = LockJar::Domain::Lockfile.read('Jarfile.lock')
19
+
20
+ # expand merged paths to include gem base path
21
+ unless lockfile.merged.empty?
22
+ lockfile.merged = LockJar::Bundler.expand_gem_paths(lockfile.merged)
32
23
  end
24
+
25
+ LockJar.load(lockfile, groups)
26
+
27
+ puts(
28
+ '[LockJar] Loaded Jars for #{groups.inspect}: '\
29
+ "#{LockJar::Registry.instance.loaded_jars.inspect}"
30
+ ) if ENV['DEBUG']
33
31
  end
34
-
32
+
35
33
  def expand_gem_paths(merged)
36
-
37
34
  merged_gem_paths = []
38
35
  Gem.path.each do |gem_root|
39
36
  merged.each do |merge|
37
+ next unless merge.start_with? 'gem:'
38
+
40
39
  # merged gems follow the notation: gem:gemname:path
41
- if merge.start_with? 'gem:'
42
- gem_path = merge.gsub(/^gem:.+:/, '')
43
- gem_path = File.join( gem_root, gem_path )
44
- if File.exists? gem_path
45
- merged_gem_paths << gem_path
46
- end
47
- end
40
+ gem_path = merge.gsub(/^gem:.+:/, '')
41
+ gem_path = File.join(gem_root, gem_path)
42
+
43
+ merged_gem_paths << gem_path if File.exist? gem_path
48
44
  end
49
45
  end
50
-
46
+
51
47
  merged_gem_paths
52
48
  end
49
+
50
+ # Create a lock file from bundled gems
51
+ def lock!(*opts)
52
+ definition = ::Bundler.definition
53
+
54
+ dsl = nil
55
+ gems_with_jars = []
56
+
57
+ jarfile_opt = opts.find { |option| option.is_a? String }
58
+
59
+ jarfile = File.expand_path(jarfile_opt || 'Jarfile')
60
+
61
+ # load local Jarfile
62
+ if File.exist?(jarfile)
63
+ dsl = LockJar::Domain::JarfileDsl.create(jarfile)
64
+
65
+ # Create new Dsl
66
+ else
67
+ dsl = LockJar::Domain::Dsl.new
68
+ end
69
+
70
+ definition.groups.each do |group|
71
+ puts '[LockJar] Group #{group}:' if ENV['DEBUG']
72
+
73
+ definition.specs_for([group]).each do |spec|
74
+ merged_dsl = merge_gem_dsl(dsl, spec, group)
75
+ dsl = merged_dsl if merged_dsl
76
+ gems_with_jars << "gem:#{spec.name}" if File.exist? File.join(spec.gem_dir, 'Jarfile')
77
+ end
78
+ end
79
+
80
+ puts "[LockJar] Locking Jars for: #{gems_with_jars.inspect}"
81
+ LockJar.lock(*([dsl] + opts))
82
+ end
83
+
84
+ private
85
+
86
+ def merge_gem_dsl(dsl, spec, group)
87
+ jarfile = File.join(spec.gem_dir, 'Jarfile')
88
+
89
+ return unless File.exist?(jarfile)
90
+
91
+ puts "[LockJar] #{spec.name} has Jarfile" if ENV['DEBUG']
92
+ spec_dsl = LockJar::Domain::GemDsl.create(spec, jarfile)
93
+ LockJar::Domain::DslMerger.new(dsl, spec_dsl, [group.to_s]).merge
94
+ end
53
95
  end
54
96
  end
55
-
56
97
  end
57
98
 
99
+ # Patch Bundler module to allow LockJar to lock and load when Bundler is run
58
100
  module Bundler
59
101
  class << self
60
- alias :_lockjar_extended_require :require
102
+ alias_method :_lockjar_extended_require, :require
61
103
  def require(*groups)
62
104
  LockJar::Bundler.load(*groups)
63
105
 
@@ -66,7 +108,7 @@ module Bundler
66
108
  _lockjar_extended_require
67
109
  end
68
110
 
69
- alias :_lockjar_extended_setup :setup
111
+ alias_method :_lockjar_extended_setup, :setup
70
112
  def setup(*groups)
71
113
  LockJar::Bundler.load(*groups)
72
114
 
@@ -76,9 +118,10 @@ module Bundler
76
118
  end
77
119
  end
78
120
 
121
+ # Patch Bundler::Runtime.require and Bundler::Runtime.setup to execute
122
+ # Lockjar::Bundler.load
79
123
  class Runtime
80
-
81
- alias :_lockjar_extended_require :require
124
+ alias_method :_lockjar_extended_require, :require
82
125
  def require(*groups)
83
126
  LockJar::Bundler.load(*groups)
84
127
 
@@ -87,7 +130,7 @@ module Bundler
87
130
  _lockjar_extended_require
88
131
  end
89
132
 
90
- alias :_lockjar_extended_setup :setup
133
+ alias_method :_lockjar_extended_setup, :setup
91
134
  def setup(*groups)
92
135
  LockJar::Bundler.load(*groups)
93
136
 
@@ -97,59 +140,17 @@ module Bundler
97
140
  end
98
141
  end
99
142
 
143
+ # Patch Bundler::Definition.to_lock to run LockJar::Bundler.lock!
100
144
  class Definition
101
- alias :_lockjar_extended_to_lock :to_lock
145
+ alias_method :_lockjar_extended_to_lock, :to_lock
102
146
  def to_lock
103
- to_lock = _lockjar_extended_to_lock
104
-
105
- if LockJar::Bundler.skip_lock != true
106
- definition = Bundler.definition
107
- #if !definition.send( :nothing_changed? )
108
- gems_with_jars = []
109
-
110
- # load local Jarfile
111
- if File.exists?( 'Jarfile' )
112
- dsl = LockJar::Domain::JarfileDsl.create( File.expand_path( 'Jarfile' ) )
113
- gems_with_jars << 'jarfile:Jarfile'
114
- # Create new Dsl
115
- else
116
- dsl = LockJar::Domain::Dsl.new
117
- end
118
-
119
- definition.groups.each do |group|
120
- if ENV["DEBUG"]
121
- puts "[LockJar] Group #{group}:"
122
- end
147
+ result = _lockjar_extended_to_lock
123
148
 
124
- definition.specs_for( [group] ).each do |spec|
125
- gem_dir = spec.gem_dir
149
+ return result if LockJar::Bundler.skip_lock
126
150
 
127
- jarfile = File.join( gem_dir, "Jarfile" )
151
+ LockJar::Bundler.lock!
128
152
 
129
- if File.exists?( jarfile )
130
- gems_with_jars << "gem:#{spec.name}"
131
-
132
- if ENV["DEBUG"]
133
- puts "[LockJar] #{spec.name} has Jarfile"
134
- end
135
-
136
- spec_dsl = LockJar::Domain::GemDsl.create( spec, "Jarfile" )
137
-
138
- dsl = LockJar::Domain::DslHelper.merge( dsl, spec_dsl, group.to_s )
139
- end
140
- end
141
-
142
- end
143
-
144
- puts "[LockJar] Locking Jars for: #{gems_with_jars.inspect}"
145
- LockJar.lock( dsl )
146
- #elsif ENV["DEBUG"]
147
- # puts "[LockJar] Locking skiped, Gemfile has not changed"
148
- #end
149
- end
150
-
151
- to_lock
153
+ result
152
154
  end
153
-
154
155
  end
155
- end
156
+ end
@@ -17,53 +17,51 @@ require 'lock_jar'
17
17
  require 'naether'
18
18
 
19
19
  module LockJar
20
-
21
20
  #
22
21
  # Create a ClassLoader populated by a lockfile
23
22
  #
24
23
  # @author Michael Guymon
25
24
  #
26
25
  class ClassLoader
27
-
28
26
  # Create new instance, populating ClassLoader with lockfile
29
- #
27
+ #
30
28
  # @param [String] lockfile path
31
- def initialize( lockfile )
29
+ def initialize(lockfile)
32
30
  # XXX: ensure Naether has been loaded, this should be handled less
33
31
  # clumsily
34
32
  LockJar::Runtime.instance.resolver(nil)
35
- @class_loader = com.tobedevoured.naether.PathClassLoader.new(JRuby.runtime.jruby_class_loader)
36
-
37
- jars = LockJar.list( lockfile, :local_paths => true )
33
+ @class_loader = com.tobedevoured.naether.PathClassLoader.new(
34
+ JRuby.runtime.jruby_class_loader)
35
+
36
+ jars = LockJar.list(lockfile, local_paths: true)
38
37
  jars.each do |jar|
39
- add_path( jar )
40
- end
38
+ add_path(jar)
39
+ end
41
40
  end
42
-
41
+
43
42
  # Execute block
44
43
  #
45
44
  # @param [Block] blk
46
- def isolate(&blk)
47
- instance_eval(&blk)
45
+ def isolate(&blk)
46
+ instance_eval(&blk)
48
47
  end
49
-
48
+
50
49
  # Add path to the ClassLoader
51
50
  #
52
51
  # @param [String] path of Jar or directory to add to ClassLoader
53
52
  # @return [Boolean] if added
54
- def add_path( path )
55
- @class_loader.addPath( path )
53
+ def add_path(path)
54
+ @class_loader.addPath(path)
56
55
  end
57
-
56
+
58
57
  #
59
58
  # Create new instance of a Java class using the populated ClassLoader
60
- #
59
+ #
61
60
  # @param [String] clazz fully qualified Java class
62
61
  # @param [Array] args arguments for constructing the Java class
63
62
  # @return [Object]
64
- def new_instance( clazz, *args )
65
- @class_loader.newInstance( clazz, *args )
63
+ def new_instance(clazz, *args)
64
+ @class_loader.newInstance(clazz, *args)
66
65
  end
67
-
68
66
  end
69
- end
67
+ end
data/lib/lock_jar/cli.rb CHANGED
@@ -4,64 +4,72 @@ require 'lock_jar'
4
4
  require 'lock_jar/logging'
5
5
 
6
6
  module LockJar
7
-
7
+ #
8
8
  class CLI < Thor
9
-
9
+ #
10
10
  module ClassMethods
11
11
  def generate_lockfile_option
12
- method_option :lockfile,
13
- aliases: "-l",
12
+ method_option(
13
+ :lockfile,
14
+ aliases: '-l',
14
15
  default: 'Jarfile.lock',
15
- desc: "Path to Jarfile.lock"
16
+ desc: 'Path to Jarfile.lock'
17
+ )
16
18
  end
17
19
 
18
20
  def generate_scopes_option
19
- method_option :scopes,
20
- aliases: "-s",
21
+ method_option(
22
+ :scopes,
23
+ aliases: '-s',
21
24
  default: ['default'],
22
- desc: "Scopes to install from Jarfile.lock",
23
- :type => :array
25
+ desc: 'Scopes to install from Jarfile.lock',
26
+ type: :array
27
+ )
24
28
  end
25
29
 
26
30
  def generate_jarfile_option
27
- method_option :jarfile,
28
- aliases: "-j",
31
+ method_option(
32
+ :jarfile,
33
+ aliases: '-j',
29
34
  default: 'Jarfile',
30
- desc: "Path to Jarfile"
35
+ desc: 'Path to Jarfile'
36
+ )
31
37
  end
32
38
 
33
39
  def verbose_option
34
- method_option :verbose,
35
- aliases: "-v",
40
+ method_option(
41
+ :verbose,
42
+ aliases: '-v',
36
43
  type: :boolean,
37
- desc: "Verbose output"
44
+ desc: 'Verbose output'
45
+ )
38
46
  end
39
47
  end
40
48
  extend(ClassMethods)
41
49
 
42
- desc "version", "LockJar version"
50
+ desc 'version', 'LockJar version'
43
51
  def version
44
52
  puts LockJar::VERSION
45
53
  end
46
54
 
47
- desc "install", "Install Jars from a Jarfile.lock"
55
+ desc 'install', 'Install Jars from a Jarfile.lock'
48
56
  generate_lockfile_option
49
57
  generate_scopes_option
50
58
  verbose_option
51
59
  def install
52
60
  handle_verbose(options[:verbose])
53
61
  puts "Installing Jars from #{options[:lockfile]} for #{options[:scopes].inspect}"
54
- LockJar.install( options[:lockfile], options[:scopes] )
62
+ LockJar.install(options[:lockfile], options[:scopes])
55
63
  end
56
64
 
57
- desc "list", "List Jars from a Jarfile.lock"
65
+ desc 'list', 'List Jars from a Jarfile.lock'
58
66
  generate_lockfile_option
59
67
  generate_scopes_option
60
68
  verbose_option
61
69
  def list
62
70
  handle_verbose(options[:verbose])
63
71
  puts "Listing Jars from #{options[:lockfile]} for #{options[:scopes].inspect}"
64
- puts LockJar.list( options[:lockfile], options[:scopes] ).inspect
72
+ puts LockJar.list(options[:lockfile], options[:scopes]).inspect
65
73
  end
66
74
 
67
75
  desc 'lock', 'Lock Jars in a Jarfile.lock'
@@ -71,15 +79,14 @@ module LockJar
71
79
  def lock
72
80
  handle_verbose(options[:verbose])
73
81
  puts "Locking #{options[:jarfile]} to #{options[:lockfile]}"
74
- LockJar.lock( options[:jarfile], { :lockfile => options[:lockfile] } )
82
+ LockJar.lock(options[:jarfile], lockfile: options[:lockfile])
75
83
  end
76
84
 
77
85
  private
86
+
78
87
  def handle_verbose(verbose)
79
- if verbose
80
- LockJar::Logging.verbose!
81
- end
88
+ LockJar::Logging.verbose! if verbose
89
+ nil
82
90
  end
83
-
84
91
  end
85
92
  end
@@ -19,53 +19,54 @@ require 'naether/notation'
19
19
 
20
20
  module LockJar
21
21
  module Domain
22
-
23
-
22
+ #
24
23
  class Artifact
25
24
  include Comparable
26
25
  attr_reader :type
27
-
28
- def <=>(another_artifact)
29
- if another_artifact.is_a? Artifact
30
- to_urn <=> another_artifact.to_urn
26
+
27
+ def <=>(other)
28
+ if other.is_a? Artifact
29
+ to_urn <=> other.to_urn
31
30
  else
32
- to_urn <=> another_artifact.to_s
31
+ to_urn <=> other.to_s
33
32
  end
34
33
  end
35
-
34
+
36
35
  def resolvable?
37
36
  true
38
37
  end
39
38
  end
40
-
39
+
40
+ #
41
41
  class Jar < Artifact
42
42
  attr_reader :notation
43
-
44
- def initialize( notation )
43
+
44
+ def initialize(notation)
45
45
  @type = 'jar'
46
- @notation = Naether::Notation.new( notation ).to_notation
46
+ @notation = Naether::Notation.new(notation).to_notation
47
47
  end
48
-
48
+
49
49
  def to_urn
50
50
  "jar:#{notation}"
51
51
  end
52
-
52
+
53
53
  def to_dep
54
54
  notation
55
55
  end
56
56
  end
57
-
57
+
58
+ #
58
59
  class Local < Artifact
59
60
  attr_reader :path
60
- def initialize( path )
61
+ def initialize(path)
61
62
  @type = 'local'
62
63
  @path = path
63
64
  end
64
-
65
+
65
66
  def to_urn
66
67
  "local:#{path}"
67
68
  end
68
-
69
+
69
70
  def to_dep
70
71
  path
71
72
  end
@@ -74,51 +75,44 @@ module LockJar
74
75
  false
75
76
  end
76
77
  end
77
-
78
+
79
+ #
78
80
  class Pom < Artifact
79
81
  attr_reader :path, :scopes
80
-
81
- def initialize( _path, _scopes = ['compile','runtime'] )
82
+
83
+ def initialize(pom_path, pom_scopes = %w(compile runtime))
82
84
  @type = 'pom'
83
- @path = _path
84
- @scopes = _scopes
85
+ @path = pom_path
86
+ @scopes = pom_scopes
85
87
  end
86
-
88
+
87
89
  def to_urn
88
90
  "pom:#{path}"
89
91
  end
90
-
92
+
91
93
  def to_dep
92
94
  { path => scopes }
93
95
  end
94
-
96
+
95
97
  def notations
96
- LockJar::Maven.dependencies( path, scopes )
98
+ LockJar::Maven.dependencies(path, scopes)
97
99
  end
98
-
99
- def ==(another_artifact)
100
- self.<=>(another_artifact) == 0
100
+
101
+ def ==(other)
102
+ self.<=>(other) == 0
101
103
  end
102
-
103
- def <=>(another_artifact)
104
- if another_artifact.is_a? Pom
105
- if to_urn == another_artifact.to_urn
106
- return 0 if Set.new(scopes) == Set.new(another_artifact.scopes)
107
-
108
- # XXX: this is not a reliable way to compare.
109
- if scopes.size > another_artifact.scopes.size
110
- return 1
111
- else
112
- return -1
113
- end
104
+
105
+ def <=>(other)
106
+ if other.is_a? Pom
107
+ if to_urn == other.to_urn
108
+ return Set.new(scopes) <=> Set.new(other.scopes)
114
109
  else
115
- to_urn <=> another_artifact.to_urn
110
+ to_urn <=> other.to_urn
116
111
  end
117
112
  else
118
113
  super
119
114
  end
120
115
  end
121
116
  end
122
-
123
117
  end
124
- end
118
+ end