Skip to content

Commit

Permalink
Allow building with base 4
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoutts committed Oct 28, 2009
1 parent 5a1982a commit e843153
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 20 deletions.
42 changes: 35 additions & 7 deletions Distribution/Client/Install.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ import Data.Maybe
( isJust, fromMaybe )
import qualified Data.Map as Map
import Control.Exception as Exception
( handle, handleJust, Exception(IOException) )
( handleJust )
#if MIN_VERSION_base(4,0,0)
import Control.Exception as Exception
( Exception(toException), catches, Handler(Handler), IOException )
import System.Exit
( ExitCode )
#else
import Control.Exception as Exception
( Exception(IOException, ExitException) )
#endif
import Distribution.Compat.Exception
( SomeException, catchIO, catchExit )
import Control.Monad
( when, unless )
import System.Directory
Expand Down Expand Up @@ -294,7 +305,11 @@ storeDetailedBuildReports verbosity logsDir reports = sequence_
warn verbosity $ "Missing log file for build report: "
++ fromMaybe "" (ioeGetFileName ioe)

#if MIN_VERSION_base(4,0,0)
missingFile ioe
#else
missingFile (IOException ioe)
#endif
| isDoesNotExistError ioe = Just ioe
missingFile _ = Nothing

Expand Down Expand Up @@ -645,9 +660,10 @@ installUnpackedPackage verbosity scriptOptions miscOptions

-- Doc generation phase
docsResult <- if shouldHaddock
then Exception.handle (\_ -> return DocsFailed) $ do
setup haddockCommand haddockFlags
return DocsOk
then (do setup haddockCommand haddockFlags
return DocsOk)
`catchIO` (\_ -> return DocsFailed)
`catchExit` (\_ -> return DocsFailed)
else return DocsNotTried

-- Tests phase
Expand Down Expand Up @@ -710,9 +726,21 @@ installUnpackedPackage verbosity scriptOptions miscOptions
else die $ "Unable to find cabal executable at: " ++ self

-- helper
onFailure :: (Exception -> BuildFailure) -> IO BuildResult -> IO BuildResult
onFailure result = Exception.handle (return . Left . result)

onFailure :: (SomeException -> BuildFailure) -> IO BuildResult -> IO BuildResult
onFailure result action =
#if MIN_VERSION_base(4,0,0)
action `catches`
[ Handler $ \ioe -> handler (ioe :: IOException)
, Handler $ \exit -> handler (exit :: ExitCode)
]
where
handler :: Exception e => e -> IO BuildResult
handler = return . Left . result . toException
#else
action
`catchIO` (return . Left . result . IOException)
`catchExit` (return . Left . result . ExitException)
#endif

withWin32SelfUpgrade :: Verbosity
-> ConfigFlags
Expand Down
14 changes: 7 additions & 7 deletions Distribution/Client/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import Distribution.Version

import Data.Map (Map)
import Network.URI (URI)
import Control.Exception
( Exception )
import Distribution.Compat.Exception
( SomeException )

newtype Username = Username { unUsername :: String }
newtype Password = Password { unPassword :: String }
Expand Down Expand Up @@ -137,11 +137,11 @@ data UnresolvedDependency

type BuildResult = Either BuildFailure BuildSuccess
data BuildFailure = DependentFailed PackageId
| DownloadFailed Exception
| UnpackFailed Exception
| ConfigureFailed Exception
| BuildFailed Exception
| InstallFailed Exception
| DownloadFailed SomeException
| UnpackFailed SomeException
| ConfigureFailed SomeException
| BuildFailed SomeException
| InstallFailed SomeException
data BuildSuccess = BuildOk DocsResult TestsResult

data DocsResult = DocsNotTried | DocsFailed | DocsOk
Expand Down
10 changes: 5 additions & 5 deletions Distribution/Client/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import System.Directory
import Distribution.Compat.TempFile
( createTempDirectory )
import qualified Control.Exception as Exception
( handle, throwIO, evaluate, finally, bracket )

( evaluate, finally, bracket )
import qualified Distribution.Compat.Exception as Exception
( onException )
-- | Generic merging utility. For sorted input lists this is a full outer join.
--
-- * The result list never contains @(Nothing, Nothing)@.
Expand Down Expand Up @@ -51,9 +52,8 @@ duplicatesBy cmp = filter moreThanOne . groupBy eq . sortBy cmp
writeFileAtomic :: FilePath -> BS.ByteString -> IO ()
writeFileAtomic targetFile content = do
(tmpFile, tmpHandle) <- openBinaryTempFile targetDir template
Exception.handle (\err -> do hClose tmpHandle
removeFile tmpFile
Exception.throwIO err) $ do
Exception.onException (do hClose tmpHandle
removeFile tmpFile) $ do
BS.hPut tmpHandle content
hClose tmpHandle
renameFile tmpFile targetFile
Expand Down
52 changes: 52 additions & 0 deletions Distribution/Compat/Exception.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -cpp #-}
{-# OPTIONS_NHC98 -cpp #-}
{-# OPTIONS_JHC -fcpp #-}
-- #hide
module Distribution.Compat.Exception (
SomeException,
onException,
catchIO,
catchExit,
throwIOIO
) where

import System.Exit
import qualified Control.Exception as Exception
#if MIN_VERSION_base(4,0,0)
import Control.Exception (SomeException)
#else
import Control.Exception (Exception)
type SomeException = Exception
#endif

onException :: IO a -> IO b -> IO a
#if MIN_VERSION_base(4,0,0)
onException = Exception.onException
#else
onException io what = io `Exception.catch` \e -> do what
Exception.throw e
#endif

throwIOIO :: Exception.IOException -> IO a
#if MIN_VERSION_base(4,0,0)
throwIOIO = Exception.throwIO
#else
throwIOIO = Exception.throwIO . Exception.IOException
#endif

catchIO :: IO a -> (Exception.IOException -> IO a) -> IO a
#if MIN_VERSION_base(4,0,0)
catchIO = Exception.catch
#else
catchIO = Exception.catchJust Exception.ioErrors
#endif

catchExit :: IO a -> (ExitCode -> IO a) -> IO a
#if MIN_VERSION_base(4,0,0)
catchExit = Exception.catch
#else
catchExit = Exception.catchJust exitExceptions
where exitExceptions (Exception.ExitException ee) = Just ee
exitExceptions _ = Nothing
#endif
2 changes: 1 addition & 1 deletion cabal-install.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Executable cabal
Distribution.Compat.TempFile
Paths_cabal_install

build-depends: base >= 2 && < 4,
build-depends: base >= 2 && < 5,
Cabal >= 1.7.5 && < 1.9,
filepath >= 1.0,
network >= 1 && < 3,
Expand Down

0 comments on commit e843153

Please sign in to comment.