|
| 1 | +# Encoding: utf-8 |
| 2 | +# Cloud Foundry Java Buildpack |
| 3 | +# Copyright (c) 2014 the original author or authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) |
| 18 | + |
| 19 | +require 'java_buildpack/logging/logger_factory' |
| 20 | +require 'java_buildpack/repository/version_resolver' |
| 21 | +require 'java_buildpack/util/configuration_utils' |
| 22 | +require 'java_buildpack/util/cache/download_cache' |
| 23 | +require 'java_buildpack/util/snake_case' |
| 24 | +require 'offline' |
| 25 | +require 'pathname' |
| 26 | +require 'yaml' |
| 27 | + |
| 28 | +module Offline |
| 29 | + |
| 30 | + class DependencyCacheTask < Rake::TaskLib |
| 31 | + include Offline |
| 32 | + |
| 33 | + attr_reader :targets |
| 34 | + |
| 35 | + def initialize |
| 36 | + JavaBuildpack::Logging::LoggerFactory.setup "#{BUILD_DIR}/" |
| 37 | + |
| 38 | + @default_repository_root = configuration('repository')['default_repository_root'].chomp('/') |
| 39 | + @cache = cache |
| 40 | + |
| 41 | + configurations = component_ids.map { |component_id| configurations(configuration(component_id)) }.flatten |
| 42 | + @targets = uris(configurations).each { |uri| create_task(uri) } |
| 43 | + end |
| 44 | + |
| 45 | + end |
| 46 | + |
| 47 | + private |
| 48 | + |
| 49 | + ARCHITECTURE_PATTERN = /\{architecture\}/.freeze |
| 50 | + |
| 51 | + DEFAULT_REPOSITORY_ROOT_PATTERN = /\{default.repository.root\}/.freeze |
| 52 | + |
| 53 | + PLATFORM_PATTERN = /\{platform\}/.freeze |
| 54 | + |
| 55 | + def augment_architecture(raw) |
| 56 | + if raw.respond_to? :map |
| 57 | + raw.map { |r| augment_architecture r } |
| 58 | + else |
| 59 | + raw =~ ARCHITECTURE_PATTERN ? ARCHITECTURES.map { |p| raw.gsub ARCHITECTURE_PATTERN, p } : raw |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def augment_path(raw) |
| 64 | + if raw.respond_to? :map |
| 65 | + raw.map { |r| augment_path r } |
| 66 | + else |
| 67 | + "#{raw.chomp('/')}/index.yml" |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def augment_platform(raw) |
| 72 | + if raw.respond_to? :map |
| 73 | + raw.map { |r| augment_platform r } |
| 74 | + else |
| 75 | + raw =~ PLATFORM_PATTERN ? PLATFORMS.map { |p| raw.gsub PLATFORM_PATTERN, p } : raw |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + def augment_repository_root(raw) |
| 80 | + if raw.respond_to? :map |
| 81 | + raw.map { |r| augment_repository_root r } |
| 82 | + else |
| 83 | + raw.gsub DEFAULT_REPOSITORY_ROOT_PATTERN, @default_repository_root |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + def cache |
| 88 | + JavaBuildpack::Util::Cache::DownloadCache.new(Pathname.new("#{STAGING_DIR}/resources/cache")).freeze |
| 89 | + end |
| 90 | + |
| 91 | + def component_ids |
| 92 | + configuration('components').values.flatten.map { |component| component.split('::').last.snake_case } |
| 93 | + end |
| 94 | + |
| 95 | + def configuration(id) |
| 96 | + JavaBuildpack::Util::ConfigurationUtils.load(id, false) |
| 97 | + end |
| 98 | + |
| 99 | + def configurations(configuration) |
| 100 | + configurations = [] |
| 101 | + |
| 102 | + if repository_configuration?(configuration) |
| 103 | + configurations << configuration |
| 104 | + else |
| 105 | + configurations << configuration.values.map { |v| configurations(v) } |
| 106 | + end |
| 107 | + |
| 108 | + configurations |
| 109 | + end |
| 110 | + |
| 111 | + def index_uris(configuration) |
| 112 | + [configuration['repository_root']] |
| 113 | + .map { |r| augment_repository_root r } |
| 114 | + .map { |r| augment_platform r } |
| 115 | + .map { |r| augment_architecture r } |
| 116 | + .map { |r| augment_path r }.flatten |
| 117 | + end |
| 118 | + |
| 119 | + def repository_configuration?(configuration) |
| 120 | + configuration['version'] && configuration['repository_root'] |
| 121 | + end |
| 122 | + |
| 123 | + def uris(configurations) |
| 124 | + uris = [] |
| 125 | + |
| 126 | + configurations.each do |configuration| |
| 127 | + index_uris(configuration).each do |index_uri| |
| 128 | + @cache.get(index_uri) do |file| |
| 129 | + index = YAML.load(file) |
| 130 | + uris << index[version(configuration, index).to_s] |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + uris |
| 136 | + end |
| 137 | + |
| 138 | + def version(configuration, index) |
| 139 | + JavaBuildpack::Repository::VersionResolver.resolve(JavaBuildpack::Util::TokenizedVersion.new(configuration['version']), index.keys) |
| 140 | + end |
| 141 | + |
| 142 | + def create_task(uri) |
| 143 | + task uri do |t| |
| 144 | + puts "Caching #{t.name}" |
| 145 | + cache.get(t.name) |
| 146 | + end |
| 147 | + |
| 148 | + uri |
| 149 | + end |
| 150 | + |
| 151 | +end |
0 commit comments