Skip to content

Commit

Permalink
detab and wibbles for ghc 7.10
Browse files Browse the repository at this point in the history
  • Loading branch information
benl23x5 committed Apr 3, 2015
1 parent e86e369 commit a652916
Showing 26 changed files with 1,158 additions and 1,161 deletions.
2 changes: 1 addition & 1 deletion buildbox/BuildBox.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

module BuildBox
( Build
( Build

-- * Building
, runBuild
62 changes: 31 additions & 31 deletions buildbox/BuildBox/Build.hs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@

-- | Defines the main `Build` monad and common utils.
module BuildBox.Build
( module BuildBox.Build.Testable
, module BuildBox.Build.BuildState
, Build

-- * Building
, runBuild
, runBuildWithState
, runBuildPrint
, runBuildPrintWithState
, successfully

-- * Errors
( module BuildBox.Build.Testable
, module BuildBox.Build.BuildState
, Build

-- * Building
, runBuild
, runBuildWithState
, runBuildPrint
, runBuildPrintWithState
, successfully

-- * Errors
, BuildError (..)
, throw
, throw
, catch
, needs
, needs

-- * Utils
, io
, whenM
-- * Utils
, io
, whenM

-- * Output
, out
, outLn
, outBlank
, outLine
, outLINE
, logSystem)
-- * Output
, out
, outLn
, outBlank
, outLine
, outLINE
, logSystem)

where
import BuildBox.Build.Base
@@ -43,12 +43,12 @@ import Prelude
-- | Log a system command to the handle in our `BuildConfig`, if any.
logSystem :: String -> Build ()
logSystem cmd
= do mHandle <- gets buildStateLogSystem
case mHandle of
Nothing -> return ()
Just handle
-> do io $ hPutStr handle "buildbox system: "
io $ hPutStrLn handle cmd
return ()
= do mHandle <- gets buildStateLogSystem
case mHandle of
Nothing -> return ()
Just handle
-> do io $ hPutStr handle "buildbox system: "
io $ hPutStrLn handle cmd
return ()


102 changes: 51 additions & 51 deletions buildbox/BuildBox/Build/Base.hs
Original file line number Diff line number Diff line change
@@ -13,73 +13,73 @@ import System.Directory

-- | The builder monad encapsulates and IO action that can fail with an error,
-- and also read some global configuration info.
type Build a = ErrorT BuildError (StateT BuildState IO) a
type Build a = ErrorT BuildError (StateT BuildState IO) a


-- Build ------------------------------------------------------------------------------------------
-- | Run a build command. The first argument is a directory that can be used for
-- temporary files (like \"/tmp\")
runBuild :: FilePath -> Build a -> IO (Either BuildError a)
runBuild scratchDir build
= do uid <- getUniqueId
let s = buildStateDefault uid scratchDir
evalStateT (runErrorT build) s
= do uid <- getUniqueId
let s = buildStateDefault uid scratchDir
evalStateT (runErrorT build) s


-- | Like 'runBuild`, but report whether it succeeded to the console.
-- If it succeeded then return Just the result, else Nothing.
runBuildPrint :: FilePath -> Build a -> IO (Maybe a)
runBuildPrint scratchDir build
= do uid <- getUniqueId
let s = buildStateDefault uid scratchDir
runBuildPrintWithState s build
= do uid <- getUniqueId
let s = buildStateDefault uid scratchDir
runBuildPrintWithState s build


-- | Like `runBuild` but also takes a `BuildState`.
runBuildWithState :: BuildState -> Build a -> IO (Maybe a)
runBuildWithState s build
= do result <- evalStateT (runErrorT build) s
case result of
Left err
-> do putStrLn $ render $ ppr err
return $ Nothing
Right x
-> do return $ Just x
= do result <- evalStateT (runErrorT build) s
case result of
Left err
-> do putStrLn $ render $ ppr err
return $ Nothing
Right x
-> do return $ Just x


-- | Like `runBuildPrint` but also takes a `BuildState`.
runBuildPrintWithState :: BuildState -> Build a -> IO (Maybe a)
runBuildPrintWithState s build
= do result <- evalStateT (runErrorT build) s
case result of
Left err
-> do putStrLn "\nBuild failed"
putStr " due to "
putStrLn $ render $ ppr err
return $ Nothing
Right x
-> do putStrLn "Build succeeded."
return $ Just x
= do result <- evalStateT (runErrorT build) s
case result of
Left err
-> do putStrLn "\nBuild failed"
putStr " due to "
putStrLn $ render $ ppr err
return $ Nothing
Right x
-> do putStrLn "Build succeeded."
return $ Just x


-- | Discard the resulting value of a compuation.
-- Used like @successfully . runBuild ...@
successfully :: IO a -> IO ()
successfully f = f >> return ()
successfully f = f >> return ()


-- | Get a unique(ish) id for this process.
-- The random seeds the global generator with the cpu time in psecs, which should be good enough.
getUniqueId :: IO Integer
getUniqueId
= randomRIO (0, 1000000000)
= randomRIO (0, 1000000000)

-- Errors -----------------------------------------------------------------------------------------
-- | Throw an error in the build monad.
throw :: BuildError -> Build a
throw = throwError
throw = throwError


-- | Run a build command, catching any exceptions it sends.
@@ -98,12 +98,12 @@ catch build handle
-- created.
needs :: FilePath -> Build ()
needs filePath
= do isFile <- io $ doesFileExist filePath
isDir <- io $ doesDirectoryExist filePath
if isFile || isDir
then return ()
else throw $ ErrorNeeds filePath
= do isFile <- io $ doesFileExist filePath
isDir <- io $ doesDirectoryExist filePath
if isFile || isDir
then return ()
else throw $ ErrorNeeds filePath


-- Utils ------------------------------------------------------------------------------------------
@@ -112,45 +112,45 @@ needs filePath
-- `ErrorIOError` exceptions in our `Build` monad.
io :: IO a -> Build a
io x
= do -- catch IOError exceptions
result <- liftIO $ try x
case result of
Left err -> throw $ ErrorIOError err
Right res -> return res
= do -- catch IOError exceptions
result <- liftIO $ try x
case result of
Left err -> throw $ ErrorIOError err
Right res -> return res


-- | Like `when`, but with teh monadz.
whenM :: Monad m => m Bool -> m () -> m ()
whenM cb cx
= do b <- cb
if b then cx else return ()
= do b <- cb
if b then cx else return ()


-- Output -----------------------------------------------------------------------------------------
-- | Print some text to stdout.
out :: Pretty a => a -> Build ()
out str
out str
= io
$ do putStr $ render $ ppr str
hFlush stdout
$ do putStr $ render $ ppr str
hFlush stdout

-- | Print some text to stdout followed by a newline.
outLn :: Pretty a => a -> Build ()
outLn str = io $ putStrLn $ render $ ppr str
outLn str = io $ putStrLn $ render $ ppr str


-- | Print a blank line to stdout
outBlank :: Build ()
outBlank = out $ text "\n"
outBlank = out $ text "\n"


-- | Print a @-----@ line to stdout
outLine :: Build ()
outLine = io $ putStr (replicate 80 '-' ++ "\n")
outLine = io $ putStr (replicate 80 '-' ++ "\n")


-- | Print a @=====@ line to stdout
outLINE :: Build ()
outLINE = io $ putStr (replicate 80 '=' ++ "\n")
outLINE = io $ putStr (replicate 80 '=' ++ "\n")
90 changes: 45 additions & 45 deletions buildbox/BuildBox/Build/BuildError.hs
Original file line number Diff line number Diff line change
@@ -2,78 +2,78 @@
{-# OPTIONS_HADDOCK hide #-}

module BuildBox.Build.BuildError
(BuildError(..))
(BuildError(..))
where
import BuildBox.Pretty
import System.Exit
import Control.Monad.Error
import BuildBox.Data.Log (Log)
import qualified BuildBox.Data.Log as Log
import BuildBox.Data.Log (Log)
import qualified BuildBox.Data.Log as Log


-- BuildError -------------------------------------------------------------------------------------
-- | The errors we recognise.
data BuildError
-- | Some generic error
= ErrorOther String

-- | Some system command fell over, and it barfed out the given stdout and stderr.
| ErrorSystemCmdFailed
{ buildErrorCmd :: String
, buildErrorCode :: ExitCode
, buildErrorStdout :: Log
, buildErrorStderr :: Log }
-- | Some miscellanous IO action failed.
| ErrorIOError IOError

-- | Some property `check` was supposed to return the given boolean value, but it didn't.
| forall prop. Show prop => ErrorCheckFailed Bool prop

-- | A build command needs the following file to continue.
-- This can be used for writing make-like bots.
| ErrorNeeds FilePath
-- | Some generic error
= ErrorOther String

-- | Some system command fell over, and it barfed out the given stdout and stderr.
| ErrorSystemCmdFailed
{ buildErrorCmd :: String
, buildErrorCode :: ExitCode
, buildErrorStdout :: Log
, buildErrorStderr :: Log }
-- | Some miscellanous IO action failed.
| ErrorIOError IOError

-- | Some property `check` was supposed to return the given boolean value, but it didn't.
| forall prop. Show prop => ErrorCheckFailed Bool prop

-- | A build command needs the following file to continue.
-- This can be used for writing make-like bots.
| ErrorNeeds FilePath

instance Error BuildError where
strMsg s = ErrorOther s

instance Pretty BuildError where
ppr err
= case err of
ErrorOther str
-> text "Other error: " <> text str
ErrorOther str
-> text "Other error: " <> text str

ErrorSystemCmdFailed{}
-> vcat
$ [ text "System command failure."
, text " command: " <> (text $ buildErrorCmd err)
, text " exit code: " <> (text $ show $ buildErrorCode err)
, blank ]
ErrorSystemCmdFailed{}
-> vcat
$ [ text "System command failure."
, text " command: " <> (text $ buildErrorCmd err)
, text " exit code: " <> (text $ show $ buildErrorCode err)
, blank ]

++ (if (not $ Log.null $ buildErrorStdout err)
then [ text "-- stdout (last 10 lines) ------------------------------------------------------"
, text $ Log.toString $ Log.lastLines 10 $ buildErrorStdout err]
else [])
then [ text "-- stdout (last 10 lines) ------------------------------------------------------"
, text $ Log.toString $ Log.lastLines 10 $ buildErrorStdout err]
else [])

++ (if (not $ Log.null $ buildErrorStderr err)
then [ text "-- stderr (last 10 lines) ------------------------------------------------------"
, text $ Log.toString $ Log.lastLines 10 $ buildErrorStderr err ]
else [])
then [ text "-- stderr (last 10 lines) ------------------------------------------------------"
, text $ Log.toString $ Log.lastLines 10 $ buildErrorStderr err ]
else [])

++ (if ( (not $ Log.null $ buildErrorStdout err)
++ (if ( (not $ Log.null $ buildErrorStdout err)
|| (not $ Log.null $ buildErrorStderr err))
then [ text "--------------------------------------------------------------------------------" ]
else [])
ErrorIOError ioerr
-> text "IO error: " <> (text $ show ioerr)
ErrorIOError ioerr
-> text "IO error: " <> (text $ show ioerr)

ErrorCheckFailed expected prop
-> text "Check failure: " <> (text $ show prop) <> (text " expected ") <> (text $ show expected)
ErrorCheckFailed expected prop
-> text "Check failure: " <> (text $ show prop) <> (text " expected ") <> (text $ show expected)

ErrorNeeds filePath
-> text "Build needs: " <> text filePath
ErrorNeeds filePath
-> text "Build needs: " <> text filePath


instance Show BuildError where
Loading

0 comments on commit a652916

Please sign in to comment.