Class: Guard::Haskell

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/haskell.rb

Defined Under Namespace

Classes: Options, Repl

Constant Summary collapse

DEFAULT_OPTIONS =
{
  cabal_target:  "spec",
  repl_options:  [],
  all_on_start:  false,
  all_on_pass:   false,
  focus_on_fail: true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_options = {}) ⇒ Haskell

Returns a new instance of Haskell


35
36
37
38
# File 'lib/guard/haskell.rb', line 35

def initialize(user_options = {})
  super
  self.opts = Options.new(*DEFAULT_OPTIONS.merge(user_options).values)
end

Instance Attribute Details

#last_runObject (readonly)

Returns the value of attribute last_run


17
18
19
# File 'lib/guard/haskell.rb', line 17

def last_run
  @last_run
end

#optsObject

Returns the value of attribute opts


16
17
18
# File 'lib/guard/haskell.rb', line 16

def opts
  @opts
end

#replObject

Returns the value of attribute repl


16
17
18
# File 'lib/guard/haskell.rb', line 16

def repl
  @repl
end

#targetsObject (readonly)

Returns the value of attribute targets


17
18
19
# File 'lib/guard/haskell.rb', line 17

def targets
  @targets
end

Instance Method Details

#reloadObject


57
58
59
60
# File 'lib/guard/haskell.rb', line 57

def reload
  stop
  start
end

#run(pattern) ⇒ Object


67
68
69
70
71
72
73
74
# File 'lib/guard/haskell.rb', line 67

def run(pattern)
  if opts.focus_on_fail and last_run == :runtime_failure
    repl.reload_and_rerun
  else
    repl.reload_and_run_matching(pattern)
  end
  success?
end

#run_allObject


62
63
64
65
# File 'lib/guard/haskell.rb', line 62

def run_all
  repl.reload_and_run_matching
  success?
end

#run_on_additions(paths) ⇒ Object


101
102
103
104
105
106
# File 'lib/guard/haskell.rb', line 101

def run_on_additions(paths)
  unless paths.all? { |path| targets.include?(path) }
    @targets += paths
    reload
  end
end

#run_on_modifications(paths) ⇒ Object


108
109
110
111
112
113
# File 'lib/guard/haskell.rb', line 108

def run_on_modifications(paths)
  case paths.first
  when /(.+)Spec\.l?hs$/, /(.+)\.l?hs$/ then run($1.to_module_name)
  when /\.cabal$/ then reload
  end
end

#startObject


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/guard/haskell.rb', line 40

def start
  @last_run = :success # try to prove it wasn't :-)
  self.repl = Repl.new(opts.cabal_target, opts.repl_options)
  throw :cabal_repl_initialization_has_failed if self.repl.status == :loading_failure
  success?

  @targets = ::Set.new(::Dir.glob("**/*.{hs,lhs}"))

  if opts.all_on_start
    run_all
  end
end

#stopObject


53
54
55
# File 'lib/guard/haskell.rb', line 53

def stop
  repl.exit
end

#success?Boolean

Returns:

  • (Boolean)

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/guard/haskell.rb', line 76

def success?
  case [last_run, repl.status]
  when [:runtime_failure, :success],
      [:compile_failure, :success]
    @last_run = :success
    Notifier.notify('Success')
    if opts.all_on_pass
      run_all
    end
  when [:success, :success]
    Notifier.notify('Success')
  when [:runtime_failure, :compile_failure],
    [:runtime_failure, :runtime_failure],
    [:compile_failure, :compile_failure]
    Notifier.notify('Failure', image: :failed)
  when [:compile_failure, :runtime_failure],
    [:success, :runtime_failure]
    @last_run = :runtime_failure
    Notifier.notify('Failure', image: :failed)
  when [:success, :compile_failure]
    @last_run = :compile_failure
    Notifier.notify('Failure', image: :failed)
  end
end