Class: Pandocomatic::ConvertDirCommand

Inherits:
ConvertListCommand show all
Defined in:
lib/pandocomatic/command/convert_dir_command.rb

Overview

Commmand to convert a directory

Instance Attribute Summary collapse

Attributes inherited from ConvertListCommand

#subcommands

Attributes inherited from Command

#errors, #index

Instance Method Summary collapse

Methods inherited from ConvertListCommand

#all_errors, #count, #execute, #multiple?, #push

Methods inherited from Command

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

Constructor Details

#initialize(current_config, src_dir, dst_dir) ⇒ ConvertDirCommand

Create a new ConvertDirCommand

Parameters:

  • current_config (Configuration)

    The configuration of pandocomatic as it was before entering the source directory

  • src_dir (String)

    the directory to convert

  • dst_dir (String)

    the directory to convert to



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
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 53

def initialize(current_config, src_dir, dst_dir)
  super()
  @src_dir = src_dir
  @config = current_config

  begin
    @config = reconfigure current_config, @src_dir
  rescue ConfigurationError => e
    @errors.push e
  end

  @dst_dir = dst_dir

  if Dir.exist? @dst_dir
    @errors.push IOError.new(:directory_is_not_readable, nil, @dst_dir) unless File.readable? @dst_dir
    @errors.push IOError.new(:directory_is_not_writable, nil, @dst_dir) unless File.writable? @dst_dir
    @errors.push IOError.new(:directory_is_not_a_directory, nil, @dst_dir) unless File.directory? @dst_dir
  end

  Dir.foreach @src_dir do |filename|
    src = File.join @src_dir, filename

    next if @config.skip? src

    @errors.push IOError.new(:file_or_directory_does_not_exist, nil, src) unless File.exist? src

    dst = File.join @dst_dir, filename

    if File.symlink?(src) && !@config.follow_links?
      subcommand = CreateLinkCommand.new(src, dst)
    elsif File.directory? src
      subcommand = if @config.recursive?
                     ConvertDirCommand.new(@config, src, dst)
                   else
                     SkipCommand.new(src, :skipping_directory)
                   end
    elsif File.file? src
      if @config.convert? src
        subcommand = ConvertFileMultipleCommand.new(@config, src, dst)
      elsif !modified_only? || file_modified?(src, dst)
        subcommand = CopyFileCommand.new(src, dst)
      end
    else
      subcommand = SkipCommand.new(src, :unclear_what_to_do)
    end

    push subcommand unless subcommand.nil? || subcommand.skip?
  end

  # Empty commands do not count to the total amount of commands to execute
  uncount if skip?
end

Instance Attribute Details

#configConfiguration

Returns configuration to use when converting directory.

Returns:

  • (Configuration)

    configuration to use when converting directory



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 42

class ConvertDirCommand < ConvertListCommand
  attr_reader :config, :src_dir, :dst_dir

  # rubocop:disable Metrics

  # Create a new ConvertDirCommand
  #
  # @param current_config [Configuration] The configuration of pandocomatic
  #   as it was before entering the source directory
  # @param src_dir [String] the directory to convert
  # @param dst_dir [String] the directory to convert to
  def initialize(current_config, src_dir, dst_dir)
    super()
    @src_dir = src_dir
    @config = current_config

    begin
      @config = reconfigure current_config, @src_dir
    rescue ConfigurationError => e
      @errors.push e
    end

    @dst_dir = dst_dir

    if Dir.exist? @dst_dir
      @errors.push IOError.new(:directory_is_not_readable, nil, @dst_dir) unless File.readable? @dst_dir
      @errors.push IOError.new(:directory_is_not_writable, nil, @dst_dir) unless File.writable? @dst_dir
      @errors.push IOError.new(:directory_is_not_a_directory, nil, @dst_dir) unless File.directory? @dst_dir
    end

    Dir.foreach @src_dir do |filename|
      src = File.join @src_dir, filename

      next if @config.skip? src

      @errors.push IOError.new(:file_or_directory_does_not_exist, nil, src) unless File.exist? src

      dst = File.join @dst_dir, filename

      if File.symlink?(src) && !@config.follow_links?
        subcommand = CreateLinkCommand.new(src, dst)
      elsif File.directory? src
        subcommand = if @config.recursive?
                       ConvertDirCommand.new(@config, src, dst)
                     else
                       SkipCommand.new(src, :skipping_directory)
                     end
      elsif File.file? src
        if @config.convert? src
          subcommand = ConvertFileMultipleCommand.new(@config, src, dst)
        elsif !modified_only? || file_modified?(src, dst)
          subcommand = CopyFileCommand.new(src, dst)
        end
      else
        subcommand = SkipCommand.new(src, :unclear_what_to_do)
      end

      push subcommand unless subcommand.nil? || subcommand.skip?
    end

    # Empty commands do not count to the total amount of commands to execute
    uncount if skip?
  end

  # rubocop:enable Metrics

  # Should this command be skipped?
  #
  # @return [Boolean] True if this command has no sub commands
  def skip?
    @subcommands.empty?
  end

  # Converts this command a directory?
  #
  # @return [Boolean] true
  def directory?
    true
  end

  # Convert this command to a String representation for a Printer
  #
  # @return [String]
  def to_s
    "convert #{@src_dir}; #{create_directory? ? 'create and ' : ''}enter #{@dst_dir}"
  end

  # Run this command
  def run
    if create_directory?
      Pandocomatic::LOG.info "  Creating directory '#{@dst_dir}'"
      Dir.mkdir @dst_dir
    end
  rescue SystemError => e
    raise IOError.new(:error_creating_directory, e, @dst_dir)
  end

  private

  def create_directory?
    !File.exist? @dst_dir or !File.directory? @dst_dir
  end

  # If the source directory contains a configuration file, use it to
  # reconfigure the converter. Otherwise, use the current configuration
  def reconfigure(current_config, src_dir)
    config_file = File.join src_dir, Configuration::CONFIG_FILE
    if File.exist? config_file
      current_config.reconfigure config_file
    else
      current_config.clone
    end
  end
