{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module File (replace, convert) where
import Configuration (Configuration (..))
import Control.Monad.Reader (MonadIO (liftIO), MonadReader (ask))
import Data.Text (Text)
import qualified Data.Text as T (replace)
import Data.Text.IO (readFile, writeFile)
import Initialise.Types (Initialise)
import Prelude hiding (readFile, writeFile)
replace :: FilePath -> Initialise ()
replace :: FilePath -> Initialise ()
replace FilePath
path = do
Text
contents <- IO Text -> ReaderT Configuration IO Text
forall a. IO a -> ReaderT Configuration IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Text -> ReaderT Configuration IO Text)
-> IO Text -> ReaderT Configuration IO Text
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Text
readFile FilePath
path
Text
contents' <- Text -> ReaderT Configuration IO Text
convert Text
contents
IO () -> Initialise ()
forall a. IO a -> ReaderT Configuration IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Initialise ()) -> IO () -> Initialise ()
forall a b. (a -> b) -> a -> b
$ FilePath -> Text -> IO ()
writeFile FilePath
path Text
contents'
convert :: Text -> Initialise Text
convert :: Text -> ReaderT Configuration IO Text
convert Text
contents = do
Configuration {Year
FilePath
LicenseId
Text
URI
name :: Text
homepage :: URI
author :: Text
maintainer :: Text
licence :: LicenseId
path :: FilePath
year :: Year
name :: Configuration -> Text
homepage :: Configuration -> URI
author :: Configuration -> Text
maintainer :: Configuration -> Text
licence :: Configuration -> LicenseId
path :: Configuration -> FilePath
year :: Configuration -> Year
..} <- ReaderT Configuration IO Configuration
forall r (m :: * -> *). MonadReader r m => m r
ask
Text -> ReaderT Configuration IO Text
forall a. a -> ReaderT Configuration IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(Text -> ReaderT Configuration IO Text)
-> (Text -> Text) -> Text -> ReaderT Configuration IO Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"templatise" Text
name
(Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"template-hs" Text
name
(Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"template.hs" Text
name
(Text -> ReaderT Configuration IO Text)
-> Text -> ReaderT Configuration IO Text
forall a b. (a -> b) -> a -> b
$ Text
contents