blob: e83906e1bb94ac4fe2b4246a868444c43feed133 (
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
require 'digest'
require 'erb'
require 'fileutils'
require_relative 'resource'
module RCM
# Backup the file on change
module FileBackup
def backup!(path)
return unless ::File.exist?(path)
backup_dir = "#{::File.dirname(path)}/.rcm"
Dir.mkdir(backup_dir) unless ::File.directory?(backup_dir)
checksum = Digest::SHA256.file(path).hexdigest
backup_path = "#{backup_dir}/#{::File.basename(path)}.#{checksum}"
return if ::File.exist?(backup_path)
info("Backing up #{path} -> #{backup_path}")
::File.rename(path, backup_path)
end
end
# Managing files
class File < Resource
attr_reader :path
include FileBackup
def initialize(path)
super(path)
@path = path
end
def content(text = nil)
return @content if text.nil?
@content = text.instance_of?(Array) ? text.join("\n") : text
end
def create_parent_directory = @create_parent = true
def from_sourcefile = @from_sourcefile = true
def from_template = @from_template = true
def ensure_line(line) = @ensure_line = line
def evaluate!
return unless super
return evaluate_ensure_line! unless @ensure_line.nil?
write_content!(real_content)
end
private
def evaluate_ensure_line!
return write_content!(@ensure_line) unless ::File.file?(@path)
return if ::File.readlines(@path, chomp: true).include?(@ensure_line)
::File.open(@path, 'a') do |fd|
fd.puts(@ensure_line)
end
end
def write_content!(text)
create_parent_directory!
debug text if option :debug
info "Managing file #{@path}"
tmp_path = "#{@path}.tmp"
::File.write(tmp_path, text)
backup!(@path)
::File.rename(tmp_path, @path)
end
def create_parent_directory!
dirname = ::File.dirname(@path)
return unless !::File.directory?(dirname) && @create_parent
info "Creating parent directory #{dirname}"
FileUtils.mkdir_p(dirname)
end
def real_content
text = @from_sourcefile ? ::File.read(@content) : @content
@from_template ? ERB.new(text).result : text
end
end
# Add file keyword to the DSL
class DSL
def file(path, &block)
return unless @conds_met
f = File.new(path)
f.content(f.instance_eval(&block))
self << f
f
end
end
end
|