end

#dst_dirString

Returns the destination directory to convert to.

Returns:

  • (String)

    the destination directory to convert to



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 42

class ConvertDirCommand < ConvertListCommand
  attr_reader :config, :src_dir, :dst_dir

  # rubocop:disable Metrics

  # Create a new ConvertDirCommand
  #
  # @param current_config [Configuration] The configuration of pandocomatic
  #   as it was before entering the source directory
  # @param src_dir [String] the directory to convert
  # @param dst_dir [String] the directory to convert to
  def initialize(current_config, src_dir, dst_dir)
    super()
    @src_dir = src_dir
    @config = current_config

    begin
      @config = reconfigure current_config, @src_dir
    rescue ConfigurationError => e
      @errors.push e
    end

    @dst_dir = dst_dir

    if Dir.exist? @dst_dir
      @errors.push IOError.new(:directory_is_not_readable, nil, @dst_dir) unless File.readable? @dst_dir
      @errors.push IOError.new(:directory_is_not_writable, nil, @dst_dir) unless File.writable? @dst_dir
      @errors.push IOError.new(:directory_is_not_a_directory, nil, @dst_dir) unless File.directory? @dst_dir
    end

    Dir.foreach @src_dir do |filename|
      src = File.join @src_dir, filename

      next if @config.skip? src

      @errors.push IOError.new(:file_or_directory_does_not_exist, nil, src) unless File.exist? src

      dst = File.join @dst_dir, filename

      if File.symlink?(src) && !@config.follow_links?
        subcommand = CreateLinkCommand.new(src, dst)
      elsif File.directory? src
        subcommand = if @config.recursive?
                       ConvertDirCommand.new(@config, src, dst)
                     else
                       SkipCommand.new(src, :skipping_directory)
                     end
      elsif File.file? src
        if @config.convert? src
          subcommand = ConvertFileMultipleCommand.new(@config, src, dst)
        elsif !modified_only? || file_modified?(src, dst)
          subcommand = CopyFileCommand.new(src, dst)
        end
      else
        subcommand = SkipCommand.new(src, :unclear_what_to_do)
      end

      push subcommand unless subcommand.nil? || subcommand.skip?
    end

    # Empty commands do not count to the total amount of commands to execute
    uncount if skip?
  end

  # rubocop:enable Metrics

  # Should this command be skipped?
  #
  # @return [Boolean] True if this command has no sub commands
  def skip?
    @subcommands.empty?
  end

  # Converts this command a directory?
  #
  # @return [Boolean] true
  def directory?
    true
  end

  # Convert this command to a String representation for a Printer
  #
  # @return [String]
  def to_s
    "convert #{@src_dir}; #{create_directory? ? 'create and ' : ''}enter #{@dst_dir}"
  end

  # Run this command
  def run
    if create_directory?
      Pandocomatic::LOG.info "  Creating directory '#{@dst_dir}'"
      Dir.mkdir @dst_dir
    end
  rescue SystemError => e
    raise IOError.new(:error_creating_directory, e, @dst_dir)
  end

  private

  def create_directory?
    !File.exist? @dst_dir or !File.directory? @dst_dir
  end

  # If the source directory contains a configuration file, use it to
  # reconfigure the converter. Otherwise, use the current configuration
  def reconfigure(current_config, src_dir)
    config_file = File.join src_dir, Configuration::CONFIG_FILE
    if File.exist? config_file
      current_config.reconfigure config_file
    else
      current_config.clone
    end
  end
