class Guard::Java

Public Class Methods

new(watchers=[], options={}) click to toggle source
Calls superclass method
# File lib/guard/java.rb, line 5
def initialize(watchers=[], options={})
  super
  @options = {
    :all_after_pass          => true,
    :all_on_start            => true,
    :focused_after_compile   => true,
    :test_runner_class       => 'org.junit.runner.JUnitCore',
    :project_name            => 'Java Project',
    :focused_cli             => nil,
    :all_cli                 => nil,
    :classpath               => '.'
  }.merge(options)
end

Public Instance Methods

all_command() click to toggle source
# File lib/guard/java.rb, line 68
def all_command
  @options[:all_cli]
end
compile(project_name, klass) click to toggle source
# File lib/guard/java.rb, line 55
def compile(project_name, klass)
  notify "Compiling because of #{klass} change", "#{project_name} file change detected", :pending  # notify any interested listeners
  do_shell(focused_command)
end
do_shell(command) click to toggle source
# File lib/guard/java.rb, line 76
def do_shell(command)
  IO.popen(command) do |out|
    until out.eof?
      puts out.gets
    end
  end

  code = $?  # Get the status code of the last-finished process
  (code == 0) ? :success : :failed
end
focused_command() click to toggle source
# File lib/guard/java.rb, line 64
def focused_command
  @options[:focused_cli]
end
notify(msg, title='', image=nil) click to toggle source
# File lib/guard/java.rb, line 72
def notify(msg, title='', image=nil)
  Notifier.notify(msg, title: title, image: image)
end
run_all() click to toggle source
# File lib/guard/java.rb, line 26
def run_all
  project_name = @options[:project_name]
  notify project_name, "Build and run all", :pending
  result = do_shell(all_command)
  result_description = "#{project_name} build results"
  notify result_description, "Build #{result.to_s.capitalize}", result # Notify of success or failure
end
run_focused_tests(project_name, klass) click to toggle source
# File lib/guard/java.rb, line 60
def run_focused_tests(project_name, klass)
  run_test_class(klass)
end
run_on_changes(classes) click to toggle source
# File lib/guard/java.rb, line 34
def run_on_changes(classes)
  project_name = @options[:project_name]
  klass = classes[0]

  result = compile(project_name, klass)

  result = run_focused_tests(project_name, klass) if result != :failed && @options[:focused_after_compile]

  result_description = "#{project_name}: test run for #{klass}"

  notify result_description, "Build #{result.to_s.capitalize}", result # Notify of success or failure

  if result == :success && @options[:all_after_pass]
    run_all
  end
  nil
end
run_test_class(klass) click to toggle source
# File lib/guard/java.rb, line 88
def run_test_class(klass)
  test_command = "java -cp #{options[:classpath]} #{options[:test_runner_class]} #{klass}"
  puts test_command
  do_shell test_command
end
start() click to toggle source
# File lib/guard/java.rb, line 19
def start
  UI.info 'Guard::Java is running'
  raise ArgumentError, ":focused_cli and :all_cli options must be set" if @options[:focused_cli].nil? || @options[:all_cli].nil?

  run_all if @options[:all_on_start]
end
stop() click to toggle source
# File lib/guard/java.rb, line 52
def stop
end