forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoc.hs
More file actions
89 lines (81 loc) · 3.52 KB
/
Doc.hs
File metadata and controls
89 lines (81 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
-- | Utilities for built documentation, shared between @stack@ and @stack-doc-server@.
module Stack.Build.Doc where
import Control.Monad
import Data.List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Maybe
import qualified Data.Text as T
import Path
import Stack.Types
import Stack.Constants
import System.Directory
import System.Environment (lookupEnv)
import System.FilePath (takeFileName)
-- | Get all packages included in documentation from directory.
getDocPackages :: Path Abs Dir -> IO (Map PackageName [Version])
getDocPackages loc =
do ls <- fmap (map (toFilePath loc ++)) (getDirectoryContents (toFilePath loc))
mdirs <- forM ls (\e -> do isDir <- doesDirectoryExist e
return $ if isDir then (Just e) else Nothing)
let sorted = -- Sort by package name ascending, version descending
sortBy (\(pa,va) (pb,vb) ->
case compare pa pb of
EQ -> compare vb va
o -> o)
(mapMaybe breakPkgVer (catMaybes mdirs))
return (M.fromAscListWith (++) (map (\(k,v) -> (k,[v])) sorted))
-- | Split a documentation directory name into package name and version.
breakPkgVer :: FilePath -> Maybe (PackageName,Version)
breakPkgVer pkgPath =
case T.breakOnEnd "-"
(T.pack (takeFileName pkgPath)) of
("",_) -> Nothing
(pkgD,verT) ->
let pkgstr = T.dropEnd 1 pkgD
in case parseVersionFromString (T.unpack verT) of
Just v
| Just pkg <-
parsePackageNameFromString (T.unpack pkgstr) ->
Just (pkg,v)
_ -> Nothing
-- | Construct a documentation directory name from package name and version.
joinPkgVer :: (PackageName,Version) -> FilePath
joinPkgVer (pkg,ver) = (packageNameString pkg ++ "-" ++ versionString ver)
--EKB TODO: doc generation for stack-doc-server
-- | Get location of user-generated documentation if it exists.
getExistingUserDocPath :: Config -> IO (Maybe (Path Abs Dir))
getExistingUserDocPath config = do
let docPath = userDocsDir config
docExists <- doesDirectoryExist (toFilePath docPath)
if docExists
then return (Just docPath)
else return Nothing
--EKB TODO: doc generation for stack-doc-server
-- | Get location of global package docs.
getGlobalDocPath :: IO (Maybe (Path Abs Dir))
getGlobalDocPath = do
--EKB TODO: move this location into Config
maybeRootEnv <- lookupEnv "STACK_DOC_ROOT"
case maybeRootEnv of
Nothing -> return Nothing
Just rootEnv -> do
pkgDocPath <- parseAbsDir rootEnv
pkgDocExists <- doesDirectoryExist (toFilePath pkgDocPath)
return (if pkgDocExists then Just pkgDocPath else Nothing)
--EKB TODO: doc generation for stack-doc-server
-- | Get location of GHC docs.
getGhcDocPath :: IO (Maybe (Path Abs Dir))
getGhcDocPath = do
maybeGhcPathS <- findExecutable "ghc"
case maybeGhcPathS of
Nothing -> return Nothing
Just ghcPathS -> do
ghcPath <- parseAbsFile ghcPathS
let ghcDocPath = parent (parent ghcPath) </> $(mkRelDir "share/doc/ghc/html/")
ghcDocExists <- doesDirectoryExist (toFilePath ghcDocPath)
return (if ghcDocExists then Just ghcDocPath else Nothing)