Class: Guard::Java

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Java.



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Method Details

#all_commandObject



68
69
70
# File 'lib/guard/java.rb', line 68

def all_command
  @options[:all_cli]
end

#compile(project_name, klass) ⇒ Object



55
56
57
58
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# 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_commandObject



64
65
66
# File 'lib/guard/java.rb', line 64

def focused_command
  @options[:focused_cli]
end

#notify(msg, title = '', image = nil) ⇒ Object



72
73
74
# File 'lib/guard/java.rb', line 72

def notify(msg, title='', image=nil)
  Notifier.notify(msg, title: title, image: image)
end

#run_allObject



26
27
28
29
30
31
32
# 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) ⇒ Object



60
61
62
# File 'lib/guard/java.rb', line 60

def run_focused_tests(project_name, klass)
  run_test_class(klass)
end

#run_on_changes(classes) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



88
89
90
91
92
# 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

#startObject

Raises:

  • (ArgumentError)


19
20
21
22
23
24
# 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

#stopObject



52
53
# File 'lib/guard/java.rb', line 52

def stop
end