lib/rubber/util.rb in rubber-1.15.0 vs lib/rubber/util.rb in rubber-2.0.0.pre1
- old
+ new
@@ -1,18 +1,18 @@
module Rubber
module Util
- def self.symbolize_keys(map)
+ def symbolize_keys(map)
map.inject({}) do |options, (key, value)|
options[key.to_sym || key] = value
options
end
end
- def self.stringify(val)
+ def stringify(val)
case val
when String
val
when Hash
val.inject({}) {|h, a| h[stringify(a[0])] = stringify(a[1]); h}
@@ -22,11 +22,11 @@
val.to_s
end
end
- def self.parse_aliases(instance_aliases)
+ def parse_aliases(instance_aliases)
aliases = []
alias_patterns = instance_aliases.to_s.strip.split(/\s*,\s*/)
alias_patterns.each do |a|
if a =~ /~/
range = a.split(/~/)
@@ -39,50 +39,71 @@
end
return aliases
end
# Opens the file for writing by root
- def self.sudo_open(path, perms, &block)
+ def sudo_open(path, perms, &block)
open("|sudo tee #{path} > /dev/null", perms, &block)
end
- def self.is_rails?
- File.exist?(File.join(RUBBER_ROOT, 'config', 'boot.rb'))
+ def is_rails?
+ File.exist?(File.join(Rubber.root, 'config', 'boot.rb'))
end
- def self.is_rails2?
- defined?(Rails) && defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 2
+ def is_bundler?
+ File.exist?(File.join(Rubber.root, 'Gemfile'))
end
- def self.is_rails3?
- defined?(Rails) && defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 3
+ def has_asset_pipeline?
+ is_rails? && Dir["#{Rubber.root}/*/assets"].size > 0
end
- def self.is_bundler?
- File.exist?(File.join(RUBBER_ROOT, 'Gemfile'))
- end
-
- def self.rubber_as_plugin?
- File.exist?(File.join(RUBBER_ROOT, 'vendor/plugins/rubber'))
- end
-
- def self.prompt(name, desc, required=false, default=nil)
+ def prompt(name, desc, required=false, default=nil)
value = ENV.delete(name)
msg = "#{desc}"
msg << " [#{default}]" if default
msg << ": "
unless value
print msg
value = gets
end
value = value.size == 0 ? default : value
- self.fatal "#{name} is required, pass using environment or enter at prompt" if required && ! value
+ fatal "#{name} is required, pass using environment or enter at prompt" if required && ! value
return value
end
- def self.fatal(msg, code=1)
+ def fatal(msg, code=1)
puts msg
exit code
end
-
+
+ # remove leading whitespace from "here" strings so they look good in code
+ # skips empty lines
+ def clean_indent(str)
+ counts = str.lines.collect {|l| l.scan(/^\s*/).first.size }
+ m = counts.reject {|x| x <= 1 }.min
+ str.lines.collect {|l| l.size < m ? l : l[m..-1] }.join("")
+ end
+
+ # execute the given block, retrying only when one of the given exceptions is raised
+ def retry_on_failure(*exception_list)
+ opts = exception_list.last.is_a?(Hash) ? exception_list.pop : {}
+ opts = {:retry_count => 3}.merge(opts)
+ retry_count = opts[:retry_count]
+ begin
+ yield
+ rescue *exception_list => e
+ if retry_count > 0
+ retry_count -= 1
+ Rubber.logger.info "Exception, trying again #{retry_count} more times"
+ sleep opts[:retry_sleep].to_i if opts[:retry_sleep]
+ retry
+ else
+ Rubber.logger.error "Too many exceptions...re-raising"
+ raise
+ end
+ end
+ end
+
+ extend self
end
end