Class: Tap::Tasks::Concat

Inherits:
FileTask
  • Object
show all
Defined in:
lib/tap/tasks/concat.rb

Overview

:startdoc::manifest concatenate files with formatting

Concatenates a list of files into the specified target. Raises an error for non-existant, non-file inputs. Concat allows a variety configurable separators to be specified. These strings are formatted using ERB so that you can concat and insert text at the same time. For instance with source files:

[one.txt]
contents of file one

[two.txt]
contents of file two

And configurations:

pre: "# <%= File.basename(source) %>\n"
post: "\n"

You obtain the following:

[concat.txt]
# one.txt
contents of file one

# two.txt
contents of file two

The ERB binding (and hence each insert configuration) has access to all task methods and the following variables:

target   the target file
source   the current source
sources  an array of the source files

Instance Method Summary collapse

Instance Method Details

#process(target, *sources) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tap/tasks/concat.rb', line 50

def process(target, *sources)
  # prepare backs up the target to allow rollback on error,
  # and ensures the target parent directory exists.
  prepare(target)
  log_basename :prepare, target
  
  # open the output file and read the file contents 
  # of each input file into the output
  File.open(target, "wb" ) do |output|
    output << ERB.new(before).result(binding) unless before.empty?
    
    sources.each do |source|
      raise "Not a file: #{source}" unless File.exists?(source) && File.file?(source)
 
      log_basename :concat, source
      output << ERB.new(pre).result(binding) unless pre.empty?
      output << File.read(source)
      output << ERB.new(post).result(binding) unless post.empty?
    end
    
    output << ERB.new(after).result(binding) unless after.empty?
  end
  
  # return the concatenated file
  target
end

#taskObject

Alias for self. Allows self to be accessed within separator ERB.



46
47
48
# File 'lib/tap/tasks/concat.rb', line 46

def task
  self
end