Sha256: 623d02e8a6bbdaa082ba413ba457adc3b3047feb1c340da4149122ee8ea44a7b

Contents?: true

Size: 1.85 KB

Versions: 201

Compression:

Stored size: 1.85 KB

Contents

require_relative "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)
      config = args.last.is_a?(Hash) ? args.pop : {}
      source = args.first
      action CreateLink.new(self, destination, source, config)
    end
    alias_method :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?
        source = File.expand_path(render, File.dirname(destination))
        exists? && File.identical?(source, destination)
      end

      def invoke!
        invoke_with_conflict_check do
          require "fileutils"
          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

201 entries across 154 versions & 22 rubygems

Version Path
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/actions/create_link.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/thor-1.3.0/lib/thor/actions/create_link.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/actions/create_link.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/actions/create_link.rb
thor-1.3.0 lib/thor/actions/create_link.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
thor-1.2.2 lib/thor/actions/create_link.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/actions/create_link.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/thor-1.1.0/lib/thor/actions/create_link.rb
thor-1.2.1 lib/thor/actions/create_link.rb
thor-1.2.0 lib/thor/actions/create_link.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/thor-1.1.0/lib/thor/actions/create_link.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/thor-1.1.0/lib/thor/actions/create_link.rb