From f0b0dae5bb2e16cf45223fc31396708bd069d06c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 7 Dec 2024 14:36:21 +0200 Subject: cosmetics --- Rakefile | 8 ++++---- lib/dsl.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++ lib/dslkeywords/file.rb | 4 ++-- lib/dslkeywords/only_when.rb | 2 +- lib/rcm.rb | 47 ------------------------------------------- 5 files changed, 55 insertions(+), 54 deletions(-) create mode 100644 lib/dsl.rb delete mode 100644 lib/rcm.rb diff --git a/Rakefile b/Rakefile index 8dff868..acb3c15 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,8 @@ -require_relative 'lib/rcm' +require_relative 'lib/dsl' desc 'Set up wireguard mesh' task :wireguard do - make_it_so do + configure do # p option :verbose # dump_config only_when { hostname is :earth } @@ -23,9 +23,9 @@ end desc 'foo task' task :foo do - make_it_so do + configure do file '/tmp/test.txt' do [ 'foo', 'bar', 'baz' ].sort end end -end +end diff --git a/lib/dsl.rb b/lib/dsl.rb new file mode 100644 index 0000000..160c1ac --- /dev/null +++ b/lib/dsl.rb @@ -0,0 +1,48 @@ +require_relative 'config' +require_relative 'options' +require_relative 'log' + +Dir["#{Dir.pwd}/lib/dslkeywords/*.rb"].each { |m| require m } + +# Ruby Configiration Management system +module RCM + # Here all starts + class DSL + attr_reader :id + + @@rcm_counter = -1 + @@objs = {} + + include Config + include Options + include Log + + def initialize + @id = "#{self.class}(#{@@rcm_counter += 1})" + @conds_met = true + @scheduled = [] + yield self if block_given? + end + + def to_s + "RCM #{@number}" + end + + def evaluate! + @scheduled.each(&:evaluate!) + end + + def <<(obj) + fatal_exit "Object #{obj.id} already declared!" if @@objs.key?(obj.id) + @scheduled << @@objs[obj.id] = obj + end + end +end + +def configure(&block) + RCM::DSL.new do |rcm| + rcm.info('Configuring...') + rcm.instance_eval(&block) + rcm.evaluate! + end +end diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index 746b41c..35db9c2 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -39,7 +39,7 @@ module RCM @from_template = true end - def do! + def evaluate! content = real_content dirname = ::File.dirname(@path) @@ -65,7 +65,7 @@ module RCM end # Add file keyword to the DSL - class RCM + class DSL def file(path, &block) return unless @conds_met diff --git a/lib/dslkeywords/only_when.rb b/lib/dslkeywords/only_when.rb index 2700bee..2f493b2 100644 --- a/lib/dslkeywords/only_when.rb +++ b/lib/dslkeywords/only_when.rb @@ -27,7 +27,7 @@ module RCM end # Add 'only_when' to DSL - class RCM + class DSL def only_when(&block) conds = OnlyWhen.new conds.instance_eval(&block) diff --git a/lib/rcm.rb b/lib/rcm.rb deleted file mode 100644 index a740cb2..0000000 --- a/lib/rcm.rb +++ /dev/null @@ -1,47 +0,0 @@ -require_relative 'config' -require_relative 'options' -require_relative 'log' - -Dir["#{Dir.pwd}/lib/dslkeywords/*.rb"].each { |m| require m } - -# Ruby Configiration Management system -module RCM - # Here all starts - class RCM - attr_reader :id - - @@rcm_counter = -1 - @@objs = {} - - include Config - include Options - include Log - - def initialize - @@rcm_counter += 1 - @id = "#{self.class}(#{@@rcm_counter})" - @conds_met = true - @scheduled = [] - end - - def to_s - "RCM #{@number}" - end - - def do! - @scheduled.each(&:do!) - end - - def <<(obj) - fatal_exit "Object #{obj.id} already declared!" if @@objs.key?(obj.id) - @scheduled << @@objs[obj.id] = obj - end - end -end - -def make_it_so(&block) - rcm = RCM::RCM.new - rcm.info('Making it so...') - rcm.instance_eval(&block) - rcm.do! -end -- cgit v1.2.3