Class: Bauk::Gen::Outputs::Filesystem

Inherits:
Base
  • Object
show all
Includes:
Utils::Log
Defined in:
lib/bauk/gen/outputs/filesystem.rb

Constant Summary collapse

FILESYSTEM_KEYS =
s(output_base)
Contents =
Bauk::Gen::Contents

Instance Method Summary collapse

Constructor Details

#initialize(generator, config = {}) ⇒ Filesystem

Returns a new instance of Filesystem.



16
17
18
19
20
21
22
23
24
25
# File 'lib/bauk/gen/outputs/filesystem.rb', line 16

def initialize(generator, config = {})
  super generator, config
  config[:outputs][:filesystem] ||= {}
  filesystem_config = config[:outputs][:filesystem]
  @output_base = File.expand_path(filesystem_config[:output_base] || '.')
  invalid_keys = filesystem_config.keys.reject { |key| FILESYSTEM_KEYS.include? key }
  unless invalid_keys.empty?
    raise "Invalid keys passed to Bauk::Gen::Outputs::Filesystem: #{invalid_keys.join ', '}"
  end
end

Instance Method Details

#default_template_dirsObject



27
28
29
30
31
32
33
# File 'lib/bauk/gen/outputs/filesystem.rb', line 27

def default_template_dirs
  [
    '.bauk/generator/templates',
    '~/.bauk/generator/templates',
    File.expand_path('../../../../templates', __dir__)
  ]
end

#merge_item(name, item) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/bauk/gen/outputs/filesystem.rb', line 64

def merge_item(name, item)
  if item[:content].respond_to? :merge
    File.write(name, item[:content].merge(File.read(name)))
  else
    log.warn "Content type for '#{name}' does not respond to merge request: #{item[:content]} (Outputting without merge)"
    output_item name, item
  end
end

#output_item(name, item) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/bauk/gen/outputs/filesystem.rb', line 73

def output_item(name, item)
  if item[:content].is_a? Contents::FileContent
    FileUtils.cp item[:content].file.path, name
  elsif item[:content].respond_to? :content
    File.write(name, item[:content].content)
  else
    raise "Invalid content found for #{name}: #{item[:content]}"
  end
end

#output_items(items) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bauk/gen/outputs/filesystem.rb', line 44

def output_items(items)
  Dir.chdir(@output_base) do
    log.info "Outputting items to dir: #{@output_base}"
    items.each do |name, item|
      log.debug "Outputting item: #{name}(#{item})"
      if name =~ %r{/}
        parent_dir = name.sub(%r{/[^/]*$}, '')
        FileUtils.mkdir_p parent_dir
      end
      if item[:attributes][:merge] and File.exist?(name)
        merge_item name, item
      elsif item[:attributes][:overwrite] == false and File.exist?(name)
        log.info "Not overwriting file: '#{name}'"
      else
        output_item name, item
      end
    end
  end
end

#sanitise_dirs(dirs) ⇒ Object

This function returns the absoluts path of only the provided dirs that exist



36
37
38
39
40
41
42
# File 'lib/bauk/gen/outputs/filesystem.rb', line 36

def sanitise_dirs(dirs)
  dirs.map do |dir|
    File.expand_path("#{dir}/#{@template_name}")
  end.select do |dir|
    File.directory? dir
  end
end