Skip to content

Commit 627371f

Browse files
committed
Prefer FileUtils over shell
This change replaces as many of the calls to shell for file manipulations (e.g. cp, mv, rm) with FileUtils as is possible. [#59117282]
1 parent 69c8f22 commit 627371f

9 files changed

Lines changed: 30 additions & 31 deletions

File tree

.idea/misc.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/java_buildpack/base_component.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'fileutils'
1718
require 'java_buildpack'
1819
require 'java_buildpack/util/application_cache'
1920
require 'java_buildpack/util/library_utils'
@@ -104,7 +105,7 @@ def download(version, uri, description = @component_name, &block)
104105
# +@lib_directory+
105106
# @param [String] description an optional description for the download. Defaults to +@component_name+.
106107
def download_jar(version, uri, jar_name, target_directory = @lib_directory, description = @component_name)
107-
download(version, uri, description) { |file| shell "cp #{file.path} #{File.join(target_directory, jar_name)}" }
108+
download(version, uri, description) { |file| FileUtils.cp file.path, File.join(target_directory, jar_name) }
108109
end
109110

110111
# Returns the additional libraries.

lib/java_buildpack/container/spring_boot_cli.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'fileutils'
1718
require 'java_buildpack/container'
1819
require 'java_buildpack/container/container_utils'
1920
require 'java_buildpack/util/format_duration'
2021
require 'java_buildpack/util/groovy_utils'
2122
require 'java_buildpack/versioned_dependency_component'
22-
require 'tmpdir'
2323

2424
module JavaBuildpack::Container
2525

@@ -60,11 +60,9 @@ def expand(file)
6060
expand_start_time = Time.now
6161
print " Expanding Spring Boot CLI to #{SPRING_BOOT_CLI_HOME} "
6262

63-
Dir.mktmpdir do |tmpdir_root|
64-
shell "rm -rf #{spring_boot_cli_home}"
65-
shell "mkdir -p #{spring_boot_cli_home}"
66-
shell "tar xzf #{file.path} -C #{spring_boot_cli_home} --strip 1 2>&1"
67-
end
63+
FileUtils.rm_rf spring_boot_cli_home
64+
FileUtils.mkdir_p spring_boot_cli_home
65+
shell "tar xzf #{file.path} -C #{spring_boot_cli_home} --strip 1 2>&1"
6866

6967
puts "(#{(Time.now - expand_start_time).duration})"
7068
end

lib/java_buildpack/container/tomcat.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ def expand(file)
115115
expand_start_time = Time.now
116116
print " Expanding Tomcat to #{@application.relative_path_to(tomcat_home)} "
117117

118-
shell "rm -rf #{tomcat_home}"
119-
shell "mkdir -p #{tomcat_home}"
118+
FileUtils.rm_rf tomcat_home
119+
FileUtils.mkdir_p tomcat_home
120120
shell "tar xzf #{file.path} -C #{tomcat_home} --strip 1 --exclude webapps --exclude #{File.join 'conf', 'server.xml'} --exclude #{File.join 'conf', 'context.xml'} 2>&1"
121121

122122
JavaBuildpack::Util::ResourceUtils.copy_resources('tomcat', tomcat_home)

lib/java_buildpack/framework/new_relic.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'fileutils'
1718
require 'java_buildpack/framework'
1819
require 'java_buildpack/util/resource_utils'
1920
require 'java_buildpack/util/service_utils'
@@ -29,9 +30,9 @@ def initialize(context)
2930
end
3031

3132
def compile
32-
shell "rm -rf #{new_relic_home}"
33-
shell "mkdir -p #{new_relic_home}"
34-
shell "mkdir -p #{File.join new_relic_home, 'logs'}"
33+
FileUtils.rm_rf new_relic_home
34+
FileUtils.mkdir_p new_relic_home
35+
FileUtils.mkdir_p File.join(new_relic_home, 'logs')
3536

3637
download_jar jar_name, new_relic_home
3738
JavaBuildpack::Util::ResourceUtils.copy_resources('new-relic', new_relic_home)

lib/java_buildpack/framework/spring_insight.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ def install_insight(agent_dir)
124124
FileUtils.mkdir_p weaver_directory
125125
FileUtils.mkdir_p insight_directory
126126

127-
shell "mv #{File.join uber_agent_directory, 'agents', 'common', 'insight-weaver-*.jar'} #{weaver_directory}"
128-
shell "mv #{File.join uber_agent_directory, 'agents', 'common', 'insight-bootstrap-generic-*.jar'} #{container_libs_directory}"
129-
shell "mv #{File.join uber_agent_directory, 'agents', 'tomcat', '7', 'lib', 'insight-bootstrap-tomcat-common-*.jar'} #{container_libs_directory}"
130-
shell "mv #{File.join uber_agent_directory, 'insight', 'collection-plugins'} #{insight_directory}"
131-
shell "mv #{File.join uber_agent_directory, 'insight', 'conf'} #{insight_directory}"
132-
shell "mv #{File.join uber_agent_directory, 'insight-agent'} #{insight_analyser_directory}"
133-
shell "mv #{File.join uber_agent_directory, 'transport', 'http', 'insight-agent-http-*.jar'} #{File.join insight_analyser_directory, 'WEB-INF', 'lib'} "
127+
FileUtils.mv Dir[File.join(uber_agent_directory, 'agents', 'common', 'insight-weaver-*.jar')][0], weaver_directory
128+
FileUtils.mv Dir[File.join(uber_agent_directory, 'agents', 'common', 'insight-bootstrap-generic-*.jar')][0], container_libs_directory
129+
FileUtils.mv Dir[File.join(uber_agent_directory, 'agents', 'tomcat', '7', 'lib', 'insight-bootstrap-tomcat-common-*.jar')][0], container_libs_directory
130+
FileUtils.mv Dir[File.join(uber_agent_directory, 'insight', 'collection-plugins')][0], insight_directory
131+
FileUtils.mv Dir[File.join(uber_agent_directory, 'insight', 'conf')][0], insight_directory
132+
FileUtils.mv Dir[File.join(uber_agent_directory, 'insight-agent')][0], insight_analyser_directory
133+
FileUtils.mv Dir[File.join(uber_agent_directory, 'transport', 'http', 'insight-agent-http-*.jar')][0], File.join(insight_analyser_directory, 'WEB-INF', 'lib')
134134
end
135135

136136
def container_libs_directory

lib/java_buildpack/jre/openjdk.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def expand(file)
6464
expand_start_time = Time.now
6565
print " Expanding JRE to #{JAVA_HOME} "
6666

67-
shell "rm -rf #{java_home}"
68-
shell "mkdir -p #{java_home}"
67+
FileUtils.rm_rf java_home
68+
FileUtils.mkdir_p java_home
6969
shell "tar xzf #{file.path} -C #{java_home} --strip 1 2>&1"
7070

7171
puts "(#{(Time.now - expand_start_time).duration})"

lib/java_buildpack/util/resource_utils.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'fileutils'
1718
require 'java_buildpack/util'
1819
require 'open3'
1920

@@ -27,14 +28,7 @@ class ResourceUtils
2728
# @param [String] subdirectory the subdirectory of the resources directory
2829
# @param [String] target the target directory
2930
def self.copy_resources(subdirectory, target)
30-
Open3.popen3("cp -r #{File.join(get_resources(subdirectory), '*')} #{target}") do |stdin, stdout, stderr, wait_thr|
31-
if wait_thr.value != 0
32-
puts "STDOUT: #{stdout.gets}"
33-
puts "STDERR: #{stderr.gets}"
34-
35-
fail
36-
end
37-
end
31+
FileUtils.cp_r File.join(get_resources(subdirectory), '.'), target
3832
end
3933

4034
# Returns the path of the given subdirectory of the buildpack resources directory.

lib/java_buildpack/versioned_dependency_component.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'fileutils'
1718
require 'java_buildpack'
1819
require 'java_buildpack/base_component'
1920
require 'java_buildpack/repository/configured_item'
21+
require 'tmpdir'
2022

2123
module JavaBuildpack
2224

@@ -72,7 +74,7 @@ def download(description = @component_name, &block)
7274
# +@lib_directory+
7375
# @param [String] description an optional description for the download. Defaults to +@component_name+.
7476
def download_jar(jar_name, target_directory = @lib_directory, description = @component_name)
75-
download(description) { |file| shell "cp #{file.path} #{File.join(target_directory, jar_name)}" }
77+
download(description) { |file| FileUtils.cp file.path, File.join(target_directory, jar_name) }
7678
end
7779

7880
# Downloads a given ZIP file and expands it to a given destination.
@@ -91,7 +93,7 @@ def download_zip(target_directory, strip_top_level_directory = true, description
9193
if strip_top_level_directory
9294
Dir.mktmpdir do |root|
9395
shell "unzip -qq #{file.path} -d #{root} 2>&1"
94-
shell "mv #{root}/$(ls #{root}) #{target_directory}"
96+
FileUtils.mv Dir[root + '/*'][0], target_directory
9597
end
9698
else
9799
shell "unzip -qq #{file.path} -d #{target_directory} 2>&1"

0 commit comments

Comments
 (0)