Class: LockJar::Runtime

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/lock_jar/runtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Returns a new instance of Runtime.



32
33
34
# File 'lib/lock_jar/runtime.rb', line 32

def initialize
  @current_resolver = nil
end

Instance Attribute Details

#current_resolverObject (readonly)

Returns the value of attribute current_resolver.



30
31
32
# File 'lib/lock_jar/runtime.rb', line 30

def current_resolver
  @current_resolver
end

Instance Method Details

#install(jarfile_lock, groups = ['default'], opts = {}, &blk) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lock_jar/runtime.rb', line 60

def install( jarfile_lock, groups = ['default'], opts = {}, &blk )
  deps = list( jarfile_lock, groups, {:with_locals => false}.merge( opts ), &blk )
  
  lockfile = LockJar::Domain::Lockfile.read( jarfile_lock )
  lockfile.remote_repositories.each do |repo|
      resolver(opts).add_remote_repository( repo )
  end
  
  files = resolver(opts).download( deps )
  
  files
end

#list(lockfile_or_path, groups = ['default'], opts = {}, &blk) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/lock_jar/runtime.rb', line 210

def list( lockfile_or_path, groups = ['default'], opts = {}, &blk )
      
  lockfile = nil
  dependencies = []
  maps = []
  with_locals = {:with_locals => true }.merge(opts).delete :with_locals
  
  if lockfile_or_path
    if lockfile_or_path.is_a? LockJar::Domain::Lockfile
      lockfile = lockfile_or_path
    elsif lockfile_or_path
      lockfile = LockJar::Domain::Lockfile.read( lockfile_or_path )
    end
    
    dependencies = lockfile_dependencies( lockfile, groups, with_locals )
    maps = lockfile.maps
  end
  
  # Support limited DSL from block
  unless blk.nil?
    dsl = LockJar::Domain::Dsl.create(&blk)
    dependencies += dsl_dependencies( dsl, groups, with_locals ).map(&:to_dep)
    maps = dsl.maps
  end
  
  if maps && maps.size > 0 
    mapped_dependencies = []
    
    maps.each do |notation, replacements|
      dependencies.each do |dep|
        if dep =~ /#{notation}/
          replacements.each do |replacement|
            mapped_dependencies << replacement
          end
        else
          mapped_dependencies << dep
        end
      end
    end
                    
    dependencies = mapped_dependencies
  end
  
  if opts[:resolve]
    dependencies = resolver(opts).resolve( dependencies )
  end
  
  if opts[:local_paths]
    opts.delete( :local_paths ) # remove list opts so resolver is not reset
    resolver(opts).to_local_paths( dependencies )
    
  else
    dependencies
  end
end

#load(lockfile_or_path, groups = ['default'], opts = {}, &blk) ⇒ Object

Load paths from a lockfile or block. Paths are loaded once per lockfile.

Parameters:

  • lockfile_path (String)

    the lockfile

  • groups (Array) (defaults to: ['default'])

    to load into classpath

  • opts (Hash) (defaults to: {})
  • blk (Block)


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/lock_jar/runtime.rb', line 272

def load( lockfile_or_path, groups = ['default'], opts = {}, &blk )
  
  lockfile = nil
  
  # lockfile is only loaded once
  unless lockfile_or_path.nil? 
    # loaded a Lockfile instance
    if lockfile_or_path.is_a? LockJar::Domain::Lockfile
      lockfile = lockfile_or_path
      
    # check if lockfile path is already loaded
    elsif LockJar::Registry.instance.lockfile_registered?( lockfile_or_path )
      return  
      
    # convert lockfile path to a Lockfile instance
    else
      lockfile = LockJar::Domain::Lockfile.read( lockfile_or_path )
    end
  
        
    if opts[:local_repo].nil? && lockfile.local_repository
      opts[:local_repo] = lockfile.local_repository
    end
  end
  
  # set local_repo if passed in the block
  unless blk.nil?
    dsl = LockJar::Domain::Dsl.create(&blk)
    
    # set local_repo from block
    if opts[:local_repo].nil? && dsl.local_repository
      opts[:local_repo] = dsl.local_repository
    end
  end
  
  # registered merged lockfiles for lockfile
  if lockfile && !lockfile.merged.empty?
    lockfile.merged.each do |path|
      LockJar::Registry.instance.register_lockfile( path )
    end
  end
  
  dependencies = LockJar::Registry.instance.register_jars( list( lockfile, groups, opts, &blk ) )
          
  resolver(opts).load_to_classpath( dependencies )
end

#lock(jarfile_or_dsl, opts = {}, &blk) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/lock_jar/runtime.rb', line 73

