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
@@ -15,79 +15,71 @@
15
15
 
16
16
  require 'singleton'
17
17
 
18
- #
19
- # Registry of resources loaded by LockJar
20
- #
21
- # @author Michael Guymon
22
- #
23
- class LockJar::Registry
18
+ module LockJar
19
+ #
20
+ # Registry of resources loaded by LockJar
21
+ #
22
+ # @author Michael Guymon
23
+ #
24
+ class Registry
24
25
  include Singleton
25
-
26
- attr_accessor :loaded_gems
27
- attr_accessor :loaded_jars
28
- attr_accessor :loaded_lockfiles
29
-
26
+
27
+ attr_accessor :loaded_gems, :loaded_jars, :loaded_lockfiles
28
+
30
29
  def initialize
31
30
  @loaded_gems = {}
32
31
  @loaded_jars = []
33
32
  @loaded_lockfiles = []
34
33
  end
35
-
36
- def lockfile_registered?( lockfile )
37
- if lockfile
38
- @loaded_lockfiles.include? File.expand_path( lockfile )
39
- end
34
+
35
+ def lockfile_registered?(lockfile)
36
+ @loaded_lockfiles.include? File.expand_path(lockfile) if lockfile
40
37
  end
41
-
42
- def register_lockfile( lockfile )
43
- if lockfile && !lockfile_registered?( lockfile )
44
- @loaded_lockfiles << File.expand_path( lockfile )
45
- end
38
+
39
+ def register_lockfile(lockfile)
40
+ return if lockfile.nil? || lockfile_registered?(lockfile)
41
+
42
+ @loaded_lockfiles << File.expand_path(lockfile)
46
43
  end
47
-
48
- def register_jars( jars )
49
- if jars
50
- jars_to_load = jars - @loaded_jars
51
-
52
- @loaded_jars += jars_to_load
53
-
54
- jars_to_load
55
- end
44
+
45
+ def register_jars(jars)
46
+ return if jars.nil?
47
+
48
+ jars_to_load = jars - @loaded_jars
49
+
50
+ @loaded_jars += jars_to_load
51
+
52
+ jars_to_load
56
53
  end
57
-
58
- def register_gem( spec )
54
+
55
+ def register_gem(spec)
59
56
  @loaded_gems[spec.name] = spec
60
57
  end
61
-
62
- def gem_registered?( spec )
58
+
59
+ def gem_registered?(spec)
63
60
  !@loaded_gems[spec.name].nil?
64
61
  end
65
-
66
- def load_gem( spec )
67
- unless gem_registered?( spec )
68
- register_gem(spec)
69
- gem_dir = spec.gem_dir
70
-
71
- lockfile = File.join( gem_dir, "Jarfile.lock" )
72
-
73
- if File.exists?( lockfile )
74
- puts "#{spec.name} has Jarfile.lock, loading jars"
75
- LockJar.load( lockfile )
76
- end
77
- end
62
+
63
+ def load_gem(spec)
64
+ return if gem_registered?(spec)
65
+
66
+ register_gem(spec)
67
+ gem_dir = spec.gem_dir
68
+
69
+ lockfile = File.join(gem_dir, 'Jarfile.lock')
70
+
71
+ return unless File.exist?(lockfile)
72
+
73
+ puts "#{spec.name} has Jarfile.lock, loading jars"
74
+ LockJar.load(lockfile)
78
75
  end
79
-
80
- def load_jars_for_gems
76
+
77
+ def load_jars_for_gems
81
78
  specs = Gem.loaded_specs
82
- if specs
83
- gems = specs.keys - @loaded_gems.keys
84
- if gems.size > 0
85
- gems.each do |key|
86
- spec = specs[key]
87
- load_gem( spec )
88
- end
89
- end
90
- end
91
- end
79
+ return if specs.nil?
92
80
 
93
- end
81
+ gems = specs.keys - @loaded_gems.keys
82
+ gems.each { |key| load_gem(specs[key]) }
83
+ end
84
+ end
85
+ end
@@ -19,18 +19,16 @@ require 'naether/bootstrap'
19
19
  require 'fileutils'
20
20
 
21
21
  module LockJar
22
+ #
22
23
  class Resolver
24
+ attr_reader :opts, :naether
23
25
 
24
- attr_reader :opts
25
- attr_reader :naether
26
-
27
- def initialize( opts = {} )
28
-
26
+ def initialize(opts = {})
29
27
  @opts = opts
