This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
forked from philopon/haddocset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
113 lines (90 loc) · 4.9 KB
/
Main.hs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TupleSections #-}
import Control.Applicative
import Control.Monad
import qualified Filesystem as P
import qualified Filesystem.Path.CurrentOS as P
import System.IO.Error
import qualified Database.SQLite.Simple as Sql
import Data.Maybe
import Options.Applicative
import Documentation.Haddocset
createCommand :: Options -> IO ()
createCommand o = do
unless (optQuiet o) $ putStrLn "[1/5] Create Directory."
P.createDirectory False (optTarget o) -- for fail when directory already exists.
P.createTree (optDocumentsDir o)
P.createDirectory True (optHaddockDir o)
unless (optQuiet o) $ putStrLn "[2/5] Writing plist."
writeFile (P.encodeString $ optTarget o P.</> "Contents/Info.plist") $ showPlist (createPlist $ optCommand o)
unless (optQuiet o) $ putStrLn "[3/5] Migrate Database."
conn <- Sql.open . P.encodeString $ optTarget o P.</> "Contents/Resources/docSet.dsidx"
Sql.execute_ conn "CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT, package TEXT);"
Sql.execute_ conn "CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path, package);"
globalDir <- globalPackageDirectory (optHcPkg o)
unless (optQuiet o) $ putStr " Global package directory: " >> putStrLn (P.encodeString globalDir)
globals <- map (globalDir P.</>) <$> packageConfs globalDir
let locals = toAddFiles $ optCommand o
iFiles <- filter diExposed . catMaybes <$> mapM readDocInfoFile (globals ++ locals)
unless (optQuiet o) $ putStr " Global package count: " >> print (length globals)
unless (optQuiet o) $ putStrLn "[4/5] Copy and populate Documents."
forM_ iFiles $ \iFile -> addSinglePackage (optQuiet o) (optDocumentsDir o) (optHaddockDir o) conn iFile
unless (optQuiet o) $ putStrLn "[5/5] Create index."
haddockIndex (optHaddockDir o) (optDocumentsDir o)
addCommand :: Options -> IO ()
addCommand o = do
conn <- Sql.open . P.encodeString $ optTarget o P.</> "Contents/Resources/docSet.dsidx"
forM_ (toAddFiles $ optCommand o) $ \i -> go conn i
`catchIOError` handler
haddockIndex (optHaddockDir o) (optDocumentsDir o)
where
go conn p = readDocInfoFile p >>= \mbIFile -> case mbIFile of
Nothing -> return ()
Just iFile -> addSinglePackage (optQuiet o) (optDocumentsDir o) (optHaddockDir o) conn iFile
handler ioe
| isDoesNotExistError ioe = print ioe
| otherwise = ioError ioe
listCommand :: Options -> IO ()
listCommand o =
mapM_ (putStrLn . P.encodeString . P.dropExtension . P.filename) =<< P.listDirectory (optHaddockDir o)
data Options
= Options { optHcPkg :: String
, optTarget :: P.FilePath
, optQuiet :: Bool
, optCommand :: Command
}
deriving Show
optHaddockDir, optDocumentsDir :: Options -> P.FilePath
optHaddockDir opt = optTarget opt P.</> "Contents/Resources/Haddock/"
optDocumentsDir opt = optTarget opt P.</> "Contents/Resources/Documents/"
data Command
= Create { createPlist :: Plist, toAddFiles :: [P.FilePath] }
| List
| Add { toAddFiles :: [P.FilePath] }
deriving Show
main :: IO ()
main = do
opts <- execParser optRule
case opts of
Options{optCommand = Create{}} -> createCommand opts
Options{optCommand = List} -> listCommand opts
Options{optCommand = Add{}} -> addCommand opts
where
optRule = info (helper <*> options) fullDesc
options = Options
<$> (strOption (long "hc-pkg" <> metavar "CMD" <> help "hc-pkg command (default: ghc-pkg)") <|> pure "ghc-pkg")
<*> fmap (docsetDir . P.decodeString)
(strOption (long "target" <> short 't' <> metavar "DOCSET" <> help "output directory (default: haskell.docset)") <|> pure "haskell")
<*> switch (long "quiet" <> short 'q' <> help "suppress output.")
<*> subparser (command "create" (info createOpts $ progDesc "crate new docset.")
<> command "list" (info (pure List) $ progDesc "list package of docset.")
<> command "add" (info addOpts $ progDesc "add package to docset."))
createOpts = Create
<$> ( Plist
<$> (strOption (long "CFBundleIdentifier") <|> pure "haskell")
<*> (strOption (long "CFBundleName") <|> pure "Haskell")
<*> (strOption (long "DocSetPlatformFamily") <|> pure "haskell"))
<*> arguments (Just . P.decodeString) (metavar "CONFS" <> help "path to installed package configuration.")
addOpts = Add <$> arguments1 (Just . P.decodeString) (metavar "CONFS" <> help "path to installed package configuration.")