Skip to content

Commit

Permalink
CodeClimate
Browse files Browse the repository at this point in the history
This change refactors some code to remove duplication.
  • Loading branch information
nebhale committed Apr 11, 2014
1 parent 2d24349 commit e9a6334
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 79 deletions.
13 changes: 11 additions & 2 deletions lib/java_buildpack/container/dist_zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/java_buildpack/container/groovy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/java_buildpack/container/ratpack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/java_buildpack/container/spring_boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions lib/java_buildpack/util/jar_finder.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 4 additions & 31 deletions lib/java_buildpack/util/ratpack_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 4 additions & 31 deletions lib/java_buildpack/util/spring_boot_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions spec/java_buildpack/util/ratpack_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions spec/java_buildpack/util/spring_boot_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e9a6334

Please sign in to comment.