summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-14 21:08:34 +0200
committerPaul Buetow <paul@buetow.org>2025-02-14 21:08:34 +0200
commit07e5f8d4402346ccb336cc0680cad98d0d5ba920 (patch)
tree06927c1e10d41c75e822762ab43606e45c833778 /lib
parentccc0fab74a0367c8236679333a40774a3f810512 (diff)
add ensure_line
Diffstat (limited to 'lib')
-rw-r--r--lib/dsl.rb1
-rw-r--r--lib/dslkeywords/file.rb27
2 files changed, 22 insertions, 6 deletions
diff --git a/lib/dsl.rb b/lib/dsl.rb
index f6f8e8e..1629afc 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -10,6 +10,7 @@ module RCM
class DSL
attr_reader :id
+ # TODO: Replace @@ with @ class variables
@@rcm_counter = -1
@@objs = {}
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