blob: 746b41c483dbef95c6cac9e2fc8520b89985bac7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
require 'erb'
require 'fileutils'
require_relative '../options'
require_relative '../log'
module RCM
# Managing files
class File
attr_reader :id, :path
include Options
include Log
def initialize(path)
@id = "#{self.class}(#{path})"
@path = path
end
def to_s
id
end
def content(content = nil)
return @content if content.nil?
@content = content.instance_of?(Array) ? content.join("\n") : content
end
def create_parent_directory
@create_parent = true
end
def from_sourcefile
@from_sourcefile = true
end
def from_template
@from_template = true
end
def do!
content = real_content
dirname = ::File.dirname(@path)
if !::File.directory?(dirname) && @create_parent
info "Creating parent directory #{parent}"
FileUtils.mkdir_p(dirname)
end
info "Creating file #{@path}"
debug content if option :debug
tmp_path = "#{@path}.tmp"
::File.write(tmp_path, content)
::File.rename(tmp_path, @path)
end
private
def real_content
content = @from_sourcefile ? ::File.read(@content) : @content
@from_template ? ERB.new(content).result : content
end
end
# Add file keyword to the DSL
class RCM
def file(path, &block)
return unless @conds_met
f = File.new(path)
f.content(f.instance_eval(&block))
self << f
f
end
end
end
|