Module: Tap::Generator::Generate

Defined in:
lib/tap/generator/generate.rb

Overview

A mixin defining how to run manifest actions.

Instance Method Summary collapse

Instance Method Details

#directory(target, options = {}) ⇒ Object

Creates the target directory if it doesn’t exist. When pretend is true, creation is logged but does not actually happen.

No options currently affect the behavior of this method.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tap/generator/generate.rb', line 16

def directory(target, options={})
  target = File.expand_path(target)
  
  case
  when File.exists?(target)
    log_relative :exists, target
  else
    log_relative :create, target
    FileUtils.mkdir_p(target) unless pretend
  end
end

#file(target, options = {}) {|source_file| ... } ⇒ Object

Creates the target file; content may be added to the file by providing block. If the target file already exists, the new and existing content is compared and the user will be prompted for how to handle collisions. All activity is logged. When pretend is true, creation is logged but does not actually happen.

No options currently affect the behavior of this method.

Yields:

  • (source_file)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tap/generator/generate.rb', line 35

def file(target, options={})
  source_file = Tempfile.new('generate')
  yield(source_file) if block_given?
  source_file.close
  
  source = source_file.path
  target = File.expand_path(target)
  
  copy_file = true
  msg = case
  when !File.exists?(target)
    :create
  when FileUtils.cmp(source, target)
    :exists
  when force_file_collision?(target)
    :force
  else
    copy_file = false
    :skip
  end
  
  log_relative msg, target
  if copy_file && !pretend
    dir = File.dirname(target)
    FileUtils.mkdir_p(dir) unless File.exists?(dir) 
    FileUtils.mv(source, target, :force => true)
  end
end

#iterate(actions) ⇒ Object

Iterates over the actions in order.



8
9
10
# File 'lib/tap/generator/generate.rb', line 8

def iterate(actions)
  actions.each {|action| yield(action) }
end