Sha256: 3e44c8a32f56ead1d7ca1b7464b1c6f93e9e3804ae2bf2606ca04cb49241213d

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

module AsProject

  ##########################
  # Custom Errors
  class ProjectError < StandardError; end
  class UsageError < ProjectError; end
  class BuildError < StandardError; end
end

RAND_CHARS = ['A','B','C','D','E','F','a','b','c','d','e','f']

def AsProject::windowize_cygwin_path(name)
  expr = /\/cygdrive\/([a-z]|[A-Z])/
  matched = name.match(expr)
  if(matched)
    prefix = matched.captures[0].upcase + ':'
    name.gsub!(expr, prefix)
  end
  name = name.split('/').join('\\')
  return name
end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
asproject-0.1.29 lib/asproject_utils.rb
asproject-0.1.30 lib/asproject_utils.rb