Skip to content

Commit

Permalink
check: Allow checking multiple files. Fixes bitc#18.
Browse files Browse the repository at this point in the history
Using this, full projects can be quickly typechecked, e.g. using

    find . -type f -name "*.hs" | xargs hdevtools check

or in shells that support it:

    hdevtools check **/*.hs
  • Loading branch information
nh2 committed Aug 21, 2013
1 parent e5b2c97 commit e9fced6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/CommandArgs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ data HDevTools
| Check
{ socket :: Maybe FilePath
, ghcOpts :: [String]
, file :: String
, files :: [String]
}
| ModuleFile
{ socket :: Maybe FilePath
Expand Down Expand Up @@ -77,7 +77,7 @@ dummyCheck :: HDevTools
dummyCheck = Check
{ socket = Nothing
, ghcOpts = []
, file = ""
, files = []
}

dummyModuleFile :: HDevTools
Expand Down Expand Up @@ -117,8 +117,8 @@ check :: Annotate Ann
check = record dummyCheck
[ socket := def += typFile += help "socket file to use"
, ghcOpts := def += typ "OPTION" += help "ghc options"
, file := def += typFile += argPos 0 += opt ""
] += help "Check a haskell source file for errors and warnings"
, files := def += typ "FILES..." += args += opt ([] :: [FilePath])
] += help "Check haskell source files for errors and warnings"

moduleFile :: Annotate Ann
moduleFile = record dummyModuleFile
Expand Down
8 changes: 4 additions & 4 deletions src/CommandLoop.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CommandLoop
, startCommandLoop
) where

import Control.Monad (when)
import Control.Monad (forM, when)
import Data.IORef
import Data.List (find)
import MonadUtils (MonadIO, liftIO)
Expand Down Expand Up @@ -101,10 +101,10 @@ configSession state clientSend ghcOpts = do
return ()

runCommand :: IORef State -> ClientSend -> Command -> GHC.Ghc ()
runCommand _ clientSend (CmdCheck file) = do
runCommand _ clientSend (CmdCheck files) = do
let noPhase = Nothing
target <- GHC.guessTarget file noPhase
GHC.setTargets [target]
targets <- forM files $ \f -> GHC.guessTarget f noPhase
GHC.setTargets targets
let handler err = GHC.printException err >> return GHC.Failed
flag <- GHC.handleSourceError handler (GHC.load GHC.LoadAllTargets)
liftIO $ case flag of
Expand Down
20 changes: 13 additions & 7 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@ doModuleFile sock args =
serverCommand sock (CmdModuleFile (module_ args)) (ghcOpts args)

doFileCommand :: String -> (HDevTools -> Command) -> FilePath -> HDevTools -> IO ()
doFileCommand cmdName cmd sock args
| null (file args) = do
progName <- getProgName
hPutStrLn stderr "You must provide a haskell source file. See:"
hPutStrLn stderr $ progName ++ " " ++ cmdName ++ " --help"
| otherwise = serverCommand sock (cmd args) (ghcOpts args)
doFileCommand cmdName cmd sock args = do
case args of
Check { files = [] } -> missingFileError
Check {} -> serverCommand sock (cmd args) (ghcOpts args)
-- The other commands take only one file; here the check is against "".
_ | null (file args) -> missingFileError
_ -> serverCommand sock (cmd args) (ghcOpts args)
where
missingFileError = do
progName <- getProgName
hPutStrLn stderr "You must provide a haskell source file. See:"
hPutStrLn stderr $ progName ++ " " ++ cmdName ++ " --help"

doCheck :: FilePath -> HDevTools -> IO ()
doCheck = doFileCommand "check" $
\args -> CmdCheck (file args)
\args -> CmdCheck (files args)

doInfo :: FilePath -> HDevTools -> IO ()
doInfo = doFileCommand "info" $
Expand Down
2 changes: 1 addition & 1 deletion src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data ClientDirective
deriving (Read, Show)

data Command
= CmdCheck FilePath
= CmdCheck [FilePath]
| CmdModuleFile String
| CmdInfo FilePath String
| CmdType FilePath (Int, Int)
Expand Down

0 comments on commit e9fced6

Please sign in to comment.