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
170 lines (133 loc) · 5 KB
/
dependency_cache_task.rb
File metadata and controls
170 lines (133 loc) · 5 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
# 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 '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
configurations = component_ids.map { |component_id| configurations(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]) { |r| augment_repository_root r }
end
def cache
JavaBuildpack::Util::Cache::DownloadCache.new(Pathname.new("#{STAGING_DIR}/resources/cache")).freeze
end
def cache_task(uri)
task uri do |t|
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(configuration)
configurations = []
if repository_configuration?(configuration)
configurations << configuration
else
configuration.values.each { |v| configurations << configurations(v) if v.is_a? Hash }
end
configurations
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])]
@cache.get(index_configuration[:uri]) do |f|
index = YAML.load f
found_version = version(configuration, index)
rake_output_message "Unable to resolve version '#{configuration['version']}' for platform '#{index_configuration[:platform]}'" if found_version.nil?
uris << index[found_version.to_s] unless found_version.nil?
end
end
end
uris
end
def version(configuration, index)
JavaBuildpack::Repository::VersionResolver.resolve(JavaBuildpack::Util::TokenizedVersion.new(configuration['version']), index.keys)
end
end
end