Class: Guard::Ragel

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/ragel.rb

Constant Summary collapse

RagelError =
Class.new(RuntimeError)
EXTENSIONS =
{
  :c => 'c',
  :d => 'd',
  :go => 'go',
  :java => 'java',
  :ruby => 'rb',
  :csharp => 'cs',
  :ocaml => 'caml',
}
FORMAT_FLAGS =
{
  'c' => '-C',
  'd' => '-D',
  'go' => '-Z',
  'java' => '-J',
  'rb' => '-R',
  'cs' => '-A',
  'caml' => '-O'
}
DEFAULTS =
{
  :output_format => :ruby,       # Output format to use if it cannot be
                                 # determined from the original filename 
  :options => '',                # Options to pass to ragel
  :notification => true,         # Enable notifications?
  :extension => nil,             # Override output extension
}

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Ragel

Returns a new instance of Ragel.



37
38
39
# File 'lib/guard/ragel.rb', line 37

def initialize(watchers = [], options = {})
  super(watchers, DEFAULTS.merge(options))
end

Instance Method Details

#build_ragel(file) ⇒ String

Runs ragel over the input file with specified options.

Parameters:

  • file (String)

    path to file to build

Returns:

  • (String)

    filename generated by running ragel

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/guard/ragel.rb', line 46

def build_ragel(file)
  output_dir = options[:output] || File.dir(file)
  
  filename, extension = output_filename(file) 
  format_type = options[:output_format]
  extension ||= EXTENSIONS[format_type]
  if extension.nil?
    raise ArgumentError, "Unable to determine output format from #{filename} with default output #{format_type}" 
  end
  format_flag = FORMAT_FLAGS[extension]

  if override_extension = options[:extension]
    extension = override_extension
  end

  output_file = File.join(output_dir, "#{filename}.#{extension}")

  result = system "ragel #{options[:options]} #{format_flag} #{file} -o #{output_file}"
  raise RagelError, $? unless result

  output_file
end

#notify(changed_files) ⇒ Object



102
103
104
105
106
107
# File 'lib/guard/ragel.rb', line 102

def notify(changed_files)
  ::Guard.guards.reject{ |guard| guard == self }.each do |guard|
    paths = Watcher.match_files(guard, changed_files)
    guard.run_on_change paths unless paths.empty?
  end
end

#run_allObject

Build all files being watched



70
71
72
# File 'lib/guard/ragel.rb', line 70

def run_all
  run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*.*'))))
end

#run_on_change(paths) ⇒ Object

Build the files given



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
# File 'lib/guard/ragel.rb', line 75

def run_on_change(paths)
  changed_files = paths.reject{ |f| File.basename(f)[0] == "_" }.map do |file|

    begin
      output_file = build_ragel(file)
      message = "generated '#{output_file}' from '#{file}'"
      ::Guard::UI.info "Guard::Ragel -> #{message}", :reset => true
      if options[:notification]
        ::Guard::Notifier.notify(message, :title => "Guard::Ragel", :image => :success)
      end
      output_file

    rescue RagelError => error
      message = "failed to generate '#{output_file}' from '#{file}', error #{error.message}"
      ::Guard::UI.error "Guard::Ragel -> #{message}"
      if options[:notification]
        ::Guard::Notifier.notify(message, :title => "Guard::Ragel", :image => :error) 
      end
      nil

    end

  end.compact

  notify changed_files
end