shake: Build haddocks

This commit is contained in:
Chris Done 2015-01-14 17:14:39 +01:00
parent 7f7250702b
commit e774fc15fd

View File

@ -11,6 +11,7 @@ import Stackage.CheckBuildPlan
import Stackage.PackageDescription import Stackage.PackageDescription
import Stackage.PerformBuild (PerformBuild(..),copyBuiltInHaddocks,renameOrCopy) import Stackage.PerformBuild (PerformBuild(..),copyBuiltInHaddocks,renameOrCopy)
import Stackage.Prelude (unFlagName) import Stackage.Prelude (unFlagName)
import System.Exit
import Control.Concurrent.STM import Control.Concurrent.STM
import Control.Concurrent.STM.TVar import Control.Concurrent.STM.TVar
@ -52,7 +53,7 @@ shakePlan :: TVar (Map String FilePath)
shakePlan haddockFiles registerLock pb shakeDir = do shakePlan haddockFiles registerLock pb shakeDir = do
fetched <- target (targetForFetched shakeDir) $ fetched <- target (targetForFetched shakeDir) $
fetchedTarget shakeDir pb fetchedTarget shakeDir pb
db <- target (targetForDb' shakeDir) $ db <- target (targetForDb shakeDir) $
databaseTarget shakeDir pb databaseTarget shakeDir pb
_ <- forM corePackages $ _ <- forM corePackages $
\name -> \name ->
@ -69,11 +70,41 @@ shakePlan haddockFiles registerLock pb shakeDir = do
shakeDir shakeDir
name name
plan plan
want packageTargets haddockTargets <- forM normalPackages $
\(name,plan) ->
target (targetForDocs shakeDir name) $
do need [targetForPackage shakeDir name]
packageDocs haddockFiles shakeDir pb plan name
if True
then want haddockTargets
else want packageTargets
where corePackages = M.keys $ siCorePackages $ bpSystemInfo $ pbPlan pb where corePackages = M.keys $ siCorePackages $ bpSystemInfo $ pbPlan pb
normalPackages = filter (not . (`elem` corePackages) . fst) $ normalPackages = filter (not . (`elem` corePackages) . fst) $
M.toList $ bpPackages $ pbPlan pb M.toList $ bpPackages $ pbPlan pb
-- | Generate haddock docs for the package.
packageDocs :: TVar (Map String FilePath)
-> FilePattern
-> PerformBuild
-> PackagePlan
-> PackageName
-> Action ()
packageDocs haddockFiles shakeDir pb plan name = do
pwd <- liftIO getCurrentDirectory
env <- liftIO (fmap (Env . (++ defaultEnv pwd)) getEnvironment)
when
(haddocksFlag /= Don'tBuild &&
not (S.null $ sdModules $ ppDesc plan)) $
generateHaddocks haddockFiles pb pkgDir env name nameVer haddocksFlag
makeFile (targetForDocs shakeDir name)
where haddocksFlag = pcHaddocks $ ppConstraints plan
defaultEnv pwd = [( "HASKELL_PACKAGE_SANDBOX"
, pwd <//> buildDatabase pb) | pbGlobalInstall pb]
pkgDir = shakeDir <//> nameVer
nameVer = display name ++
"-" ++
display (ppVersion plan)
-- | Initialize the database if there one needs to be, and in any case -- | Initialize the database if there one needs to be, and in any case
-- create the target file. -- create the target file.
databaseTarget :: FilePath -> PerformBuild -> Action () databaseTarget :: FilePath -> PerformBuild -> Action ()
@ -85,7 +116,7 @@ databaseTarget shakeDir pb = do
liftIO (removeDirectoryRecursive dir) liftIO (removeDirectoryRecursive dir)
() <- cmd "ghc-pkg" "init" dir () <- cmd "ghc-pkg" "init" dir
liftIO $ copyBuiltInHaddocks $ FP.decodeString $ pbDocDir pb liftIO $ copyBuiltInHaddocks $ FP.decodeString $ pbDocDir pb
makeFile (targetForDb' shakeDir) makeFile (targetForDb shakeDir)
where dir = buildDatabase pb where dir = buildDatabase pb
-- | Build, test and generate documentation for the package. -- | Build, test and generate documentation for the package.
@ -116,9 +147,6 @@ packageTarget haddockFiles registerLock pb shakeDir name plan = do
"-" ++ "-" ++
display (ppVersion plan) display (ppVersion plan)
{-when (pcHaddocks (ppConstraints plan) /= Don'tBuild) $
(generateHaddocks haddockFiles pb pkgDir env name nameVer)-}
-- | Make sure all package archives have been fetched. -- | Make sure all package archives have been fetched.
fetchedTarget :: FilePath -> PerformBuild -> Action () fetchedTarget :: FilePath -> PerformBuild -> Action ()
fetchedTarget shakeDir pb = do fetchedTarget shakeDir pb = do
@ -182,43 +210,54 @@ generateHaddocks :: TVar (Map String FilePath)
-> CmdOption -> CmdOption
-> PackageName -> PackageName
-> FilePattern -> FilePattern
-> TestState
-> Action () -> Action ()
generateHaddocks haddockFiles pb pkgDir env name nameVer = do generateHaddocks haddockFiles pb pkgDir env name nameVer expected = do
hfs <- liftIO $ readTVarIO haddockFiles hfs <- liftIO $ readTVarIO haddockFiles
() <- cmd exitCode <- cmd
(Cwd pkgDir) (Cwd pkgDir)
env env
"cabal" "cabal"
"haddock" "haddock"
"--hyperlink-source" "--hyperlink-source"
"--html" "--html"
"--hoogle" "--hoogle"
"--html-location=../$pkg-$version/" "--html-location=../$pkg-$version/"
(map (map
(\(pkgVer,hf) -> (\(pkgVer,hf) ->
concat concat
[ "--haddock-options=--read-interface=" [ "--haddock-options=--read-interface="
, "../" , "../"
, pkgVer , pkgVer
, "/," , "/,"
, hf]) , hf])
(M.toList hfs)) (M.toList hfs))
liftIO $ case (exitCode,expected) of
renameOrCopy (ExitSuccess,ExpectFailure) -> return () -- FIXME: warn.
(FP.decodeString (ExitFailure{},ExpectSuccess) -> throw exitCode -- FIXME: report it
(pkgDir <//> "dist" <//> "doc" <//> "html" <//> display name)) _ -> return ()
(FP.decodeString copy
(pbDocDir pb <//> nameVer)) where copy = do
enewPath <- liftIO $ liftIO $
try $ do let orig = pkgDir <//> "dist" <//> "doc" <//> "html" <//>
canonicalizePath (display name)
(pbDocDir pb <//> nameVer <//> display name ++ ".haddock") exists <- doesDirectoryExist orig
case enewPath of when exists $
Left (e :: IOException) -> return () -- FIXME: log it with Shake. renameOrCopy
Right newPath -> liftIO $ (FP.decodeString orig)
atomically $ (FP.decodeString
modifyTVar haddockFiles $ (pbDocDir pb <//> nameVer))
M.insert nameVer newPath enewPath <- liftIO $
try $
canonicalizePath
(pbDocDir pb <//> nameVer <//> display name ++
".haddock")
case enewPath of
Left (e :: IOException) -> return () -- FIXME: log it with Shake.
Right newPath -> liftIO $
atomically $
modifyTVar haddockFiles $
M.insert nameVer newPath
-- | Generate a flags string for the package plan. -- | Generate a flags string for the package plan.
planFlags :: PackagePlan -> String planFlags :: PackagePlan -> String
@ -248,9 +287,14 @@ targetForPackage :: FilePath -> PackageName -> FilePath
targetForPackage shakeDir name = targetForPackage shakeDir name =
shakeDir <//> "packages" <//> display name shakeDir <//> "packages" <//> display name
-- | Get the target file for a package.
targetForDocs :: FilePath -> PackageName -> FilePath
targetForDocs shakeDir name =
shakeDir <//> "docs" <//> display name
-- | Get a package database path. -- | Get a package database path.
targetForDb' :: FilePath -> FilePath targetForDb :: FilePath -> FilePath
targetForDb' shakeDir = targetForDb shakeDir =
shakeDir <//> "pkgdb" shakeDir <//> "pkgdb"
-- | Declare a target, returning the target name. -- | Declare a target, returning the target name.