Navigation Menu

Skip to content

Commit

Permalink
Lazy-load fileutils
Browse files Browse the repository at this point in the history
Since fileutils is a gem as of 2.5
  • Loading branch information
segiddins committed Apr 25, 2017
1 parent 53919e3 commit 687337a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/thor/actions.rb
@@ -1,4 +1,3 @@
require "fileutils"
require "uri"
require "thor/core_ext/io_binary_read"
require "thor/actions/create_file"
Expand Down Expand Up @@ -175,13 +174,15 @@ def inside(dir = "", config = {}, &block)

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

if pretend
# In pretend mode, just yield down to the block
block.arity == 1 ? yield(destination_root) : yield
else
require "fileutils"
FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
end

Expand Down
1 change: 1 addition & 0 deletions lib/thor/actions/create_file.rb
Expand Up @@ -58,6 +58,7 @@ def render

def invoke!
invoke_with_conflict_check do
require "fileutils"
FileUtils.mkdir_p(File.dirname(destination))
File.open(destination, "wb") { |f| f.write render }
end
Expand Down
1 change: 1 addition & 0 deletions lib/thor/actions/create_link.rb
Expand Up @@ -38,6 +38,7 @@ def identical?

def invoke!
invoke_with_conflict_check do
require "fileutils"
FileUtils.mkdir_p(File.dirname(destination))
# Create a symlink by default
config[:symbolic] = true if config[:symbolic].nil?
Expand Down
2 changes: 2 additions & 0 deletions lib/thor/actions/empty_directory.rb
Expand Up @@ -48,12 +48,14 @@ def exists?

def invoke!
invoke_with_conflict_check do
require "fileutils"
::FileUtils.mkdir_p(destination)
end
end

def revoke!
say_status :remove, :red
require "fileutils"
::FileUtils.rm_rf(destination) if !pretend? && exists?
given_destination
end
Expand Down
10 changes: 8 additions & 2 deletions lib/thor/actions/file_manipulation.rb
Expand Up @@ -140,7 +140,10 @@ def chmod(path, mode, config = {})
return unless behavior == :invoke
path = File.expand_path(path, destination_root)
say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true)
FileUtils.chmod_R(mode, path) unless options[:pretend]
unless options[:pretend]
require "fileutils"
FileUtils.chmod_R(mode, path)
end
end

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

say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true)
::FileUtils.rm_rf(path) if !options[:pretend] && File.exist?(path)
if !options[:pretend] && File.exist?(path)
require "fileutils"
::FileUtils.rm_rf(path)
end
end
alias_method :remove_dir, :remove_file

Expand Down
5 changes: 4 additions & 1 deletion lib/thor/runner.rb
Expand Up @@ -2,7 +2,6 @@
require "thor/group"
require "thor/core_ext/io_binary_read"

require "fileutils"
require "yaml"
require "digest/md5"
require "pathname"
Expand Down Expand Up @@ -103,6 +102,7 @@ def install(name) # rubocop:disable MethodLength
if package == :file
File.open(destination, "w") { |f| f.puts contents }
else
require "fileutils"
FileUtils.cp_r(name, destination)
end

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

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

if File.directory? File.expand_path(name)
require "fileutils"
FileUtils.rm_rf(File.join(thor_root, old_filename))

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

unless File.exist?(yaml_file)
require "fileutils"
FileUtils.mkdir_p(thor_root)
yaml_file = File.join(thor_root, "thor.yml")
FileUtils.touch(yaml_file)
Expand Down

0 comments on commit 687337a

Please sign in to comment.