summaryrefslogtreecommitdiff
path: root/lib/dslkeywords/symlink.rb
blob: 160da6f59dbdadf537d5d328c6fa9420ceef9997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'fileutils'

require_relative 'file'

module RCM
  # Manages symbolic links: creates or removes them, optionally under
  # a managed parent directory, and applies permissions afterwards.
  class Symlink < BaseFile
    def evaluate!
      return unless super
      return evaluate_absent! if %i[absent purged].include?(@is)
      return if ::File.symlink?(@file_path) && ::File.readlink(@file_path) == content

      create_parent_directory! if @manage_directory
      do? "Creating symlink #{@file_path}" do
        FileUtils.ln_sf(content, @file_path)
      end
    ensure
      permissions!
    end
  end

  class DSL
    def symlink(file_path = nil, &block)
      return :symlink if file_path.nil?
      return unless @conds_met

      s = Symlink.new(file_path)
      s.content(s.instance_eval(&block))
      self << s
      s
    end
  end
end