Class: Cosmos::LauncherConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/tools/launcher/launcher_config.rb

Overview

Reads and interprets the Launcher configuration file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LauncherConfig

Processes a file and adds in the configuration defined in the file



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
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
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 36

def initialize(filename)
  # Initialize instance variables
  @title = 'COSMOS Launcher'
  @tool_font_settings  = ['Arial', 12]
  @label_font_settings = ['Arial', 16]
  @num_columns = 4
  @items = []

  if File.exist?(filename.to_s)
    multitool = false
    multitool_text = nil
    multitool_icon_filename = nil
    multitool_settings = nil

    # Loop over each line of the configuration file
    parser = ConfigParser.new
    parser.parse_file(filename) do |keyword, params|
      # Handle each keyword
      case keyword

      when 'TOOL'
        if multitool
          parser.verify_num_parameters(1, 1, "TOOL <Shell command>")
          multitool_settings << [:TOOL, format_shell_command(params[0]), true]
        else
          parser.verify_num_parameters(2, nil, "TOOL <Button Text> <Shell command> <Icon Filename (optional)> <Parameter Name #1 (optional)> <Parameter Value #1 (optional)> ...")
          variable_params = nil
          if params.length > 3
            raise parser.error("Unbalanced variable params for #{params[0]}") if (params.length % 2) != 1
            variable_params = []
            params[3..-1].each_slice(2) { |variable_parameter| variable_params << variable_parameter }
          end
          @items << [:TOOL, params[0], format_shell_command(params[1]), true, ConfigParser.handle_nil(params[2]), variable_params]
        end

      when 'MULTITOOL_START'
        parser.verify_num_parameters(1, 2, "MULTITOOL_START <Button Text> <Icon Filename (optional)>")
        multitool = true
        multitool_text = params[0]
        multitool_icon_filename = ConfigParser.handle_nil(params[1])
        multitool_icon_filename = 'multi.png' unless multitool_icon_filename
        multitool_settings = []

      when 'MULTITOOL_END'
        parser.verify_num_parameters(0, 0, "MULTITOOL_END")
        @items << [:MULTITOOL, multitool_text, multitool_settings, true, multitool_icon_filename, nil]
        multitool = false
        multitool_text = nil
        multitool_icon_filename = nil
        multitool_settings = nil

      when 'DELAY'
        if multitool
          parser.verify_num_parameters(1, 1, "DELAY <Delay in seconds>")
          multitool_settings << [:DELAY, Float(params[0]), true]
        else
          raise parser.error("DELAY keyword only valid within MULTITOOL")
        end

      when 'DIVIDER'
        parser.verify_num_parameters(0, 0, "DIVIDER")
        @items << [:DIVIDER, nil, nil, nil, nil]

      when 'LABEL'
        parser.verify_num_parameters(1, 1, "LABEL <Label Text>")
        @items << [:LABEL, params[0], nil, nil, nil]

      when 'TOOL_FONT'
        parser.verify_num_parameters(2, 2, "TOOL_FONT <Font Name> <Font Size>")
        @tool_font_settings = [params[0], Integer(params[1])]

      when 'LABEL_FONT'
        parser.verify_num_parameters(2, 2, "LABEL_FONT <Font Name> <Font Size>")
        @label_font_settings = [params[0], Integer(params[1])]

      when 'TITLE'
        parser.verify_num_parameters(1, 1, "TITLE <Title Text>")
        @title = params[0]

      when 'NUM_COLUMNS'
        parser.verify_num_parameters(1, 1, "NUM_COLUMNS <Num Columns>")
        @num_columns = params[0].to_i

      when 'DONT_CAPTURE_IO'
        parser.verify_num_parameters(0, 0, "DONT_CAPTURE_IO")
        if multitool
          if multitool_settings[-1].nil? || multitool_settings[-1][0] != :TOOL
            raise parser.error("DONT_CAPTURE_IO must follow a TOOL")
          end
          multitool_settings[-1][2] = false
        else
          if @items[-1].nil? || @items[-1][0] != :TOOL
            raise parser.error("DONT_CAPTURE_IO must follow a TOOL")
          end
          @items[-1][3] = false
        end

      else # UNKNOWN
        raise parser.error("Unknown keyword '#{keyword}'.") if keyword

      end # case keyword

    end # parser.parse_file

  else
    raise "Launcher configuration file does not exist: #{filename}"
  end

end

Instance Attribute Details

#itemsObject (readonly)

Array of [[item_type, text, shell command, icon filename, variable params], …] Where variable params is nil or an array of [[parameter name, parameter value], …]



30
31
32
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 30

def items
  @items
end

#label_font_settingsObject (readonly)

Array for label font settings [font_name, font_size]



26
27
28
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 26

def label_font_settings
  @label_font_settings
end

#num_columnsObject (readonly)

Number of columns per row



33
34
35
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 33

def num_columns
  @num_columns
end

#titleObject (readonly)

Launcher title



20
21
22
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 20

def title
  @title
end

#tool_font_settingsObject (readonly)

Array for tool font settings [font_name, font_size]



23
24
25
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 23

def tool_font_settings
  @tool_font_settings
end

Instance Method Details

#format_shell_command(shell_command) ⇒ Object

def initialize



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 146

def format_shell_command(shell_command)
  if Kernel.is_windows?
    rubyw_sub = 'rubyw'
  else
    rubyw_sub = 'ruby'
  end

  split_command = shell_command.split
  if split_command[0] == 'LAUNCH'
    if Kernel.is_mac? and File.exist?(File.join(USERPATH, 'tools', 'mac'))
      shell_command = "open tools/mac/#{split_command[1]}.app --args #{split_command[2..-1].join(' ')}"
    else
      shell_command = "RUBYW tools/#{split_command[1]} #{split_command[2..-1].join(' ')}"
    end
  elsif split_command[0] == 'LAUNCH_TERMINAL'
    if Kernel.is_mac?
      shell_command = "osascript -e 'tell application \"Terminal\" to do script \"cd #{File.expand_path(USERPATH)} && ruby tools/#{split_command[1]} #{split_command[2..-1].join(' ')}\"' -e 'return'"
    elsif Kernel.is_windows?
      shell_command = "start ruby tools/#{split_command[1]} #{split_command[2..-1].join(' ')}"
    else
      shell_command = "gnome-terminal -e \"ruby tools/#{split_command[1]} #{split_command[2..-1].join(' ')}\""
    end
  end
  shell_command.gsub!('RUBYW', rubyw_sub)
  shell_command
end