Sha256: 173a276b301fc809c14f00f882943fba4157b3ffd420dbebfc761deef0b9b662

Contents?: true

Size: 1.75 KB

Versions: 30

Compression:

Stored size: 1.75 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)
      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?
        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

30 entries across 29 versions & 7 rubygems

Version Path
tdiary-5.0.13 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.12.1 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.11 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.9 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.8 vendor/bundle/gems/tdiary-5.0.7/vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.7 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.6 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
bibtex_to_scrapbox-0.2.0 gems/gems/thor-0.19.4/lib/thor/actions/create_link.rb
bibtex_to_scrapbox-0.1.0 gems/gems/thor-0.19.4/lib/thor/actions/create_link.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/thor-0.19.4/lib/thor/actions/create_link.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/thor-0.19.4/lib/thor/actions/create_link.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/thor-0.19.4/lib/thor/actions/create_link.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.5 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/thor-0.19.4/lib/thor/actions/create_link.rb
tdiary-5.0.4 vendor/bundle/gems/thor-0.19.4/lib/thor/actions/create_link.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/thor-0.19.4/lib/thor/actions/create_link.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/thor-0.19.4/lib/thor/actions/create_link.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/thor-0.19.4/lib/thor/actions/create_link.rb