diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | lib/dslkeywords/file.rb | 22 | ||||
| -rw-r--r-- | lib/dslkeywords/package.rb | 44 |
3 files changed, 23 insertions, 44 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..15c87f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.rcm diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index e83906e..51e241e 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -7,12 +7,9 @@ require_relative 'resource' module RCM # Backup the file on change module FileBackup - def backup!(path) - return unless ::File.exist?(path) - + def backup!(path, checksum) backup_dir = "#{::File.dirname(path)}/.rcm" Dir.mkdir(backup_dir) unless ::File.directory?(backup_dir) - checksum = Digest::SHA256.file(path).hexdigest backup_path = "#{backup_dir}/#{::File.basename(path)}.#{checksum}" return if ::File.exist?(backup_path) @@ -62,12 +59,25 @@ module RCM end def write_content!(text) + info "Managing file #{@path}" + create_parent_directory! debug text if option :debug - info "Managing file #{@path}" + tmp_path = "#{@path}.tmp" ::File.write(tmp_path, text) - backup!(@path) + + if ::File.file?(@path) + checksum = Digest::SHA256.file(@path).hexdigest + tmp_checksum = Digest::SHA256.file(tmp_path).hexdigest + + if tmp_checksum == checksum + ::File.delete(tmp_path) # File has not changed, not doing anything + return + end + backup!(@path, checksum) # File changed, backup! + end + ::File.rename(tmp_path, @path) end diff --git a/lib/dslkeywords/package.rb b/lib/dslkeywords/package.rb index d0bce85..b28ed50 100644 --- a/lib/dslkeywords/package.rb +++ b/lib/dslkeywords/package.rb @@ -13,54 +13,22 @@ module RCM @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 + def packages(*pks) + raise 'Not yet implemented' 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 + def evaluate! + nil unless super end end # Add file keyword to the DSL class DSL - def file(name, &block) + def package(name, &block) return unless @conds_met f = Package.new(name) - f.content(f.instance_eval(&block)) + f.packages(f.instance_eval(&block)) self << f f end |
