-rw-r--r-- src/Index.hs
1 {-# LANGUAGE OverloadedStrings #-} 2 3 module Index ( 4 runIndex, 5 ) where 6 7 import Control.Monad (unless) 8 import qualified Data.HashMap.Strict as HashMap 9 import qualified Data.Text as T 10 import qualified Data.Text.Lazy.IO as TL 11 import Path (toFilePath) 12 import System.FilePath (combine) 13 import Text.Ginger.GVal (GVal, toGVal) 14 15 import Env (Env (..)) 16 import Templates (Template (..), render) 17 import Types 18 19 {- 20 This creates the main index files from the index templates, using information from all 21 configured respositories. 22 -} 23 runIndex :: Env -> [Repo] -> IO () 24 runIndex env repos = 25 mapM_ (runIndexFile indexLookup outputDir quiet) templates 26 where 27 outputDir = toFilePath . envOutput $ env 28 quiet = envQuiet env 29 templates = envIndexTemplates env 30 31 scope :: HashMap.HashMap T.Text (GVal RunRepo) 32 scope = 33 HashMap.fromList 34 [ ("host", toGVal $ envHost env) 35 , ("repositories", toGVal repos) 36 ] 37 38 indexLookup :: T.Text -> RunRepo (GVal RunRepo) 39 indexLookup = return . toGVal . flip HashMap.lookup scope 40 41 {- 42 Use the scope created above to render a single index template. 43 -} 44 runIndexFile :: 45 (T.Text -> RunRepo (GVal RunRepo)) -> 46 FilePath -> 47 Bool -> 48 Template -> 49 IO () 50 runIndexFile indexLookup outputDir quiet template = do 51 let output = combine outputDir . toFilePath . templatePath $ template 52 unless quiet . putStrLn $ "Writing " <> output 53 TL.writeFile output =<< render indexLookup template 54