Skip to content

Commit 4b95e2e

Browse files
authored
Merge pull request #560 from segiddins/seg-lazy-load-gems
Lazy-load date & fileutils gems
2 parents 0cbb5ac + 2405f6c commit 4b95e2e

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "erb"
2-
require "open-uri"
32

43
class Thor
54
module Actions
@@ -78,7 +77,12 @@ def get(source, *args, &block)
7877
config = args.last.is_a?(Hash) ? args.pop : {}
7978
destination = args.first
8079

81-
source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ %r{^https?\://}
80+
if source =~ %r{^https?\://}
81+
require "open-uri"
82+
else
83+
source = File.expand_path(find_in_source_paths(source.to_s))
84+
end
85+
8286
render = open(source) { |input| input.binmode.read }
8387

8488
destination ||= if block_given?
@@ -136,7 +140,10 @@ def chmod(path, mode, config = {})
136140
return unless behavior == :invoke
137141
path = File.expand_path(path, destination_root)
138142
say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true)
139-
FileUtils.chmod_R(mode, path) unless options[:pretend]
143+
unless options[:pretend]
144+
require "fileutils"
145+
FileUtils.chmod_R(mode, path)
146+
end
140147
end
141148

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

315322
say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true)
316-
::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
317327
end
318328
alias_method :remove_dir, :remove_file
319329

lib/thor/runner.rb

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

5-
require "fileutils"
6-
require "open-uri"
75
require "yaml"
86
require "digest/md5"
97
require "pathname"
@@ -104,6 +102,7 @@ def install(name) # rubocop:disable MethodLength
104102
if package == :file
105103
File.open(destination, "w") { |f| f.puts contents }
106104
else
105+
require "fileutils"
107106
FileUtils.cp_r(name, destination)
108107
end
109108

@@ -120,6 +119,7 @@ def version
120119
def uninstall(name)
121120
raise Error, "Can't find module '#{name}'" unless thor_yaml[name]
122121
say "Uninstalling #{name}."
122+
require "fileutils"
123123
FileUtils.rm_rf(File.join(thor_root, (thor_yaml[name][:filename]).to_s))
124124

125125
thor_yaml.delete(name)
@@ -138,6 +138,7 @@ def update(name)
138138
self.options = options.merge("as" => name)
139139

140140
if File.directory? File.expand_path(name)
141+
require "fileutils"
141142
FileUtils.rm_rf(File.join(thor_root, old_filename))
142143

143144
thor_yaml.delete(old_filename)
@@ -194,6 +195,7 @@ def save_yaml(yaml)
194195
yaml_file = File.join(thor_root, "thor.yml")
195196

196197
unless File.exist?(yaml_file)
198+
require "fileutils"
197199
FileUtils.mkdir_p(thor_root)
198200
yaml_file = File.join(thor_root, "thor.yml")
199201
FileUtils.touch(yaml_file)

lib/thor/shell/basic.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require "tempfile"
2-
31
class Thor
42
module Shell
53
class Basic
@@ -349,6 +347,7 @@ def file_collision_help #:nodoc:
349347
def show_diff(destination, content) #:nodoc:
350348
diff_cmd = ENV["THOR_DIFF"] || ENV["RAILS_DIFF"] || "diff -u"
351349

350+
require "tempfile"
352351
Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
353352
temp.write content
354353
temp.rewind

0 commit comments

Comments
 (0)