Class: Dcm::CLI

Inherits:
Thor show all
Defined in:
lib/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/cli.rb', line 277

def self.exit_on_failure?
  true
end

Instance Method Details

#begu2creta(expr, codingpath, file) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/cli.rb', line 83

def begu2creta(expr, codingpath, file)
  filter = VariantFilter.new(expr)
  list = parse_file(file)

  puts Codinginfo.new(codingpath, filter)
    .unflatten_list(list)
    .to_dcm
end

#cat(file = nil) ⇒ Object



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
# File 'lib/cli.rb', line 120

def cat(file=nil)
  list = parse_file(file)
  if options[:include] != ".*" || options[:reject] != "(?!x)x"
    selector = Regexp.new(options[:include], true)
    rejector = Regexp.new(options[:reject], true)
    list = list
      .select { |l| l.match(selector) }
      .reject { |l| l.match(rejector) }
  end
  if options[:inlab].present?
    labfile = Ecu::LabelList.from_lab(File.read(options[:inlab]))
    list = list.select { |l| labfile.any? { |s| s.name == l.name } }
  end
  if options[:notinlab].present?
    labfile = Ecu::LabelList.from_lab(File.read(options[:notinlab]))
    list = list.reject { |l| labfile.any? { |s| s.name == l.name } }
  end
  list.headers    += options[:headers]
  list.subheaders += options[:subheaders]
  if options[:mfile]
    puts list.to_mfile
  else
    puts list.to_dcm(options[:indented])
  end
end

#clearcache(file = nil) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/cli.rb', line 172

def clearcache(file=nil)
  if file
    FileCache.clear(file)
  else
    FileCache.clear_all
  end
end

#compare(file1, file2) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cli.rb', line 187

def compare(file1, file2)
  comparison = render_comparison(file1, file2, precision: options[:precision])

  if options[:left]
    puts to_lab(comparison.names(:left_exclusive))
  elsif options[:right]
    puts to_lab(comparison.names(:right_exclusive))
  elsif options[:equal]
    puts to_lab(comparison.names(:equal))
  elsif options[:unequal]
    puts to_lab(comparison.names(:nonequal))
  elsif options[:comparable]
    puts (
      comparison.names(:equal).map { |l| "#{l}: equal".colorize(:green) } +
      comparison.names(:nonequal).map { |l| "#{l}: unequal".colorize(:red) }
    )
  else
    puts "#{comparison.names(:left_exclusive).size.quantify("label")} exclusive to #{file1}"
    puts "#{comparison.names(:right_exclusive).size.quantify("label")} exclusive to #{file2}"
    puts "#{comparison.names(:common).size.quantify("label")} comparable"
    puts "  #{comparison.names(:equal).size.quantify("label")} equal".colorize(:green)
    puts "  #{comparison.names(:nonequal).size.quantify("label")} unequal".colorize(:red)
    puts "DCMs are identical!".colorize(:green) if comparison.identical?
  end
end

#creta2begu(expr, codingpath, file) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/cli.rb', line 63

def creta2begu(expr, codingpath, file)
  filter = VariantFilter.new(expr)
  list = parse_file(file)

  puts Codinginfo.new(codingpath, filter)
    .flatten_list(list)
    .to_dcm
end

#creta2filt(expr, codingpath, file) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/cli.rb', line 73

def creta2filt(expr, codingpath, file)
  filter = VariantFilter.new(expr)
  list = parse_file(file)

  puts Codinginfo.new(codingpath, filter)
    .filter_list(list)
    .to_dcm
end

#diff(file1, file2) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cli.rb', line 227

