Class: Pandocomatic::Command

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

Overview

Command is a base class of all actions pandocomatic executes while converting a file or a directory of files.

Constant Summary collapse

@@total =

rubocop:disable Style/ClassVars

0
@@dry_run =
false
@@quiet =
false
@@src_root =
'.'
@@modified_only =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Create a new Command



46
47
48
49
50
# File 'lib/pandocomatic/command/command.rb', line 46

def initialize
  @errors = []
  @@total += 1
  @index = @@total
end

Instance Attribute Details

#errorsError[]

Returns list of errors created while preparing and running a command.

Returns:

  • (Error[])

    list of errors created while preparing and running a command



34
35
36
37
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pandocomatic/command/command.rb', line 34

class Command
  attr_reader :errors, :index

  # rubocop:disable Style/ClassVars

  @@total = 0
  @@dry_run = false
  @@quiet = false
  @@src_root = '.'
  @@modified_only = false

  # Create a new Command
  def initialize
    @errors = []
    @@total += 1
    @index = @@total
  end

  # Reset all Commands
  #
  # @param configuration [Configuration] the configuration used to convert
  def self.reset(configuration)
    @@src_root = configuration.src_root
    @@dry_run = configuration.dry_run?
    @@quiet = configuration.quiet?
    @@modified_only = configuration.modified_only?
    @@total = 0
  end

  # Get the root directory of this Command's conversion process
  #
  # @return [String]
  def src_root
    @@src_root
  end

  # Does this Command not actually execute?
  #
  # @return [Boolean]
  def dry_run?
    @@dry_run
  end

  # Is this Command executed silently?
  #
  # @return [Boolean]
  def quiet?
    @@quiet
  end

  # Is this Command only executed on modified files?
  #
  # @return [Boolean]
  def modified_only?
    @@modified_only
  end

  # The number of commands executed by this Command; a Command can have sub
  # commands as well.
  #
  # @return [Number]
  def count
    1
  end

  # Get all the errors generated while executing this Command
  #
  # @return [Error[]]
  def all_errors
    @errors
  end

  # Make this Command run quietly
  def make_quiet
    @@quiet = true
  end

  # Convert this Command's index to a string representation
  #
  # @return [String]
  def index_to_s
    (@@total - @index + 1).to_s.rjust(@@total.to_s.size)
  end

  # Execute this Command. A Command can be dry-run as well, in which it is
  # not actually run.
  def execute
    description = CommandPrinter.new(self)
    Pandocomatic::LOG.info description
    description.print unless quiet?
    run if !dry_run? && runnable?
  end

  # Actually run this Command
  def run; end

  # Are there any errors while configuring this Command? If not, this
  # Command is runnable.
  #
  # @return [Boolean]
  def runnable?
    !errors?
  end

  # Create a String representation of this Command
  #
  # @return [String]
  def to_s
    'command'
  end

  # Is this Command converting a directory?
  #
  # @return [Boolean] false
  def directory?
    false
  end

  # Does this Command convert a file multiple times?
  #
  # @return [Boolean] false
  def multiple?
    false
  end

  # Will this Command be skipped, thus not executed?
  #
  # @return [Boolean] false
  def skip?
    false
  end

  # Decrement the total number of conversion commands by 1
  def uncount
    @@total -= 1
  end

  # rubocop:enable Style/ClassVars

  # Has this Command run in any errors?
  #
  # @return [Error[]]
  def errors?
    !@errors.empty?
  end

  # Is the source file newer than the destination file?
  #
  # @param src [String] the source file
  # @param dst [String] the destination file
  #
  # @return [Boolean] True if src has been modified after dst has been last
  def file_modified?(src, dst)
    !File.exist? dst or File.mtime(src) > File.mtime(dst)
  end
end

#indexNumber

Returns the index of this Command in the list with all commands to run when running pandocomatic.

Returns:

  • (Number)

    the index of this Command in the list with all commands to run when running pandocomatic.



34
35
36
37
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pandocomatic/command/command.rb', line 34

class Command
  attr_reader :errors, :index

  # rubocop:disable Style/ClassVars

  @@total = 0
  @@dry_run = false
  @@quiet = false
  @@src_root = '.'
  @@modified_only = false

  # Create a new Command
  def initialize
    @errors = []
    @@total += 1
    @index = @@total
  end

  # Reset all Commands
  #
  # @param configuration [Configuration] the configuration used to convert
  def self.reset(configuration)
    @@src_root = configuration.src_root
    @@dry_run = configuration.dry_run?
    @@quiet = configuration.quiet?
    @@modified_only = configuration.modified_only?
    @@total = 0
  end

  # Get the root directory of this Command's conversion process
  #
  # @return [String]
  def src_root
    @@src_root
  end

  # Does this Command not actually execute?
  #
  # @return [Boolean]
  def dry_run?
    @@dry_run
  end

  # Is this Command executed silently?
  #
  # @return [Boolean]
  def quiet?
    @@quiet
  end

  # Is this Command only executed on modified files?
  #
  # @return [Boolean]
  def modified_only?
    @@modified_only
  end

  # The number of commands executed by this Command; a Command can have sub
  # commands as well.
  #
  # @return [Number]
  def count
    1
  end

  # Get all the errors generated while executing this Command
  #
  # @return [Error[]]
  def all_errors
    @errors
  end

  # Make this Command run quietly
  def make_quiet
    @@quiet = true
  end

  # Convert this Command's index to a string representation
  #
  # @return [String]
  def index_to_s
    (@@total - @index + 1).to_s.rjust(@@total.to_s.size)
  end

  # Execute this Command. A Command can be dry-run as well, in which it is
  # not actually run.
  def execute
    description = CommandPrinter.new(self)
    Pandocomatic::LOG.info description
    description.print unless quiet?
    run if !dry_run? && runnable?
  end

  # Actually run this Command
  def run; end

  # Are there any errors while configuring this Command? If not, this
  # Command is runnable.
  #
  # @return [Boolean]
  def runnable?
    !errors?
  end

  # Create a String representation of this Command
  #
  # @return [String]
  def to_s
    'command'
  end

  # Is this Command converting a directory?
  #
  # @return [Boolean] false
  def directory?
    false
  end

  # Does this Command convert a file multiple times?
  #
  # @return [Boolean] false
  def multiple?
    false
  end

  # Will this Command be skipped, thus not executed?
  #
  # @return [Boolean] false
  def skip?
    false
  end

  # Decrement the total number of conversion commands by 1
  def uncount
    @@total -= 1
  end

  # rubocop:enable Style/ClassVars

  # Has this Command run in any errors?
  #
  # @return [Error[]]
  def errors?
    !@errors.empty?
  end

  # Is the source file newer than the destination file?
  #
  # @param src [String] the source file
  # @param dst [String] the destination file
  #
  # @return [Boolean] True if src has been modified after dst has been last
  def file_modified?(src, dst)
    !File.exist? dst or File.mtime(src) > File.mtime(dst)
  end
