CabalApi.hs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. module CabalApi (
  2. cabalParseFile,
  3. cabalBuildInfo,
  4. cabalDependPackages
  5. ) where
  6. import Control.Applicative
  7. import Data.Maybe (fromJust, maybeToList)
  8. import Data.Set (fromList, toList)
  9. import Distribution.Verbosity (silent)
  10. import Distribution.Package (Dependency(Dependency), PackageName(PackageName))
  11. import Distribution.PackageDescription
  12. (GenericPackageDescription,
  13. condLibrary, condExecutables, condTestSuites, condBenchmarks,
  14. BuildInfo, libBuildInfo, buildInfo,
  15. CondTree, condTreeConstraints, condTreeData)
  16. import Distribution.PackageDescription.Parse (readPackageDescription)
  17. ----------------------------------------------------------------
  18. cabalParseFile :: FilePath -> IO GenericPackageDescription
  19. cabalParseFile = readPackageDescription silent
  20. -- Causes error, catched in the upper function.
  21. cabalBuildInfo :: GenericPackageDescription -> IO BuildInfo
  22. cabalBuildInfo pd =
  23. return . fromJust $ fromLibrary pd <|> fromExecutable pd
  24. where
  25. fromLibrary c = libBuildInfo . condTreeData <$> condLibrary c
  26. fromExecutable c = buildInfo . condTreeData . snd <$> toMaybe (condExecutables c)
  27. toMaybe [] = Nothing
  28. toMaybe (x:_) = Just x
  29. getDepsOfPairs :: [(a1, CondTree v [b] a)] -> [b]
  30. getDepsOfPairs = concatMap (condTreeConstraints . snd)
  31. allDependsOfDescription :: GenericPackageDescription -> [Dependency]
  32. allDependsOfDescription pd =
  33. concat [depLib, depExe, depTests, depBench]
  34. where
  35. depLib = concatMap condTreeConstraints (maybeToList . condLibrary $ pd)
  36. depExe = getDepsOfPairs . condExecutables $ pd
  37. depTests = getDepsOfPairs . condTestSuites $ pd
  38. depBench = getDepsOfPairs . condBenchmarks $ pd
  39. getDependencyPackageName :: Dependency -> String
  40. getDependencyPackageName (Dependency (PackageName n) _) = n
  41. cabalDependPackages :: GenericPackageDescription -> IO [String]
  42. cabalDependPackages =
  43. return . toList . fromList
  44. . map getDependencyPackageName
  45. . allDependsOfDescription