lib/ruby_sync/util/utilities.rb in rubysync-0.0.2 vs lib/ruby_sync/util/utilities.rb in rubysync-0.0.3
- old
+ new
@@ -16,15 +16,24 @@
require 'fileutils'
require 'irb'
+class ::File
+ def self.delete_if_exists(files)
+ files.kind_of?(Array) or files = [files]
+ files.each do |file|
+ File.delete(file) if File.exist?(file)
+ end
+ end
+end
# Generally useful methods
module RubySync
module Utilities
+ @@base_path=nil
# Perform an action and rescue any exceptions thrown, display the exception with the specified text
def with_rescue text
begin
yield
@@ -48,19 +57,21 @@
log.info "Result of #{last_action}: #{hint}\n" + YAML.dump(event)
end
# Ensure that a given path exists as a directory
- def ensure_dir_exists path
- raise Exception.new("Can't create nil directory") unless path
- if File.exist? path
- unless File.directory? path
- raise Exception.new("'#{path}' exists but is not a directory")
+ def ensure_dir_exists paths
+ paths.as_array.each do |path|
+ raise Exception.new("Can't create nil directory") unless path
+ if File.exist? path
+ unless File.directory? path
+ raise Exception.new("'#{path}' exists but is not a directory")
+ end
+ else
+ log.info "Creating directory '#{path}'"
+ FileUtils.mkpath path
end
- else
- log.info "Creating directory '#{path}'"
- FileUtils.mkpath path
end
end
def pipeline_called name
something_called name, "pipeline"
@@ -86,26 +97,26 @@
$:.unshift path unless $:.include?(path)
end
# Return the base_path
def base_path
- @base_path = find_base_path unless defined? @base_path
- @base_path
+ @@base_path = find_base_path unless @@base_path
+ @@base_path
end
# Locate a configuration directory by checking the current directory and
# all of it's ancestors until it finds one that looks like a rubysync configuration
# directory.
# Returns false if no suitable directory was found
def find_base_path
- base_path = File.expand_path(".")
+ bp = File.expand_path(".")
last = nil
# Keep going up until we start repeating ourselves
- while File.directory?(base_path) && base_path != last && base_path != "/"
- return base_path if File.directory?("#{base_path}/pipelines") &&
- File.directory?("#{base_path}/connectors")
- last = base_path
- base_path = File.expand_path("#{base_path}/..")
+ while File.directory?(bp) && bp != last && bp != "/"
+ return bp if File.directory?("#{bp}/pipelines") &&
+ File.directory?("#{bp}/connectors")
+ last = bp
+ bp = File.expand_path("#{bp}/..")
end
return false
end
# Make and instance method _name_ that returns the value set by the
\ No newline at end of file