Class: Sbuilder::SnippetLoaderFacade

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils::MyLogger
Defined in:
lib/sbuilder/facade/snippet_loader_facade.rb

Constant Summary collapse

PROGNAME =

progname for logger

nil
DEFAULT_SNIPPET_HEADER =
"(* SNIPPET: {{metatype}}/{{appName}} --> {{specName}} *)\n".unindent
"(* --END OF SNIPPET-- *)\n\n".unindent
@@snippetType2extensionPointMapper =

Map parser node_type to extension point name

{
  'Macro'  => Sbuilder::Constants::EXTENSION_POINT_MACRO,
  'Procedure'  => Sbuilder::Constants::EXTENSION_POINT_IMPLEMENTATION,
  'VariableDef'  => Sbuilder::Constants::EXTENSION_POINT_STATE,
  'OperatorDef'  => Sbuilder::Constants::EXTENSION_POINT_OPERATIONS,
}

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Construtor collapse

Prepare Facade collapse

SnippetLoaderPlugin defines metatype collapse

SnippetLoaderPlugin hands over snippet collapse

controller processes snippets collected collapse

Support code generation collapse

Miscellaneous services collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(factory, options = {}) ⇒ SnippetLoaderFacade




122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 122

def initialize( factory, options = {} )

  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} initialized" )

  # collaborators init
  setFactory( factory )
  @controller = nil

  # snippets handed over to facade
  @snippets = []
  
  @options = options

  @snippetHeader = DEFAULT_SNIPPET_HEADER
  @snippetFooter = DEFAULT_SNIPPET_FOOTER

end

Instance Attribute Details

#controllerObject (readonly)

Sbuilder:Controller


86
87
88
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 86

def controller
  @controller
end

#snippetFooterObject (readonly)

Returns the value of attribute snippetFooter.



92
93
94
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 92

def snippetFooter
  @snippetFooter
end

#snippetHeaderObject (readonly)



89
90
91
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 89

def snippetHeader
  @snippetHeader
end

#snippetsObject (readonly)

Hash:Array

of snippets handed over to facade



83
84
85
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 83

def snippets
  @snippets
end

Instance Method Details

#acceptSnippets(parserResolver) ⇒ Object

Method called by controller to accept snippets, which snippet plugin has handed over to facade.

