Class: Sbuilder::TemplateContextReader

Inherits:
TemplateReader show all
Extended by:
Forwardable
Includes:
Utils::MyLogger
Defined in:
lib/sbuilder/mustache/template_reader_context.rb

Overview


Use setup configuration to filter out part of reander templates.

Implementation overrides ‘whitelist’ method in ‘TemplateReader’. Uses ‘TlaParserS::Resolver’ to parse TLA+ snippets, and resolve modules to put in whitelist’

Overview:

  • configSetup -methods receives ‘steps’ ie. array of interface names called in setup, and array of ‘interfaces’, which map interfaces to interface implementations

  • TlaParserS::Resolver#initSnippets parses tla-sbuilder code

  • repository

  • TlaParserS::Resolver#entryPoints resolves modules required to satisfy interface implementations used in setup

Constant Summary collapse

PROGNAME =

progname for logger

"creader"
GLOBALS =

TLA+ standard library symbols

%w( TRUE FALSE Cardinality)
SBUILDER =

Sbuilder constant symbols

%w( Nil InfrastructureServiceReturn InfrastructureServiceGetStatus 
InfrastructureServiceGetResponse InTransaction)
DOC_DIRS =

these directories not unmustached

[ "docs", "doc" ]

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Attributes inherited from TemplateReader

#partials, #template_paths, #templates

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Methods inherited from TemplateReader

#filtered_template, #get_template, #get_template_filepath, #template_read

Constructor Details

#initialize(controller, options = {}) ⇒ TemplateContextReader


Constructor



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 79

def initialize( controller, options={} )
  super( controller, options )
  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} created, options='#{options}" )

  @controller = controller

  @options = options


  # # create '@resolver' && parse snippet repository
  # if  isOptFilterSrc then
  # end
  
end

Instance Attribute Details

#controllerObject (readonly)

Sbuilder::Controller


71
72
73
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 71

def controller
  @controller
end

#optionsObject (readonly)


Attributes



65
66
67
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 65

def options
  @options
end

#resolverObject (readonly)


Lazy intialize (so that configuration can take place)



97
98
99
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 97

def resolver
  @resolver
end

#setupNameObject (readonly)

currently generating



74
75
76
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 74

def setupName
  @setupName
end

Instance Method Details

#filter_liststring

Returns file path for filter list.

Returns:

  • (string)

    file path for filter list



116
117
118
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 116

def filter_list
  options[:filter_list]
end

#initResolver(snippet_repository, options = {}) ⇒ Object

Create ‘@resolver’ and initSnippets in ‘snippet_repository’

Parameters:

  • options (Hash) (defaults to: {})

    defining options for ‘Resolver’



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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 274

def initResolver( snippet_repository, options={} )
  
  
  output( 1, "Parse snippet from repository #{snippet_repository} - start" )

  cnt = 0
  resolver = nil
  Dir.chdir( snippet_repository ) do
    
    @logger.info( "#{__method__}: init snippets in snippet_repository=#{snippet_repository}" )
    snippet_files = Dir.glob( "**/*").map { |fileName| File.new(fileName)}
    resolver =  TlaParserS::Resolver.new( options )

    # Add Globals, Sbuilder and model symbols to resolver
    initResolverContext( resolver )

    # Parse 'snippet_files' - 
    resolver.initSnippets( snippet_files ) do |stat,entry,e|
      
      if stat
        # success output dots no newline
        output( 1, ".", false )
        cnt += 1
        true
      else

        # parses error
        if filter_list
          msg = "\n          Parser error: option '--filter-list' set --> continue\n          \#{e.message} \n          EOS\n          @logger.error( msg )\n          warn msg\n          true\n        else\n          msg = <<-EOS\n          Parser error:  option '--filter-list' not set --> exiting\n          \#{e.message} \n          EOS\n          @logger.error( msg )\n          warn msg\n          false\n        end # if filter_list\n        \n      end # else - false: error\n    end # block\n    \n  end # chdir\n  \n  # start new line\n  output( 1, \"\" )\n  output( 1, \"Parse snippet from repository \#{snippet_repository} - done: parsed \#{cnt} snippets\" )\n\n  resolver\nend\n"

#initResolverContext(resolver) ⇒ Object

Add Globals, Sbuilder and model symbols to resolver



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 184

def initResolverContext( resolver )

  output( 1, " Initialize resolver context symbols" )

  
  output( 2, "   TLA+ symbols: #{GLOBALS.join( ',')}" )      
  output( 1, "   #{GLOBALS.length} TLA+ symbols" )
  resolver.initContext( GLOBALS )

  output( 2, "   Sbuilder symbols: #{SBUILDER.join( ',')}" )            
  output( 1, "   #{SBUILDER.length} SBuilder symbols" )
  resolver.initContext( SBUILDER )

  model_names = getModelNames
  output( 2, "   Model symbols: #{model_names.join( ',')}" )            
  output( 1, "   #{model_names.length} model symbols" )
  resolver.initContext( model_names )

  output( 1, " Initialize resolver context symbols - done" )      
  
end

#setSetupName(setupName) ⇒ Object


config



124
125
126
127
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 124

def setSetupName( setupName )
  @logger.info( "#{__method__} setupName=#{setupName}" )
  @setupName = setupName
end

#show_template(name) ⇒ boolean

Overridden from super class. Wrapper around check actual check routine to simplify logging.

Parameters:

  • name (String)

    of template to check

Returns:

  • (boolean)

    true when shown normally,else false



161
162
163
164
165
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 161

def show_template( name )
  ret = show_template_do( name )
  @logger.info( "#{__method__}: check show=#{name}-#{ret[1]} -> #{ret[0]}" )            
  ret[0]
end

#show_template_do(name) ⇒ Bool, String


Try to find a reason to show template

when template should be shown normally, else false

Parameters:

  • name (String)

    of template to check @return [boolean] true

Returns:

  • (Bool, String)

    show and text description



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 137

def show_template_do( name )

  @logger.debug( "#{__method__}: check name=#{name}" )
  
  # show all 'mustache templates'
  return true, "mustache-template" if /.+\.mustache$/ =~ name

  # show template witout extesions
  return true, "no extension" if /^[^\/\.]*$/ =~ name

  # show template if name is in whitelist 'opts[:filter_list]'
  # puts "#{whitelist.join(',')}: '#{name}'--> #{whitelist.include?(name)}"
  return true, "in whitelist" if whitelist.include?( name.to_s  )

  # (failed) there is no reason to show the template
  return false, "default false"
end

#unmustache(unmustache_dir) ⇒ Object

Access controller to get information needed in un-mustache operation



208
209
210
211
212
213
214
215
216
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 208

def unmustache( unmustache_dir )
  
  if ! Dir.exists?( unmustache_dir )
    `mkdir #{unmustache_dir}`
    exit unless $?.success?
  end
  
  unmustacheDo( controller.getCodeRepository, unmustache_dir, controller.getPreferencesData(setupName) )
end

#unmustacheDo(src_dir, target_dir, preferences) ⇒ Object

Unmustache files in ‘src_dir’ to ‘target_dir’. Pass preferences json to mustache rendering

Parameters:

  • src_dir (String)

    path source directory

  • dest_dir (String)

    path destination directory



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
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 224

def unmustacheDo( src_dir, target_dir, preferences )
  @logger.info( "#{__method__}, src_dir=#{src_dir}, target_dir=#{target_dir}, preferences=#{preferences}" )

  output( 1, "Unmustache  #{src_dir} --> #{target_dir} - start" )

  source_files = Dir::glob(  "#{src_dir}/**/*" )
  source_files.each do |file|
    
    next if File.directory?( file )

    # extract relatve path in respect to src_dir
    source = Pathname.new( File.expand_path( file) )
    source_root = Pathname.new( File.expand_path( src_dir )  )
    src = source.relative_path_from( source_root )

    # do not unmustache anythins under docs, doc directory
    if DOC_DIRS.include?( File.dirname( src ).split(File::SEPARATOR).first )
      output( 1, "   document file  #{src} not unmustached" )          
      next
    end

    
    target = "#{target_dir}/#{src}"
    # puts "#{source} --> #{target}"
    
    if !File.exists?( File.dirname(target) )
      cmd = "mkdir -p #{File.dirname(target)}"
      output( 1, cmd )
      `#{cmd}`
      exit unless $?.success?
    end # create dir

    if !File.exists?(target) or (File.mtime( source ) > File.mtime(target))
      cmd =  "echo '#{preferences.to_json}' | mustache - #{source} > #{target}"
      output( 2, "   " + cmd )
      `#{cmd}`
      exit unless $?.success?
    end # create file
  end

  output( 1, "Unmustache  #{src_dir} --> #{target_dir} - done" )

  
end

#whitelistArray

Return whitelisted template names.

Returns:

  • (Array)

    of whitelistes template names



362
363
364
365
366
367
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 362

def whitelist
  return @whitelist if @whitelist      
  @whitelist = whitelist_file + whitelist_resolved
  @logger.info( "#{__method__}, whitelist=#{@whitelist}" )
  @whitelist
end

#whitelist_fileArray

Return whitelisted template names.

Implementation reads, and caches lines, in a file pointed by options

Returns:

  • (Array)

    of whitelistes template names



176
177
178
179
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 176

def whitelist_file
  return [] unless filter_list
  File.readlines( filter_list ).each {|l| l.chomp! } || []
end

#whitelist_resolvedString:Array

Call resolver#resolveModule to find out the whitelist modules. Implementation uses ‘entry_points’ configured in ‘configSetup’

Returns:

  • (String:Array)

    of white listed modules



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/sbuilder/mustache/template_reader_context.rb', line 336

def whitelist_resolved
  rslv = resolver
  output( 1, "Resolving symbols - start" )
  entrypoints = getEntryPoints + getSetupPossibilities + getModelInvariants + getSetupAssumes
  @logger.debug("#{__method__}: entrypoints=#{entrypoints}")
  ret =  rslv.resolveModules( entrypoints  ) do |type,arr|
    case type
    when "unresolved"
      # defult to output unresolved - if some unresolved symbols
      output( arr.any? ? 1 : 2, " #{type} : [#{arr.join(',')}]" )
      output( 1, " #{type} : count #{arr.length}" )          
    else
      output( 2, " #{type} : [#{arr.join(',')}]" )
      output( 1, " #{type} : count #{arr.length} #{ !output_show( 2 ) ? '(add verbosity to show list)' : '' }" )
    end
  end
  output( 1, "Resolving symbols - done" )
  return ret
end