|
| 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 'pathname' |
| 18 | +require 'java_buildpack/util' |
| 19 | + |
| 20 | +module JavaBuildpack |
| 21 | + module Util |
| 22 | + |
| 23 | + # A base class for utilities that need to find a JAR file |
| 24 | + class JarFinder |
| 25 | + |
| 26 | + # Creates a new instance |
| 27 | + # |
| 28 | + # @param [RegExp] pattern the pattern to use when filtering JAR files |
| 29 | + def initialize(pattern) |
| 30 | + @pattern = pattern |
| 31 | + end |
| 32 | + |
| 33 | + # Indicates whether an application has a JAR file |
| 34 | + # |
| 35 | + # @param [Application] application the application to search |
| 36 | + # @return [Boolean] +true+ if the application has a JAR file, +false+ otherwise |
| 37 | + def is?(application) |
| 38 | + jar application |
| 39 | + end |
| 40 | + |
| 41 | + # The version of of the JAR file used by the application |
| 42 | + # |
| 43 | + # @param [Application] application the application to search |
| 44 | + # @return [String] the version of the JAR file used by the application |
| 45 | + def version(application) |
| 46 | + jar(application).to_s.match(@pattern)[1] |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + def jar(application) |
| 52 | + (application.root + '**/lib/*.jar').glob.find { |jar| jar.to_s =~ @pattern } |
| 53 | + end |
| 54 | + |
| 55 | + end |
| 56 | + |
| 57 | + end |
| 58 | +end |
0 commit comments