Commit 757d14e9 authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Add a non-throwing variant of 'requireProgramVersion'.

parent b3a0e6da
......@@ -79,6 +79,7 @@ module Distribution.Simple.Program (
, userSpecifyArgss
, userSpecifiedArgs
, lookupProgram
, lookupProgramVersion
, updateProgram
, configureProgram
, configureAllKnownPrograms
......
......@@ -50,6 +50,7 @@ module Distribution.Simple.Program.Db (
-- ** Query and manipulate the program db
configureProgram,
configureAllKnownPrograms,
lookupProgramVersion,
reconfigurePrograms,
requireProgram,
requireProgramVersion,
......@@ -73,6 +74,7 @@ import Distribution.Verbosity
( Verbosity )
import Data.Binary (Binary(..))
import Data.Functor ((<$>))
import Data.List
( foldl' )
import Data.Maybe
......@@ -410,32 +412,35 @@ requireProgram verbosity prog conf = do
-- | Check that a program is configured and available to be run.
--
-- Additionally check that the version of the program number is suitable and
-- return it. For example you could require 'AnyVersion' or
-- @'orLaterVersion' ('Version' [1,0] [])@
-- Additionally check that the program version number is suitable and return
-- it. For example you could require 'AnyVersion' or @'orLaterVersion'
-- ('Version' [1,0] [])@
--
-- It raises an exception if the program could not be configured or the version
-- is unsuitable, otherwise it returns the configured program and its version
-- number.
-- It returns the configured program, its version number and a possibly updated
-- 'ProgramDb'. If the program could not be configured or the version is
-- unsuitable, it returns an error value.
--
requireProgramVersion :: Verbosity -> Program -> VersionRange
-> ProgramDb
-> IO (ConfiguredProgram, Version, ProgramDb)
requireProgramVersion verbosity prog range conf = do
lookupProgramVersion
:: Verbosity -> Program -> VersionRange -> ProgramDb
-> IO (Either String (ConfiguredProgram, Version, ProgramDb))
lookupProgramVersion verbosity prog range programDb = do
-- If it's not already been configured, try to configure it now
conf' <- case lookupProgram prog conf of
Nothing -> configureProgram verbosity prog conf
Just _ -> return conf
programDb' <- case lookupProgram prog programDb of
Nothing -> configureProgram verbosity prog programDb
Just _ -> return programDb
case lookupProgram prog conf' of
Nothing -> die notFound
case lookupProgram prog programDb' of
Nothing -> return $! Left notFound
Just configuredProg@ConfiguredProgram { programLocation = location } ->
case programVersion configuredProg of
Just version
| withinRange version range -> return (configuredProg, version, conf')
| otherwise -> die (badVersion version location)
Nothing -> die (noVersion location)
| withinRange version range ->
return $! Right (configuredProg, version ,programDb')
| otherwise ->
return $! Left (badVersion version location)
Nothing ->
return $! Left (noVersion location)
where notFound = "The program '"
++ programName prog ++ "'" ++ versionRequirement
......@@ -451,3 +456,13 @@ requireProgramVersion verbosity prog range conf = do
versionRequirement
| isAnyVersion range = ""
| otherwise = " version " ++ display range
-- | Like 'lookupProgramVersion', but raises an exception in case of error
-- instead of returning 'Left errMsg'.
--
requireProgramVersion :: Verbosity -> Program -> VersionRange
-> ProgramDb
-> IO (ConfiguredProgram, Version, ProgramDb)
requireProgramVersion verbosity prog range programDb =
join $ either die return <$>
lookupProgramVersion verbosity prog range programDb
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment