{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Headroom.HeaderFn
( runHeaderFn
, configuredHeaderFn
, postProcessHeader
, ConfiguredEnv(..)
, mkConfiguredEnv
)
where
import Headroom.Configuration.Types ( CtHeaderFnConfigs
, HeaderFnConfig(..)
, HeaderFnConfigs(..)
, UpdateCopyrightConfig(..)
)
import Headroom.Data.Has ( Has(..) )
import Headroom.Data.Lens ( suffixLenses
, suffixLensesFor
)
import Headroom.HeaderFn.Types ( HeaderFn(..) )
import Headroom.HeaderFn.UpdateCopyright
( SelectedAuthors(..)
, UpdateCopyrightMode(..)
, updateCopyright
)
import Headroom.Meta ( TemplateType )
import Headroom.Template ( Template(..) )
import Headroom.Types ( CurrentYear(..) )
import Headroom.Variables.Types ( Variables(..) )
import Lens.Micro ( traverseOf )
import RIO
suffixLenses ''HeaderFnConfigs
suffixLenses ''UpdateCopyrightConfig
suffixLensesFor ["hfcConfig"] ''HeaderFnConfig
runHeaderFn :: HeaderFn env
-> env
-> Text
-> Text
runHeaderFn (HeaderFn fn) env input = runReader (fn input) env
configuredHeaderFn :: (Has CurrentYear env, Has UpdateCopyrightMode env)
=> CtHeaderFnConfigs
-> HeaderFn env
configuredHeaderFn HeaderFnConfigs {..} = mconcat
[ifEnabled hfcsUpdateCopyright updateCopyright]
where
ifEnabled HeaderFnConfig {..} fn | hfcEnabled = fn
| otherwise = mempty
postProcessHeader :: ConfiguredEnv
-> Text
-> Text
postProcessHeader env = runHeaderFn (configuredHeaderFn configs) env
where configs = ceHeaderFnConfigs env
data ConfiguredEnv = ConfiguredEnv
{ ceCurrentYear :: !CurrentYear
, ceHeaderFnConfigs :: !CtHeaderFnConfigs
, ceUpdateCopyrightMode :: !UpdateCopyrightMode
}
deriving (Eq, Show)
suffixLensesFor ["ceCurrentYear", "ceUpdateCopyrightMode"] ''ConfiguredEnv
instance Has CurrentYear ConfiguredEnv where
hasLens = ceCurrentYearL
instance Has UpdateCopyrightMode ConfiguredEnv where
hasLens = ceUpdateCopyrightModeL
mkConfiguredEnv :: (MonadThrow m)
=> CurrentYear
-> Variables
-> CtHeaderFnConfigs
-> m ConfiguredEnv
mkConfiguredEnv ceCurrentYear vars configs = do
ceHeaderFnConfigs <- compileTemplates vars configs
let ceUpdateCopyrightMode = mode ceHeaderFnConfigs
pure ConfiguredEnv { .. }
where
authorsL = hfcsUpdateCopyrightL . hfcConfigL . uccSelectedAuthorsL
mode configs' = maybe UpdateAllAuthors
(UpdateSelectedAuthors . SelectedAuthors)
(configs' ^. authorsL)
compileTemplates :: (MonadThrow m)
=> Variables
-> CtHeaderFnConfigs
-> m CtHeaderFnConfigs
compileTemplates vars configs = configs & traverseOf authorsL compileAuthors'
where
authorsL = hfcsUpdateCopyrightL . hfcConfigL . uccSelectedAuthorsL
compileAuthors' = mapM . mapM $ compileAuthor
compileAuthor author = do
parsed <- parseTemplate @TemplateType (Just $ "author " <> author) author
renderTemplate vars parsed