end

Class Method Details

.reset(configuration) ⇒ Object

Reset all Commands

Parameters:

  • configuration (Configuration)

    the configuration used to convert



55
56
57
58
59
60
61
# File 'lib/pandocomatic/command/command.rb', line 55

def self.reset(configuration)
  @@src_root = configuration.src_root
  @@dry_run = configuration.dry_run?
  @@quiet = configuration.quiet?
  @@modified_only = configuration.modified_only?
  @@total = 0
end

Instance Method Details

#all_errorsError[]

Get all the errors generated while executing this Command

Returns:

  • (Error[])


102
103
104
# File 'lib/pandocomatic/command/command.rb', line 102

def all_errors
  @errors
end

#countNumber

The number of commands executed by this Command; a Command can have sub commands as well.

Returns:

  • (Number)


95
96
97
# File 'lib/pandocomatic/command/command.rb', line 95

def count
  1
end

#directory?Boolean

Is this Command converting a directory?

Returns:

  • (Boolean)

    false



148
149
150
# File 'lib/pandocomatic/command/command.rb', line 148

def directory?
  false
end

#dry_run?Boolean

Does this Command not actually execute?

Returns:

  • (Boolean)


73
74
75
# File 'lib/pandocomatic/command/command.rb', line 73

def dry_run?
  @@dry_run
end

#errors?Error[]

Has this Command run in any errors?

Returns:

  • (Error[])


176
177
178
# File 'lib/pandocomatic/command/command.rb', line 176

def errors?
  !@errors.empty?
end

#executeObject

Execute this Command. A Command can be dry-run as well, in which it is not actually run.



120
121
122
123
124
125
# File 'lib/pandocomatic/command/command.rb', line 120

def execute
  description = CommandPrinter.new(self)
  Pandocomatic::LOG.info description
  description.print unless quiet?
  run if !dry_run? && runnable?
end

#file_modified?(src, dst) ⇒ Boolean

Is the source file newer than the destination file?

Parameters:

  • src (String)

    the source file

  • dst (String)

    the destination file

Returns:

  • (Boolean)

    True if src has been modified after dst has been last



186
187
188
# File 'lib/pandocomatic/command/command.rb', line 186

def file_modified?(src, dst)
  !File.exist? dst or File.mtime(src) > File.mtime(dst)
end

#index_to_sString

Convert this Command’s index to a string representation

Returns:

  • (String)


114
115
116
# File 'lib/pandocomatic/command/command.rb', line 114

def index_to_s
  (@@total - @index + 1).to_s.rjust(@@total.to_s.size)
end

#make_quietObject

Make this Command run quietly



107
108
109
# File 'lib/pandocomatic/command/command.rb', line 107

def make_quiet
  @@quiet = true
end

#modified_only?Boolean

Is this Command only executed on modified files?

Returns:

  • (Boolean)


87
88
89
# File 'lib/pandocomatic/command/command.rb', line 87

def modified_only?
  @@modified_only
end

#multiple?Boolean

Does this Command convert a file multiple times?

Returns:

  • (Boolean)

    false



155
156
157
# File 'lib/pandocomatic/command/command.rb', line 155

def multiple?
  false
end

#quiet?Boolean

Is this Command executed silently?

Returns:

  • (Boolean)


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

def quiet?
  @@quiet
end

#runObject

Actually run this Command



128
# File 'lib/pandocomatic/command/command.rb', line 128

def run; end

#runnable?Boolean

Are there any errors while configuring this Command? If not, this Command is runnable.

Returns:

  • (Boolean)


134
135
136
# File 'lib/pandocomatic/command/command.rb', line 134

def runnable?
  !errors?
end

#skip?Boolean

Will this Command be skipped, thus not executed?

Returns:

  • (Boolean)

    false



162
163
164
# File 'lib/pandocomatic/command/command.rb', line 162

def skip?
  false
end

#src_rootString

Get the root directory of this Command’s conversion process

Returns:

  • (String)


66
67
68
# File 'lib/pandocomatic/command/command.rb', line 66

def src_root
  @@src_root
end

#to_sString

Create a String representation of this Command

Returns:

  • (String)


141
142
143
# File 'lib/pandocomatic/command/command.rb', line 141

def to_s
  'command'
end

#uncountObject

Decrement the total number of conversion commands by 1



167
168
169
# File 'lib/pandocomatic/command/command.rb', line 167

def uncount
  @@total -= 1
end