def diff(file1, file2)
  comparison = render_comparison(file1, file2, precision: options[:precision])

  showall = !(options[:left] || options[:comparable] || options[:right])

  puts "-#{file1}".colorize(:red)
  puts "+#{file2}".colorize(:green)
  puts ""
  if options[:left] || showall
    comparison.left_exclusive.each do |label|
      puts DiffViewer.new(label.to_s(detail: true), "")
    end
  end
  if options[:comparable] || showall
    comparison.differences.each do |left, right|
      puts DiffViewer.new(left.to_s(detail: true), right.to_s(detail: true))
    end
  end
  if options[:right] || showall
    comparison.right_exclusive.each do |label|
      puts DiffViewer.new("", label.to_s(detail: true))
    end
  end
end

#gitdiff(path, file1, hex1, mode1, file2, hex2, mode2) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/cli.rb', line 214

def gitdiff(path, file1, hex1, mode1, file2, hex2, mode2)
  if [file1, file2].all? { |f| File.extname(f).downcase == ".dcm" }
    invoke(:diff, [file1, file2])
  else
    puts DiffViewer.new(File.read(file1), File.read(file2))
  end
end

#help(*cmds) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/cli.rb', line 28

def help(*cmds)
  super
  puts <<~EOS if cmds.any? { _1.match?("creta") }

    Options:
      FILTER may be any of the following keys: #{VariantFilter::ALLOWED_FILTERKEYS.join(", ")}
      and should have the format key:value
  EOS
end

#json(file = nil) ⇒ Object



48
49
50
# File 'lib/cli.rb', line 48

def json(file=nil)
  display_list(list: parse_file(file), format: :json, detail: options[:pretty])
end

#list(file = nil) ⇒ Object



94
95
96
97
# File 'lib/cli.rb', line 94

def list(file=nil)
  display_list(list: parse_file(file),
               format: (options[:lab] ? :lab : :tty))
end

#merge(*files) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/cli.rb', line 260

def merge(*files)
  baselist = parse_file(files.shift)
  while not files.empty?
    mergelist = parse_file(files.shift)
    baselist = Ecu::LabelListComparison.new(baselist, mergelist).
      merge(priority: options[:priority].downcase.to_sym)
  end
  puts baselist.to_dcm(options[:indented])
end

#new(*spec) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/cli.rb', line 54

def new(*spec)
  list = parse_spec(spec)
  puts list.to_dcm(options[:indented])
rescue RuntimeError => e
  puts e.message
  exit 1
end

#rename(mapping = nil, file = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/cli.rb', line 156

def rename(mapping=nil, file=nil)
  list = parse_file(file)
  mappings = JSON.parse(FileReader.read(mapping))
  list = list.map_int do |label|
    label.with \
      name: mappings.filter_map { |m| m["new"] if m["old"] == label.name }&.first || label.name
  end
  puts list.to_dcm(options[:indented])
end

#select(file = nil) ⇒ Object



148
149
150
151
152
153
# File 'lib/cli.rb', line 148

def select(file=nil)
  list = parse_file(file)
  puts FuzzySelector.new(list, "dcm cat -f {} #{file} | dcm show")
    .then { _1.select_by(&:name) }
    .then { _1.to_dcm(options[:indented]) }
end

#show(file = nil) ⇒ Object



42
43
44
# File 'lib/cli.rb', line 42

def show(file=nil)
  display_list(list: parse_file(file), detail: options[:oneline] ? :onelinefull : true)
end

#size(file) ⇒ Object



253
254
255
256
# File 'lib/cli.rb', line 253

def size(file)
  list = parse_file(file)
  puts Filesize.new(list.map(&:bytesize).reduce(:+)).pretty
end

#summary(file = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/cli.rb', line 101

def summary(file=nil)
  list = parse_file(file)
  puts "DCM file with #{list.count.quantify("label")}"
  if options[:verbose]
    list.group_by(&:function).each do |function, labels|
      puts "  #{function}: #{labels.count}"
    end
  end
end

#validate(file = nil) ⇒ Object



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

def validate(file=nil)
  parse_file(file)
end

#versionObject



273
274
275
# File 'lib/cli.rb', line 273

def version
  puts "v#{Dcm::VERSION}"
end