Class: Pandocomatic::CreateLinkCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/pandocomatic/command/create_link_command.rb

Overview

A command to create a link to a file

Instance Attribute Summary collapse

Attributes inherited from Command

#errors, #index

Instance Method Summary collapse

Methods inherited from Command

#all_errors, #count, #debug?, #directory?, #dry_run?, #errors?, #execute, #file_modified?, #index_to_s, #make_quiet, #modified_only?, #multiple?, #quiet?, reset, #src_root, #uncount

Constructor Details

#initialize(src, dst) ⇒ CreateLinkCommand

Create a new CreateLinkCommand

Parameters:

  • src (String)

    the path to the file to link

  • dst (String)

    the path to the name of the link to create



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pandocomatic/command/create_link_command.rb', line 45

def initialize(src, dst)
  super()
  @src = src
  begin
    src_target = File.readlink @src

    if src_target.start_with? '.'
      full_src_target = File.expand_path src_target, File.dirname(src)

      if full_src_target.start_with? src_root
        @dst = dst
        @dst_target = src_target
      else
        WarningPrinter.new(Warning.new(:skipping_link_because_it_points_outside_the_source_tree, @src)).print
      end

      uncount if skip?
    end
  rescue StandardError => e
    @errors.push IOError.new(:unable_to_read_symbolic_link, e, @src)
  end
end

Instance Attribute Details

#dstString

Returns the link name to create.

Returns:

  • (String)

    the link name to create



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pandocomatic/command/create_link_command.rb', line 38

class CreateLinkCommand < Command
  attr_reader :src, :dst, :dst_target

  # Create a new CreateLinkCommand
  #
  # @param src [String] the path to the file to link
  # @param dst [String] the path to the name of the link to create
  def initialize(src, dst)
    super()
    @src = src
    begin
      src_target = File.readlink @src

      if src_target.start_with? '.'
        full_src_target = File.expand_path src_target, File.dirname(src)

        if full_src_target.start_with? src_root
          @dst = dst
          @dst_target = src_target
        else
          WarningPrinter.new(Warning.new(:skipping_link_because_it_points_outside_the_source_tree, @src)).print
        end

        uncount if skip?
      end
    rescue StandardError => e
      @errors.push IOError.new(:unable_to_read_symbolic_link, e, @src)
    end
  end

  # Run this CreateLinkCommand
  def run
    File.symlink @dst_target, @dst unless File.exist? @dst
  rescue StandardError => e
    raise IOError.new(:unable_to_create_symbolic_link, e, [@src, @dst])
  end

  # Can this CreateLinkCommand be run?
  #
  # @return [Boolean] True if there are no errors and both source and
  #   destination do exist
  def runnable?
    !(errors? or @dst.nil? or @dst_target.nil? or @src.nil?)
  end

  # Create a string representation of this CreateLinkCommand
  def to_s
    "link #{File.basename @dst} -> #{@dst_target}"
  end

  # Should this CreateLinkCommand be skipped?
  #
  # @return [Boolean]
  def skip?
    !modified_only? or !modified?
  end

  # Has the source file been modified?
  #
  # @return [Boolean]
  def modified?
    if File.exist? @dst
      absolute_dst = File.realpath @dst
      target = File.expand_path(@dst_target, absolute_dst)
      absolute_dst != target
    else
      true
    end
  end
end

#dst_targetString

Returns the link in the destination tree to link to.

Returns:

  • (String)

    the link in the destination tree to link to



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pandocomatic/command/create_link_command.rb', line 38

