Class: Guard::Haskell::Repl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepl

Returns a new instance of Repl.



6
7
8
9
# File 'lib/guard/haskell/repl.rb', line 6

def initialize
  @running = false
  @result  = :success
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



4
5
6
# File 'lib/guard/haskell/repl.rb', line 4

def reader
  @reader
end

#resultObject (readonly)

Returns the value of attribute result.



4
5
6
# File 'lib/guard/haskell/repl.rb', line 4

def result
  @result
end

#stdinObject (readonly)

Returns the value of attribute stdin.



4
5
6
# File 'lib/guard/haskell/repl.rb', line 4

def stdin
  @stdin
end

#threadObject (readonly)

Returns the value of attribute thread.



4
5
6
# File 'lib/guard/haskell/repl.rb', line 4

def thread
  @thread
end

Instance Method Details

#_repl(command) ⇒ Object



70
71
72
73
# File 'lib/guard/haskell/repl.rb', line 70

def _repl command
  @running = true
  stdin.write command
end

#exitObject



48
49
50
51
# File 'lib/guard/haskell/repl.rb', line 48

def exit
  ::Process::kill "TERM", thread.pid
  ::Thread.kill(reader)
end

#init(spec) ⇒ Object



44
45
46
# File 'lib/guard/haskell/repl.rb', line 44

def init spec
  _repl ":load #{spec}\n"
end

#rerunObject



61
62
63
# File 'lib/guard/haskell/repl.rb', line 61

def rerun
  _repl ":reload\n:main --color --rerun\n"
end

#run(pattern = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/guard/haskell/repl.rb', line 53

def run pattern = nil
  if pattern.nil?
    _repl ":reload\n:main --color\n"
  else
    _repl ":reload\n:main --color --match #{pattern}\n"
  end
end

#start(ghci_options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/guard/haskell/repl.rb', line 11

def start ghci_options
  cmd = ["ghci"]
  Dir["*"].each { |d| cmd << "-i#{d}" if File.directory?(d) }
  sandbox = ::Dir[".cabal-sandbox/*packages.conf.d"].first
  cmd << "-package-db=#{sandbox}" if sandbox
  cmd.concat(ghci_options)

  @stdin, stdout, @thread = ::Open3.popen2e(*cmd)
  @reader = ::Thread.new do
    loop do
      while (out = stdout.gets)
        print out
        if @running
          case out
          when /\d+ examples?, 0 failures/
            @result  = :success
            @running = false
          when /\d+ examples?, \d+ failures?/
            @result  = :runtime_failure
            @running = false
          when /Failed, modules loaded:/,
               /\*{3} Exception:/,
               /phase `C preprocessor' failed/,
               /^GHCi runtime linker: fatal error:/
            @result  = :compile_failure
            @running = false
          end
        end
      end
    end
  end
end