{-# LANGUAGE PatternGuards, Rank2Types, CPP #-}
module CabalBounds.Dependencies
( Dependencies(..)
, dependencies
, filterDependency
, allDependency
, dependencyIf
, filterLibrary
) where
import Control.Lens
import qualified CabalBounds.Args as A
import qualified CabalLenses as CL
import CabalBounds.Types
import Distribution.Package (Dependency(..), unPackageName)
import Distribution.PackageDescription (GenericPackageDescription)
data Dependencies = AllDependencies
| OnlyDependencies [String]
| IgnoreDependencies [String]
deriving (Show, Eq)
dependencies :: A.Args -> Dependencies
dependencies args
| ds@(_:_) <- A.only args
= OnlyDependencies ds
| ds@(_:_) <- A.ignore args
= IgnoreDependencies ds
| otherwise
= AllDependencies
filterDependency :: Dependencies -> Traversal' Dependency Dependency
filterDependency AllDependencies =
filtered (const True)
filterDependency (OnlyDependencies deps) =
filtered (\(Dependency pkg _) -> (unPackageName pkg) `elem` deps)
filterDependency (IgnoreDependencies deps) =
filtered (\(Dependency pkg _) -> (unPackageName pkg) `notElem` deps)
allDependency :: Traversal' GenericPackageDescription Dependency
allDependency =
#if MIN_VERSION_Cabal(1,22,1)
CL.allBuildInfo . CL.targetBuildDependsL . traversed
#else
CL.allDependency
#endif
dependencyIf :: CL.CondVars -> CL.Section -> Traversal' GenericPackageDescription Dependency
dependencyIf condVars section =
#if MIN_VERSION_Cabal(1,22,1)
CL.buildInfoIf condVars section . CL.targetBuildDependsL . traversed
#else
CL.dependencyIf condVars section
#endif
filterLibrary :: Dependencies -> Traversal' Library Library
filterLibrary AllDependencies =
filtered (const True)
filterLibrary (OnlyDependencies deps) =
filtered (\(libName, _) -> libName `elem` deps)
filterLibrary (IgnoreDependencies deps) =
filtered (\(libName, _) -> libName `notElem` deps)