Class: Thor::Actions::CreateFile

Inherits:
EmptyDirectory show all
Defined in:
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.

Direct Known Subclasses

CreateLink

Instance Attribute Summary collapse

Attributes inherited from EmptyDirectory

#base, #config, #destination, #given_destination, #relative_destination

Instance Method Summary collapse

Methods inherited from EmptyDirectory

#convert_encoded_instructions, #exists?, #invoke_with_conflict_check, #on_file_clash_behavior, #pretend?, #revoke!, #say_status

Constructor Details

#initialize(base, destination, data, config = {}) ⇒ CreateFile

Returns a new instance of CreateFile.



35
36
37
38
# File 'lib/thor/actions/create_file.rb', line 35

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

Instance Attribute Details

#dataObject (readonly)

:nodoc:



33
34
35
# File 'lib/thor/actions/create_file.rb', line 33

def data
  @data
end

Instance Method Details

#force_on_collision?Boolean (protected)

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

Returns:

  • (Boolean)


99
100
101
# File 'lib/thor/actions/create_file.rb', line 99

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

#force_or_skip_or_conflict(force, skip, &block) ⇒ Object (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.



85
86
87
88
89
90
91
92
93
94
95
# File '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

#identical?Boolean

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.

Returns:

  • (Boolean)


45
46
47
# File 'lib/thor/actions/create_file.rb', line 45

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

#invoke!Object



59
60
61
62
63
64
65
66
# File '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) ⇒ Object (protected)

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



72
73
74
75
76
77
78
79
# File '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

#renderObject

Holds the content to be added to the file.



51
52
53
54
55
56
57
# File 'lib/thor/actions/create_file.rb', line 51

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