Parameters:

  • parserResolver (TlaParserS::Resolver)

    to parse ‘snippetCode`



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
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 260

def acceptSnippets( parserResolver )

  # resove SPEC_NAMEs and convert 'snippetBody' to
  # 'snippetCode'. Processing continues in block
  
  generateSnippetBodies( getMustache ) do |metatype, appName, specName, snippetCode|
    @logger.info( "#{__method__}: process #{metatype}, appName=#{appName}" )
    @logger.debug( "#{__method__}: snippetCode = #{snippetCode}" ) if @logger.debug?

    # create an single hash holding properties, and which can be
    # also passed to parser.
    parseEntry = parseEntry( metatype, appName, specName, snippetCode )

    # write to file to the file pointed by modulePath (which is
    # also identified module during parsing)
    writeSnippetModule( parseEntry[:modulePath], snippetCode )

    # par
    snippets = parserResolver.initSnippets(  parseEntry  )
    @logger.info "#{__method__}, snippets=#{snippets.map {|s| { :node_type => s[:node_type], :value => s[:value] } } }"

    # snippetCode should be parseable
    raise Sbuilder::FacadeException.new  "    No snippets found in an entry '\#{metatype}:\#{appName}':\n\n    \#{snippetCode}\n\n    EOS\n    \n    # snippet loader can process only one snippet at a time\n    raise Sbuilder::FacadeException.new  <<-EOS if snippets.length > 1\n    Too many snippets defined in an entry:\n\n    \#{snippetCode}\n\n    EOS\n\n    # extract name && type of the snippet from the parsed result\n    snippetName = snippetName( snippets[0] )\n    snippetType = snippetType( snippets[0] )\n\n    # Map snippet type to extension point type, and add the\n    # processes snippet as partial to the extension point\n    addPartial( parseEntry, snippetType )\n\n    # add \n\n    # validate that snippetCode.snippetName == specName\n    raise Sbuilder::FacadeException.new <<-EOS if snippetName( snippets[0] ) != specName \n    Snippet name '\#{ snippetName }' differs from specName '\#{specName}' for metatype='\#{metatype}', and appName='\#{appName}'\n    \n    Difference found in snippet code: \n\n    \#{snippetCode}\n    EOS\n\n  end\n  \nend\n" if snippets.nil? || !snippets.any?

#baseMetatypesHash

Return array of base metatypes in sbuilder context

Parameters:

  • baseMetatype (Hash)

    a customizable set of options

Returns:

  • (Hash)

    baseMetatypes



184
185
186
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 184

def baseMetatypes
  controller.baseMetatypes
end

#createLogger(logname = nil) ⇒ Logger

Returns new logger object.

Parameters:

  • logname (String) (defaults to: nil)

    progname to set on logger

Returns:

  • (Logger)

    new logger object



676
677
678
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 676

def createLogger( logname=nil )
  getLogger( logname, @options )
end

#getCacheDirString

Return cache directory location from opitions

Returns:

  • (String)

    path to cache -directory



664
665
666
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 664

def getCacheDir
  controller.getCacheDir
end

#handOver(metatype, appName, snippetBody = nil, specName = nil) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 225

def handOver( metatype, appName, snippetBody=nil, specName=nil  )
  # collect snippets handed over
  @logger.info "#{__method__}, metatype=#{metatype}, appName=#{appName}, snippetBody=#{snippetBody}"

  # generate specName & combine to hash
  entry = snippetEntry( metatype, appName, snippetBody )

  # override default specName
  entry['specName'] = specName unless specName.nil?

  # enable mapping from 'metatype/appName' to 'specName'
  addSymbol( metatype, appName, entry['specName'] )

  # Snippet entry defines behavior for an interface implementation
  # or complelation?
  if ( metatype == Sbuilder::Constants::META_MODEL_SERVICE_IMPLEMENTATION ) then
    interfaceExtension(  appName, entry['specName'], 'implementation' )
  elsif metatype == Sbuilder::Constants::META_MODEL_SERVICE_COMPLETION
    interfaceExtension(  appName, entry['specName'], 'completion' )        
  end
  
  @snippets << entry
end

#parseEntry(metatype, appName, specName, snippetCode) ⇒ Hash

Create hash which can be passed to tla-parser. Particularly this hash defines methods (read, moduleName) used by parser.

Parameters:

  • parseEntry (Hash)

    a customizable set of options

Returns:

  • (Hash)

    parseEntry to pass to parser#initSnippets



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 507

def parseEntry( metatype, appName, specName, snippetCode )
  ret = { :read => snippetCode, :name=> "Api #{metatype}:#{appName}",
          :modulePath => snippetModulePath(metatype, appName, specName ),
          :moduleName => File.join( metatype, specName ),             
        }

  # make it look like a file (supporting read-method)
  ret.define_singleton_method( :read ) do
    self[:read]
  end
  # parser identifies `snippetCode` as module
  ret.define_singleton_method( :modulePath ) do
    self[:modulePath]        
  end
  # parser identifies `snippetCode` as module
  ret.define_singleton_method( :moduleName ) do
    self[:moduleName]        
  end

  ret

end

#registerMetatype(metatype, desc, prefix = nil) ⇒ Object

Add ‘metatype’ with ‘desc’ and optional ‘prefix’ to sbuilder context.



192
193
194
195
196
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 192

def registerMetatype( metatype, desc, prefix=nil )
  @logger.info "#{__method__}, metatype='#{metatype}', desc=#{desc}"
  metatypeDef = createMetatypeDef( metatype, desc, prefix )
  controller.registerMetatype( metatypeDef )
end

#setController(controller) ⇒ Object

Allow facade to access controller services

Parameters:



148
149
150
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 148

def setController( controller )
  @controller = controller
end

#setFactory(factory) ⇒ Object

Facade delagates the task to create new objects to ‘factory

Parameters:



154
155
156
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 154

def setFactory( factory )
  @factory = factory
end

#setMustache(mustache) ⇒ Object



158
159
160
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 158

def setMustache( mustache )
  @mustache = mustache
end

#setSnippetFooter(snippetFooter) ⇒ Object

Set ‘snippetFooter’ to wrap around snippets



169
170
171
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 169

def setSnippetFooter( snippetFooter )
  @snippetFooter = snippetFooter
end

#setSnippetHeader(snippetHeader) ⇒ Object

Set ‘snippetHeader’ to wrap around snippets



164
165
166
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 164

def setSnippetHeader( snippetHeader )
  @snippetHeader = snippetHeader
end

#snippetModulePath(metatype, appName, specName) ⇒ String

Create module path for ‘snippetCode’

Returns:

  • (String)

    modulePath where ‘snippetCode’ is saved



534
535
536
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 534

def snippetModulePath( metatype, appName, specName )
  File.join( snippetRepository( metatype ), specName )
end

#snippetRepository(metatype) ⇒ String

to store snippets

Parameters:

  • metatype (String)

    in specification model

Returns:

  • (String)

    repositoryPath to root direcnoty under, which

  • (String)

    path to an existing snippet direcotory



545
546
547
548
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 545

def snippetRepository( metatype )
  snippetRepo = controller.snippetRepository
  controller.metatypeSnippetRepository( snippetRepo, metatype )
end

#versionString

Returns semver number.

Returns:



669
670
671
# File 'lib/sbuilder/facade/snippet_loader_facade.rb', line 669

def version
  Sbuilder::version
end