123456789_123456789_123456789_123456789_123456789_

Class: Bundler::Thor::Actions::EmptyDirectory

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb

Overview

Class which holds create directory logic. This is the base class for other actions like create_file and directory.

This implementation is based in Templater actions, created by Jonas Nicklas and Michael S. Klishin under MIT LICENSE.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(base, destination, config = {}) ⇒ EmptyDirectory

Initializes given the source and destination.

Parameters

base<Bundler::Thor::Base>

A ::Bundler::Thor::Base instance

source<String>

Relative path to the source of this file

destination<String>

Relative path to the destination of this file

config<Hash>

give :verbose => false to not log the status.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 34

def initialize(base, destination, config = {})
  @base = base
  @config = {:verbose => true}.merge(config)
  self.destination = destination
end

Instance Attribute Details

#base (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 24

attr_reader :base, :destination, :given_destination, :relative_destination, :config

#config (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 24

attr_reader :base, :destination, :given_destination, :relative_destination, :config

#destination (rw)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 24

attr_reader :base, :destination, :given_destination, :relative_destination, :config

#destination=(destination) (rw, protected)

Sets the absolute destination value from a relative destination value. It also stores the given and relative destination. Let’s suppose our script is being executed on “dest”, it sets the destination root to “dest”. The destination, given_destination and relative_destination are related in the following way:

inside "bar" do
  empty_directory "baz"
end

destination          #=> dest/bar/baz
relative_destination #=> bar/baz
given_destination    #=> baz
[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 85

def destination=(destination)
  return unless destination
  @given_destination = convert_encoded_instructions(destination.to_s)
  @destination = ::File.expand_path(@given_destination, base.destination_root)
  @relative_destination = base.relative_to_original_destination_root(@destination)
end

#exists?Boolean (readonly)

Checks if the destination file already exists.

Returns

Boolean

true if the file exists, false otherwise.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 45

def exists?
  ::File.exist?(destination)
end

#given_destination (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 24

attr_reader :base, :destination, :given_destination, :relative_destination, :config

#pretend?Boolean (readonly, protected)

Shortcut for pretend.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 67

def pretend?
  base.options[:pretend]
end

#relative_destination (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 24

attr_reader :base, :destination, :given_destination, :relative_destination, :config

Instance Method Details

#convert_encoded_instructions(filename) (protected)

Filenames in the encoded form are converted. If you have a file:

%file_name%.rb

It calls #file_name from the base and replaces %-string with the return value (should be String) of #file_name:

user.rb

The method referenced can be either public or private.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 103

def convert_encoded_instructions(filename)
  filename.gsub(/%(.*?)%/) do |initial_string|
    method = $1.strip
    base.respond_to?(method, true) ? base.send(method) : initial_string
  end
end

#invoke!

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 49

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

#invoke_with_conflict_check(&block) (protected)

Receives a hash of options and just execute the block if some conditions are met.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 113

def invoke_with_conflict_check(&block)
  if exists?
    on_conflict_behavior(&block)
  else
    yield unless pretend?
    say_status :create, :green
  end

  destination
rescue Errno::EISDIR, Errno::EEXIST
  on_file_clash_behavior
end

#on_conflict_behavior (protected)

What to do when the destination file already exists.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 132

def on_conflict_behavior
  say_status :exist, :blue
end

#on_file_clash_behavior (protected)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 126

def on_file_clash_behavior
  say_status :file_clash, :red
end

#revoke!

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 56

def revoke!
  say_status :remove, :red
  require "fileutils"
  ::FileUtils.rm_rf(destination) if !pretend? && exists?
  given_destination
end

#say_status(status, color) (protected)

Shortcut to say_status shell method.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb', line 138

def say_status(status, color)
  base.shell.say_status status, relative_destination, color if config[:verbose]
end