30
28
  local_repo = opts[:local_repo] || Naether::Bootstrap.default_local_repo
31
29
 
32
30
  # Bootstrap Naether
33
- Naether::Bootstrap.bootstrap_local_repo( local_repo, opts )
31
+ Naether::Bootstrap.bootstrap_local_repo(local_repo, opts)
34
32
 
35
33
  # Bootstrapping naether will create an instance from downloaded jars.
36
34
  # If jars exist locally already, create manually
@@ -47,14 +45,13 @@ module LockJar
47
45
  @naether.clear_remote_repositories
48
46
  end
49
47
 
50
- def add_remote_repository( repo )
51
- @naether.add_remote_repository( repo )
48
+ def add_remote_repository(repo)
49
+ @naether.add_remote_repository(repo)
52
50
  end
53
51
 
54
-
55
- def resolve( dependencies, download_artifacts = true )
52
+ def resolve(dependencies, download_artifacts = true)
56
53
  @naether.dependencies = dependencies
57
- @naether.resolve_dependencies( download_artifacts )
54
+ @naether.resolve_dependencies(download_artifacts)
58
55
  @naether.dependencies_notation
59
56
  end
60
57
 
@@ -62,37 +59,37 @@ module LockJar
62
59
  @naether.dependencies_graph
63
60
  end
64
61
 
65
- def download( dependencies )
66
- @naether.download_artifacts( dependencies )
62
+ def download(dependencies)
63
+ @naether.download_artifacts(dependencies)
67
64
  end
68
65
 
69
- def to_local_paths( notations )
66
+ def to_local_paths(notations)
70
67
  paths = []
71
68
  notations.each do |notation|
72
- if File.exists?(notation)
69
+ if File.exist?(notation)
73
70
  paths << notation
74
71
  else
75
- paths = paths + @naether.to_local_paths( [notation] )
72
+ paths += @naether.to_local_paths([notation])
76
73
  end
77
74
  end
78
75
 
79
76
  paths
80
77
  end
81
78
 
82
- def load_to_classpath( artifacts )
79
+ def load_to_classpath(artifacts)
83
80
  paths = []
84
81
  notations = []
85
82
 
86
83
  artifacts.each do |art|
87
- if File.exists?(art)
84
+ if File.exist?(art)
88
85
  paths << art
89
86
  else
90
87
  notations << art
91
88
  end
92
89
  end
93
90
 
94
- paths += @naether.to_local_paths( notations )
95
- Naether::Java.load_paths( paths )
91
+ paths += @naether.to_local_paths(notations)
92
+ Naether::Java.load_paths(paths)
96
93
 
97
94
  paths
98
95
  end
