ghc-mod/src/GHCModi.hs

66 lines
1.9 KiB
Haskell
Raw Normal View History

2014-03-19 01:23:47 +00:00
module Main where
2014-03-24 08:32:06 +00:00
import Control.Monad (when, void)
2014-03-19 01:23:47 +00:00
import CoreMonad (liftIO)
2014-03-24 08:32:06 +00:00
import Data.List (intercalate)
2014-03-19 01:23:47 +00:00
import Data.Set as S
2014-03-20 08:40:06 +00:00
import Exception (ghandle, SomeException(..))
2014-03-19 01:23:47 +00:00
import GHC
import Language.Haskell.GhcMod
import Language.Haskell.GhcMod.Internal
2014-03-24 08:32:06 +00:00
import System.IO (hFlush,stdout)
2014-03-19 01:23:47 +00:00
main :: IO ()
main = do
cradle <- findCradle
2014-03-20 08:40:06 +00:00
run ls $ do
2014-03-19 01:23:47 +00:00
(readLog,_) <- initializeFlagsWithCradle opt cradle ["-Wall"] True
loop readLog ls S.empty
where
opt = defaultOptions
ls = lineSeparator opt
2014-03-20 08:40:06 +00:00
run :: LineSeparator -> Ghc () -> IO ()
run (LineSeparator ls) body = do
mlibdir <- getSystemLibDir
ghandle ignore $ runGhc mlibdir $ do
dflags <- getSessionDynFlags
defaultCleanupHandler dflags body
where
ignore (SomeException e) = do
putStr "ghc-modi:0:0:Error:"
let x = intercalate ls $ lines $ show e
putStrLn x
putStrLn "NG"
2014-03-19 01:23:47 +00:00
loop :: IO [String] -> LineSeparator -> Set FilePath -> Ghc ()
loop readLog ls set = do
2014-03-24 08:32:06 +00:00
cmdArg <- liftIO $ getLine
let (cmd,arg') = break (== ' ') cmdArg
arg = dropWhile (== ' ') arg'
(msgs,ok,set') <- case cmd of
"check" -> checkStx readLog ls set arg
_ -> return ([], False, set)
mapM_ (liftIO . putStrLn) msgs
liftIO $ putStrLn $ if ok then "OK" else "NG"
liftIO $ hFlush stdout
when ok $ loop readLog ls set'
checkStx :: IO [String]
-> LineSeparator
-> Set FilePath
-> FilePath
-> Ghc ([String], Bool, Set FilePath)
checkStx readLog ls set file = do
2014-03-19 01:23:47 +00:00
let add = not $ S.member file set
2014-03-24 08:32:06 +00:00
ghandle handler $ do
2014-03-19 01:23:47 +00:00
when add $ addTargetFiles [file]
void $ load LoadAllTargets
msgs <- liftIO $ readLog
2014-03-24 08:32:06 +00:00
let set' = if add then S.insert file set else set
return (msgs, True, set')
2014-03-19 01:23:47 +00:00
where
handler err = do
errmsgs <- handleErrMsg ls err
2014-03-24 08:32:06 +00:00
return (errmsgs, False, set)