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 =
{
  top_spec:      "test/Spec.hs",
  ghci_options:  [],
  all_on_start:  false,
  all_on_pass:   false,
  focus_on_fail: true,
  sandbox_glob: ".cabal-sandbox/*packages.conf.d",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_options = {}) ⇒ Haskell

Returns a new instance of Haskell.



46
47
48
49
# File 'lib/guard/haskell.rb', line 46

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.



26
27
28
# File 'lib/guard/haskell.rb', line 26

def last_run
  @last_run
end

#optsObject

Returns the value of attribute opts.



25
26
27
# File 'lib/guard/haskell.rb', line 25

def opts
  @opts
end

#replObject

Returns the value of attribute repl.



25
26
27
# File 'lib/guard/haskell.rb', line 25

def repl
  @repl
end

#targetsObject (readonly)

Returns the value of attribute targets.



26
27
28
# File 'lib/guard/haskell.rb', line 26

def targets
  @targets
end

Instance Method Details

#reloadObject



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

def reload
  stop
  start
end

#run(pattern) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/guard/haskell.rb', line 77

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



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

def run_all
  repl.reload_and_run_matching
  success?
end

#run_on_additions(paths) ⇒ Object



111
112
113
114
115
116
# File 'lib/guard/haskell.rb', line 111

def run_on_additions paths
  unless paths.all? { |path| targets.include? path }
    @targets += paths
    repl.init(opts.top_spec)
  end
end

#run_on_modifications(paths) ⇒ Object



118
119
120
121
122
123
# File 'lib/guard/haskell.rb', line 118

def run_on_modifications paths
  case paths.first
  when /(.+)Spec\.l?hs$/, /(.+)\.l?hs$/
    run($1.strip_lowercase_directories.path_to_module_name)
  end
end

#startObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/guard/haskell.rb', line 51

def start
  @last_run = :success # try to prove it wasn't :-)
  self.repl = Repl.new(opts.ghci_options, opts.sandbox_glob)
  repl.init(opts.top_spec)

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

  if opts.all_on_start
    run_all
  end
end

#stopObject



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

def stop
  repl.exit
end

#success?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/guard/haskell.rb', line 86

def success?
  case [last_run, repl.result]
  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