@@ -0,0 +1,28 @@
1
+ module LockJar
2
+ #
3
+ class Runtime
4
+ #
5
+ module Install
6
+ def install(jarfile_lock, groups = ['default'], opts = {}, &blk)
7
+ deps = list(jarfile_lock, groups, { with_locals: false }.merge(opts), &blk)
8
+
9
+ lockfile = LockJar::Domain::Lockfile.read(jarfile_lock)
10
+ if opts[:local_repo].nil? && lockfile.local_repository
11
+ opts[:local_repo] = lockfile.local_repository
12
+ end
13
+
14
+ # Older Jarfile expected the defaul maven repo, but did not write
15
+ # it to the lockfile
16
+ resolver(opts).clear_remote_repositories if lockfile.version.to_f >= 0.11
17
+
18
+ lockfile.remote_repositories.each do |repo|
19
+ resolver(opts).add_remote_repository(repo)
20
+ end
21
+
22
+ files = resolver(opts).download(deps)
23
+
24
+ files
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,55 @@
1
+ module LockJar
2
+ #
3
+ class Runtime
4
+ #
5
+ module List
6
+ # rubocop:disable Metrics/PerceivedComplexity, MethodLength
7
+ def list(lockfile_or_path, groups = ['default'], opts = {}, &blk)
8
+ dependencies = []
9
+ maps = []
10
+ with_locals = { with_locals: true }.merge(opts).delete(:with_locals)
11
+
12
+ if lockfile_or_path
13
+ if lockfile_or_path.is_a? LockJar::Domain::Lockfile
14
+ lockfile = lockfile_or_path
15
+ elsif lockfile_or_path
16
+ lockfile = LockJar::Domain::Lockfile.read(lockfile_or_path)
17
+ end
18
+
19
+ dependencies = lockfile_dependencies(lockfile, groups, with_locals)
20
+ maps = lockfile.maps
21
+ end
22
+
23
+ # Support limited DSL from block
24
+ unless blk.nil?
25
+ dsl = LockJar::Domain::Dsl.create(&blk)
26
+ dependencies += dsl_dependencies(dsl, groups, with_locals).map(&:to_dep)
27
+ maps = dsl.maps
28
+ end
29
+
30
+ if maps && maps.size > 0
31
+ maps.each do |notation, replacements|
32
+ dependencies = dependencies.flat_map do |dep|
33
+ if dep =~ /#{notation}/
34
+ replacements
35
+ else
36
+ dep
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ dependencies = resolver(opts).resolve(dependencies) if opts[:resolve]
43
+
44
+ if opts[:local_paths]
45
+ opts.delete(:local_paths) # remove list opts so resolver is not reset
46
+ resolver(opts).to_local_paths(dependencies)
47
+
48
+ else
49
+ dependencies
50
+ end
51
+ end
52
+ # rubocop:enable Metrics/PerceivedComplexity, MethodLength
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,54 @@
1
+ module LockJar
2
+ #
3
+ class Runtime
4
+ #
5
+ module Load
6
+ # Load paths from a lockfile or block. Paths are loaded once per lockfile.
7
+ #
8
+ # @param [String] lockfile_path the lockfile
9
+ # @param [Array] groups to load into classpath
10
+ # @param [Hash] opts
11
+ # @param [Block] blk
12
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
13
+ def load(lockfile_or_path, groups = ['default'], opts = {}, &blk)
14
+ # lockfile is only loaded once
15
+ unless lockfile_or_path.nil?
16
+ # loaded a Lockfile instance
17
+ if lockfile_or_path.is_a? LockJar::Domain::Lockfile
18
+ lockfile = lockfile_or_path
19
+
20
+ # check if lockfile path is already loaded
21
+ elsif LockJar::Registry.instance.lockfile_registered?(lockfile_or_path)
22
+ return
23
+
24
+ # convert lockfile path to a Lockfile instance
25
+ else
26
+ lockfile = LockJar::Domain::Lockfile.read(lockfile_or_path)
27
+ end
28
+
29
+ if opts[:local_repo].nil? && lockfile.local_repository
30
+ opts[:local_repo] = lockfile.local_repository
31
+ end
32
+ end
33
+
34
+ # set local_repo if passed in the block
35
+ unless blk.nil?
36
+ dsl = LockJar::Domain::Dsl.create(&blk)
37
+
38
+ # set local_repo from block
39
+ opts[:local_repo] = dsl.local_repository if opts[:local_repo].nil? && dsl.local_repository
40
+ end
41
+
42
+ # registered merged lockfiles for lockfile
43
+ if lockfile && !lockfile.merged.empty?
44
+ lockfile.merged.each { |path| LockJar::Registry.instance.register_lockfile(path) }
45
+ end
46
+
47
+ dependencies = LockJar::Registry.instance.register_jars(list(lockfile, groups, opts, &blk))
48
+
49
+ resolver(opts).load_to_classpath(dependencies)
50
+ end
51
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,152 @@
1
+ require 'delegate'
2
+
3
+ module LockJar
4
+ #
5
+ class Runtime
6
+ #
7
+ class Lock < SimpleDelegator
8
+ attr_accessor :jarfile, :lockfile, :opts
9
+
10
+ def initialize(runtime)
11
+ super(runtime)
12
+
13
+ @lockfile = LockJar::Domain::Lockfile.new
14
+ end
15
+
16
+ def lock(jarfile_or_dsl, opts = {}, &blk)
17
+ @opts = { download: true }.merge(opts)
18
+
19
+ create_dsl!(jarfile_or_dsl, &blk)
20
+
21
+ apply_repositories!
22
+
23
+ apply_maps!
24
+
25
+ apply_excludes!
26
+
27
+ apply_merged!
28
+
29
+ artifacts = jarfile.artifacts.values.flatten
30
+ apply_artifacts!(artifacts) unless artifacts.empty?
31
+
32
+ lockfile.write(@opts[:lockfile] || 'Jarfile.lock')
33
+
34
+ lockfile
35
+ end
36
+
37
+ private
38
+
39
+ def create_dsl!(jarfile_or_dsl, &blk)
40
+ if jarfile_or_dsl
41
+ if jarfile_or_dsl.is_a? LockJar::Domain::Dsl
42
+ @jarfile = jarfile_or_dsl
43
+ else
44
+ @jarfile = LockJar::Domain::JarfileDsl.create(jarfile_or_dsl)
45
+ end
46
+ end
47
+
48
+ return @jarfile if blk.nil?
49
+
50
+ if @jarfile
51
+ @jarfile = LockJar::Domain::DslMerger.new(
52
+ jarfile, LockJar::Domain::Dsl.create(&blk)).merge
53
+ else
54
+ @jarfile = LockJar::Domain::Dsl.create(&blk)
55
+ end
56
+ end
57
+
58
+ # rubocop:disable Metrics/AbcSize
59
+ def apply_artifacts!(artifacts)
60
+ # Build the dependencies_graph hash in the resolver
61
+ resolver(opts).resolve(
62
+ artifacts.select(&:resolvable?).map(&:to_dep), opts[:download] == true
63
+ )
64
+
65
+ jarfile.artifacts.each do |group_name, group_artifacts|
66
+ group = { 'locals' => [], 'dependencies' => [], 'artifacts' => [] }
67
+
68
+ group_artifacts.each do |artifact|
69
+ artifact_data = {}
70
+
71
+ add_artifact!(group, artifact_data, artifact)
72
+
73
+ next unless artifact_data['transitive']
74
+
75
+ # flatten the graph of nested hashes
76
+ group['dependencies'] += dependency_merge(artifact_data['transitive'])
77
+ # xxX: set required_by ?
78
+ group['artifacts'] << { artifact.to_urn => artifact_data }
79
+ end
80
+
81
+ lockfile.excludes.each do |exclude|
82
+ group['dependencies'].delete_if { |dep| dep =~ /#{exclude}/ }
83
+ end if lockfile.excludes
84
+
85
+ group['dependencies'] = group['dependencies'].sort.uniq
86
+
87
+ group.delete 'locals' if group['locals'].empty?
88
+
89
+ lockfile.groups[group_name] = group
90
+ end
91
+ end
92
+ # rubocop:enable Metrics/AbcSize
93
+
94
+ def apply_excludes!
95
+ lockfile.excludes = jarfile.excludes if jarfile.excludes.size > 0
96
+ end
97
+
98
+ def apply_maps!
99
+ lockfile.maps = jarfile.maps if jarfile.maps.size > 0
100
+ end
101
+
102
+ def apply_merged!
103
+ lockfile.merged = jarfile.merged unless jarfile.merged.empty?
104
+ end
105
+
106
+ def apply_repositories!
107
+ if jarfile.clear_repositories
108
+ resolver(opts).clear_remote_repositories
109
+ else
110
+ repos = resolver(opts).remote_repositories
111
+ lockfile.remote_repositories += repos.to_a if repos
112
+ end
113
+
114
+ # Add remote repos to the resolver and the lockfile
115
+ jarfile.remote_repositories.each do |repo|
116
+ resolver(opts).add_remote_repository(repo)
117
+ lockfile.remote_repositories << repo
118
+ end
119
+
120
+ lockfile.local_repository = opts[:local_repo] || jarfile.local_repository
121
+ end
122
+
123
+ def add_artifact!(group, artifact_data, artifact)
124
+ if artifact.is_a? LockJar::Domain::Jar
125
+ group['dependencies'] << artifact.notation
126
+ g = resolver(opts).dependencies_graph[artifact.notation]
127
+ artifact_data['transitive'] = g.to_hash if g
128
+
129
+ elsif artifact.is_a? LockJar::Domain::Pom
130
+ artifact_data['scopes'] = artifact.scopes
131
+
132
+ # iterate each dependency in Pom to map transitive dependencies
133
+ transitive = {}
134
+ artifact.notations.each do |notation|
135
+ transitive.merge!(notation => resolver(opts).dependencies_graph[notation])
136
+ end
137
+ artifact_data['transitive'] = transitive
138
+
139
+ elsif artifact.is_a? LockJar::Domain::Local
140
+ group['locals'] << artifact.path
141
+ else
142
+ fail("Unsupported artifact: #{artifact.inspect}")
143
+ end
144
+ end
145
+
146
+ def dependency_merge(graph)
147
+ deps = graph.keys
148
+ graph.values.flat_map { |next_step| deps += dependency_merge(next_step) }
149
+ end
150
+ end
151
+ end
152
+ end