diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-14 21:08:34 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-14 21:08:34 +0200 |
| commit | 07e5f8d4402346ccb336cc0680cad98d0d5ba920 (patch) | |
| tree | 06927c1e10d41c75e822762ab43606e45c833778 /lib/dslkeywords | |
| parent | ccc0fab74a0367c8236679333a40774a3f810512 (diff) | |
add ensure_line
Diffstat (limited to 'lib/dslkeywords')
| -rw-r--r-- | lib/dslkeywords/file.rb | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index 7e24801..403f01e 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -28,21 +28,36 @@ module RCM 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! - create_parent_directory! - content = real_content + return evaluate_ensure_line! unless @ensure_line.nil? - info "Creating file #{@path}" - debug content if option :debug + write_content!(real_content) + end + + private + + def evaluate_ensure_line! + return write_content!(@ensure_line) unless ::File.file?(@path) + + lines = ::File.readlines(@path, chomp: true) + return if lines.include?(@ensure_line) + ::File.open(@path, 'a') do |fd| + fd.puts(@ensure_line) + end + end + + def write_content!(content) + create_parent_directory! + debug content if option :debug + info "Creating file #{@path}" tmp_path = "#{@path}.tmp" ::File.write(tmp_path, content) ::File.rename(tmp_path, @path) end - private - def create_parent_directory! dirname = ::File.dirname(@path) return unless !::File.directory?(dirname) && @create_parent |
