lock_jar 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,177 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require 'lock_jar/maven'
17
+ require 'lock_jar/domain/dsl_helper'
18
+ require 'lock_jar/domain/artifact'
19
+
20
+ module LockJar
21
+ module Domain
22
+ class Dsl
23
+
24
+ DEFAULT_GROUP = ['default']
25
+
26
+ attr_accessor :artifacts, :remote_repositories, :local_repository, :groups,
27
+ :maps, :excludes, :merged
28
+
29
+ attr_reader :file_path
30
+
31
+ class << self
32
+
33
+ def create(jarfile = nil, &blk)
34
+ builder = new
35
+ evaluate(builder, jarfile, &blk)
36
+ end
37
+
38
+ def evaluate(builder, jarfile = nil, &blk)
39
+ if jarfile.nil? && blk.nil?
40
+ raise "jarfile or block must be set"
41
+ end
42
+
43
+ if jarfile
44
+ builder.instance_eval(DslHelper.read_file(jarfile.to_s), jarfile.to_s, 1)
45
+ end
46
+
47
+ if blk
48
+ builder.instance_eval(&blk)
49
+ end
50
+
51
+ builder
52
+ end
53
+
54
+ end
55
+
56
+ def initialize
57
+
58
+ @remote_repositories = []
59
+ @artifacts = { 'default' => [] }
60
+
61
+ @group_changed = false
62
+
63
+ @present_group = 'default'
64
+
65
+ @local_repository = nil
66
+ @maps = {}
67
+
68
+ @excludes = []
69
+
70
+ @merged = []
71
+ end
72
+
73
+ def exclude(*notations)
74
+ @excludes += notations
75
+ end
76
+
77
+ def jar(notation, *args)
78
+ opts = {}
79
+ if args.last.is_a? Hash
80
+ opts.merge!( args.last )
81
+ end
82
+
83
+ artifact = Jar.new( notation )
84
+
85
+ assign_groups( artifact, opts[:group] )
86
+ end
87
+
88
+ def local( *args )
89
+ if !args.empty? && File.directory?( File.expand_path( args.first ) )
90
+ warn "[DEPRECATION] `local` to set local_repository is deprecated. Please use `local_repo` instead."
91
+ local_repo(args.first)
92
+ else
93
+ # XXX: support local artifacts
94
+ end
95
+ end
96
+
97
+ def local_repo( path )
98
+ @local_repository = path
99
+ end
100
+
101
+ alias_method :name, :local_repository
102
+
103
+ # Map a dependency to another dependency or local directory.
104
+ def map( notation, *args )
105
+ @maps[notation] = args
106
+ end
107
+
108
+ #
109
+ def pom(path, *args)
110
+ unless path =~ /\.xml$/i
111
+ raise "#{path} is an invalid pom path"
112
+ end
113
+
114
+ opts = { :scopes => ['runtime', 'compile'] }
115
+
116
+ if args.last.is_a? Hash
117
+ opts.merge! args.last
118
+ end
119
+
120
+ artifact = Pom.new( path, opts[:scopes] )
121
+
122
+ assign_groups( artifact, opts[:groups] )
123
+ end
124
+
125
+ def remote_repo( url, opts = {} )
126
+ @remote_repositories << url
127
+ end
128
+ alias_method :remote_repository, :remote_repo
129
+ alias_method :repository, :remote_repo
130
+
131
+ def group(*groups, &blk)
132
+ @group_changed = true
133
+ groups.each do |group|
134
+ @present_group = group.to_s
135
+ yield
136
+ end
137
+ @group_changed = false
138
+ @present_group = 'default'
139
+ end
140
+
141
+ # @deprecated Please use {#group} instead
142
+ def scope(*scopes, &blk)
143
+ warn "[DEPRECATION] `scope` is deprecated. Please use `group` instead."
144
+ group(*scopes,&blk)
145
+ end
146
+
147
+ private
148
+ def assign_groups(artifact, groups=nil)
149
+
150
+ if groups
151
+
152
+ unless groups.is_a? Array
153
+ groups = [groups]
154
+ end
155
+
156
+ # include present group if within a group block
157
+ if @group_changed
158
+ groups << @present_group
159
+ end
160
+
161
+ else
162
+ groups = [@present_group]
163
+ end
164
+
165
+ if artifact
166
+ groups.uniq.each do |group|
167
+ group_key = group.to_s
168
+ @artifacts[group_key] = [] unless @artifacts[group_key]
169
+ @artifacts[group_key] << artifact
170
+ end
171
+ end
172
+
173
+ end
174
+
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,66 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ module LockJar
17
+ module Domain
18
+ class DslHelper
19
+
20
+ class << self
21
+
22
+ #
23
+ # Merge LockJar::Domain::Dsl
24
+ #
25
+ def merge( into_dsl, from_dsl )
26
+ into_dsl.remote_repositories = (into_dsl.remote_repositories + from_dsl.remote_repositories).uniq
27
+
28
+ from_dsl.artifacts.each do |group, artifacts|
29
+ group_artifacts = into_dsl.artifacts[group] || []
30
+ artifacts.each do |art|
31
+ unless group_artifacts.include? art
32
+ group_artifacts << art
33
+ end
34
+ end
35
+ into_dsl.artifacts[group] = group_artifacts
36
+ end
37
+
38
+ from_dsl.maps.each do |artifact,paths|
39
+ existing_map = into_dsl.maps[artifact]
40
+ if existing_map
41
+ into_dsl.maps[artifact] = (existing_map + paths).uniq
42
+ else
43
+ into_dsl.maps[artifact] = paths
44
+ end
45
+ end
46
+
47
+ from_dsl.excludes.each do |exclude|
48
+ unless into_dsl.include? exclude
49
+ into_dsl.excludes << exclude
50
+ end
51
+ end
52
+
53
+ if from_dsl.file_path
54
+ into_dsl.merged << from_dsl.file_path
55
+ end
56
+
57
+ into_dsl
58
+ end
59
+
60
+ def read_file(file)
61
+ File.open(file, "rb") { |f| f.read }
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,45 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require 'lock_jar/maven'
17
+ require 'lock_jar/domain/jarfile_dsl'
18
+ require 'lock_jar/domain/dsl_helper'
19
+
20
+ module LockJar
21
+ module Domain
22
+ class GemDsl < JarfileDsl
23
+
24
+ attr_accessor :gem_dir
25
+
26
+ class << self
27
+ alias :overriden_create :create
28
+ def create(spec, jarfile)
29
+ builder = new
30
+ builder.gem_dir = spec.gem_dir
31
+
32
+ jarfile = File.join( spec.gem_dir, jarfile )
33
+ builder.file_path = "gem:#{spec.name}:#{jarfile.gsub( "#{spec.base_dir}/", "" )}"
34
+
35
+ evaluate(builder, jarfile)
36
+ end
37
+ end
38
+
39
+ alias :overriden_pom :pom
40
+ def pom(path, *args)
41
+ overriden_pom( File.join( gem_dir, path), *args)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,37 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require 'lock_jar/maven'
17
+ require 'lock_jar/domain/dsl_helper'
18
+
19
+ module LockJar
20
+ module Domain
21
+ class JarfileDsl < Dsl
22
+
23
+ attr_accessor :file_path
24
+
25
+ class << self
26
+ alias :overriden_create :create
27
+ def create(jarfile)
28
+ builder = new
29
+ builder.file_path = jarfile
30
+
31
+ evaluate(builder, jarfile)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,114 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require "yaml"
17
+ require 'lock_jar/version'
18
+
19
+ module LockJar
20
+ module Domain
21
+ class Lockfile
22
+
23
+ attr_accessor :local_repository, :maps, :excludes, :remote_repositories,
24
+ :version, :groups, :gems, :merged
25
+ attr_reader :force_utf8
26
+
27
+
28
+ def self.read( path )
29
+ lockfile = Lockfile.new
30
+
31
+ lock_data = YAML.load_file( path )
32
+
33
+ lockfile.version = lock_data['version'] || LockJar::VERSION
34
+
35
+ lockfile.local_repository = lock_data['local_repository']
36
+ lockfile.merged = lock_data['merged'] || []
37
+ lockfile.maps = lock_data['maps'] || []
38
+ lockfile.excludes = lock_data['excludes'] || []
39
+ lockfile.groups = lock_data['groups'] || lock_data['scopes'] || {}
40
+ lockfile.remote_repositories = lock_data['remote_repositories'] || lock_data['repositories'] || []
41
+ lockfile.gems = lock_data['gems'] || []
42
+ lockfile
43
+ end
44
+
45
+ def initialize
46
+ @force_utf8 ||= RUBY_VERSION =~ /^1.9/
47
+ @groups = { 'default' => {} }
48
+ @maps = []
49
+ @excludes = []
50
+ @remote_repositories = []
51
+ @gems = []
52
+ @merged = []
53
+
54
+ @version = LockJar::VERSION # default version
55
+ end
56
+
57
+ def to_hash
58
+ lock_data = { 'version' => @version }
59
+
60
+ unless local_repository.nil?
61
+ lock_data['local_repository'] = local_repository
62
+
63
+ if @force_utf8
64
+ lock_data['local_repository'] = lock_data['local_repository'].force_encoding("UTF-8")
65
+ end
66
+ end
67
+
68
+ unless merged.empty?
69
+ lock_data['merged'] = merged
70
+ end
71
+
72
+ if maps.size > 0
73
+ lock_data['maps'] = maps
74
+ end
75
+
76
+ if excludes.size > 0
77
+ lock_data['excludes'] = excludes
78
+
79
+ if @force_utf8
80
+ lock_data['excludes'].map! { |exclude| exclude.force_encoding("UTF-8") }
81
+ end
82
+ end
83
+
84
+ unless gems.empty?
85
+ lock_data['gems'] = gems
86
+ end
87
+
88
+ lock_data['groups'] = groups
89
+
90
+ #if @force_utf8
91
+ # lock_data['groups'].each do |group, group_notations|
92
+ # group_notations.map! { |notation| notation.force_encoding("UTF-8") }
93
+ # end
94
+ #end
95
+
96
+ if remote_repositories.size > 0
97
+ lock_data['remote_repositories'] = remote_repositories
98
+ end
99
+
100
+ lock_data
101
+ end
102
+
103
+ def to_yaml
104
+ to_hash.to_yaml
105
+ end
106
+
107
+ def write( path )
108
+ File.open( path, "w") do |f|
109
+ f.write( to_yaml )
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -30,7 +30,6 @@ module LockJar
30
30
  # Get the version of a POM
