|
| 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 'java_buildpack/component/base_component' |
| 18 | +require 'java_buildpack/container' |
| 19 | +require 'java_buildpack/util/dash_case' |
| 20 | +require 'java_buildpack/util/ratpack_utils' |
| 21 | + |
| 22 | +module JavaBuildpack::Container |
| 23 | + |
| 24 | + # Encapsulates the detect, compile, and release functionality for Ratpack applications. |
| 25 | + class Ratpack < JavaBuildpack::Component::BaseComponent |
| 26 | + |
| 27 | + def initialize(context) |
| 28 | + super(context) |
| 29 | + end |
| 30 | + |
| 31 | + def detect |
| 32 | + JavaBuildpack::Util::RatpackUtils.is?(@application) ? id(version) : nil |
| 33 | + end |
| 34 | + |
| 35 | + def compile |
| 36 | + @droplet.additional_libraries.link_to lib_dir |
| 37 | + end |
| 38 | + |
| 39 | + def release |
| 40 | + @droplet.java_opts.add_system_property 'ratpack.port', '$PORT' |
| 41 | + |
| 42 | + [ |
| 43 | + @droplet.java_home.as_env_var, |
| 44 | + @droplet.java_opts.as_env_var, |
| 45 | + "$PWD/#{start_script.relative_path_from(@application.root)}" |
| 46 | + ].flatten.compact.join(' ') |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + RATPACK_CORE_FILE_PATTERN = 'lib/ratpack-core-*.jar'.freeze |
| 52 | + |
| 53 | + def id(version) |
| 54 | + "#{Ratpack.to_s.dash_case}=#{version}" |
| 55 | + end |
| 56 | + |
| 57 | + def lib_dir |
| 58 | + root + 'lib' |
| 59 | + end |
| 60 | + |
| 61 | + def root |
| 62 | + roots = (@droplet.root + '*').glob.select { |child| child.directory? } |
| 63 | + roots.size == 1 ? roots.first : @droplet.root |
| 64 | + end |
| 65 | + |
| 66 | + def start_script |
| 67 | + candidates = (root + 'bin/*').glob |
| 68 | + candidates.size == 1 ? candidates.first : candidates.find { |candidate| Pathname.new("#{candidate}.bat").exist? } |
| 69 | + end |
| 70 | + |
| 71 | + def version |
| 72 | + (root + RATPACK_CORE_FILE_PATTERN).glob.first.to_s.match(/.*ratpack-core-(.*)\.jar/)[1] |
| 73 | + end |
| 74 | + |
| 75 | + end |
| 76 | + |
| 77 | +end |
0 commit comments