Class: Propane::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/propane/runner.rb

Overview

Utility class to handle the different commands that the 'propane' offers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



11
12
13
# File 'lib/propane/runner.rb', line 11

def initialize
  @options = {}
end

Instance Attribute Details

#argcObject (readonly)

Returns the value of attribute argc.



9
10
11
# File 'lib/propane/runner.rb', line 9

def argc
  @argc
end

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/propane/runner.rb', line 9

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/propane/runner.rb', line 9

def options
  @options
end

Class Method Details

.executeObject

Start running a propane command from the passed-in arguments



16
17
18
19
20
# File 'lib/propane/runner.rb', line 16

def self.execute
  runner = new
  runner.parse_options(ARGV)
  runner.execute!
end

Instance Method Details

#createObject



67
68
69
70
# File 'lib/propane/runner.rb', line 67

def create
  require_relative 'creators/sketch_writer'
  SketchWriter.new(File.basename(filename, '.rb'), argc).write
end

#execute!Object

Dispatch central.



23
24
25
26
27
28
# File 'lib/propane/runner.rb', line 23

def execute!
  show_help if options.empty?
  show_version if options[:version]
  create if options[:create]
  install(filename) if options[:install]
end

#install(library) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/propane/runner.rb', line 84

def install(library)
  library ||= 'new'
  choice = library.downcase
  case choice
  when /samples|sound|video/
    system "cd #{PROPANE_ROOT}/vendors && rake download_and_copy_#{choice}"
  when /new/
    system "cd #{PROPANE_ROOT}/vendors && rake download_and_copy_samples"
  else
    warn format('No installer for %<lib>s', lib: choice)
  end
end

#parse_options(args) ⇒ Object

Parse the command-line options. Keep it simple.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/propane/runner.rb', line 31

def parse_options(args)
  opt_parser = OptionParser.new do |opts|
    # Set a banner, displayed at the top
    # of the help screen.
    opts.banner = 'Usage: propane [options] [<sketch.rb>]'

    # Define the options, and what they do
    options[:version] = false
    opts.on('-v', '--version', 'Propane Version') do
      options[:version] = true
    end

    options[:install] = false
    message = '<Samples><Video><Sound> Install samples or library'
    opts.on('-i', '--install', message) do
      options[:install] = true
    end

    options[:create] = false
    opts.on('-c', '--create', 'Create new sketch outline') do
      options[:create] = true
    end

    # This displays the help screen, all programs are
    # assumed to have this option.
    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      puts ''
      puts 'Run a sketch: jruby [--dev] [<sketch.rb>]'
      exit
    end
  end
  @argc = opt_parser.parse(args)
  @filename = argc.shift
end

#show_versionObject

Raises:



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/propane/runner.rb', line 72

def show_version
  require 'erb'
  require_relative 'helpers/version_error'
  raise JDKVersionError if ENV_JAVA['java.specification.version'] < '11'

  template = ERB.new "    propane version <%= Propane::VERSION %>\n    JRuby version <%= JRUBY_VERSION %>\n  VERSION\n  puts template.result(binding)\nend\n"