Class: Redcar::FilterCommand::CommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_command/command_parser.rb

Overview

Finds, evaluates and replaces supported Redcar environment variables in shell commands

Class Method Summary collapse

Class Method Details

.substitute_variables(doc, command) ⇒ Object

Sets and inserts supported variables into the shell command enviroment and replaces shortened substitution characters. Supported variables require that an editable tab is focussed for substitution to occur.

Parameters:

  • doc (Redcar::Document)

    a document for use in the current command

  • command (String)

    the command to be executed



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/filter_command/command_parser.rb', line 18

def self.substitute_variables doc, command
  output = {}
  ENV['REDCAR_SELECTION'] = doc.selection? ? doc.selected_text : doc.get_line_without_end_of_line(doc.cursor_line)
  ENV['REDCAR_DOCUMENT'] = doc.get_all_text
  ENV['REDCAR_WORD'] = doc.current_word
  ENV['REDCAR_LINE'] = doc.get_line_without_end_of_line(doc.cursor_line)
  ENV['REDCAR_SCOPE'] = doc.cursor_scope
  supported_variables.each do |var|
    unless ENV['REDCAR_'+var].nil?
      var_alias = var == 'SCOPE' ? '#c' : /(##{var.downcase.split(//).first})(\W|\z)/
      var_env = to_env(var,Redcar.platform)
      command = command.gsub(var,var_env).gsub(var_alias,"#{var_env}\\2")
    end
  end
  if doc.mirror and doc.mirror.respond_to?(:path) and path = doc.mirror.path
    path = "\\\\\"#{path}\\\\\""
    command = command.gsub('FILE_PATH',path).gsub(/(#f)(\W|\z)/,"#{path}\\2")
  end
  if command.include?('TEMP') or command =~ /(#t)(\W|\z)/
    temp = java.io.File.create_temp_file("tempfile",".txt")
    temp.delete_on_exit
    path = temp.get_absolute_path
    output['temp'] = path
    path = "\\\\\"#{path}\\\\\""
    command = command.gsub('TEMP',path).gsub(/(#t)(\W|\z)/,path)
  end
  output['command'] = command
  output
end

.supported_variablesObject

A list of supported variables



9
10
11
# File 'lib/filter_command/command_parser.rb', line 9

def self.supported_variables
  ['SELECTION','DOCUMENT','WORD','LINE','SCOPE']
end

.to_env(variable, platform) ⇒ Object

Proper escaping for environment variables (platform-specific)



49
50
51
52
53
54
55
56
# File 'lib/filter_command/command_parser.rb', line 49

def self.to_env variable, platform
  case platform
  when :windows
    return "%REDCAR_#{variable}%"
  when :linux,:osx
    return "\\\\\"$REDCAR_#{variable}\\\\\""
  end
end