Class: TlaTraceFilter::Cli

Inherits:
Thor
  • Object
show all
Includes:
Util::MyLogger
Defined in:
lib/cli/cli.rb

Constant Summary collapse

API_CALL_INIT =

name mustache template called once

"api-call-init"
API_CALL_TEMPLATE =

root template for command ‘api-call’

"api-call"
"add-links"
API_CALL_PRE =

render before anything other templates

"api-call-pre"
API_CALL_POST =

render last

"api-call-post"
VERSION_BANNER =
<<~EOS
#{File.basename $0}: #{File.readlines( File.join File.dirname( __FILE__ ), "../../VERSION" ).first.gsub( /\n/, "" )}
EOS
MUSTACHE_DEFAULT =
[ "mustache/" ]
MUSTACHE_OPTION =
<<-EOS
Output is processed using mustache templates in directory 
#{File.dirname __FILE__}/../../mustache. Mustache templates can be
overridden using templates in directory pointeted by line option
:mustache. Default = #{MUSTACHE_DEFAULT}

Parameter values ending with /-character are directories,
otherwise it is GEM name, where partials are in 'mustache' 
sub-directory.
  
EOS

Constants included from Util::MyLogger

Util::MyLogger::LOGFILE

Constructor collapse

Enable shared options collapse

Commands collapse

Methods included from Util::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(*args) ⇒ Cli




68
69
70
71
72
# File 'lib/cli/cli.rb', line 68

def initialize( *args )
  super
  @logger = getLogger( nil, options )
  @logger.info "#{__method__}: initalized"
end

Class Method Details

.add_shared_option(name, options = {}) ⇒ Object



83
84
85
86
# File 'lib/cli/cli.rb', line 83

def add_shared_option(name, options = {})
  @shared_options = {} if @shared_options.nil?
  @shared_options[name] =  options
end

.shared_options(*option_names) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/cli/cli.rb', line 88

def shared_options(*option_names)
  option_names.each do |option_name|
    opt =  @shared_options[option_name]
    raise "Tried to access shared option '#{option_name}' but it was not previously defined" if opt.nil?
    yield option_name, opt if block_given?
    option option_name, opt
  end
end

Instance Method Details



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

def add_links( setup, file=nil )

  # unfreeze 'options' && set defaults (:tla_dir)
  setOptions( setup )
  
  # Default FILE vs. given on cmd line
  file = traceFile( setup, file )
  
  template = "{{>#{ADD_LINKS_TEMPLATE}}}"
  # no_strings = true && !options[:embed]

  # read 'file' yielding String/Hash and parsedLines producing Hash
  processInput( file ) do |data,parsedLines|
    if data.is_a?(String)
      # Not parseable line 
      puts data if options[:embed]
    elsif data.is_a?(Hash)
      # template = "{{>add-links}}" or {{>api-calls}}
      rendered =  rendering.render(template, data)
      puts rendered if rendered.length > 0

      # output parsedLines - producing 'data'
      puts parsedLines.join( "" ) if options[:embed]

    end

  end

end

#api_calls(setup, file = nil) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/cli/cli.rb', line 215

def api_calls( setup, file=nil)

  # unfreeze 'options' && set defaults (:tla_dir)
  setOptions( setup )

  # init sha1 calculation
  sha1Init

  # Load fixed named template - allow used to define mapping
  apiInitTemplate = "{{>#{API_CALL_INIT} }}"
  @logger.info "#{__method__}, apiInitTemplate=#{apiInitTemplate}"
  rendered =  rendering.render(apiInitTemplate, options)
  @logger.info "#{__method__}, output from init '#{rendered}'"
  # outputRendered(rendered)

  # preamble partial
  outputRendered( rendering.render( "{{>#{API_CALL_PRE}}}", preAndPostHash(options ) ) )
  
  # Default FILE vs. given on cmd line
  file = traceFile( setup, file )
  
  template = "{{>#{API_CALL_TEMPLATE} }}"
  @logger.info "#{__method__}, template=#{template}"

  # queue length of 1
  interfaceBeforeState = nil
  interfaceEnterState = nil
  interfaceEndState = nil

  # read 'file' yielding String/Hash and parsedLines producing Hash
  processInput( file ) do |data, parsedLines|

    if data.is_a?(String)
      # api - call do not output trace lines
    elsif data.is_a?(Hash)
      
      # reason to output api-call
      if data[:interface_started]

        if interfaceBeforeState.nil?
          # first time processing state 0 
          interfaceBeforeState = data
          @logger.debug "loop: init interfaceBeforeState: #{interfaceBeforeState}" if @logger.debug?
        elsif interfaceEnterState.nil?
        # state 0 has been processed - is this the first interface
          interfaceEnterState = data
        else

          # New iterface has started to execute. Now we can render
          # apiStep for previous interface call using state collected in
          #
          # * beforeState: takens from 'interfaceBeforeState': it s either the first
          #   'data' parsed OR 'data' parsed  , which was reached
          #   after executing previous interface call.

          # * afterState: taken from preceding 'data' in 'interfaceEndState'
          apiOutput = canonizeData( interfaceBeforeState, interfaceEnterState, interfaceEndState )
          @logger.debug "loop: apiOutput #{apiOutput}" if @logger.debug?
          rendered =  rendering.render(template, apiOutput)
          outputRendered( rendered ) 

          # init 'interfaceBeforeState' as 'interfaceEndState'
          # from the previous step (which was just rendered)
          interfaceBeforeState = interfaceEndState
          # init 'interfaceEnterState' to current 'data' parsed from inputfile
          interfaceEnterState = data
          @logger.debug "loop: set interfaceBeforeState #{interfaceBeforeState}" if @logger.debug?
        end 
        
      end  # interface_started

      # advance 'interfaceEndState' with current 'data'
      interfaceEndState = data
      
    end # elsif Hash?
  end # block

  # flush interfaceBeforeState
  if !interfaceBeforeState.nil?
    # render parsed data && output if any ouput
    apiOutput = canonizeData( interfaceBeforeState, interfaceEnterState || interfaceEndState, interfaceEndState )
    @logger.debug "loop:exit: apiOutput (quit) #{apiOutput}" if @logger.debug?
    rendered =  rendering.render(template, apiOutput)
    outputRendered( rendered ) 
    
  end # 

  # postamble (including setting sha1) - but not calculating it
  rendered =  rendering.render("{{>#{API_CALL_POST}}}", preAndPostHash(options ) )
  puts rendered if rendered.length > 0
  
end

#versionObject



131
132
133
# File 'lib/cli/cli.rb', line 131

def version()
  puts VERSION_BANNER
end