forked from cloudfoundry/java-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependency_cache_task.rb
More file actions
209 lines (167 loc) · 6.54 KB
/
dependency_cache_task.rb
File metadata and controls
209 lines (167 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
209
# Encoding: utf-8
# Cloud Foundry Java Buildpack
# Copyright (c) 2014 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'java_buildpack/logging/logger_factory'
require 'java_buildpack/repository/version_resolver'
require 'java_buildpack/util/configuration_utils'
require 'java_buildpack/util/cache/download_cache'
require 'java_buildpack/util/snake_case'
require 'monitor'
require 'rake/tasklib'
require 'rakelib/package'
require 'pathname'
require 'yaml'
module Package
class DependencyCacheTask < Rake::TaskLib
include Package
def initialize
return unless BUILDPACK_VERSION.offline
JavaBuildpack::Logging::LoggerFactory.instance.setup "#{BUILD_DIR}/"
@default_repository_root = default_repository_root
@cache = cache
@monitor = Monitor.new
configurations = component_ids.map { |component_id| component_configuration(component_id) }.flatten
uris(configurations).each { |uri| multitask PACKAGE_NAME => [cache_task(uri)] }
end
private
ARCHITECTURE_PATTERN = /\{architecture\}/.freeze
DEFAULT_REPOSITORY_ROOT_PATTERN = /\{default.repository.root\}/.freeze
PLATFORM_PATTERN = /\{platform\}/.freeze
private_constant :ARCHITECTURE_PATTERN, :DEFAULT_REPOSITORY_ROOT_PATTERN, :PLATFORM_PATTERN
def augment(raw, key, pattern, candidates, &block)
if raw.respond_to? :at
raw.map(&block)
else
if raw[:uri] =~ pattern
candidates.map do |candidate|
dup = raw.clone
dup[key] = candidate
dup[:uri] = raw[:uri].gsub pattern, candidate
dup
end
else
raw
end
end
end
def augment_architecture(raw)
augment(raw, :architecture, ARCHITECTURE_PATTERN, ARCHITECTURES) { |r| augment_architecture r }
end
def augment_path(raw)
if raw.respond_to? :at
raw.map { |r| augment_path r }
else
raw[:uri] = "#{raw[:uri].chomp('/')}/index.yml"
raw
end
end
def augment_platform(raw)
augment(raw, :platform, PLATFORM_PATTERN, PLATFORMS) { |r| augment_platform r }
end
def augment_repository_root(raw)
augment(raw, :repository_root, DEFAULT_REPOSITORY_ROOT_PATTERN, [@default_repository_root]) do |r|
augment_repository_root r
end
end
def cache
JavaBuildpack::Util::Cache::DownloadCache.new(Pathname.new("#{STAGING_DIR}/resources/cache")).freeze
end
def cache_task(uri)
task uri do |t|
@monitor.synchronize { rake_output_message "Caching #{t.name}" }
cache.get(t.name) {}
end
uri
end
def component_ids
configuration('components').values.flatten.map { |component| component.split('::').last.snake_case }
end
def configuration(id)
JavaBuildpack::Util::ConfigurationUtils.load id, false
end
def configurations(component_id, configuration, sub_component_id = nil)
configurations = []
if repository_configuration?(configuration)
configuration['component_id'] = component_id
configuration['sub_component_id'] = sub_component_id if sub_component_id
configurations << configuration
else
configuration.each { |k, v| configurations << configurations(component_id, v, k) if v.is_a? Hash }
end
configurations
end
def component_configuration(component_id)
configurations(component_id, configuration(component_id))
end
def default_repository_root
configuration('repository')['default_repository_root'].chomp('/')
end
def index_configuration(configuration)
[configuration['repository_root']]
.map { |r| { uri: r } }
.map { |r| augment_repository_root r }
.map { |r| augment_platform r }
.map { |r| augment_architecture r }
.map { |r| augment_path r }.flatten
end
def repository_configuration?(configuration)
configuration['version'] && configuration['repository_root']
end
def uris(configurations)
uris = []
configurations.each do |configuration|
index_configuration(configuration).each do |index_configuration|
multitask PACKAGE_NAME => [cache_task(index_configuration[:uri])]
get_from_cache(configuration, index_configuration, uris)
end
end
uris
end
def get_from_cache(configuration, index_configuration, uris)
@cache.get(index_configuration[:uri]) do |f|
index = YAML.load f
found_version = version(configuration, index)
pin_version(configuration, found_version.to_s) if ENV['PINNED'].to_b
if found_version.nil?
rake_output_message "Unable to resolve version '#{configuration['version']}' for platform " \
"'#{index_configuration[:platform]}'"
end
uris << index[found_version.to_s] unless found_version.nil?
end
end
def pin_version(old_configuration, version)
component_id = old_configuration['component_id']
sub_component_id = old_configuration['sub_component_id']
rake_output_message "Pinning #{sub_component_id ? sub_component_id : component_id} version to #{version}"
configuration_to_update = JavaBuildpack::Util::ConfigurationUtils.load(component_id, true)
update_configuration(configuration_to_update, version, sub_component_id)
JavaBuildpack::Util::ConfigurationUtils.write(component_id, configuration_to_update, true)
end
def update_configuration(config, version, sub_component)
if sub_component.nil?
config['version'] = version
elsif config.key?(sub_component)
config[sub_component]['version'] = version
else
config.values.each { |v| update_configuration(v, version, sub_component) if v.is_a? Hash }
end
end
def version(configuration, index)
JavaBuildpack::Repository::VersionResolver.resolve(
JavaBuildpack::Util::TokenizedVersion.new(configuration['version']), index.keys)
end
end
end