Class: Glimmer::Launcher

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

Constant Summary collapse

OPERATING_SYSTEMS_SUPPORTED =
["mac", "windows", "linux"]
TEXT_USAGE_PREFIX =
"Usage: glimmer [--quiet] [--debug] [--log-level=VALUE] [[ENV_VAR=VALUE]...] [[-jruby-option]...] (application.rb or task[task_args]) [[application2.rb]...]\n\nRuns Glimmer applications/tasks.\n\nEither a single task or one or more applications may be specified.\n\nWhen a task is specified, it runs via rake. Some tasks take arguments in square brackets.\n\nAvailable tasks are below (you may also lookup by adding `require 'glimmer/rake_task'` in Rakefile and running rake -T):\n"
TEXT_USAGE_SUFFIX =
"\nWhen applications are specified, they are run using JRuby, \nautomatically preloading the glimmer Ruby gem and SWT jar dependency.\n\nOptionally, extra Glimmer options, JRuby options and environment variables may be passed in.\n\nGlimmer options:\n- \"--quiet\"           : Does not announce file path of Glimmer application being launched\n- \"--debug\"           : Displays extra debugging information, passes \"--debug\" to JRuby, and enables debug logging\n- \"--log-level=VALUE\" : Sets Glimmer's Ruby logger level (\"ERROR\" / \"WARN\" / \"INFO\" / \"DEBUG\"; default is none)\n\nExample: glimmer samples/hello_world.rb\n\nThis runs the Glimmer application samples/hello_world.rb\n"
GLIMMER_LIB_LOCAL =
File.expand_path(File.join('lib', 'glimmer-dsl-swt.rb'))
GLIMMER_LIB_GEM =
'glimmer-dsl-swt'
GLIMMER_OPTIONS =
%w[--log-level --quiet]
GLIMMER_OPTION_ENV_VAR_MAPPING =
{
  '--log-level' => 'GLIMMER_LOGGER_LEVEL'
}
REGEX_RAKE_TASK_WITH_ARGS =
/^([^\[]+)\[?([^\]]*)\]?$/
@@mutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_options) ⇒ Launcher

Returns a new instance of Launcher.



123
124
125
126
127
128
129
130
# File 'lib/glimmer/launcher.rb', line 123

def initialize(raw_options)
  raw_options << '--quiet' if !caller.join("\n").include?('/bin/glimmer:') && !raw_options.join.include?('--quiet=')
  raw_options << '--log-level=DEBUG' if raw_options.join.include?('--debug') && !raw_options.join.include?('--log-level=')
  @application_paths = extract_application_paths(raw_options)
  @env_vars = extract_env_vars(raw_options)
  @glimmer_options = extract_glimmer_options(raw_options)
  @jruby_options = raw_options
end

Instance Attribute Details

#application_pathsObject (readonly)

Returns the value of attribute application_paths.



118
119
120
# File 'lib/glimmer/launcher.rb', line 118

def application_paths
  @application_paths
end

#env_varsObject (readonly)

Returns the value of attribute env_vars.



119
120
121
# File 'lib/glimmer/launcher.rb', line 119

def env_vars
  @env_vars
end

#glimmer_optionsObject (readonly)

Returns the value of attribute glimmer_options.



120
121
122
# File 'lib/glimmer/launcher.rb', line 120

def glimmer_options
  @glimmer_options
end

#jruby_optionsObject (readonly)

Returns the value of attribute jruby_options.



121
122
123
# File 'lib/glimmer/launcher.rb', line 121

def jruby_options
  @jruby_options
end

Class Method Details

.glimmer_libObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/glimmer/launcher.rb', line 63

def glimmer_lib
  @@mutex.synchronize do
    unless @glimmer_lib
      @glimmer_lib = GLIMMER_LIB_GEM
      glimmer_gem_listing = `jgem list #{GLIMMER_LIB_GEM}`.split("\n").map {|l| l.split.first}
      if !glimmer_gem_listing.include?(GLIMMER_LIB_GEM) && File.exists?(GLIMMER_LIB_LOCAL)
        @glimmer_lib = GLIMMER_LIB_LOCAL
        puts "[DEVELOPMENT MODE] (detected #{@glimmer_lib})"
      end
    end
  end
  @glimmer_lib
