Sha256: d1fcd7e940e66dd79a58ef7dfc39f84622f7de9763b41ea144eec7cead144d61

Contents?: true

Size: 999 Bytes

Versions: 22

Compression:

Stored size: 999 Bytes

Contents

require 'fileutils'
require 'tempfile'

# This class provides an easy way of creating a temporary
# directory and having it removed when the application exits.
class Tempdir
  attr_reader :path

  def initialize(basename="vagrant")
    @path = nil

    # Loop and attempt to create a temporary directory until
    # it succeeds.
    while @path.nil?
      file = Tempfile.new(basename)
      @path = file.path
      file.unlink

      begin
        Dir.mkdir(@path)
      rescue
        @path = nil
      end
    end

    # Setup a finalizer to delete the directory. This is the same way
    # that Tempfile and friends do this...
    @cleanup_proc = lambda do
      FileUtils.rm_rf(@path) if File.directory?(@path)
    end

    ObjectSpace.define_finalizer(self, @cleanup_proc)
  end

  # This deletes the temporary directory.
  def unlink
    # Delete the directory
    @cleanup_proc.call

    # Undefine the finalizer since we're all cleaned up
    ObjectSpace.undefine_finalizer(self)
  end
end

Version data entries

22 entries across 22 versions & 7 rubygems

Version Path
tamtam-vagrant-reload-1.1.3 vendor/cache/vagrant-0ac2a8738841/test/support/tempdir.rb
tamtam-vagrant-reload-1.1.2 vendor/cache/vagrant-0ac2a8738841/test/support/tempdir.rb
tamtam-vagrant-reload-1.1.1 vendor/cache/vagrant-0ac2a8738841/test/support/tempdir.rb
tamtam-vagrant-reload-1.1 vendor/cache/vagrant-0ac2a8738841/test/support/tempdir.rb
vagrant-parallels-0.2.1 test/support/tempdir.rb
vagrant-parallels-0.2.0 test/support/tempdir.rb
vagrant-parallels-0.1.3 test/support/tempdir.rb
tnargav-1.3.6 test/support/tempdir.rb
tnargav-1.3.3 test/support/tempdir.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/test/support/tempdir.rb
tnargav-1.2.3 test/support/tempdir.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/test/support/tempdir.rb
vagrant-shell-0.2.6 vendor/bundle/gems/tnargav-1.2.2/test/support/tempdir.rb
vagrant-shell-0.2.5 vendor/bundle/gems/tnargav-1.2.2/test/support/tempdir.rb
tnargav-1.2.2 test/support/tempdir.rb
vagrantup-1.1.3 test/support/tempdir.rb
vagrantup-1.1.2 test/support/tempdir.rb
vagrantup-1.1.1 test/support/tempdir.rb
vagrantup-1.1.0 test/support/tempdir.rb
vagrantup-1.1.4 test/support/tempdir.rb