31
31
  #
32
32
  # @param [String] pom_path path to the pom
33
- # @param [Hash] options
34
33
  #
35
34
  # @return [String] version of POM
36
35
  #
@@ -39,6 +38,19 @@ module LockJar
39
38
  maven.version()
40
39
  end
41
40
 
41
+ #
42
+ # Get dependencies of a Pom
43
+ #
44
+ # @param [String] pom_path path to the pom
45
+ # @param [Array] scopes
46
+ #
47
+ # @return [String] version of POM
48
+ #
49
+ def dependencies( pom_path, scopes = ['compile', 'runtime'] )
50
+ maven = Naether::Maven.create_from_pom( pom_path )
51
+ maven.dependencies(scopes)
52
+ end
53
+
42
54
  #
43
55
  # Write a POM from list of notations
44
56
  #
@@ -1,13 +1,46 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
1
15
 
16
+ #
17
+ # Registry of resources loaded by LockJar
18
+ #
19
+ # @author Michael Guymon
20
+ #
2
21
  class LockJar::Registry
3
22
  include Singleton
4
23
 
5
24
  attr_accessor :loaded_gems
6
25
  attr_accessor :loaded_jars
26
+ attr_accessor :loaded_lockfiles
7
27
 
8
28
  def initialize
9
29
  @loaded_gems = {}
