From 1f085e83f018048931079e9bf0a564b461fd713c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 Feb 2025 14:58:47 +0200 Subject: fix --- TODO.md | 3 +++ lib/dslkeywords/file.rb | 9 ++++++--- lib/options.rb | 3 ++- test/lib/dslkeywords/file_test.rb | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 1af7ab4..1e61407 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,8 @@ # TODO +* Implement dry run mode, e.g. use if option :dry ... + * Go through all keywords and evaluate methods! + * Print out what it would do but don't do it (helper method?) * Backup file when it gets deleted * Recursively install a directory * Support for file deletion diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index d9a31fa..a734042 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -8,7 +8,7 @@ require_relative '../chained' module RCM # Backup the file on change module FileBackup - def backup!(file_path, checksum) + def backup!(file_path, checksum = Digest::SHA256.file(file_path).hexdigest) return if @without_backup backup_dir = "#{::File.dirname(file_path)}/.rcm" @@ -68,10 +68,13 @@ module RCM def evaluate_absent! if ::File.exist?(@file_path) info("Deleting #{@file_path}") - ::File.delete(@file_path) + backup!(@file_path) + ::File.delete(@file_path) if ::File.file?(@file_path) end - return unless @manage_directory + cleanup_directory! if @manage_directory + end + def cleanup_directory! parent_dir = ::File.dirname(@file_path) while Dir.empty?(parent_dir) info("Deleting empty parent directory #{parent_dir}") diff --git a/lib/options.rb b/lib/options.rb index ee30295..401d135 100644 --- a/lib/options.rb +++ b/lib/options.rb @@ -3,12 +3,13 @@ require 'optparse' module RCM # Command line options module Options - @@options = { debug: false } + @@options = { debug: false, dry: false } if (after_double_dash = ARGV.slice_before('--').to_a.last&.drop(1)) OptionParser.new do |opts| opts.banner = 'Usage: rake [task] -- [options]' opts.on('-v', '--[no-]debug', 'debug output') { |v| @@options[:debug] = v } + opts.on('-d', '--dry', 'dry mode') { |v| @@options[:dry] = v } end.parse!(after_double_dash) end diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb index 001d272..9390aed 100644 --- a/test/lib/dslkeywords/file_test.rb +++ b/test/lib/dslkeywords/file_test.rb @@ -62,7 +62,7 @@ class RCMFileTest < Minitest::Test file delete File empty directory do path file_path is absent - manage directory + manage directory and without backup requires file create File empty directory end end -- cgit v1.2.3