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

TEXT_USAGE =

TODO update the verbiage below for Glimmer DSL for LibUI

<<~MULTI_LINE_STRING
  Glimmer DSL for LibUI (Prerequisite-Free Ruby Desktop Development Cross-Platform Native GUI Library) - Ruby Gem: glimmer-dsl-libui v#{File.read(File.expand_path('../../../VERSION', __FILE__))}
  Usage: glimmer [--bundler] [--pd] [--quiet] [--debug] [--log-level=VALUE] [[ENV_VAR=VALUE]...] [[-ruby-option]...] (application.rb or task[task_args])

  Runs Glimmer applications and tasks.

  When applications are specified, they are run using Ruby,
  automatically preloading the glimmer-dsl-libui Ruby gem.

  Optionally, extra Glimmer options, Ruby options, and/or environment variables may be passed in.

  Glimmer options:
  - "--bundler=GROUP"   : Activates gems in Bundler default group in Gemfile
  - "--pd=BOOLEAN"      : Requires puts_debuggerer to enable pd method
  - "--quiet=BOOLEAN"   : Does not announce file path of Glimmer application being launched
  - "--debug"           : Displays extra debugging information and enables debug logging
  - "--log-level=VALUE" : Sets Glimmer's Ruby logger level ("ERROR" / "WARN" / "INFO" / "DEBUG"; default is none)

  Tasks are run via rake. Some tasks take arguments in square brackets (surround with double-quotes if using Zsh).

  Available tasks are below (if you do not see any, please add `require 'glimmer/rake_task'` to Rakefile and rerun or run rake -T):
  
MULTI_LINE_STRING
GLIMMER_LIB_GEM =
'glimmer-dsl-libui'
GLIMMER_LIB_LOCAL =
File.expand_path(File.join('lib', "#{GLIMMER_LIB_GEM}.rb"))
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.



134
135
136
137
138
139
140
141
# File 'lib/glimmer/launcher.rb', line 134

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)
  @ruby_options = raw_options
end

Instance Attribute Details

#application_pathsObject (readonly)

Returns the value of attribute application_paths.



129
130
131
# File 'lib/glimmer/launcher.rb', line 129

def application_paths
  @application_paths
end

#env_varsObject (readonly)

Returns the value of attribute env_vars.



130
131
132
# File 'lib/glimmer/launcher.rb', line 130

def env_vars
  @env_vars
end

#glimmer_optionsObject (readonly)

Returns the value of attribute glimmer_options.



131
132
133
# File 'lib/glimmer/launcher.rb', line 131

def glimmer_options
  @glimmer_options
end

#ruby_optionsObject (readonly)

Returns the value of attribute ruby_options.



132
133
134
# File 'lib/glimmer/launcher.rb', line 132

def ruby_options
  @ruby_options
end

Class Method Details

.dev_mode?Boolean

Returns:

  • (Boolean)


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

def dev_mode?
  glimmer_lib == GLIMMER_LIB_LOCAL
end

.glimmer_libObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/glimmer/launcher.rb', line 73

def glimmer_lib
  unless @glimmer_lib
    @glimmer_lib = GLIMMER_LIB_GEM
    if File.exist?(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



88
89
90
91
92
# File 'lib/glimmer/launcher.rb', line 88

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

.is_arm64?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/glimmer/launcher.rb', line 68

def is_arm64?
  host_cpu = OS.host_cpu.downcase
  host_cpu.include?('aarch64') || host_cpu.include?('arm')
end

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/glimmer/launcher.rb', line 100

def launch(application, ruby_options: [], env_vars: {}, glimmer_options: {})
  ruby_options_string = ruby_options.join(' ') + ' ' if ruby_options.any?
  env_vars = env_vars.merge(glimmer_option_env_vars(glimmer_options))
  load_env_vars(env_vars)
  the_glimmer_lib = glimmer_lib
  require 'puts_debuggerer' if (ENV['PD'] || ENV['pd']).to_s.downcase == 'true' || 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:', '')}
     
    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)
    rake_task = "glimmer:#{application}"
    puts "Running Glimmer rake task: #{rake_task}" if ruby_options_string.to_s.include?('--debug')
    Rake::Task[rake_task].invoke(*rake_task_args)
  else
    puts "Launching Glimmer Application: #{application}" if ruby_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



94
95
96
97
98
# File 'lib/glimmer/launcher.rb', line 94

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

Instance Method Details

#launchObject



143
144
145
146
147
148
149
# File 'lib/glimmer/launcher.rb', line 143

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