module AsProject ########################## # Custom Errors class ProjectError < StandardError; end class UsageError < ProjectError; end end RAND_CHARS = ['A','B','C','D','E','F','a','b','c','d','e','f'] def Dir::unique_tmpdir(application='RubyApplication') rand_max = RAND_CHARS.size uniqueish_name = '' 24.times{ uniqueish_name << RAND_CHARS[rand(rand_max)] } tmp = Dir.tmpdir app_tmp = File.join(tmp, application) if(!File.exists?(app_tmp)) Dir.mkdir(app_tmp) end tmp = File.expand_path(File.join(app_tmp, uniqueish_name)) if(File.exists?(tmp) && is_retry) raise ProjectError.new('Dir.unique_tmpdir attempted to create a new dir over an existing dir') # else if(File.exists?(tmp)) # Try again once before failing, uniqueish names aren't too unique... # Dir.unique_tmpdir(application, true) end Dir.mkdir(tmp) return tmp end def Dir::tmpdir tmp = nil for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], ENV['USERPROFILE'], '/tmp'] if dir and File.directory?(dir) and File.writable?(dir) tmp = dir break end end if(tmp.nil?) raise ProjectError.new('AsProject was unable to find a valid temporary directory on this computer.') end # Has to actually CD to the dir, because # on os x, tmp == /tmp until you're in it, # then it's /private/tmp?! # TestCases are relying on this value to # Be identical when you retrieve it and # when you are 'in' it... start = Dir.pwd Dir.chdir(tmp) tmp = Dir.pwd Dir.chdir(start) return tmp end