Class: PuppetFixtures::Symlink

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_fixtures.rb

Instance Method Summary collapse

Constructor Details

#initialize(target:, link:) ⇒ Symlink

Returns a new instance of Symlink.

Parameters:

  • target (String)

    the target directory

  • link (String)

    the name of the link you wish to create



412
413
414
415
# File 'lib/puppet_fixtures.rb', line 412

def initialize(target:, link:)
  @target = target
  @link = link
end

Instance Method Details

#createObject

Create a junction on Windows or otherwise a symlink works on windows and linux



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/puppet_fixtures.rb', line 419

def create
  return if File.symlink?(@link)

  if PuppetFixtures.windows?
    begin
      require 'win32/dir'
    rescue LoadError
      # the require only works on windows
    end
    target = Pathname.new(@target).absolute? ? @target : File.join(File.dirname(@link), @target)
    if Dir.respond_to?(:create_junction)
      Dir.create_junction(@link, target)
    else
      warn 'win32-dir gem not installed, falling back to executing mklink directly'
      # TODO: use run_command
      system("call mklink /J \"#{@link.tr('/', '\\')}\" \"#{target.tr('/', '\\')}\"")
    end
  else
    FileUtils.ln_sf(@target, @link)
  end
end

#removeObject



441
442
443
# File 'lib/puppet_fixtures.rb', line 441

def remove
  FileUtils.rm_f(@link)
end

#to_sObject



445
446
447
448
# File 'lib/puppet_fixtures.rb', line 445

def to_s
  # TODO: relative?
  "#{@link} => #{@target}"
end