diff --git a/lib/java_buildpack/container/dist_zip.rb b/lib/java_buildpack/container/dist_zip.rb index dbd696ce79..2851225fb8 100644 --- a/lib/java_buildpack/container/dist_zip.rb +++ b/lib/java_buildpack/container/dist_zip.rb @@ -27,6 +27,15 @@ module Container # Encapsulates the detect, compile, and release functionality for +distZip+ style applications. class DistZip < JavaBuildpack::Container::DistZipLike + # Creates an instance + # + # @param [Hash] context a collection of utilities used the component + def initialize(context) + super(context) + @ratpack_utils = JavaBuildpack::Util::RatpackUtils.new + @spring_boot_utils = JavaBuildpack::Util::SpringBootUtils.new + end + protected # (see JavaBuildpack::Container::DistZipLike#id) @@ -39,8 +48,8 @@ def supports? start_script(root) && start_script(root).exist? && jars? && - !JavaBuildpack::Util::RatpackUtils.is?(@application) && - !JavaBuildpack::Util::SpringBootUtils.is?(@application) && + !@ratpack_utils.is?(@application) && + !@spring_boot_utils.is?(@application) && !JavaBuildpack::Util::Play::Factory.create(@droplet) end diff --git a/lib/java_buildpack/container/groovy.rb b/lib/java_buildpack/container/groovy.rb index 51a2534d92..43486aee4a 100644 --- a/lib/java_buildpack/container/groovy.rb +++ b/lib/java_buildpack/container/groovy.rb @@ -39,6 +39,7 @@ class Groovy < JavaBuildpack::Component::VersionedDependencyComponent # @param [Hash] context a collection of utilities used the component def initialize(context) @logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger Groovy + @ratpack_utils = JavaBuildpack::Util::RatpackUtils.new super(context) { |candidate_version| candidate_version.check_size(3) } end @@ -66,7 +67,7 @@ def release # (see JavaBuildpack::Component::VersionedDependencyComponent#supports?) def supports? JavaBuildpack::Util::ClassFileUtils.class_files(@application).empty? && main_groovy && - !JavaBuildpack::Util::RatpackUtils.is?(@application) + !@ratpack_utils.is?(@application) end private diff --git a/lib/java_buildpack/container/ratpack.rb b/lib/java_buildpack/container/ratpack.rb index dae7d9be9e..7ed3bed548 100644 --- a/lib/java_buildpack/container/ratpack.rb +++ b/lib/java_buildpack/container/ratpack.rb @@ -25,6 +25,14 @@ module Container # Encapsulates the detect, compile, and release functionality for Ratpack applications. class Ratpack < JavaBuildpack::Container::DistZipLike + # Creates an instance + # + # @param [Hash] context a collection of utilities used the component + def initialize(context) + super(context) + @ratpack_utils = JavaBuildpack::Util::RatpackUtils.new + end + protected # (see JavaBuildpack::Container::DistZipLike#id) @@ -34,13 +42,13 @@ def id # (see JavaBuildpack::Container::DistZipLike#supports?) def supports? - JavaBuildpack::Util::RatpackUtils.is? @application + @ratpack_utils.is? @application end private def version - JavaBuildpack::Util::RatpackUtils.version @application + @ratpack_utils.version @application end end diff --git a/lib/java_buildpack/container/spring_boot.rb b/lib/java_buildpack/container/spring_boot.rb index 5d65506d01..459dd46578 100644 --- a/lib/java_buildpack/container/spring_boot.rb +++ b/lib/java_buildpack/container/spring_boot.rb @@ -25,6 +25,14 @@ module Container # Encapsulates the detect, compile, and release functionality for Spring Boot applications. class SpringBoot < JavaBuildpack::Container::DistZipLike + # Creates an instance + # + # @param [Hash] context a collection of utilities used the component + def initialize(context) + super(context) + @spring_boot_utils = JavaBuildpack::Util::SpringBootUtils.new + end + # (see JavaBuildpack::Container::DistZipLike#release) def release "SERVER_PORT=$PORT #{super}" @@ -39,13 +47,13 @@ def id # (see JavaBuildpack::Container::DistZipLike#supports?) def supports? - JavaBuildpack::Util::SpringBootUtils.is? @application + @spring_boot_utils.is? @application end private def version - JavaBuildpack::Util::SpringBootUtils.version @application + @spring_boot_utils.version @application end end diff --git a/lib/java_buildpack/util/jar_finder.rb b/lib/java_buildpack/util/jar_finder.rb new file mode 100644 index 0000000000..7e2ea914d9 --- /dev/null +++ b/lib/java_buildpack/util/jar_finder.rb @@ -0,0 +1,58 @@ +# Encoding: utf-8 +# Cloud Foundry Java Buildpack +# Copyright 2013 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. + +require 'pathname' +require 'java_buildpack/util' + +module JavaBuildpack + module Util + + # A base class for utilities that need to find a JAR file + class JarFinder + + # Creates a new instance + # + # @param [RegExp] pattern the pattern to use when filtering JAR files + def initialize(pattern) + @pattern = pattern + end + + # Indicates whether an application has a JAR file + # + # @param [Application] application the application to search + # @return [Boolean] +true+ if the application has a JAR file, +false+ otherwise + def is?(application) + jar application + end + + # The version of of the JAR file used by the application + # + # @param [Application] application the application to search + # @return [String] the version of the JAR file used by the application + def version(application) + jar(application).to_s.match(@pattern)[1] + end + + private + + def jar(application) + (application.root + '**/lib/*.jar').glob.find { |jar| jar.to_s =~ @pattern } + end + + end + + end +end diff --git a/lib/java_buildpack/util/ratpack_utils.rb b/lib/java_buildpack/util/ratpack_utils.rb index 3b101a9381..acb88286b3 100644 --- a/lib/java_buildpack/util/ratpack_utils.rb +++ b/lib/java_buildpack/util/ratpack_utils.rb @@ -16,43 +16,16 @@ require 'pathname' require 'java_buildpack/util' +require 'java_buildpack/util/jar_finder' module JavaBuildpack module Util # Utilities for dealing with Ratpack applications - class RatpackUtils - - private_class_method :new - - class << self - - # Indicates whether a application is a Ratpack application - # - # @param [Application] application the application to search - # @return [Boolean] +true+ if the application is a Ratpack application, +false+ otherwise - def is?(application) - jar application - end - - # The version of Ratpack used by the application - # - # @param [Application] application the application to search - # @return [String] the version of Ratpack used by the application - def version(application) - jar(application).to_s.match(RATPACK_CORE_FILE_PATTERN)[1] - end - - private - - RATPACK_CORE_FILE_PATTERN = /.*ratpack-core-(.*)\.jar/.freeze - - private_constant :RATPACK_CORE_FILE_PATTERN - - def jar(application) - (application.root + '**/lib/*.jar').glob.find { |jar| jar.to_s =~ RATPACK_CORE_FILE_PATTERN } - end + class RatpackUtils < JarFinder + def initialize + super(/.*ratpack-core-(.*)\.jar/) end end diff --git a/lib/java_buildpack/util/spring_boot_utils.rb b/lib/java_buildpack/util/spring_boot_utils.rb index d54beb721c..451fc5509a 100644 --- a/lib/java_buildpack/util/spring_boot_utils.rb +++ b/lib/java_buildpack/util/spring_boot_utils.rb @@ -16,43 +16,16 @@ require 'pathname' require 'java_buildpack/util' +require 'java_buildpack/util/jar_finder' module JavaBuildpack module Util # Utilities for dealing with Spring Boot applications - class SpringBootUtils - - private_class_method :new - - class << self - - # Indicates whether a application is a Spring Boot application - # - # @param [Application] application the application to search - # @return [Boolean] +true+ if the application is a Spring Boot application, +false+ otherwise - def is?(application) - jar application - end - - # The version of Spring Boot used by the application - # - # @param [Application] application the application to search - # @return [String] the version of Spring Boot used by the application - def version(application) - jar(application).to_s.match(SPRING_BOOT_CORE_FILE_PATTERN)[1] - end - - private - - SPRING_BOOT_CORE_FILE_PATTERN = /.*spring-boot-([^-]*)\.jar/.freeze - - private_constant :SPRING_BOOT_CORE_FILE_PATTERN - - def jar(application) - (application.root + '**/lib/*.jar').glob.find { |jar| jar.to_s =~ SPRING_BOOT_CORE_FILE_PATTERN } - end + class SpringBootUtils < JarFinder + def initialize + super(/.*spring-boot-([^-]*)\.jar/) end end diff --git a/spec/java_buildpack/util/ratpack_utils_spec.rb b/spec/java_buildpack/util/ratpack_utils_spec.rb index 4397e15851..53cee32e81 100644 --- a/spec/java_buildpack/util/ratpack_utils_spec.rb +++ b/spec/java_buildpack/util/ratpack_utils_spec.rb @@ -21,34 +21,36 @@ describe JavaBuildpack::Util::RatpackUtils do include_context 'application_helper' + let(:utils) { described_class.new } + it 'should detect a dist Ratpack application', app_fixture: 'container_ratpack_dist' do - expect(described_class.is?(application)).to be + expect(utils.is?(application)).to be end it 'should detect a staged Ratpack application', app_fixture: 'container_ratpack_staged' do - expect(described_class.is?(application)).to be + expect(utils.is?(application)).to be end it 'should not detect a non-Ratpack application', app_fixture: 'container_main' do - expect(described_class.is?(application)).not_to be + expect(utils.is?(application)).not_to be end it 'should determine the version a dist Ratpack application', app_fixture: 'container_ratpack_dist' do - expect(described_class.version(application)).to match(/0.9.0/) + expect(utils.version(application)).to match(/0.9.0/) end it 'should determine the version a staged Ratpack application', app_fixture: 'container_ratpack_staged' do - expect(described_class.version(application)).to match(/0.9.0/) + expect(utils.version(application)).to match(/0.9.0/) end end diff --git a/spec/java_buildpack/util/spring_boot_utils_spec.rb b/spec/java_buildpack/util/spring_boot_utils_spec.rb index a55f006d15..5b48562413 100644 --- a/spec/java_buildpack/util/spring_boot_utils_spec.rb +++ b/spec/java_buildpack/util/spring_boot_utils_spec.rb @@ -21,34 +21,36 @@ describe JavaBuildpack::Util::SpringBootUtils do include_context 'application_helper' + let(:utils) { described_class.new } + it 'should detect a dist Spring Boot application', app_fixture: 'container_spring_boot_dist' do - expect(described_class.is?(application)).to be + expect(utils.is?(application)).to be end it 'should detect a staged Spring Boot application', app_fixture: 'container_spring_boot_staged' do - expect(described_class.is?(application)).to be + expect(utils.is?(application)).to be end it 'should not detect a non-Spring Boot application', app_fixture: 'container_main' do - expect(described_class.is?(application)).not_to be + expect(utils.is?(application)).not_to be end it 'should determine the version a dist Spring Boot application', app_fixture: 'container_spring_boot_dist' do - expect(described_class.version(application)).to match(/1.0.0.RELEASE/) + expect(utils.version(application)).to match(/1.0.0.RELEASE/) end it 'should determine the version a staged Spring Boot application', app_fixture: 'container_spring_boot_staged' do - expect(described_class.version(application)).to match(/1.0.0.RELEASE/) + expect(utils.version(application)).to match(/1.0.0.RELEASE/) end end