Skip to content

Commit e9a6334

Browse files
committed
CodeClimate
This change refactors some code to remove duplication.
1 parent 2d24349 commit e9a6334

File tree

9 files changed

+113
-79
lines changed

9 files changed

+113
-79
lines changed

lib/java_buildpack/container/dist_zip.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ module Container
2727
# Encapsulates the detect, compile, and release functionality for +distZip+ style applications.
2828
class DistZip < JavaBuildpack::Container::DistZipLike
2929

30+
# Creates an instance
31+
#
32+
# @param [Hash] context a collection of utilities used the component
33+
def initialize(context)
34+
super(context)
35+
@ratpack_utils = JavaBuildpack::Util::RatpackUtils.new
36+
@spring_boot_utils = JavaBuildpack::Util::SpringBootUtils.new
37+
end
38+
3039
protected
3140

3241
# (see JavaBuildpack::Container::DistZipLike#id)
@@ -39,8 +48,8 @@ def supports?
3948
start_script(root) &&
4049
start_script(root).exist? &&
4150
jars? &&
42-
!JavaBuildpack::Util::RatpackUtils.is?(@application) &&
43-
!JavaBuildpack::Util::SpringBootUtils.is?(@application) &&
51+
!@ratpack_utils.is?(@application) &&
52+
!@spring_boot_utils.is?(@application) &&
4453
!JavaBuildpack::Util::Play::Factory.create(@droplet)
4554
end
4655

lib/java_buildpack/container/groovy.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Groovy < JavaBuildpack::Component::VersionedDependencyComponent
3939
# @param [Hash] context a collection of utilities used the component
4040
def initialize(context)
4141
@logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger Groovy
42+
@ratpack_utils = JavaBuildpack::Util::RatpackUtils.new
4243
super(context) { |candidate_version| candidate_version.check_size(3) }
4344
end
4445

@@ -66,7 +67,7 @@ def release
6667
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
6768
def supports?
6869
JavaBuildpack::Util::ClassFileUtils.class_files(@application).empty? && main_groovy &&
69-
!JavaBuildpack::Util::RatpackUtils.is?(@application)
70+
!@ratpack_utils.is?(@application)
7071
end
7172

7273
private

lib/java_buildpack/container/ratpack.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ module Container
2525
# Encapsulates the detect, compile, and release functionality for Ratpack applications.
2626
class Ratpack < JavaBuildpack::Container::DistZipLike
2727

28+
# Creates an instance
29+
#
30+
# @param [Hash] context a collection of utilities used the component
31+
def initialize(context)
32+
super(context)
33+
@ratpack_utils = JavaBuildpack::Util::RatpackUtils.new
34+
end
35+
2836
protected
2937

3038
# (see JavaBuildpack::Container::DistZipLike#id)
@@ -34,13 +42,13 @@ def id
3442

3543
# (see JavaBuildpack::Container::DistZipLike#supports?)
3644
def supports?
37-
JavaBuildpack::Util::RatpackUtils.is? @application
45+
@ratpack_utils.is? @application
3846
end
3947

4048
private
4149

4250
def version
43-
JavaBuildpack::Util::RatpackUtils.version @application
51+
@ratpack_utils.version @application
4452
end
4553

4654
end

lib/java_buildpack/container/spring_boot.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ module Container
2525
# Encapsulates the detect, compile, and release functionality for Spring Boot applications.
2626
class SpringBoot < JavaBuildpack::Container::DistZipLike
2727

28+
# Creates an instance
29+
#
30+
# @param [Hash] context a collection of utilities used the component
31+
def initialize(context)
32+
super(context)
33+
@spring_boot_utils = JavaBuildpack::Util::SpringBootUtils.new
34+
end
35+
2836
# (see JavaBuildpack::Container::DistZipLike#release)
2937
def release
3038
"SERVER_PORT=$PORT #{super}"
@@ -39,13 +47,13 @@ def id
3947

4048
# (see JavaBuildpack::Container::DistZipLike#supports?)
4149
def supports?
42-
JavaBuildpack::Util::SpringBootUtils.is? @application
50+
@spring_boot_utils.is? @application
4351
end
4452

4553
private
4654

4755
def version
48-
JavaBuildpack::Util::SpringBootUtils.version @application
56+
@spring_boot_utils.version @application
4957
end
5058

5159
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

lib/java_buildpack/util/ratpack_utils.rb

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,16 @@
1616

1717
require 'pathname'
1818
require 'java_buildpack/util'
19+
require 'java_buildpack/util/jar_finder'
1920

2021
module JavaBuildpack
2122
module Util
2223

2324
# Utilities for dealing with Ratpack applications
24-
class RatpackUtils
25-
26-
private_class_method :new
27-
28-
class << self
29-
30-
# Indicates whether a application is a Ratpack application
31-
#
32-
# @param [Application] application the application to search
33-
# @return [Boolean] +true+ if the application is a Ratpack application, +false+ otherwise
34-
def is?(application)
35-
jar application
36-
end
37-
38-
# The version of Ratpack used by the application
39-
#
40-
# @param [Application] application the application to search
41-
# @return [String] the version of Ratpack used by the application
42-
def version(application)
43-
jar(application).to_s.match(RATPACK_CORE_FILE_PATTERN)[1]
44-
end
45-
46-
private
47-
48-
RATPACK_CORE_FILE_PATTERN = /.*ratpack-core-(.*)\.jar/.freeze
49-
50-
private_constant :RATPACK_CORE_FILE_PATTERN
51-
52-
def jar(application)
53-
(application.root + '**/lib/*.jar').glob.find { |jar| jar.to_s =~ RATPACK_CORE_FILE_PATTERN }
54-
end
25+
class RatpackUtils < JarFinder
5526

