|
| 1 | +# Encoding: utf-8 |
| 2 | +# Cloud Foundry Java Buildpack |
| 3 | +# Copyright 2013 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 | +require 'fileutils' |
| 18 | +require 'java_buildpack/component' |
| 19 | +require 'java_buildpack/component/base_component' |
| 20 | +require 'java_buildpack/repository/configured_item' |
| 21 | +require 'java_buildpack/util/dash_case' |
| 22 | +require 'tmpdir' |
| 23 | + |
| 24 | +module JavaBuildpack::Component |
| 25 | + |
| 26 | + # A convenience base class for all components that are built modularly. In addition to the functionality inherited |
| 27 | + # from +BaseComponent+ this class also ensures that the collection of modules are iterated over for each lifecycle |
| 28 | + # event. |
| 29 | + class ModularComponent < BaseComponent |
| 30 | + |
| 31 | + # Creates an instance. In addition to the functionality inherited from +BaseComponent+, a +@sub_components+ |
| 32 | + # instance variable is exposed. |
| 33 | + # |
| 34 | + # @param [Hash] context a collection of utilities used by components |
| 35 | + # @param [Block, nil] version_validator an optional version validation block |
| 36 | + def initialize(context, &version_validator) |
| 37 | + super(context) |
| 38 | + @sub_components = supports? ? sub_components(context) : [] |
| 39 | + end |
| 40 | + |
| 41 | + # @macro base_component_detect |
| 42 | + def detect |
| 43 | + supports? ? @sub_components.map { |m| m.detect }.flatten.compact : nil |
| 44 | + end |
| 45 | + |
| 46 | + # @macro base_component_compile |
| 47 | + def compile |
| 48 | + @sub_components.each { |m| m.compile } |
| 49 | + end |
| 50 | + |
| 51 | + # @macro base_component_release |
| 52 | + def release |
| 53 | + @sub_components.map { |m| m.release } |
| 54 | + command |
| 55 | + end |
| 56 | + |
| 57 | + protected |
| 58 | + |
| 59 | + # @!macro [new] modular_component_command |
| 60 | + # The command for this component |
| 61 | + # |
| 62 | + # @return [void, String] components other than containers are not expected to return any value. Container |
| 63 | + # components are expected to return the command required to run the application. |
| 64 | + def command |
| 65 | + fail "Method 'command' must be defined" |
| 66 | + end |
| 67 | + |
| 68 | + # @!macro [new] modular_component_sub_components |
| 69 | + # The sub_components that make up this component |
| 70 | + # |
| 71 | + # @param [Hash] context the context of the component |
| 72 | + # @return [Array<BaseComponent>] a collection of +BaseComponent+s that make up the sub_components of this |
| 73 | + # component |
| 74 | + def sub_components(context) |
| 75 | + fail "Method 'sub_components' must be defined" |
| 76 | + end |
| 77 | + |
| 78 | + # Returns a copy of the context, but with a subset of the original configuration |
| 79 | + # |
| 80 | + # @param [Hash] context the original context of the component |
| 81 | + # @param [String] key the key to get a subset of the context from |
| 82 | + # @return [Hash] context a copy of the original context, but with a subset of the original configuration |
| 83 | + def sub_configuration_context(context, key) |
| 84 | + c = context.clone |
| 85 | + c[:configuration] = context[:configuration][key] |
| 86 | + c |
| 87 | + end |
| 88 | + |
| 89 | + # @!macro [new] modular_component_supports |
| 90 | + # Whether or not this component supports this application |
| 91 | + # |
| 92 | + # @return [Boolean] whether or not this component supports this application |
| 93 | + def supports? |
| 94 | + fail "Method 'supports?' must be defined" |
| 95 | + end |
| 96 | + |
| 97 | + end |
| 98 | + |
| 99 | +end |
0 commit comments