Class: Gloo::App::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/app/engine.rb

Constant Summary collapse

TEST_LIB_NAME =

Constants for the name of the test library. This is the short name as it would be used with the load verb.

'test'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Engine

Set up the engine with basic elements.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gloo/app/engine.rb', line 30

def initialize( context )
  @args = Args.new( self, context.params )
  @settings = Settings.new( self, context.user_root )

  @log = context.log.new( self, @args.quiet? )
  @log.debug "log (class: #{@log.class.name}) in use ..."

  @platform = context.platform
  @log.debug "platform (class: #{@platform.class.name}) in use ..."

  @log.debug 'engine intialized...'
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def args
  @args
end

#context_objectObject

Returns the value of attribute context_object.



24
25
26
# File 'lib/gloo/app/engine.rb', line 24

def context_object
  @context_object
end

#converterObject

Returns the value of attribute converter.



24
25
26
# File 'lib/gloo/app/engine.rb', line 24

def converter
  @converter
end

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def dictionary
  @dictionary
end

#event_managerObject

Returns the value of attribute event_manager.



24
25
26
# File 'lib/gloo/app/engine.rb', line 24

def event_manager
  @event_manager
end

#exec_envObject

Returns the value of attribute exec_env.



24
25
26
# File 'lib/gloo/app/engine.rb', line 24

def exec_env
  @exec_env
end

#ext_managerObject (readonly)

Returns the value of attribute ext_manager.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def ext_manager
  @ext_manager
end

#factoryObject (readonly)

Returns the value of attribute factory.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def factory
  @factory
end

#heapObject (readonly)

Returns the value of attribute heap.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def heap
  @heap
end

#last_cmdObject

Returns the value of attribute last_cmd.



24
25
26
# File 'lib/gloo/app/engine.rb', line 24

def last_cmd
  @last_cmd
end

#lib_managerObject (readonly)

Returns the value of attribute lib_manager.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def lib_manager
  @lib_manager
end

#logObject (readonly)

Returns the value of attribute log.



20
21
22
# File 'lib/gloo/app/engine.rb', line 20

def log
  @log
end

#modeObject (readonly)

Returns the value of attribute mode.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def mode
  @mode
end

#parserObject (readonly)

Returns the value of attribute parser.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def parser
  @parser
end

#persist_manObject

Returns the value of attribute persist_man.



24
25
26
# File 'lib/gloo/app/engine.rb', line 24

def persist_man
  @persist_man
end

#platformObject (readonly)

Returns the value of attribute platform.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def platform
  @platform
end

#runningObject (readonly)

Returns the value of attribute running.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def running
  @running
end

#running_appObject (readonly)

Returns the value of attribute running_app.



20
21
22
# File 'lib/gloo/app/engine.rb', line 20

def running_app
  @running_app
end

#settingsObject (readonly)

Returns the value of attribute settings.



20
21
22
# File 'lib/gloo/app/engine.rb', line 20

def settings
  @settings
end

Instance Method Details

#app_running?Boolean

Is there a running app?

Returns:

  • (Boolean)


268
269
270
# File 'lib/gloo/app/engine.rb', line 268

def app_running?
  return @running_app ? true : false
end

#err(msg, backtrace = nil) ⇒ Object

Report an error. Write it to the log and set the heap error value.



301
302
303
304
305
306
# File 'lib/gloo/app/engine.rb', line 301

def err( msg, backtrace=nil )
  @log.error msg
  @heap.error.set_to msg

  @event_manager.on_error( msg, backtrace)
end

#error?Boolean

Did the last command result in an error?

Returns:

  • (Boolean)


293
294
295
# File 'lib/gloo/app/engine.rb', line 293

def error?
  return !@heap.error.value.nil?
end

#last_cmd_blank?Boolean

Is the last command entered blank?

Returns:

  • (Boolean)


187
188
189
190
191
192
# File 'lib/gloo/app/engine.rb', line 187

def last_cmd_blank?
  return true if @last_cmd.nil?
  return true if @last_cmd.strip.empty?

  return false
end

#load_filesObject

Load all file specified in the CLI.



130
131
132
# File 'lib/gloo/app/engine.rb', line 130

def load_files
  @args.files.each { |f| @persist_man.load( f ) }
end

#log_exception(ex) ⇒ Object

Log an exception. This function does not log the full backtrace, but does write part of it to the log.



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/gloo/app/engine.rb', line 313

def log_exception ex
  # Get the stack trace, and if needed truncate to fit.
  msg_lines = ex.backtrace
  if msg_lines.count > 27
    msg_lines = msg_lines[0..13] + [ '... truncated ...' ] + msg_lines[-13..-1]
  end
  backtrace = msg_lines.join( "\n" )
  @log.error backtrace

  err( ex.message, backtrace)
end

#loopObject

Prompt, Get input, process.



197
198
199
200
201
202
# File 'lib/gloo/app/engine.rb', line 197

def loop
  while @running
    @last_cmd = @platform.prompt.ask
    process_cmd
  end
