summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-18 19:02:29 +0200
committerPaul Buetow <paul@buetow.org>2025-02-18 19:02:29 +0200
commit21c96e516044cf59a3b89c3b40c7efb279b0767c (patch)
tree30ccc58ee47ff0b5798ca55f4ea021cbd0e21401
parentde93bdd4a9897a4dbe1e54a8e13944cce62b7ac9 (diff)
refactor file DSL
-rw-r--r--lib/dslkeywords/file.rb46
-rw-r--r--playground/Rakefile11
-rw-r--r--test/lib/dslkeywords/file_test.rb12
3 files changed, 34 insertions, 35 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index b3f5be4..e17cfeb 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -38,34 +38,23 @@ module RCM
@content = text.instance_of?(Array) ? text.join("\n") : text
end
- def manage(what)
- what = what.to_sym
- raise UnsupportedOperation, "Unsupported 'manage' operation #{what}" unless %i[directory].include?(what)
-
- @manage_directory = true
- end
-
- def directory = :directory
-
- def from_sourcefile = @from_sourcefile = true
- def from_template = @from_template = true
- def without_backup = @without_backup = true # TODO: Rename to 'without backup'
def line(line) = @ensure_line = line
+ def path(file_path = nil) = file_path.nil? ? @file_path : @file_path = file_path
- def path(file_path = nil)
- return @file_path if file_path.nil?
+ # TODO: Replace :is with current method name dynamically
+ def is(what) = @is = validate_op(:is, what, present, absent)
+ def present = :present
+ def absent = :absent
- @file_path = file_path
- end
+ def manage(what) = @manage_directory = validate_op(:is, what, directory) == directory
+ def directory = :directory
- def is(what)
- @is = what.to_sym
- raise UnsupportedOperation, "Unsupported 'is' operation #{@is}" unless %i[present absent].include?(@is)
- end
+ def without(what) = @without_backup = validate_op(:without, what, backup) == backup
+ def backup = :backup
- # To allow 'is present'
- def present = :present
- def absent = :absent
+ def from(what) = @from = validate_op(:from, what, sourcefile, template)
+ def sourcefile = :sourcefile
+ def template = :template
def evaluate!
return unless super
@@ -77,6 +66,13 @@ module RCM
private
+ def validate_op(matter, what, *valids)
+ what = what.to_sym
+ raise UnsupportedOperation, "Unsupported '#{matter}' operation #{what}" unless valids.include?(what)
+
+ what
+ end
+
def evaluate_ensure_line!
return evaluate_ensure_line_absent! if %i[absent].include?(@is)
return write!(@ensure_line) unless ::File.file?(@file_path)
@@ -142,8 +138,8 @@ module RCM
end
def real_content
- text = @from_sourcefile ? ::File.read(@content) : @content
- @from_template ? ERB.new(text).result : text
+ text = @from == sourcefile ? ::File.read(@content) : @content
+ @from == template ? ERB.new(text).result : text
end
end
diff --git a/playground/Rakefile b/playground/Rakefile
index 35ae811..d5c8fcc 100644
--- a/playground/Rakefile
+++ b/playground/Rakefile
@@ -8,13 +8,15 @@ task :wireguard do
only_when { hostname is :earth }
file '/tmp/test/wg/wg0.conf' do
- create_parent_directory and from_template
+ manage directory
+ from template
'the content is here and the result is <%= 1 + 2 %>'
end
file '/tmp/test/wg/wg1.conf' do
- create_parent_directory and from_sourcefile
+ manage directory
+ from sourcefile
'./Rakefile'
end
@@ -27,7 +29,7 @@ task :hosts do
only_when { hostname is :earth }
file '/etc/hosts.test' do
- ensure_line '192.168.1.101 foo'
+ line '192.168.1.101 foo'
end
end
end
@@ -36,7 +38,8 @@ desc 'foo'
task :foo do
configure do
file '/tmp/foo.txt' do
- ensure_line 'foo'
+ line ensure_line 'foo'
+ # TODO: Rename to requires
depends_on file '/tmp/bar.txt'
end
diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb
index 4c38dc9..f414cf1 100644
--- a/test/lib/dslkeywords/file_test.rb
+++ b/test/lib/dslkeywords/file_test.rb
@@ -28,13 +28,13 @@ class RCMFileTest < Minitest::Test
configure_from_scratch do
file :create_file do
path FILE_PATH
- is :present
+ is present
:text
end
file :delete_file do
path FILE_PATH
- is :absent
+ is absent
end
end
@@ -71,7 +71,7 @@ class RCMFileTest < Minitest::Test
configure_from_scratch do
file FILE_PATH do
- from_sourcefile
+ from sourcefile
source_path
end
end
@@ -83,7 +83,7 @@ class RCMFileTest < Minitest::Test
def test_create_file_from_template
configure_from_scratch do
file FILE_PATH do
- from_template
+ from template
'One plus two is <%= 1 + 2 %>!'
end
end
@@ -101,7 +101,7 @@ class RCMFileTest < Minitest::Test
configure_from_scratch do
file(FILE_PATH) do
line 'Whats up?'
- is :absent
+ is absent
end
end
assert_equal 'Hey there', File.read(FILE_PATH)
@@ -110,7 +110,7 @@ class RCMFileTest < Minitest::Test
configure_from_scratch do
file(FILE_PATH) do
line 'Hey there'
- is :absent
+ is absent
end
end
assert_equal 'Whats up?', File.read(FILE_PATH)