Class: Glimmer::Launcher

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

Overview

Launcher of glimmer applications and main entry point for the ‘glimmer` command.

Constant Summary collapse

OPERATING_SYSTEMS_SUPPORTED =
["mac", "windows", "linux"]
TEXT_USAGE =

TODO convert to a bash script to achieve faster startup time

"Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v\#{File.read(File.expand_path('../../../VERSION', __FILE__))}\nUsage: glimmer [--bundler] [--pd] [--quiet] [--debug] [--log-level=VALUE] [[ENV_VAR=VALUE]...] [[-jruby-option]...] (application.rb or task[task_args])\n\nRuns Glimmer applications and tasks.\n\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/or environment variables may be passed in.\n\nGlimmer options:\n- \"--bundler=GROUP\"   : Activates gems in Bundler default group in Gemfile\n- \"--pd=BOOLEAN\"      : Requires puts_debuggerer to enable pd method\n- \"--quiet=BOOLEAN\"   : 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\nTasks are run via rake. Some tasks take arguments in square brackets.\n\nAvailable tasks are below (if you do not see any, please add `require 'glimmer/rake_task'` to Rakefile and rerun or run rake -T):\n  \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 --bundler --pd]
GLIMMER_OPTION_ENV_VAR_MAPPING =
{
  '--log-level' => 'GLIMMER_LOGGER_LEVEL'   ,
  '--bundler'   => 'GLIMMER_BUNDLER_SETUP'  ,
  '--pd'        => 'PD'  ,
}
REGEX_RAKE_TASK_WITH_ARGS =
/^([^\[]+)\[?([^\]]*)\]?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_options) ⇒ Launcher

Returns a new instance of Launcher.



156
157
158
159
160
161
162
163
# File 'lib/glimmer/launcher.rb', line 156

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_path = extract_application_path(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.



151
152
153
# File 'lib/glimmer/launcher.rb', line 151

def application_paths
  @application_paths
end

#env_varsObject (readonly)

Returns the value of attribute env_vars.



152
153
154
# File 'lib/glimmer/launcher.rb', line 152

def env_vars
  @env_vars
end

#glimmer_optionsObject (readonly)

Returns the value of attribute glimmer_options.



153
154
155
# File 'lib/glimmer/launcher.rb', line 153

def glimmer_options
  @glimmer_options
end

#jruby_optionsObject (readonly)

Returns the value of attribute jruby_options.



154
155
156
# File 'lib/glimmer/launcher.rb', line 154

def jruby_options
  @jruby_options
end

Class Method Details

.dev_mode?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/glimmer/launcher.rb', line 100

def dev_mode?
  glimmer_lib == GLIMMER_LIB_LOCAL
end

.glimmer_libObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/glimmer/launcher.rb', line 89

def glimmer_lib
  unless @glimmer_lib
    @glimmer_lib = GLIMMER_LIB_GEM
    if File.exists?(GLIMMER_LIB_LOCAL)
      @glimmer_lib = GLIMMER_LIB_LOCAL
      puts "[DEVELOPMENT MODE] (detected #{@glimmer_lib})"
    end
  end
  @glimmer_lib
end

.glimmer_option_env_vars(glimmer_options) ⇒ Object



104
105
106
107
108
# File 'lib/glimmer/launcher.rb', line 104

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



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

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

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/glimmer/launcher.rb', line 116

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.each do |k,v|
    ENV[k] = v
  end
  the_glimmer_lib = glimmer_lib
  require 'puts_debuggerer' if the_glimmer_lib == GLIMMER_LIB_LOCAL
  is_rake_task = !application.end_with?('.rb')
  rake_tasks = []
  if is_rake_task
    load File.expand_path('./Rakefile') if File.exist?(File.expand_path('./Rakefile')) && caller.join("\n").include?('/bin/glimmer:')
    require_relative 'rake_task'
    rake_tasks = Rake.application.tasks.map(&:to_s).map {|t| t.sub('glimmer:', '')}
     
    # handle a bash quirk with calling package[msi] while there is a "packages" directory locally (it passes package[msi] as packages)
    application = 'package[msi]' if application == 'packages'
    
    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(',')
  end
  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
    puts "Launching Glimmer Application: #{application}" if jruby_options_string.to_s.include?('--debug') || glimmer_options['--quiet'].to_s.downcase != 'true'
    require the_glimmer_lib
    load File.expand_path(application)
  end
end

.load_env_vars(env_vars) ⇒ Object



110
111
112
113
114
# File 'lib/glimmer/launcher.rb', line 110

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

.platform_osObject



71
72
73
# File 'lib/glimmer/launcher.rb', line 71

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

.special_cpu_architecture_suffixObject



75
76
77
78
79
# File 'lib/glimmer/launcher.rb', line 75

def special_cpu_architecture_suffix
  host_cpu = OS.host_cpu.downcase
  special = host_cpu.include?('aarch64') || host_cpu.include?('arm')
  special ? "_aarch64" : ''
end

.swt_jar_fileObject



81
82
83
# File 'lib/glimmer/launcher.rb', line 81

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

Instance Method Details

#launchObject



165
166
167
168
169
170
171
# File 'lib/glimmer/launcher.rb', line 165

def launch
  if @application_path.nil?
    display_usage
  else
    launch_application
  end
end