module Language.Haskell.LSP.Test.Server (withServer) where
import Control.Concurrent.Async
import Control.Monad
import Language.Haskell.LSP.Test.Compat
import System.IO
import System.Process hiding (withCreateProcess)
withServer :: String -> Bool -> (Handle -> Handle -> ProcessHandle -> IO a) -> IO a
withServer serverExe logStdErr f = do
let cmd:args = words serverExe
createProc = (proc cmd args) { std_in = CreatePipe, std_out = CreatePipe, std_err = CreatePipe }
withCreateProcess createProc $ \(Just serverIn) (Just serverOut) (Just serverErr) serverProc -> do
hSetBuffering serverErr NoBuffering
hSetBinaryMode serverErr True
let errSinkThread = forever $ hGetLine serverErr >>= when logStdErr . putStrLn
withAsync errSinkThread $ \_ -> do
f serverIn serverOut serverProc