module Language.Haskell.Ghcid(
Ghci, GhciError(..), Stream(..),
Load(..), Severity(..),
startGhci, stopGhci, interrupt, process, execStream,
showModules, reload, exec, quit
) where
import System.IO
import System.IO.Error
import System.Process
import System.Time.Extra
import Control.Concurrent.Extra
import Control.Exception.Extra
import Control.Monad.Extra
import Data.Function
import Data.List.Extra
import Data.Maybe
import Data.IORef
import Control.Applicative
import Data.Unique
import System.Console.CmdArgs.Verbosity
import Language.Haskell.Ghcid.Parser
import Language.Haskell.Ghcid.Types as T
import Language.Haskell.Ghcid.Util
import Prelude
data Ghci = Ghci
{ghciProcess :: ProcessHandle
,ghciInterrupt :: IO ()
,ghciExec :: String -> (Stream -> String -> IO ()) -> IO ()
,ghciUnique :: Unique
}
instance Eq Ghci where
a == b = ghciUnique a == ghciUnique b
startGhci :: String -> Maybe FilePath -> (Stream -> String -> IO ()) -> IO (Ghci, [Load])
startGhci cmd directory echo0 = do
(Just inp, Just out, Just err, ghciProcess) <-
createProcess (shell cmd){std_in=CreatePipe, std_out=CreatePipe, std_err=CreatePipe, cwd=directory, create_group=True}
hSetBuffering out LineBuffering
hSetBuffering err LineBuffering
hSetBuffering inp LineBuffering
let writeInp x = do
whenLoud $ outStrLn $ "%STDIN: " ++ x
hPutStrLn inp x
hPutStrLn inp ""
let ghcid_prefix = "#~GHCID-START~#"
let removePrefix = dropPrefixRepeatedly ghcid_prefix
syncCount <- newVar 0
let syncReplay = do
i <- readVar syncCount
let msg = "#~GHCID-FINISH-" ++ show i ++ "~#"
writeInp $ "INTERNAL_GHCID.putStrLn " ++ show msg ++ "\n" ++
"INTERNAL_GHCID.hPutStrLn INTERNAL_GHCID.stderr " ++ show msg
return $ isInfixOf msg
let syncFresh = do
modifyVar_ syncCount $ return . succ
syncReplay
let consume :: Stream -> (String -> IO (Maybe a)) -> IO (Maybe a)
consume name finish = do
let h = if name == Stdout then out else err
fix $ \rec -> do
el <- tryBool isEOFError $ hGetLine h
case el of
Left _ -> return Nothing
Right l -> do
whenLoud $ outStrLn $ "%" ++ upper (show name) ++ ": " ++ l
res <- finish $ removePrefix l
case res of
Nothing -> rec
Just a -> return $ Just a
let consume2 :: String -> (Stream -> String -> IO (Maybe a)) -> IO (a,a)
consume2 msg finish = do
res1 <- onceFork $ consume Stdout (finish Stdout)
res2 <- consume Stderr (finish Stderr)
res1 <- res1
case liftM2 (,) res1 res2 of
Nothing -> throwIO $ UnexpectedExit cmd msg
Just v -> return v
isInterrupting <- newLock
isRunning <- newLock
let ghciExec command echo = do
withLock isInterrupting $ return ()
res <- withLockTry isRunning $ do
writeInp command
stop <- syncFresh
void $ consume2 command $ \strm s ->
if stop s then return $ Just () else do echo strm s; return Nothing
when (isNothing res) $
fail "Ghcid.exec, computation is already running, must be used single-threaded"
let ghciInterrupt = withLock isInterrupting $
whenM (fmap isNothing $ withLockTry isRunning $ return ()) $ do
whenLoud $ outStrLn "%INTERRUPT"
interruptProcessGroupOf ghciProcess
syncReplay
withLock isRunning $ return ()
stop <- syncFresh
void $ consume2 "Interrupt" $ \_ s -> return $ if stop s then Just () else Nothing
ghciUnique <- newUnique
let ghci = Ghci{..}
stdout <- newIORef []
stderr <- newIORef []
sync <- newIORef $ const False
consume2 "" $ \strm s -> do
stop <- readIORef sync
if stop s then
return $ Just ()
else do
s <- return $ maybe s (removePrefix . snd) $ stripInfix ghcid_prefix s
whenLoud $ outStrLn $ "%STDOUT2: " ++ s
modifyIORef (if strm == Stdout then stdout else stderr) (s:)
when ("GHCi, version " `isPrefixOf` s) $ do
writeIORef stdout []
writeIORef stderr []
writeInp "import qualified System.IO as INTERNAL_GHCID"
writeInp $ ":set prompt " ++ ghcid_prefix
writeInp ":set -v1 -fno-break-on-exception -fno-break-on-error"
writeIORef sync =<< syncFresh
echo0 strm s
return Nothing
r <- parseLoad . reverse <$> ((++) <$> readIORef stderr <*> readIORef stdout)
execStream ghci "" echo0
return (ghci, r)
execStream :: Ghci -> String -> (Stream -> String -> IO ()) -> IO ()
execStream = ghciExec
interrupt :: Ghci -> IO ()
interrupt = ghciInterrupt
process :: Ghci -> ProcessHandle
process = ghciProcess
execBuffer :: Ghci -> String -> (Stream -> String -> IO ()) -> IO [String]
execBuffer ghci cmd echo = do
stdout <- newIORef []
stderr <- newIORef []
execStream ghci cmd $ \strm s -> do
modifyIORef (if strm == Stdout then stdout else stderr) (s:)
echo strm s
reverse <$> ((++) <$> readIORef stderr <*> readIORef stdout)
exec :: Ghci -> String -> IO [String]
exec ghci cmd = execBuffer ghci cmd $ \_ _ -> return ()
showModules :: Ghci -> IO [(String,FilePath)]
showModules ghci = parseShowModules <$> exec ghci ":show modules"
reload :: Ghci -> IO [Load]
reload ghci = parseLoad <$> exec ghci ":reload"
quit :: Ghci -> IO ()
quit ghci = do
interrupt ghci
handle (\UnexpectedExit{} -> return ()) $ void $ exec ghci ":quit"
ignore $
void $ waitForProcess $ process ghci
stopGhci :: Ghci -> IO ()
stopGhci ghci = do
forkIO $ do
sleep 5
terminateProcess $ process ghci
quit ghci