Skip to content

Commit 687337a

Browse files
committed
Lazy-load fileutils
Since fileutils is a gem as of 2.5
1 parent 53919e3 commit 687337a

File tree

6 files changed

+18
-4
lines changed

6 files changed

+18
-4
lines changed

lib/thor/actions.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require "fileutils"
21
require "uri"
32
require "thor/core_ext/io_binary_read"
43
require "thor/actions/create_file"
@@ -175,13 +174,15 @@ def inside(dir = "", config = {}, &block)
175174

176175
# If the directory doesnt exist and we're not pretending
177176
if !File.exist?(destination_root) && !pretend
177+
require "fileutils"
178178
FileUtils.mkdir_p(destination_root)
179179
end
180180

181181
if pretend
182182
# In pretend mode, just yield down to the block
183183
block.arity == 1 ? yield(destination_root) : yield
184184
else
185+
require "fileutils"
185186
FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
186187
end
187188

lib/thor/actions/create_file.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def render
5858

5959
def invoke!
6060
invoke_with_conflict_check do
61+
require "fileutils"
6162
FileUtils.mkdir_p(File.dirname(destination))
6263
File.open(destination, "wb") { |f| f.write render }
6364
end

lib/thor/actions/create_link.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def identical?
3838

3939
def invoke!
4040
invoke_with_conflict_check do
41+
require "fileutils"
4142
FileUtils.mkdir_p(File.dirname(destination))
4243
# Create a symlink by default
4344
config[:symbolic] = true if config[:symbolic].nil?

lib/thor/actions/empty_directory.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ def exists?
4848

4949
def invoke!
5050
invoke_with_conflict_check do
51+
require "fileutils"
5152
::FileUtils.mkdir_p(destination)
5253
end
5354
end
5455

5556
def revoke!
5657
say_status :remove, :red
58+
require "fileutils"
5759
::FileUtils.rm_rf(destination) if !pretend? && exists?
5860
given_destination
5961
end

lib/thor/actions/file_manipulation.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ def chmod(path, mode, config = {})
140140
return unless behavior == :invoke
141141
path = File.expand_path(path, destination_root)
142142
say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true)
143-
FileUtils.chmod_R(mode, path) unless options[:pretend]
143+
unless options[:pretend]
144+
require "fileutils"
145+
FileUtils.chmod_R(mode, path)
146+
end
144147
end
145148

146149
# Prepend text to a file. Since it depends on insert_into_file, it's reversible.
@@ -317,7 +320,10 @@ def remove_file(path, config = {})
317320
path = File.expand_path(path, destination_root)
318321

319322
say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true)
320-
::FileUtils.rm_rf(path) if !options[:pretend] && File.exist?(path)
323+
if !options[:pretend] && File.exist?(path)
324+
require "fileutils"
325+
::FileUtils.rm_rf(path)
326+
end
321327
end
322328
alias_method :remove_dir, :remove_file
323329

lib/thor/runner.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require "thor/group"
33
require "thor/core_ext/io_binary_read"
44

5-
require "fileutils"
65
require "yaml"
76
require "digest/md5"
87
require "pathname"
@@ -103,6 +102,7 @@ def install(name) # rubocop:disable MethodLength
103102
if package == :file
104103
File.open(destination, "w") { |f| f.puts contents }
105104
else
105+
require "fileutils"
106106
FileUtils.cp_r(name, destination)
107107
end
108108

@@ -119,6 +119,7 @@ def version
119119
def uninstall(name)
120120
raise Error, "Can't find module '#{name}'" unless thor_yaml[name]
121121
say "Uninstalling #{name}."
122+
require "fileutils"
122123
FileUtils.rm_rf(File.join(thor_root, (thor_yaml[name][:filename]).to_s))
123124

124125
thor_yaml.delete(name)
@@ -137,6 +138,7 @@ def update(name)
137138
self.options = options.merge("as" => name)
138139

139140
if File.directory? File.expand_path(name)
141+
require "fileutils"
140142
FileUtils.rm_rf(File.join(thor_root, old_filename))
141143

142144
thor_yaml.delete(old_filename)
@@ -193,6 +195,7 @@ def save_yaml(yaml)
193195
yaml_file = File.join(thor_root, "thor.yml")
194196

195197
unless File.exist?(yaml_file)
198+
require "fileutils"
196199
FileUtils.mkdir_p(thor_root)
197200
yaml_file = File.join(thor_root, "thor.yml")
198201
FileUtils.touch(yaml_file)

0 commit comments

Comments
 (0)