summaryrefslogtreecommitdiff
path: root/lib/dslkeywords
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-18 12:25:54 +0200
committerPaul Buetow <paul@buetow.org>2025-02-18 12:25:54 +0200
commit703fd14764974ba1513c987dcb461e4a23d633e1 (patch)
treeeb591dbd82c9c2bf158feed790ae5140fb32cdaa /lib/dslkeywords
parent5c2865998aec2db34c42ee092952ec37ebd3cf16 (diff)
add package initially
Diffstat (limited to 'lib/dslkeywords')
-rw-r--r--lib/dslkeywords/package.rb68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/dslkeywords/package.rb b/lib/dslkeywords/package.rb
new file mode 100644
index 0000000..d0bce85
--- /dev/null
+++ b/lib/dslkeywords/package.rb
@@ -0,0 +1,68 @@
+require 'erb'
+require 'fileutils'
+
+require_relative 'resource'
+
+module RCM
+ # Managing packages
+ class Package < Resource
+ attr_reader :path
+
+ def initialize(name)
+ super(name)
+ @name = name
+ end
+
+ 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 ::Package.file?(@name)
+ return if ::Package.readlines(@name, chomp: true).include?(@ensure_line)
+
+ ::Package.open(@name, 'a') do |fd|
+ fd.puts(@ensure_line)
+ end
+ end
+
+ def write_content!(text)
+ create_parent_directory!
+ debug text if option :debug
+ info "Creating file #{@name}"
+ tmp_path = "#{@name}.tmp"
+ ::Package.write(tmp_path, text)
+ ::Package.rename(tmp_path, @name)
+ end
+
+ def create_parent_directory!
+ dirname = ::Package.dirname(@name)
+ return unless !::Package.directory?(dirname) && @create_parent
+
+ info "Creating parent directory #{dirname}"
+ PackageUtils.mkdir_p(dirname)
+ end
+
+ def real_content
+ text = @from_sourcefile ? ::Package.read(@content) : @content
+ @from_template ? ERB.new(text).result : text
+ end
+ end
+
+ # Add file keyword to the DSL
+ class DSL
+ def file(name, &block)
+ return unless @conds_met
+
+ f = Package.new(name)
+ f.content(f.instance_eval(&block))
+ self << f
+ f
+ end
+ end
+end