Permalink
Browse files

A little work on determining what packages have documentation installed.

  • Loading branch information...
1 parent e5a3060 commit 6d98a2394c8a39e45b29f987debe538a6fa226f7 Andy Gimblett committed Apr 23, 2010
Showing with 64 additions and 5 deletions.
  1. +1 −0 .ghci
  2. +29 −5 src/Distribution/GhcPkgList.hs
  3. +34 −0 src/DocSources.hs
View
1 .ghci
@@ -0,0 +1 @@
+:set -isrc
@@ -10,7 +10,8 @@
module Distribution.GhcPkgList (
PkgDb(..),
PkgInfo(..),
- listPackages
+ listPackages,
+ readPkgName
) where
import Control.Monad (when)
@@ -32,7 +33,13 @@ data PkgInfo = PkgInfo {
pkgName :: String,
pkgVersion :: [Int],
pkgHidden :: Bool
- } deriving (Eq, Ord, Show)
+ } deriving (Eq, Ord)
+
+instance Show PkgInfo where
+ show (PkgInfo n v h) = if h then "(" ++ np ++ ")" else np
+ where np = n ++ "-" ++ (intercalate "." $ map show v)
+
+-- nb: Haven't bothered with a Read instance yet.
-- | Return a list of package database information as reported by
-- calling "ghc-pkg list".
@@ -43,7 +50,17 @@ listPackages = do
Left err -> error $ show err
Right x -> return x
+-- | Given a package name string, turn it into a name and version.
+readPkgName :: String -> Maybe (String, [Int])
+readPkgName s = case ds of
+ Just ds' -> Just (n, ds')
+ Nothing -> Nothing
+ where n = intercalate "-" $ init parts
+ ds :: Maybe [Int]
+ ds = sequence $ map readInt $ split "." $ last parts
+ parts = split "-" s
+
-- Helpers from here on.
@@ -74,6 +91,13 @@ pkgInfo = do
h <- option False (char '(' >> (return True))
ps <- (many $ choice [letter, digit, char '.']) `sepBy1` (char '-')
when h $ char ')' >> (return ())
- let n = intercalate "-" $ init ps
- ds = map read $ split "." $ last ps
- return $ PkgInfo n ds h
+ let pn = readPkgName $ intercalate "-" ps
+ case pn of
+ Just (n, ds) -> return $ PkgInfo n ds h
+ Nothing -> error $ "Can't read package version: " ++ intercalate "-" ps
+
+-- | Safe read of integer string
+readInt :: String -> Maybe Int
+readInt s = case (reads s) :: [(Int, String)] of
+ [(x, "")] -> Just x
+ _ -> Nothing
View
@@ -0,0 +1,34 @@
+module DocSources
+where
+
+import Control.Monad
+import Data.Maybe
+import Distribution.GhcPkgList (PkgInfo(..), readPkgName)
+import System.Directory (doesDirectoryExist, getDirectoryContents)
+import System.FilePath ((</>))
+
+-- | Doc sources can be global or local. On conflict, prefer local.
+data DocSrcKind = DocSrcGlobal
+ | DocSrcUser
+ deriving (Eq, Ord, Show)
+
+data DocSrc = DocSrc {
+ docSrcPath :: FilePath
+ , docSrcKind :: DocSrcKind
+ } deriving (Eq, Ord, Show)
+
+-- | List of doc sources; hard code for now, parameterise later.
+sources :: [DocSrc]
+sources = [DocSrc "/usr/local/share/doc" DocSrcGlobal
+ ,DocSrc "/Users/gimbo/.cabal/share/doc" DocSrcUser]
+
+-- | Given a path to a doc source, return all the paths corresponding
+-- to Haskell packages.
+pkgDirs :: FilePath -> IO [PkgInfo]
+pkgDirs p = do
+ cs <- getDirectoryContents p
+ ds <- filterM (\x -> doesDirectoryExist $ p </> x) cs
+ return $ map (\(n, v) -> PkgInfo n v False) $ mapMaybe readPkgName ds
+
+test :: IO [[PkgInfo]]
+test = mapM (pkgDirs . docSrcPath) sources

0 comments on commit 6d98a23

Please sign in to comment.