end

#open_start_fileObject

Get the setting for the start_with file and open it.



179
180
181
182
# File 'lib/gloo/app/engine.rb', line 179

def open_start_file
  name = @settings.start_with
  @persist_man.load( name ) if name
end

#process_cmd(cmd = nil) ⇒ Object

Process the command.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/gloo/app/engine.rb', line 207

def process_cmd cmd=nil
  @last_cmd = cmd if cmd
  
  if last_cmd_blank?
    @platform.clear_screen
    return
  end

  begin
    @parser.run @last_cmd
  rescue => e
    log_exception e
  end
end

#quitObject

Do any clean up and quit.



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/gloo/app/engine.rb', line 232

def quit
  if app_running?
    @log.debug 'stopping running app...'
    stop_running_app
  end

  @log.debug 'triggering on_quit events...'
  @event_manager.on_quit

  @log.info 'Gloo engine is quitting...'
end

#reset_stateObject

Reset the engine state. Clear out anything that needs clearing or resetting to get the engine back to a clean state.



75
76
77
78
79
# File 'lib/gloo/app/engine.rb', line 75

def reset_state
  stop_running_app
  @heap = Gloo::Core::Heap.new( self )
  @persist_man = Gloo::Persist::PersistMan.new( self )
end

#restartObject

Restart the engine with the same settings and the same mode (with files, etc).



85
86
87
88
89
# File 'lib/gloo/app/engine.rb', line 85

def restart
  @log.info 'Restarting the engine...'
  reset_state
  run_mode
end

#runObject

Run in interactive mode.



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/gloo/app/engine.rb', line 154

def run
  # Open default file(s)
  self.open_start_file

  # Open any files specifed in args
  load_files

  unless @mode == Mode::SCRIPT || @args.quiet?
    self.loop
  end

  quit
end

#run_filesObject

Run files specified on the CLI. Then quit.



122
123
124
125
# File 'lib/gloo/app/engine.rb', line 122

def run_files
  load_files
  quit
end

#run_keep_aliveObject

Run in Embedded mode, which means we’ll start the engine, but wait for external inputs.



172
173
174
# File 'lib/gloo/app/engine.rb', line 172

def run_keep_alive
  @log.debug 'Running in Embedded mode…'
end

#run_modeObject

Run gloo in the selected mode.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gloo/app/engine.rb', line 99

def run_mode
  @log.info "Running gloo in #{@mode} mode"

  if @mode == Mode::VERSION
    run_version
  elsif @mode == Mode::SCRIPT
    run_files
  elsif @mode == Mode::EMBED
    run_keep_alive
  elsif @mode == Mode::APP
    @settings.override_project_path @args.app_path
    run
  elsif @mode == Mode::TEST
    run_test
  else
    run
  end
end

#run_testObject

Run in gloo unit test mode.



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gloo/app/engine.rb', line 137

def run_test
  @log.debug 'Running in Test mode…'

  begin
    @lib_manager.load_lib TEST_LIB_NAME
    TestRunner.new( self, @args.files ).run
  rescue => ex
    log_exception ex
  end

  @log.debug 'Tests complete.'
  quit
end

#run_versionObject

Show the version information and then quit.



280
281
282
283
# File 'lib/gloo/app/engine.rb', line 280

def run_version
  @platform.show Info.full_version unless @args.quiet?
  quit
end

#startObject

Start the engine. Load object and verb definitions and setup engine elements.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gloo/app/engine.rb', line 47

def start
  @log.debug 'starting the engine...'
  @log.debug Gloo::App::Info.display_title
  @mode = @args.detect_mode
  @running = true

  @dictionary = Gloo::Core::Dictionary.get

  @parser = Gloo::Core::Parser.new( self )
  @heap = Gloo::Core::Heap.new( self )
  @factory = Gloo::Core::Factory.new( self )
  @persist_man = Gloo::Persist::PersistMan.new( self )
  @event_manager = Gloo::Core::EventManager.new( self )
  @ext_manager = Gloo::Plugin::ExtManager.new( self )
  @lib_manager = Gloo::Plugin::LibManager.new( self )

  @exec_env = Gloo::Exec::ExecEnv.new( self )
  @converter = Gloo::Convert::Converter.new( self )

  @log.info 'The gloo engine has started'
  run_mode
end

#start_running_app(obj) ⇒ Object

Set the running app object within gloo.



252
253
254
255
# File 'lib/gloo/app/engine.rb', line 252

def start_running_app( obj )
  @running_app = Gloo::App::RunningApp.new( obj, self )
  @running_app.start
end

#stop_runningObject

Request the engine to stop running.



225
226
227
# File 'lib/gloo/app/engine.rb', line 225

def stop_running
  @running = false
end

#stop_running_appObject

Stop the running app object within gloo.



260
261
262
263
# File 'lib/gloo/app/engine.rb', line 260

def stop_running_app
  @running_app.stop if @running_app
  @running_app = nil
end