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
@@debug =
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



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

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
190
191
192
193
194
195
196
# 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
  @@debug = 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?
    @@debug = configuration.debug?
    @@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 executed in debug mode?
  #
  # @return [Boolean]
  def debug?
    @@debug
  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
    CommandPrinter.new(self).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
190
191
192
193
194
195
196
# 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
  @@debug = 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?
    @@debug = configuration.debug?
    @@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 executed in debug mode?
  #
  # @return [Boolean]
  def debug?
    @@debug
  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
    CommandPrinter.new(self).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



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

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

Instance Method Details

#all_errorsError[]

Get all the errors generated while executing this Command

Returns:

  • (Error[])


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

def all_errors
  @errors
end

#countNumber

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

Returns:

  • (Number)


104
105
106
# File 'lib/pandocomatic/command/command.rb', line 104

def count
  1
end

#debug?Boolean

Is this Command executed in debug mode?

Returns:

  • (Boolean)


89
90
91
# File 'lib/pandocomatic/command/command.rb', line 89

def debug?
  @@debug
end

#directory?Boolean

Is this Command converting a directory?

Returns:

  • (Boolean)

    false



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

def directory?
  false
end

#dry_run?Boolean

Does this Command not actually execute?

Returns:

  • (Boolean)


75
76
77
# File 'lib/pandocomatic/command/command.rb', line 75

def dry_run?
  @@dry_run
end

#errors?Error[]

Has this Command run in any errors?

Returns:

  • (Error[])


183
184
185
# File 'lib/pandocomatic/command/command.rb', line 183

def errors?
  !@errors.empty?
end

#executeObject

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



129
130
131
132
# File 'lib/pandocomatic/command/command.rb', line 129

def execute
  CommandPrinter.new(self).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



193
194
195
# File 'lib/pandocomatic/command/command.rb', line 193

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)


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

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

#make_quietObject

Make this Command run quietly



116
117
118
# File 'lib/pandocomatic/command/command.rb', line 116

def make_quiet
  @@quiet = true
end

#modified_only?Boolean

Is this Command only executed on modified files?

Returns:

  • (Boolean)


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

def modified_only?
  @@modified_only
end

#multiple?Boolean

Does this Command convert a file multiple times?

Returns:

  • (Boolean)

    false



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

def multiple?
  false
end

#quiet?Boolean

Is this Command executed silently?

Returns:

  • (Boolean)


82
83
84
# File 'lib/pandocomatic/command/command.rb', line 82

def quiet?
  @@quiet
end

#runObject

Actually run this Command



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

def run; end

#runnable?Boolean

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

Returns:

  • (Boolean)


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

def runnable?
  !errors?
end

#skip?Boolean

Will this Command be skipped, thus not executed?

Returns:

  • (Boolean)

    false



169
170
171
# File 'lib/pandocomatic/command/command.rb', line 169

def skip?
  false
end

#src_rootString

Get the root directory of this Command’s conversion process

Returns:

  • (String)


68
69
70
# File 'lib/pandocomatic/command/command.rb', line 68

def src_root
  @@src_root
end

#to_sString

Create a String representation of this Command

Returns:

  • (String)


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

def to_s
  'command'
end

#uncountObject

Decrement the total number of conversion commands by 1



174
175
176
# File 'lib/pandocomatic/command/command.rb', line 174

def uncount
  @@total -= 1
end