123456789_123456789_123456789_123456789_123456789_

Class: Bundler::Thor::Actions::CreateFile

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Bundler::Thor::Actions::EmptyDirectory
Defined in: lib/bundler/vendor/thor/lib/thor/actions/create_file.rb

Overview

CreateFile is a subset of Template, which instead of rendering a file with ERB, it gets the content from the user.

Class Method Summary

EmptyDirectory - Inherited

.new

Initializes given the source and destination.

Instance Attribute Summary

EmptyDirectory - Inherited

#base, #config, #destination,
#exists?

Checks if the destination file already exists.

#given_destination, #relative_destination,
#destination=

Sets the absolute destination value from a relative destination value.

#pretend?

Shortcut for pretend.

Instance Method Summary

EmptyDirectory - Inherited

#invoke!, #revoke!,
#convert_encoded_instructions

Filenames in the encoded form are converted.

#invoke_with_conflict_check

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

#on_conflict_behavior

What to do when the destination file already exists.

#on_file_clash_behavior,
#say_status

Shortcut to say_status shell method.

Constructor Details

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

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/create_file.rb', line 35

def initialize(base, destination, data, config = {})
  @data = data
  super(base, destination, config)
end

Instance Attribute Details

#data (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/create_file.rb', line 33

attr_reader :data

#force_on_collision?Boolean (readonly, protected)

Shows the file collision menu to the user and gets the result.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/create_file.rb', line 99

def force_on_collision?
  base.shell.file_collision(destination) { render }
end

#identical?Boolean (readonly)

Checks if the content of the file at the destination is identical to the rendered result.

Returns

Boolean

true if it is identical, false otherwise.

[ GitHub ]

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

def identical?
  exists? && File.binread(destination) == render
end

Instance Method Details

#force_or_skip_or_conflict(force, skip, &block) (protected)

If force is true, run the action, otherwise check if it’s not being skipped. If both are false, show the file_collision menu, if the menu returns true, force it, otherwise skip.

[ GitHub ]

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

def force_or_skip_or_conflict(force, skip, &block)
  if force
    say_status :force, :yellow
    yield unless pretend?
  elsif skip
    say_status :skip, :yellow
  else
    say_status :conflict, :red
    force_or_skip_or_conflict(force_on_collision?, true, &block)
  end
end

#invoke!

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/create_file.rb', line 59

def invoke!
  invoke_with_conflict_check do
    require "fileutils"
    FileUtils.mkdir_p(File.dirname(destination))
    File.open(destination, "wb") { |f| f.write render }
  end
  given_destination
end

#on_conflict_behavior(&block) (protected)

Now on conflict we check if the file is identical or not.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/create_file.rb', line 72

def on_conflict_behavior(&block)
  if identical?
    say_status :identical, :blue
  else
    options = base.options.merge(config)
    force_or_skip_or_conflict(options[:force], options[:skip], &block)
  end
end

#render

Holds the content to be added to the file.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/create_file.rb', line 51

def render
  @render ||= if data.is_a?(Proc)
    data.call
  else
    data
  end
end