<commit_before>require 'fileutils'
require 'wright/provider'
require 'wright/util/file'

module Wright
  class Provider
    # Symlink provider. Used as a provider for {Resource::Symlink}.
    class Symlink < Wright::Provider
      # Creates or updates the symlink.
      #
      # @return [void]
      def create
        if exist?
          symlink = symlink_to_s(@resource.name, @resource.to)
          Wright.log.debug "symlink already created: #{symlink}"
          return
        end

        fail Errno::EEXIST, link_name if regular_file?
        create_link
        @updated = true
      end

      # Removes the symlink.
      #
      # @return [void]
      def remove
        if ::File.exist?(link_name) && !::File.symlink?(link_name)
          fail "'#{link_name}' is not a symlink"
        end

        if ::File.symlink?(link_name)
          remove_symlink
          @updated = true
        else
          Wright.log.debug "symlink already removed: '#{@resource.name}'"
        end
      end

      private

      # Checks if the specified link exists.
      #
      # Returns true if the link exists and points to the specified
      # target and false otherwise.
      def exist?
        ::File.symlink?(link_name) &&
          ::File.readlink(link_name) == link_to
      end

      def create_link
        symlink = symlink_to_s(@resource.name, @resource.to)
        unless_dry_run("create symlink: #{symlink}") do
          Wright::Util::File.ln_sfn(link_to, link_name)
        end
      end

      def symlink_to_s(link_name, target)
        "'#{link_name}' -> '#{target}'"
      end

      def remove_symlink
        unless_dry_run("remove symlink: '#{@resource.name}'") do
          FileUtils.rm(link_name)
        end
      end

      def regular_file?
        ::File.exist?(link_name) && !::File.symlink?(link_name)
      end

      def link_to
        Wright::Util::File.expand_tilde_path(@resource.to)
      end

      def link_name
        Wright::Util::File.expand_tilde_path(@resource.name)
      end
    end
  end
end
<commit_msg>Clean up symlink provider update checks<commit_after>require 'fileutils'
require 'wright/provider'
require 'wright/util/file'

module Wright
  class Provider
    # Symlink provider. Used as a provider for {Resource::Symlink}.
    class Symlink < Wright::Provider
      # Creates or updates the symlink.
      #
      # @return [void]
      def create
        fail Errno::EEXIST, link_name if regular_file?

        symlink = symlink_to_s(@resource.name, @resource.to)
        unless_uptodate(:create, "symlink already created: #{symlink}") do
          create_link
        end
      end

      # Removes the symlink.
      #
      # @return [void]
      def remove
        fail "'#{link_name}' is not a symlink" if regular_file?

        symlink = @resource.name
        unless_uptodate(:remove, "symlink already removed: '#{symlink}'") do
          remove_symlink
        end
      end

      private

      def uptodate?(action)
        case action
        when :create
          ::File.symlink?(link_name) &&
            ::File.readlink(link_name) == link_to
        when :remove
          !::File.exist?(link_name) && !::File.symlink?(link_name)
        end
      end

      def create_link
        symlink = symlink_to_s(@resource.name, @resource.to)
        unless_dry_run("create symlink: #{symlink}") do
          Wright::Util::File.ln_sfn(link_to, link_name)
        end
      end

      def symlink_to_s(link_name, target)
        "'#{link_name}' -> '#{target}'"
      end

      def remove_symlink
        unless_dry_run("remove symlink: '#{@resource.name}'") do
          FileUtils.rm(link_name)
        end
      end

      def regular_file?
        ::File.exist?(link_name) && !::File.symlink?(link_name)
      end

      def link_to
        return nil if @resource.to.nil?
        Wright::Util::File.expand_tilde_path(@resource.to)
      end

      def link_name
        Wright::Util::File.expand_tilde_path(@resource.name)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.10539398458861546, 'fraction_numerical': 0.0, 'mean_word_length': 2.138845553822153, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1530731', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1252, 'n_words': 295}