Sha256: d1d6e9da2518d30a47c5fefcc6432733023057a6236aa543af50b9ca4765c066

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

module Tap
  module Generator
    
    # A mixin defining how to run manifest actions in reverse.
    module Destroy
      
      # Iterates over the actions in reverse, and collects the results.
      def iterate(actions)
        results = []
        actions.reverse_each {|action| results << yield(action) }
        results
      end
      
      # Removes the target directory if it exists.  Missing, non-directory and 
      # non-empty targets are simply logged and not removed.  When pretend is
      # true, removal is logged but does not actually happen.
      #
      # No options currently affect the behavior of this method. 
      def directory(target, options={})
        target = File.expand_path(target)
        
        case
        when !File.exists?(target)
          log_relative :missing, target
        when !File.directory?(target)
          log_relative 'not a directory', target
        when !Root::Utils.empty?(target)
          log_relative 'not empty', target
        else
          log_relative :rm, target
          FileUtils.rmdir(target) unless pretend
        end
        
        target
      end
      
      # Removes the target file if it exists.  Missing and non-file and targets
      # are simply logged and not removed.  When pretend is true, removal is 
      # logged but does not actually happen.
      #
      # No options currently affect the behavior of this method.
      def file(target, options={})
        target = File.expand_path(target)
        
        case
        when File.file?(target)
          log_relative :rm, target
          FileUtils.rm(target) unless pretend
        when File.directory?(target)
          log_relative 'not a file', target
        else
          log_relative :missing, target
        end
        
        target
      end
      
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tap-gen-0.1.3 lib/tap/generator/destroy.rb
tap-gen-0.1.1 lib/tap/generator/destroy.rb
tap-gen-0.1.0 lib/tap/generator/destroy.rb
tap-gen-0.1.2 lib/tap/generator/destroy.rb