end

#src_dirString

Returns the source directory to convert from.

Returns:

  • (String)

    the source directory to convert from



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 42

class ConvertDirCommand < ConvertListCommand
  attr_reader :config, :src_dir, :dst_dir

  # rubocop:disable Metrics

  # Create a new ConvertDirCommand
  #
  # @param current_config [Configuration] The configuration of pandocomatic
  #   as it was before entering the source directory
  # @param src_dir [String] the directory to convert
  # @param dst_dir [String] the directory to convert to
  def initialize(current_config, src_dir, dst_dir)
    super()
    @src_dir = src_dir
    @config = current_config

    begin
      @config = reconfigure current_config, @src_dir
    rescue ConfigurationError => e
      @errors.push e
    end

    @dst_dir = dst_dir

    if Dir.exist? @dst_dir
      @errors.push IOError.new(:directory_is_not_readable, nil, @dst_dir) unless File.readable? @dst_dir
      @errors.push IOError.new(:directory_is_not_writable, nil, @dst_dir) unless File.writable? @dst_dir
      @errors.push IOError.new(:directory_is_not_a_directory, nil, @dst_dir) unless File.directory? @dst_dir
    end

    Dir.foreach @src_dir do |filename|
      src = File.join @src_dir, filename

      next if @config.skip? src

      @errors.push IOError.new(:file_or_directory_does_not_exist, nil, src) unless File.exist? src

      dst = File.join @dst_dir, filename

      if File.symlink?(src) && !@config.follow_links?
        subcommand = CreateLinkCommand.new(src, dst)
      elsif File.directory? src
        subcommand = if @config.recursive?
                       ConvertDirCommand.new(@config, src, dst)
                     else
                       SkipCommand.new(src, :skipping_directory)
                     end
      elsif File.file? src
        if @config.convert? src
          subcommand = ConvertFileMultipleCommand.new(@config, src, dst)
        elsif !modified_only? || file_modified?(src, dst)
          subcommand = CopyFileCommand.new(src, dst)
        end
      else
        subcommand = SkipCommand.new(src, :unclear_what_to_do)
      end

      push subcommand unless subcommand.nil? || subcommand.skip?
    end

    # Empty commands do not count to the total amount of commands to execute
    uncount if skip?
  end

  # rubocop:enable Metrics

  # Should this command be skipped?
  #
  # @return [Boolean] True if this command has no sub commands
  def skip?
    @subcommands.empty?
  end

  # Converts this command a directory?
  #
  # @return [Boolean] true
  def directory?
    true
  end

  # Convert this command to a String representation for a Printer
  #
  # @return [String]
  def to_s
    "convert #{@src_dir}; #{create_directory? ? 'create and ' : ''}enter #{@dst_dir}"
  end

  # Run this command
  def run
    if create_directory?
      Pandocomatic::LOG.info "  Creating directory '#{@dst_dir}'"
      Dir.mkdir @dst_dir
    end
  rescue SystemError => e
    raise IOError.new(:error_creating_directory, e, @dst_dir)
  end

  private

  def create_directory?
    !File.exist? @dst_dir or !File.directory? @dst_dir
  end

  # If the source directory contains a configuration file, use it to
  # reconfigure the converter. Otherwise, use the current configuration
  def reconfigure(current_config, src_dir)
    config_file = File.join src_dir, Configuration::CONFIG_FILE
    if File.exist? config_file
      current_config.reconfigure config_file
    else
      current_config.clone
    end
  end
end

Instance Method Details

#directory?Boolean

Converts this command a directory?

Returns:

  • (Boolean)

    true



118
119
120
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 118

def directory?
  true
end

#runObject

Run this command



130
131
132
133
134
135
136
137
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 130

def run
  if create_directory?
    Pandocomatic::LOG.info "  Creating directory '#{@dst_dir}'"
    Dir.mkdir @dst_dir
  end
rescue SystemError => e
  raise IOError.new(:error_creating_directory, e, @dst_dir)
end

#skip?Boolean

Should this command be skipped?

Returns:

  • (Boolean)

    True if this command has no sub commands



111
112
113
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 111

def skip?
  @subcommands.empty?
end

#to_sString

Convert this command to a String representation for a Printer

Returns:

  • (String)


125
126
127
# File 'lib/pandocomatic/command/convert_dir_command.rb', line 125

def to_s
  "convert #{@src_dir}; #{create_directory? ? 'create and ' : ''}enter #{@dst_dir}"
end