Sha256: 3d4b61b41187594691e26e2e378394b08c7490228ec030673747b63164799e55

Contents?: true

Size: 1.76 KB

Versions: 309

Compression:

Stored size: 1.76 KB

Contents

require 'thor/actions/create_file'

class Thor
  module Actions

    # Create a new file relative to the destination root from the given source.
    #
    # ==== Parameters
    # destination<String>:: the relative path to the destination root.
    # source<String|NilClass>:: the relative path to the source root.
    # config<Hash>:: give :verbose => false to not log the status.
    #   :: give :symbolic => false for hard link.
    #
    # ==== Examples
    #
    #   create_link "config/apache.conf", "/etc/apache.conf"
    #
    def create_link(destination, *args, &block)
      config = args.last.is_a?(Hash) ? args.pop : {}
      source = args.first
      action CreateLink.new(self, destination, source, config)
    end
    alias :add_link :create_link

    # CreateLink is a subset of CreateFile, which instead of taking a block of
    # data, just takes a source string from the user.
    #
    class CreateLink < CreateFile #:nodoc:
      attr_reader :data

      # Checks if the content of the file at the destination is identical to the rendered result.
      #
      # ==== Returns
      # Boolean:: true if it is identical, false otherwise.
      #
      def identical?
        exists? && File.identical?(render, destination)
      end

      def invoke!
        invoke_with_conflict_check do
          FileUtils.mkdir_p(File.dirname(destination))
          # Create a symlink by default
          config[:symbolic] = true if config[:symbolic].nil?
          File.unlink(destination) if exists?
          if config[:symbolic]
            File.symlink(render, destination)
          else
            File.link(render, destination)
          end
        end
        given_destination
      end

      def exists?
        super || File.symlink?(destination)
      end
    end
  end
end

Version data entries

309 entries across 301 versions & 21 rubygems

Version Path
skylight-3.1.2 lib/skylight/vendor/cli/thor/actions/create_link.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/thor-0.18.1/lib/thor/actions/create_link.rb
skylight-3.1.1 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-3.1.0 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-3.1.0.beta3 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-1.7.1 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-3.1.0.beta2 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-3.1.0.beta lib/skylight/vendor/cli/thor/actions/create_link.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/thor-0.18.1/lib/thor/actions/create_link.rb
skylight-3.0.0 lib/skylight/vendor/cli/thor/actions/create_link.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/thor-0.18.1/lib/thor/actions/create_link.rb
skylight-3.0.0.beta lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-2.0.2 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-2.0.2.beta.1 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-2.0.2.beta lib/skylight/vendor/cli/thor/actions/create_link.rb
vagrant-unbundled-2.1.1.0 vendor/bundle/ruby/2.5.0/gems/thor-0.18.1/lib/thor/actions/create_link.rb
vagrant-unbundled-2.0.4.0 vendor/bundle/ruby/2.5.0/gems/thor-0.18.1/lib/thor/actions/create_link.rb
skylight-2.0.1 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-2.0.0 lib/skylight/vendor/cli/thor/actions/create_link.rb
skylight-1.7.0 lib/skylight/vendor/cli/thor/actions/create_link.rb