27+
def initialize
28+
super(/.*ratpack-core-(.*)\.jar/)
5629
end
5730

5831
end

lib/java_buildpack/util/spring_boot_utils.rb

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,16 @@
1616

1717
require 'pathname'
1818
require 'java_buildpack/util'
19+
require 'java_buildpack/util/jar_finder'
1920

2021
module JavaBuildpack
2122
module Util
2223

2324
# Utilities for dealing with Spring Boot applications
24-
class SpringBootUtils
25-
26-
private_class_method :new
27-
28-
class << self
29-
30-
# Indicates whether a application is a Spring Boot application
31-
#
32-
# @param [Application] application the application to search
33-
# @return [Boolean] +true+ if the application is a Spring Boot application, +false+ otherwise
34-
def is?(application)
35-
jar application
36-
end
37-
38-
# The version of Spring Boot used by the application
39-
#
40-
# @param [Application] application the application to search
41-
# @return [String] the version of Spring Boot used by the application
42-
def version(application)
43-
jar(application).to_s.match(SPRING_BOOT_CORE_FILE_PATTERN)[1]
44-
end
45-
46-
private
47-
48-
SPRING_BOOT_CORE_FILE_PATTERN = /.*spring-boot-([^-]*)\.jar/.freeze
49-
50-
private_constant :SPRING_BOOT_CORE_FILE_PATTERN
51-
52-
def jar(application)
53-
(application.root + '**/lib/*.jar').glob.find { |jar| jar.to_s =~ SPRING_BOOT_CORE_FILE_PATTERN }
54-
end
25+
class SpringBootUtils < JarFinder
5526

27+
def initialize
28+
super(/.*spring-boot-([^-]*)\.jar/)
5629
end
5730

5831
end

spec/java_buildpack/util/ratpack_utils_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,36 @@
2121
describe JavaBuildpack::Util::RatpackUtils do
2222
include_context 'application_helper'
2323

24+
let(:utils) { described_class.new }
25+
2426
it 'should detect a dist Ratpack application',
2527
app_fixture: 'container_ratpack_dist' do
2628

27-
expect(described_class.is?(application)).to be
29+
expect(utils.is?(application)).to be
2830
end
2931

3032
it 'should detect a staged Ratpack application',
3133
app_fixture: 'container_ratpack_staged' do
3234

33-
expect(described_class.is?(application)).to be
35+
expect(utils.is?(application)).to be
3436
end
3537

3638
it 'should not detect a non-Ratpack application',
3739
app_fixture: 'container_main' do
3840

39-
expect(described_class.is?(application)).not_to be
41+
expect(utils.is?(application)).not_to be
4042
end
4143

4244
it 'should determine the version a dist Ratpack application',
4345
app_fixture: 'container_ratpack_dist' do
4446

45-
expect(described_class.version(application)).to match(/0.9.0/)
47+
expect(utils.version(application)).to match(/0.9.0/)
4648
end
4749

4850
it 'should determine the version a staged Ratpack application',
4951
app_fixture: 'container_ratpack_staged' do
5052

51-
expect(described_class.version(application)).to match(/0.9.0/)
53+
expect(utils.version(application)).to match(/0.9.0/)
5254
end
5355

5456
end

spec/java_buildpack/util/spring_boot_utils_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,36 @@
2121
describe JavaBuildpack::Util::SpringBootUtils do
2222
include_context 'application_helper'
2323

24+
let(:utils) { described_class.new }
25+
2426
it 'should detect a dist Spring Boot application',
2527
app_fixture: 'container_spring_boot_dist' do
2628

27-
expect(described_class.is?(application)).to be
29+
expect(utils.is?(application)).to be
2830
end
2931

3032
it 'should detect a staged Spring Boot application',
3133
app_fixture: 'container_spring_boot_staged' do
3234

33-
expect(described_class.is?(application)).to be
35+
expect(utils.is?(application)).to be
3436
end
3537

3638
it 'should not detect a non-Spring Boot application',
3739
app_fixture: 'container_main' do
3840

39-
expect(described_class.is?(application)).not_to be
41+
expect(utils.is?(application)).not_to be
4042
end
4143

4244
it 'should determine the version a dist Spring Boot application',
4345
app_fixture: 'container_spring_boot_dist' do
4446

45-
expect(described_class.version(application)).to match(/1.0.0.RELEASE/)
47+
expect(utils.version(application)).to match(/1.0.0.RELEASE/)
4648
end
4749

4850
it 'should determine the version a staged Spring Boot application',
4951
app_fixture: 'container_spring_boot_staged' do
5052

51-
expect(described_class.version(application)).to match(/1.0.0.RELEASE/)
53+
expect(utils.version(application)).to match(/1.0.0.RELEASE/)
5254
end
5355

5456
end

0 commit comments

Comments
 (0)