class CreateLinkCommand < Command
  attr_reader :src, :dst, :dst_target

  # Create a new CreateLinkCommand
  #
  # @param src [String] the path to the file to link
  # @param dst [String] the path to the name of the link to create
  def initialize(src, dst)
    super()
    @src = src
    begin
      src_target = File.readlink @src

      if src_target.start_with? '.'
        full_src_target = File.expand_path src_target, File.dirname(src)

        if full_src_target.start_with? src_root
          @dst = dst
          @dst_target = src_target
        else
          WarningPrinter.new(Warning.new(:skipping_link_because_it_points_outside_the_source_tree, @src)).print
        end

        uncount if skip?
      end
    rescue StandardError => e
      @errors.push IOError.new(:unable_to_read_symbolic_link, e, @src)
    end
  end

  # Run this CreateLinkCommand
  def run
    File.symlink @dst_target, @dst unless File.exist? @dst
  rescue StandardError => e
    raise IOError.new(:unable_to_create_symbolic_link, e, [@src, @dst])
  end

  # Can this CreateLinkCommand be run?
  #
  # @return [Boolean] True if there are no errors and both source and
  #   destination do exist
  def runnable?
    !(errors? or @dst.nil? or @dst_target.nil? or @src.nil?)
  end

  # Create a string representation of this CreateLinkCommand
  def to_s
    "link #{File.basename @dst} -> #{@dst_target}"
  end

  # Should this CreateLinkCommand be skipped?
  #
  # @return [Boolean]
  def skip?
    !modified_only? or !modified?
  end

  # Has the source file been modified?
  #
  # @return [Boolean]
  def modified?
    if File.exist? @dst
      absolute_dst = File.realpath @dst
      target = File.expand_path(@dst_target, absolute_dst)
      absolute_dst != target
    else
      true
    end
  end
end

#srcString

Returns the path to the file to link to.

Returns:

  • (String)

    the path to the file to link to



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pandocomatic/command/create_link_command.rb', line 38

class CreateLinkCommand < Command
  attr_reader :src, :dst, :dst_target

  # Create a new CreateLinkCommand
  #
  # @param src [String] the path to the file to link
  # @param dst [String] the path to the name of the link to create
  def initialize(src, dst)
    super()
    @src = src
    begin
      src_target = File.readlink @src

      if src_target.start_with? '.'
        full_src_target = File.expand_path src_target, File.dirname(src)

        if full_src_target.start_with? src_root
          @dst = dst
          @dst_target = src_target
        else
          WarningPrinter.new(Warning.new(:skipping_link_because_it_points_outside_the_source_tree, @src)).print
        end

        uncount if skip?
      end
    rescue StandardError => e
      @errors.push IOError.new(:unable_to_read_symbolic_link, e, @src)
    end
  end

  # Run this CreateLinkCommand
  def run
    File.symlink @dst_target, @dst unless File.exist? @dst
  rescue StandardError => e
    raise IOError.new(:unable_to_create_symbolic_link, e, [@src, @dst])
  end

  # Can this CreateLinkCommand be run?
  #
  # @return [Boolean] True if there are no errors and both source and
  #   destination do exist
  def runnable?
    !(errors? or @dst.nil? or @dst_target.nil? or @src.nil?)
  end

  # Create a string representation of this CreateLinkCommand
  def to_s
    "link #{File.basename @dst} -> #{@dst_target}"
  end

  # Should this CreateLinkCommand be skipped?
  #
  # @return [Boolean]
  def skip?
    !modified_only? or !modified?
  end

  # Has the source file been modified?
  #
  # @return [Boolean]
  def modified?
    if File.exist? @dst
      absolute_dst = File.realpath @dst
      target = File.expand_path(@dst_target, absolute_dst)
      absolute_dst != target
    else
      true
    end
  end
end

Instance Method Details

#modified?Boolean

Has the source file been modified?

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
# File 'lib/pandocomatic/command/create_link_command.rb', line 98

def modified?
  if File.exist? @dst
    absolute_dst = File.realpath @dst
    target = File.expand_path(@dst_target, absolute_dst)
    absolute_dst != target
  else
    true
  end
end

#runObject

Run this CreateLinkCommand



69
70
71
72
73
# File 'lib/pandocomatic/command/create_link_command.rb', line 69

def run
  File.symlink @dst_target, @dst unless File.exist? @dst
rescue StandardError => e
  raise IOError.new(:unable_to_create_symbolic_link, e, [@src, @dst])
end

#runnable?Boolean

Can this CreateLinkCommand be run?

Returns:

  • (Boolean)

    True if there are no errors and both source and destination do exist



79
80
81
# File 'lib/pandocomatic/command/create_link_command.rb', line 79

def runnable?
  !(errors? or @dst.nil? or @dst_target.nil? or @src.nil?)
end

#skip?Boolean

Should this CreateLinkCommand be skipped?

Returns:

  • (Boolean)


91
92
93
# File 'lib/pandocomatic/command/create_link_command.rb', line 91

def skip?
  !modified_only? or !modified?
end

#to_sObject

Create a string representation of this CreateLinkCommand



84
85
86
# File 'lib/pandocomatic/command/create_link_command.rb', line 84

def to_s
  "link #{File.basename @dst} -> #{@dst_target}"
end