Refactoring Cabal stuff.

This commit is contained in:
Kazu Yamamoto 2013-03-01 15:25:43 +09:00
parent 561bb38900
commit 7761691795
3 changed files with 36 additions and 17 deletions

View File

@ -2,7 +2,7 @@
module Cabal (initializeGHC, getDirs, fromCabal) where
import CabalApi (cabalParseFile, cabalBuildInfo, cabalDependPackages)
import CabalApi
import Control.Applicative
import Control.Exception
import Control.Monad
@ -47,7 +47,7 @@ fromCabal ghcOptions = do
idirs = case hsSourceDirs of
[] -> [cdir,owdir]
dirs -> map (cdir </>) dirs ++ [owdir]
let depPkgs = removeMe cfile $ cabalDependPackages cabal
let depPkgs = removeMe cfile $ cabalAllDependPackages cabal
return (gopts,idirs,depPkgs)
removeMe :: FilePath -> [String] -> [String]

View File

@ -1,19 +1,19 @@
module CabalApi (
cabalParseFile
, cabalBuildInfo
, cabalDependPackages
, getBuildInfos
, cabalAllDependPackages
, cabalAllExtentions
) where
import Control.Applicative
import Data.Maybe (fromJust, maybeToList)
import Data.Maybe (fromJust, maybeToList, catMaybes)
import Data.Set (fromList, toList)
import Distribution.Package (Dependency(Dependency), PackageName(PackageName))
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse (readPackageDescription)
import Distribution.Text (display)
import Distribution.Verbosity (silent)
import Language.Haskell.Extension (Extension(..))
----------------------------------------------------------------
@ -33,24 +33,38 @@ cabalBuildInfo pd = fromJust $ fromLibrary pd <|> fromExecutable pd
----------------------------------------------------------------
cabalDependPackages :: GenericPackageDescription -> [String]
cabalDependPackages = uniqueAndSort . map getDependencyPackageName . allDependsOfDescription
cabalAllDependPackages :: GenericPackageDescription -> [String]
cabalAllDependPackages pd = uniqueAndSort pkgs
where
uniqueAndSort = toList . fromList
pkgs = map getDependencyPackageName $ cabalAllDependency pd
allDependsOfDescription :: GenericPackageDescription -> [Dependency]
allDependsOfDescription = fromPackageDescription getDeps getDeps getDeps getDeps
cabalAllDependency :: GenericPackageDescription -> [Dependency]
cabalAllDependency = fromPackageDescription getDeps getDeps getDeps getDeps
where
getDeps :: [Tree a] -> [Dependency]
getDeps = concatMap condTreeConstraints
getDependencyPackageName :: Dependency -> String
getDependencyPackageName (Dependency (PackageName n) _) = n
getDependencyPackageName (Dependency (PackageName nm) _) = nm
----------------------------------------------------------------
getBuildInfos :: GenericPackageDescription -> [BuildInfo]
getBuildInfos = fromPackageDescription f1 f2 f3 f4
cabalAllExtentions :: GenericPackageDescription -> [String]
cabalAllExtentions pd = uniqueAndSort exts
where
buildInfos = cabalAllBuildInfos pd
eexts = concatMap oldExtensions buildInfos
++ concatMap defaultExtensions buildInfos
exts = catMaybes $ map getExtensionName eexts
getExtensionName :: Extension -> Maybe String
getExtensionName (EnableExtension nm) = Just (display nm)
getExtensionName _ = Nothing
----------------------------------------------------------------
cabalAllBuildInfos :: GenericPackageDescription -> [BuildInfo]
cabalAllBuildInfos = fromPackageDescription f1 f2 f3 f4
where
f1 = map (libBuildInfo . condTreeData)
f2 = map (buildInfo . condTreeData)
@ -73,3 +87,8 @@ fromPackageDescription f1 f2 f3 f4 pd = lib ++ exe ++ tests ++ bench
exe = f2 . map snd . condExecutables $ pd
tests = f3 . map snd . condTestSuites $ pd
bench = f4 . map snd . condBenchmarks $ pd
----------------------------------------------------------------
uniqueAndSort :: [String] -> [String]
uniqueAndSort = toList . fromList

View File

@ -6,9 +6,9 @@ import CabalApi
spec :: Spec
spec = do
describe "cabalDependPackages" $ do
describe "cabalAllDependPackages" $ do
it "extracts dependent packages" $ do
pkgs <- cabalDependPackages <$> cabalParseFile "test/data/cabalapi.cabal"
pkgs <- cabalAllDependPackages <$> cabalParseFile "test/data/cabalapi.cabal"
pkgs `shouldBe` ["Cabal","base","containers","convertible","directory","filepath","ghc","ghc-paths","ghc-syb-utils","hlint","hspec","io-choice","old-time","process","regex-posix","syb","time","transformers"]
describe "cabalBuildInfo" $ do