Class: Vedeu::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/runtime/launcher.rb

Overview

This class ensures that STDIN, STDOUT and STDERR point to the correct places. It also handles the initial configuration of the application, the starting of the application, the handling of uncaught exceptions and finally the exiting of the application with the correct exit code.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = [], stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ Vedeu::Launcher

Returns a new instance of Vedeu::Launcher.

Parameters:

  • argv (Array) (defaults to: [])
  • stdin (IO) (defaults to: STDIN)
  • stdout (IO) (defaults to: STDOUT)
  • stderr (IO) (defaults to: STDERR)
  • kernel (Kernel) (defaults to: Kernel)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vedeu/runtime/launcher.rb', line 39

def initialize(argv   = [],
               stdin  = STDIN,
               stdout = STDOUT,
               stderr = STDERR,
               kernel = Kernel)
  @argv      = argv
  @stdin     = stdin
  @stdout    = stdout
  @stderr    = stderr
  @kernel    = kernel
  @exit_code = 1
end

Instance Attribute Details

#argvArray<String> (readonly, protected)

Returns The command line arguments provided.

Returns:

  • (Array<String>)

    The command line arguments provided.



92
93
94
# File 'lib/vedeu/runtime/launcher.rb', line 92

def argv
  @argv
end

#exit_codeFixnum (readonly)

Return value indicating successful execution (0) or an error occurred (1).

Returns:

  • (Fixnum)

    Return value indicating successful execution (0) or an error occurred (1).



18
19
20
# File 'lib/vedeu/runtime/launcher.rb', line 18

def exit_code
  @exit_code
end

Class Method Details

.execute!(argv = [], stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ Object

:nocov:

Parameters:

  • argv (Array) (defaults to: [])
  • stdin (IO) (defaults to: STDIN)
  • stdout (IO) (defaults to: STDOUT)
  • stderr (IO) (defaults to: STDERR)
  • kernel (Kernel) (defaults to: Kernel)


22
23
24
25
26
27
28
# File 'lib/vedeu/runtime/launcher.rb', line 22

def self.execute!(argv = [],
                  stdin  = STDIN,
                  stdout = STDOUT,
                  stderr = STDERR,
                  kernel = Kernel)
  new(argv, stdin, stdout, stderr, kernel).execute!
end

Instance Method Details

#execute!void

This method returns an undefined value.

Alters the STD to those requested by the client application, then starts the application. If an uncaught exception occurs during the application runtime, we exit ungracefully with any error message(s).

If profiling is enabled, execute the application within the profiling context. At the moment, this simple uses ‘ruby-prof’ to profile the running application.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vedeu/runtime/launcher.rb', line 62

def execute!
  $stdin  = @stdin
  $stdout = @stdout
  $stderr = @stderr

  optionally_profile { Vedeu::Runtime::Application.start }

  @exit_code = 0

  terminate!

rescue StandardError => uncaught_exception
  message = uncaught_exception.message + "\n"
  trace   = uncaught_exception.backtrace.join("\n")

  output = if Vedeu.config.debug?
             message + Vedeu.esc.screen_colour_reset + trace

           else
             message

           end

  Vedeu.log_stderr(message: output)
end

#optionally_profile(&block) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • block (Proc)


98
99
100
101
102
103
104
105
106
# File 'lib/vedeu/runtime/launcher.rb', line 98

def optionally_profile(&block)
  if Vedeu.config.profile?
    Vedeu.profile { yield }

  else
    yield

  end
end

#terminate!void (private)

This method returns an undefined value.

:nocov: Terminates the application after resetting $stdin, $stdout and $stderr.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vedeu/runtime/launcher.rb', line 113

def terminate!
  Vedeu.log(message: "Exiting gracefully.\n")

  $stdin  = STDIN
  $stdout = STDOUT
  $stderr = STDERR

  # Commenting out this line means Vedeu can be run as a module within an
  # application. Specifically, this allows me to run some very basic
  # integration tests.
  #
  # @kernel.exit(exit_code)
end