end

.glimmer_option_env_vars(glimmer_options) ⇒ Object



77
78
79
80
81
# File 'lib/glimmer/launcher.rb', line 77

def glimmer_option_env_vars(glimmer_options)
  GLIMMER_OPTION_ENV_VAR_MAPPING.reduce({}) do |hash, pair|
    glimmer_options[pair.first] ? hash.merge(GLIMMER_OPTION_ENV_VAR_MAPPING[pair.first] => glimmer_options[pair.first]) : hash
  end
end

.jruby_os_specific_optionsObject



55
56
57
# File 'lib/glimmer/launcher.rb', line 55

def jruby_os_specific_options
  OS.mac? ? "-J-XstartOnFirstThread" : ""
end

.jruby_swt_optionsObject



59
60
61
# File 'lib/glimmer/launcher.rb', line 59

def jruby_swt_options
  "#{jruby_os_specific_options} -J-classpath \"#{swt_jar_file}\""
end

.launch(application, jruby_options: [], env_vars: {}, glimmer_options: {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/glimmer/launcher.rb', line 89

def launch(application, jruby_options: [], env_vars: {}, glimmer_options: {})
  jruby_options_string = jruby_options.join(' ') + ' ' if jruby_options.any?
  env_vars = env_vars.merge(glimmer_option_env_vars(glimmer_options))
  env_vars_string = env_vars.map {|k,v| "#{k}=#{v}"}.join(' ')
  the_glimmer_lib = glimmer_lib
  devmode_require = nil
  if the_glimmer_lib == GLIMMER_LIB_LOCAL
    devmode_require = '-r puts_debuggerer '
  end
  rake_tasks = Rake.application.tasks.map(&:to_s).map {|t| t.sub('glimmer:', '')}
  potential_rake_task_parts = application.match(REGEX_RAKE_TASK_WITH_ARGS)
  application = potential_rake_task_parts[1]
  rake_task_args = potential_rake_task_parts[2].split(',')
  if rake_tasks.include?(application)
    load_env_vars(glimmer_option_env_vars(glimmer_options))
    rake_task = "glimmer:#{application}"
    puts "Running Glimmer rake task: #{rake_task}" if jruby_options_string.to_s.include?('--debug')
    Rake::Task[rake_task].invoke(*rake_task_args)
  else
    @@mutex.synchronize do
      puts "Launching Glimmer Application: #{application}" if jruby_options_string.to_s.include?('--debug') || glimmer_options['--quiet'].to_s.downcase != 'true'
    end
    command = "#{env_vars_string} jruby #{jruby_options_string}#{jruby_os_specific_options} #{devmode_require}-r #{the_glimmer_lib} -S #{application}"
    puts command if jruby_options_string.to_s.include?('--debug')
    system command
  end
end

.load_env_vars(env_vars) ⇒ Object



83
84
85
86
87
# File 'lib/glimmer/launcher.rb', line 83

def load_env_vars(env_vars)
  env_vars.each do |key, value|
    ENV[key] = value
  end
end

.platform_osObject



47
48
49
# File 'lib/glimmer/launcher.rb', line 47

def platform_os
  OPERATING_SYSTEMS_SUPPORTED.detect {|os| OS.send("#{os}?")}
end

.swt_jar_fileObject



51
52
53
# File 'lib/glimmer/launcher.rb', line 51

def swt_jar_file
  @swt_jar_file ||= File.expand_path(File.join(__FILE__, '..', '..', '..', 'vendor', 'swt', platform_os, 'swt.jar'))
end

Instance Method Details

#launchObject



132
133
134
135
136
137
138
# File 'lib/glimmer/launcher.rb', line 132

def launch
  if @application_paths.empty?
    display_usage
  else
    launch_application
  end
end