2019-04-11 00:15:52 +03:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
2021-04-28 18:36:00 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
module Generator.MockWriteableMonad
|
2021-04-28 18:36:00 +03:00
|
|
|
( MockWriteableMonad,
|
|
|
|
MockWriteableMonadLogs (..),
|
|
|
|
MockWriteableMonadConfig (..),
|
|
|
|
getMockLogs,
|
|
|
|
defaultMockConfig,
|
|
|
|
)
|
|
|
|
where
|
2019-04-11 00:15:52 +03:00
|
|
|
|
|
|
|
import Control.Monad.State
|
2019-04-22 13:58:31 +03:00
|
|
|
import qualified Data.Aeson as Aeson
|
2021-04-28 18:36:00 +03:00
|
|
|
import Data.Text (Text, pack)
|
2021-07-03 12:00:01 +03:00
|
|
|
import Fixtures (systemSPRoot)
|
2021-04-28 18:36:00 +03:00
|
|
|
import Generator.FileDraft.WriteableMonad
|
|
|
|
import Generator.Templates (TemplatesDir)
|
2021-07-03 12:00:01 +03:00
|
|
|
import StrongPath (Abs, Dir, File', Path', Rel, reldir, (</>))
|
2019-04-11 00:15:52 +03:00
|
|
|
|
|
|
|
-- TODO: Instead of manually defining mock like this, consider using monad-mock package,
|
|
|
|
-- it should do most of this automatically, now there is a lot of boilerplate.
|
|
|
|
-- Or we ourselves can maybe use template haskell to reduce duplication.
|
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
defaultMockConfig :: MockWriteableMonadConfig
|
2021-04-28 18:36:00 +03:00
|
|
|
defaultMockConfig =
|
|
|
|
MockWriteableMonadConfig
|
2021-07-03 12:00:01 +03:00
|
|
|
{ getTemplatesDirAbsPath_impl = systemSPRoot </> [reldir|mock/templates/dir|],
|
|
|
|
getTemplateFileAbsPath_impl = \path -> systemSPRoot </> [reldir|mock/templates/dir|] </> path,
|
2021-04-28 18:36:00 +03:00
|
|
|
compileAndRenderTemplate_impl = \_ _ -> pack "Mock template content",
|
|
|
|
doesFileExist_impl = const True
|
2019-04-11 00:15:52 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
getMockLogs :: MockWriteableMonad a -> MockWriteableMonadConfig -> MockWriteableMonadLogs
|
|
|
|
getMockLogs mock config = fst $ execState (unMockWriteableMonad mock) (emptyLogs, config)
|
2019-04-11 00:15:52 +03:00
|
|
|
where
|
2020-01-15 15:08:13 +03:00
|
|
|
emptyLogs = MockWriteableMonadLogs [] [] [] [] [] []
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
instance WriteableMonad MockWriteableMonad where
|
2021-04-28 18:36:00 +03:00
|
|
|
writeFileFromText dstPath text = MockWriteableMonad $ do
|
|
|
|
modifyLogs (writeFileFromText_addCall dstPath text)
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
getTemplatesDirAbsPath = MockWriteableMonad $ do
|
|
|
|
modifyLogs getTemplatesDirAbsPath_addCall
|
|
|
|
(_, config) <- get
|
|
|
|
return $ getTemplatesDirAbsPath_impl config
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
createDirectoryIfMissing createParents path = MockWriteableMonad $ do
|
|
|
|
modifyLogs (createDirectoryIfMissing_addCall createParents path)
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
copyFile srcPath dstPath = MockWriteableMonad $ do
|
|
|
|
modifyLogs (copyFile_addCall srcPath dstPath)
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
getTemplateFileAbsPath path = MockWriteableMonad $ do
|
|
|
|
modifyLogs (getTemplateFileAbsPath_addCall path)
|
|
|
|
(_, config) <- get
|
|
|
|
return $ getTemplateFileAbsPath_impl config path
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
compileAndRenderTemplate path json = MockWriteableMonad $ do
|
|
|
|
modifyLogs (compileAndRenderTemplate_addCall path json)
|
|
|
|
(_, config) <- get
|
|
|
|
return $ compileAndRenderTemplate_impl config path json
|
2021-04-16 13:43:57 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
doesFileExist path = MockWriteableMonad $ do
|
|
|
|
(_, config) <- get
|
|
|
|
return $ doesFileExist_impl config path
|
2021-04-16 13:43:57 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
throwIO = throwIO
|
2021-04-16 13:43:57 +03:00
|
|
|
|
|
|
|
instance MonadIO MockWriteableMonad where
|
2021-04-28 18:36:00 +03:00
|
|
|
liftIO = undefined
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2019-04-22 13:58:31 +03:00
|
|
|
modifyLogs :: MonadState (a, b) m => (a -> a) -> m ()
|
2019-04-11 00:15:52 +03:00
|
|
|
modifyLogs f = modify (\(logs, config) -> (f logs, config))
|
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
newtype MockWriteableMonad a = MockWriteableMonad
|
2021-04-28 18:36:00 +03:00
|
|
|
{ unMockWriteableMonad :: State (MockWriteableMonadLogs, MockWriteableMonadConfig) a
|
|
|
|
}
|
|
|
|
deriving (Monad, Applicative, Functor)
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
data MockWriteableMonadLogs = MockWriteableMonadLogs
|
2021-04-28 18:36:00 +03:00
|
|
|
{ writeFileFromText_calls :: [(FilePath, Text)],
|
|
|
|
getTemplatesDirAbsPath_calls :: [()],
|
|
|
|
createDirectoryIfMissing_calls :: [(Bool, FilePath)],
|
|
|
|
copyFile_calls :: [(FilePath, FilePath)],
|
2021-07-03 12:00:01 +03:00
|
|
|
getTemplateFileAbsPath_calls :: [(Path' (Rel TemplatesDir) File')],
|
|
|
|
compileAndRenderTemplate_calls :: [(Path' (Rel TemplatesDir) File', Aeson.Value)]
|
2021-04-28 18:36:00 +03:00
|
|
|
}
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
data MockWriteableMonadConfig = MockWriteableMonadConfig
|
2021-07-03 12:00:01 +03:00
|
|
|
{ getTemplatesDirAbsPath_impl :: Path' Abs (Dir TemplatesDir),
|
|
|
|
getTemplateFileAbsPath_impl :: Path' (Rel TemplatesDir) File' -> Path' Abs File',
|
|
|
|
compileAndRenderTemplate_impl :: Path' (Rel TemplatesDir) File' -> Aeson.Value -> Text,
|
2021-04-28 18:36:00 +03:00
|
|
|
doesFileExist_impl :: FilePath -> Bool
|
|
|
|
}
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
writeFileFromText_addCall :: FilePath -> Text -> MockWriteableMonadLogs -> MockWriteableMonadLogs
|
2019-04-11 00:15:52 +03:00
|
|
|
writeFileFromText_addCall path text logs =
|
2021-04-28 18:36:00 +03:00
|
|
|
logs {writeFileFromText_calls = (path, text) : (writeFileFromText_calls logs)}
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
getTemplatesDirAbsPath_addCall :: MockWriteableMonadLogs -> MockWriteableMonadLogs
|
2019-04-11 00:15:52 +03:00
|
|
|
getTemplatesDirAbsPath_addCall logs =
|
2021-04-28 18:36:00 +03:00
|
|
|
logs {getTemplatesDirAbsPath_calls = () : (getTemplatesDirAbsPath_calls logs)}
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
getTemplateFileAbsPath_addCall :: Path' (Rel TemplatesDir) File' -> MockWriteableMonadLogs -> MockWriteableMonadLogs
|
2019-04-11 00:15:52 +03:00
|
|
|
getTemplateFileAbsPath_addCall path logs =
|
2021-04-28 18:36:00 +03:00
|
|
|
logs {getTemplateFileAbsPath_calls = (path) : (getTemplateFileAbsPath_calls logs)}
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
copyFile_addCall :: FilePath -> FilePath -> MockWriteableMonadLogs -> MockWriteableMonadLogs
|
2019-04-11 00:15:52 +03:00
|
|
|
copyFile_addCall srcPath dstPath logs =
|
2021-04-28 18:36:00 +03:00
|
|
|
logs {copyFile_calls = (srcPath, dstPath) : (copyFile_calls logs)}
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2020-01-15 15:08:13 +03:00
|
|
|
createDirectoryIfMissing_addCall :: Bool -> FilePath -> MockWriteableMonadLogs -> MockWriteableMonadLogs
|
2019-04-11 00:15:52 +03:00
|
|
|
createDirectoryIfMissing_addCall createParents path logs =
|
2021-04-28 18:36:00 +03:00
|
|
|
logs
|
|
|
|
{ createDirectoryIfMissing_calls =
|
|
|
|
(createParents, path) : (createDirectoryIfMissing_calls logs)
|
|
|
|
}
|
2019-04-11 00:15:52 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
compileAndRenderTemplate_addCall ::
|
2021-07-03 12:00:01 +03:00
|
|
|
Path' (Rel TemplatesDir) File' ->
|
2021-04-28 18:36:00 +03:00
|
|
|
Aeson.Value ->
|
|
|
|
MockWriteableMonadLogs ->
|
|
|
|
MockWriteableMonadLogs
|
2019-04-11 00:15:52 +03:00
|
|
|
compileAndRenderTemplate_addCall path json logs =
|
2021-04-28 18:36:00 +03:00
|
|
|
logs
|
|
|
|
{ compileAndRenderTemplate_calls =
|
|
|
|
(path, json) : (compileAndRenderTemplate_calls logs)
|
|
|
|
}
|