10
30
  @loaded_jars = []
31
+ @loaded_lockfiles = []
32
+ end
33
+
34
+ def lockfile_registered?( lockfile )
35
+ if lockfile
36
+ @loaded_lockfiles.include? File.expand_path( lockfile )
37
+ end
38
+ end
39
+
40
+ def register_lockfile( lockfile )
41
+ if lockfile && !lockfile_registered?( lockfile )
42
+ @loaded_lockfiles << File.expand_path( lockfile )
43
+ end
11
44
  end
12
45
 
13
46
  def register_jars( jars )
@@ -20,9 +53,17 @@ class LockJar::Registry
20
53
  end
21
54
  end
22
55
 
56
+ def register_gem( spec )
57
+ @loaded_gems[spec.name] = spec
58
+ end
59
+
60
+ def gem_registered?( spec )
61
+ !@loaded_gems[spec.name].nil?
62
+ end
63
+
23
64
  def load_gem( spec )
24
- if @loaded_gems[spec.name].nil?
25
- @loaded_gems[spec.name] = spec
65
+ unless gem_registered?( spec )
66
+ register_gem(spec)
26
67
  gem_dir = spec.gem_dir
27
68
 
28
69
  lockfile = File.join( gem_dir, "Jarfile.lock" )
@@ -37,9 +78,9 @@ class LockJar::Registry
37
78
  def load_jars_for_gems
38
79
  specs = Gem.loaded_specs
39
80
  if specs
40
- gems_to_check = specs.keys - @loaded_gems.keys
41
- if gems_to_check.size > 0
42
- gems_to_check.each do |key|
81
+ gems = specs.keys - @loaded_gems.keys
82
+ if gems.size > 0
83
+ gems.each do |key|
43
84
  spec = specs[key]
44
85
  load_gem( spec )
45
86
  end
@@ -22,7 +22,7 @@ module LockJar
22
22
 
23
23
  attr_reader :opts
24
24
  attr_reader :naether
25
-
25
+
26
26
  def initialize( opts = {} )
27
27
 
28
28
  @opts = opts
@@ -53,6 +53,10 @@ module LockJar
53
53
  @naether.dependencies_notation
54
54
  end
55
55
 
56
+ def dependencies_graph
57
+ @naether.dependencies_graph
58
+ end
59
+
56
60
  def download( dependencies )
57
61
  @naether.download_artifacts( dependencies )
58
62
  end