diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-15 10:22:50 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-15 10:22:50 +0200 |
| commit | 8729aafe5bca25d0f7e94bd3c7ee1e0f60f99035 (patch) | |
| tree | 2a88726109ad3482f79937d0b87f6fc05be43c6c | |
| parent | 6879b03b2735b082b913ab17e63857f464f53c93 (diff) | |
add depends_on syntax
| -rw-r--r-- | lib/dsl.rb | 3 | ||||
| -rw-r--r-- | lib/dslkeywords/file.rb | 4 | ||||
| -rw-r--r-- | lib/dslkeywords/keyword.rb | 2 | ||||
| -rw-r--r-- | lib/dslkeywords/notify.rb | 32 | ||||
| -rw-r--r-- | lib/dslkeywords/resource.rb | 31 | ||||
| -rw-r--r-- | playground/Rakefile | 1 | ||||
| -rw-r--r-- | test/lib/dslkeywords/dependency_test.rb | 17 |
7 files changed, 86 insertions, 4 deletions
@@ -4,6 +4,7 @@ require_relative 'log' require_relative 'dslkeywords/file' require_relative 'dslkeywords/only_when' +require_relative 'dslkeywords/notify' # Ruby Configiration Management system module RCM @@ -24,7 +25,7 @@ module RCM def initialize(reset) DSL.reset! if reset - @id = "DSL[#{@@rcm_counter += 1}]" + @id = "dsl[#{@@rcm_counter += 1}]" @conds_met = true @scheduled = [] yield self if block_given? diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index 50f753e..501b7c8 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -1,11 +1,11 @@ require 'erb' require 'fileutils' -require_relative 'keyword' +require_relative 'resource' module RCM # Managing files - class File < Keyword + class File < Resource attr_reader :path def initialize(path) diff --git a/lib/dslkeywords/keyword.rb b/lib/dslkeywords/keyword.rb index 33d03db..2696e41 100644 --- a/lib/dslkeywords/keyword.rb +++ b/lib/dslkeywords/keyword.rb @@ -9,7 +9,7 @@ module RCM include Options include Log - def initialize(name) = @id = "#{self.class.to_s.sub('RCM::', '')}[#{name}]" + def initialize(name) = @id = "#{self.class.to_s.sub('RCM::', '').downcase}['#{name}']" def to_s = @id end end diff --git a/lib/dslkeywords/notify.rb b/lib/dslkeywords/notify.rb new file mode 100644 index 0000000..c7d50d9 --- /dev/null +++ b/lib/dslkeywords/notify.rb @@ -0,0 +1,32 @@ +require 'erb' +require 'fileutils' + +require_relative 'resource' + +module RCM + # Only to print out something + class Notify < Resource + def initialize(message) + super(message) + @message = message + end + + def message(msg) + @message = msg unless msg.nil? + end + + def evaluate! = puts "#{id} => #{@message}" + end + + # Add notify keyword to the DSL + class DSL + def notify(message, &block) + return unless @conds_met + + n = Notify.new(message) + n.message(n.instance_eval(&block)) if block + self << n + n + end + end +end diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb new file mode 100644 index 0000000..6612428 --- /dev/null +++ b/lib/dslkeywords/resource.rb @@ -0,0 +1,31 @@ +require_relative 'keyword' + +module RCM + # To track recource dependencies + module Dependency + # Only to have the resourcename[id] syntax available in the DSL + class SyntaxSugar + def initialize(name) + @name = name + end + + def [](other) = "#{@name}['#{other}']" + end + + def method_missing(method_name) = SyntaxSugar.new(method_name) + def respond_to_missing? = true + + def depends_on(*others) + @depends_on = {} if @depends_on.nil? + others.each do |other| + info "Registered dependency on #{other}" + @depends_on[other] = {} + end + end + end + + # A resource is something concrete to be managed, e.g. a file, or a CRON job. + class Resource < Keyword + include Dependency + end +end diff --git a/playground/Rakefile b/playground/Rakefile index aff881d..ef53d00 100644 --- a/playground/Rakefile +++ b/playground/Rakefile @@ -37,6 +37,7 @@ task :foo do configure do file '/tmp/foo.txt' do ensure_line 'foo' + depends_on file['/tmp/bar.txt'] end file '/tmp/bar.txt' do diff --git a/test/lib/dslkeywords/dependency_test.rb b/test/lib/dslkeywords/dependency_test.rb new file mode 100644 index 0000000..ccbaec6 --- /dev/null +++ b/test/lib/dslkeywords/dependency_test.rb @@ -0,0 +1,17 @@ +require 'minitest/autorun' +require 'fileutils' + +require_relative '../../../lib/dsl' + +class RCMDependencyTest < Minitest::Test + def test_dependency + configure_from_scratch do + notify 'foo' do + depends_on notify['bar'], file['baz'] + :HELLO + end + + notify 'bar' + end + end +end |
