Class: Thor::Actions::Templater

Inherits:
Object
  • Object
show all
Defined in:
lib/thor/actions/templater.rb

Overview

This is the base class for templater actions, ie. that copies something from some directory (source) to another (destination).

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

Direct Known Subclasses

CopyFile, CreateFile, Directory, EmptyDirectory, Get, Template

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, source, destination, log_status = true) ⇒ Templater

Initializes given the source and destination.

Parameters

base<Thor::Base>

A Thor::Base instance

source<String>

Relative path to the source of this file

destination<String>

Relative path to the destination of this file

log_status<Boolean>

If false, does not log the status. True by default. Templater log status does not accept color.



22
23
24
25
26
# File 'lib/thor/actions/templater.rb', line 22

def initialize(base, source, destination, log_status=true)
  @base, @log_status = base, log_status
  self.source = source
  self.destination = destination
end

Instance Attribute Details

#baseObject (readonly)

:nodoc:



11
12
13
# File 'lib/thor/actions/templater.rb', line 11

def base
  @base
end

#destinationObject

:nodoc:



11
12
13
# File 'lib/thor/actions/templater.rb', line 11

def destination
  @destination
end

#given_destinationObject (readonly)

:nodoc:



11
12
13
# File 'lib/thor/actions/templater.rb', line 11

def given_destination
  @given_destination
end

#relative_destinationObject (readonly)

:nodoc:



11
12
13
# File 'lib/thor/actions/templater.rb', line 11

def relative_destination
  @relative_destination
end

#sourceObject

:nodoc:



11
12
13
# File 'lib/thor/actions/templater.rb', line 11

def source
  @source
end

Instance Method Details

#exists?Boolean

Checks if the destination file already exists.

Returns

Boolean

true if the file exists, false otherwise.

Returns:

  • (Boolean)


42
43
44
# File 'lib/thor/actions/templater.rb', line 42

def exists?
  ::File.exists?(destination)
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)


51
52
53
# File 'lib/thor/actions/templater.rb', line 51

def identical?
  exists? && (is_not_comparable? || ::File.read(destination) == render)
end

#invoke!Object

Invokes the action. By default it adds to the file the content rendered, but you can modify in the subclass.



58
59
60
61
62
63
# File 'lib/thor/actions/templater.rb', line 58

def invoke!
  invoke_with_options!(base.options) do
    ::FileUtils.mkdir_p(::File.dirname(destination))
    ::File.open(destination, 'w'){ |f| f.write render }
  end
end

#revoke!Object

Revokes the action.



67
68
69
70
# File 'lib/thor/actions/templater.rb', line 67

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