def lock( jarfile_or_dsl, opts = {}, &blk )
  
  opts = {:download => true }.merge( opts )

  jarfile = nil

  if jarfile_or_dsl
    if jarfile_or_dsl.is_a? LockJar::Domain::Dsl
      jarfile = jarfile_or_dsl
    else
      jarfile = LockJar::Domain::JarfileDsl.create( jarfile_or_dsl )
    end
  end
  
  unless blk.nil?
    dsl = LockJar::Domain::Dsl.create(&blk)
    if jarfile.nil?
      jarfile = dsl
    else
      jarfile = LockJar::Domain::DslHelper.merge( jarfile, dsl )
    end
  end

  if jarfile.respond_to?(:bundler_enabled ) && jarfile.bundler_enabled
    require 'lock_jar_bundler/bundler'

    LockJar::Bundler.bundled_jarfiles(jarfile.bundler_enabled).each do |bundled_jarfile|
      jarfile = LockJar::Domain::DslHelper.merge( jarfile, LockJar::Domain::JarfileDsl.create(bundled_jarfile) )
    end
  end

  
  # If not set in opts, and is set in  dsl
  if opts[:local_repo].nil? && jarfile.local_repository
    opts[:local_repo] = jarfile.local_repository 
  end
          
  lockfile = LockJar::Domain::Lockfile.new
  
  jarfile.remote_repositories.each do |repo|
    resolver(opts).add_remote_repository( repo )
  end

  unless jarfile.local_repository.nil?
    lockfile.local_repository = jarfile.local_repository
  end
          
  if jarfile.maps.size > 0
    lockfile.maps = jarfile.maps
  end
  
  if jarfile.excludes.size > 0 
    lockfile.excludes = jarfile.excludes
  end
  
  artifacts = []
  jarfile.artifacts.each do |group, group_artifacts|
    group_artifacts.each do |artifact|
      artifacts += group_artifacts
    end
  end
  
  if !jarfile.merged.empty?
    lockfile.merged = jarfile.merged
  end
  
  if !artifacts.empty?   
    resolved_notations = resolver(opts).resolve( artifacts.select{ |artifact| artifact.resolvable? }.map(&:to_dep), opts[:download] == true )
    
    lockfile.remote_repositories = resolver(opts).remote_repositories - ['http://repo1.maven.org/maven2/']
    
    jarfile.artifacts.each do |group_name, group_artifacts|
      group = {'locals' => [], 'dependencies' => [], 'artifacts' => []}
      
      group_artifacts.each do |artifact|
      
        artifact_data = {}
        
        if artifact.is_a? LockJar::Domain::Jar
          group['dependencies'] << artifact.notation
          artifact_data["transitive"] = resolver(opts).dependencies_graph[artifact.notation].to_hash

        elsif artifact.is_a? LockJar::Domain::Pom
          artifact_data['scopes'] = artifact.scopes
          
          # iterate each dependency in Pom to map transitive dependencies
          transitive = {}
          artifact.notations.each do |notation|               
            transitive.merge!( notation => resolver(opts).dependencies_graph[notation] )
          end
          artifact_data["transitive"] = transitive
          
        elsif artifact.is_a? LockJar::Domain::Local
          group['locals'] << artifact.path
        else
          # XXX: handle unsupported artifact
          
        end

        # flatten the graph of nested hashes
        dep_merge = lambda do |graph|
          deps = graph.keys
          graph.values.each do |next_step|
            deps += dep_merge.call(next_step)
          end
          deps
        end
        
        if artifact_data["transitive"]
          group['dependencies'] += dep_merge.call( artifact_data["transitive"] )
          
          # xxX: set required_by ?
          
          group['artifacts'] << { artifact.to_urn => artifact_data }
        end
      end
  
      if lockfile.excludes
        lockfile.excludes.each do |exclude|
           group['dependencies'].delete_if { |dep| dep =~ /#{exclude}/ }
        end
      end
      
      group['dependencies'].sort!
      if group['locals'].empty?
        group.delete 'locals'
      end
      
      lockfile.groups[group_name] = group         
    end                        
  end
  
  lockfile.write( opts[:lockfile] || "Jarfile.lock" )
  
  lockfile
end

#resolver(opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lock_jar/runtime.rb', line 36

def resolver( opts = {} )
  
  # XXX: Caches the resolver by the options. Passing in nil opts will replay
  #      from the cache. This need to change.
  
  unless opts.nil?
    if opts[:local_repo]
      opts[:local_repo] = File.expand_path(opts[:local_repo])
    end
  else
    if @current_resolver
      opts = @current_resolver.opts
    else
      opts = {}
    end
  end
  
  if @current_resolver.nil? || opts != @current_resolver.opts
    @current_resolver = LockJar::Resolver.new( opts )
  end
  
  @current_resolver
end