1
1
mirror of https://github.com/srid/ema.git synced 2024-12-01 15:13:36 +03:00

Parametrize SiteOutput

So that non-Asset outputs are possible (for composition).
This commit is contained in:
Sridhar Ratnakumar 2022-07-03 13:50:09 -04:00
parent 83302865e1
commit 873c53fab9
6 changed files with 21 additions and 15 deletions

View File

@ -19,7 +19,7 @@ import Ema.Dynamic (Dynamic (Dynamic))
import Ema.Generate (generateSiteFromModel) import Ema.Generate (generateSiteFromModel)
import Ema.Route.Class (IsRoute (RouteModel)) import Ema.Route.Class (IsRoute (RouteModel))
import Ema.Server qualified as Server import Ema.Server qualified as Server
import Ema.Site (EmaSite (SiteArg, siteInput)) import Ema.Site (EmaSite (SiteArg, siteInput), EmaStaticSite)
import System.Directory (getCurrentDirectory) import System.Directory (getCurrentDirectory)
{- | Run the given Ema site, {- | Run the given Ema site,
@ -31,7 +31,7 @@ import System.Directory (getCurrentDirectory)
-} -}
runSite :: runSite ::
forall r. forall r.
(Show r, Eq r, EmaSite r) => (Show r, Eq r, EmaStaticSite r) =>
-- | The input required to create the `Dynamic` of the `RouteModel` -- | The input required to create the `Dynamic` of the `RouteModel`
SiteArg r -> SiteArg r ->
IO [FilePath] IO [FilePath]
@ -46,7 +46,7 @@ runSite input = do
pure fs pure fs
-- | Like @runSite@ but discards the result -- | Like @runSite@ but discards the result
runSite_ :: forall r. (Show r, Eq r, EmaSite r) => SiteArg r -> IO () runSite_ :: forall r. (Show r, Eq r, EmaStaticSite r) => SiteArg r -> IO ()
runSite_ = void . runSite @r runSite_ = void . runSite @r
{- | Like @runSite@ but takes the CLI action. Also returns more information. {- | Like @runSite@ but takes the CLI action. Also returns more information.
@ -57,7 +57,7 @@ runSite_ = void . runSite @r
-} -}
runSiteWithCli :: runSiteWithCli ::
forall r. forall r.
(Show r, Eq r, EmaSite r) => (Show r, Eq r, EmaStaticSite r) =>
CLI.Cli -> CLI.Cli ->
SiteArg r -> SiteArg r ->
IO IO

View File

@ -22,7 +22,7 @@ import Ema.Route.Encoder (
applyRouteEncoder, applyRouteEncoder,
checkRouteEncoderGivenRoute, checkRouteEncoderGivenRoute,
) )
import Ema.Site (EmaSite (siteOutput)) import Ema.Site (EmaSite (siteOutput), EmaStaticSite)
import Optics.Core (review) import Optics.Core (review)
import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist, doesFileExist, doesPathExist) import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist, doesFileExist, doesPathExist)
import System.FilePath (takeDirectory, (</>)) import System.FilePath (takeDirectory, (</>))
@ -37,7 +37,7 @@ log = logWithoutLoc "ema.generate"
-} -}
generateSiteFromModel :: generateSiteFromModel ::
forall r m. forall r m.
(MonadIO m, MonadLoggerIO m, MonadFail m, Eq r, Show r, IsRoute r, EmaSite r) => (MonadIO m, MonadLoggerIO m, MonadFail m, Eq r, Show r, IsRoute r, EmaStaticSite r) =>
-- | Target directory to write files to. Must exist. -- | Target directory to write files to. Must exist.
FilePath -> FilePath ->
-- | The model data used to generate assets. -- | The model data used to generate assets.
@ -61,7 +61,7 @@ generateSiteFromModel dest model =
-- | Like `generateSiteFromModel` but without buffering or error handling. -- | Like `generateSiteFromModel` but without buffering or error handling.
generateSiteFromModel' :: generateSiteFromModel' ::
forall r m. forall r m.
(MonadIO m, MonadLoggerIO m, MonadError Text m, Eq r, Show r, EmaSite r) => (MonadIO m, MonadLoggerIO m, MonadError Text m, Eq r, Show r, EmaStaticSite r) =>
FilePath -> FilePath ->
RouteModel r -> RouteModel r ->
-- | List of generated files. -- | List of generated files.

View File

@ -6,7 +6,7 @@ module Ema.Route.Lib.Folder (
import Data.Text qualified as T import Data.Text qualified as T
import Ema.Route.Class (IsRoute (..)) import Ema.Route.Class (IsRoute (..))
import Ema.Route.Encoder (RouteEncoder, mapRouteEncoder) import Ema.Route.Encoder (RouteEncoder, mapRouteEncoder)
import Ema.Site (EmaSite (..)) import Ema.Site (EmaSite (..), EmaStaticSite)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import Optics.Core (coercedTo, prism', (%)) import Optics.Core (coercedTo, prism', (%))
import System.FilePath ((</>)) import System.FilePath ((</>))
@ -24,7 +24,7 @@ instance (IsRoute r, KnownSymbol prefix) => IsRoute (FolderRoute prefix r) where
routeEncoder = prefixRouteEncoder @prefix @r @(RouteModel r) $ routeEncoder @r routeEncoder = prefixRouteEncoder @prefix @r @(RouteModel r) $ routeEncoder @r
allRoutes m = FolderRoute <$> allRoutes @r m allRoutes m = FolderRoute <$> allRoutes @r m
instance (EmaSite r, KnownSymbol prefix) => EmaSite (FolderRoute prefix r) where instance (EmaStaticSite r, KnownSymbol prefix) => EmaSite (FolderRoute prefix r) where
type SiteArg (FolderRoute prefix r) = SiteArg r type SiteArg (FolderRoute prefix r) = SiteArg r
siteInput cliAct = siteInput cliAct =
siteInput @r cliAct siteInput @r cliAct

View File

@ -13,7 +13,7 @@ module Ema.Route.Lib.Multi (
import Data.SOP (I (..), NP (..), NS (..)) import Data.SOP (I (..), NP (..), NS (..))
import Ema.Route.Class (IsRoute (..)) import Ema.Route.Class (IsRoute (..))
import Ema.Route.Encoder import Ema.Route.Encoder
import Ema.Site (EmaSite (..)) import Ema.Site (EmaSite (..), EmaStaticSite)
import Optics.Core (equality, iso, prism', (%)) import Optics.Core (equality, iso, prism', (%))
{- | The merged site's route is represented as a n-ary sum (`NS`) of the {- | The merged site's route is represented as a n-ary sum (`NS`) of the
@ -59,8 +59,8 @@ instance EmaSite (MultiRoute '[]) where
siteOutput _ Nil = \case {} siteOutput _ Nil = \case {}
instance instance
( EmaSite r ( EmaStaticSite r
, EmaSite (MultiRoute rs) , EmaStaticSite (MultiRoute rs)
, SiteArg (MultiRoute rs) ~ NP I (MultiSiteArg rs) , SiteArg (MultiRoute rs) ~ NP I (MultiSiteArg rs)
, RouteModel (MultiRoute rs) ~ NP I (MultiModel rs) , RouteModel (MultiRoute rs) ~ NP I (MultiModel rs)
) => ) =>

View File

@ -21,7 +21,7 @@ import Ema.Route.Encoder (
checkRouteEncoderGivenFilePath, checkRouteEncoderGivenFilePath,
) )
import Ema.Route.Url (urlToFilePath) import Ema.Route.Url (urlToFilePath)
import Ema.Site (EmaSite (siteOutput)) import Ema.Site (EmaSite (siteOutput), EmaStaticSite)
import GHC.IO.Unsafe (unsafePerformIO) import GHC.IO.Unsafe (unsafePerformIO)
import NeatInterpolation (text) import NeatInterpolation (text)
import Network.HTTP.Types qualified as H import Network.HTTP.Types qualified as H
@ -45,7 +45,7 @@ runServerWithWebSocketHotReload ::
, MonadLoggerIO m , MonadLoggerIO m
, Eq r , Eq r
, IsRoute r , IsRoute r
, EmaSite r , EmaStaticSite r
) => ) =>
Host -> Host ->
Maybe Port -> Maybe Port ->

View File

@ -2,6 +2,7 @@
module Ema.Site ( module Ema.Site (
EmaSite (..), EmaSite (..),
EmaStaticSite,
) where ) where
import Control.Monad.Logger (MonadLoggerIO) import Control.Monad.Logger (MonadLoggerIO)
@ -42,6 +43,9 @@ class IsRoute r => EmaSite r where
-- By default Ema sites have no site arguments. -- By default Ema sites have no site arguments.
type SiteArg r = () type SiteArg r = ()
type SiteOutput r :: Type
type SiteOutput r = Asset LByteString
{- Get the model's time-varying value as a `Dynamic`. {- Get the model's time-varying value as a `Dynamic`.
If your model is not time-varying, use `pure` to produce a constant value. If your model is not time-varying, use `pure` to produce a constant value.
@ -59,4 +63,6 @@ class IsRoute r => EmaSite r where
m (Dynamic m (RouteModel r)) m (Dynamic m (RouteModel r))
-- | Return the generated asset for the given route and model. -- | Return the generated asset for the given route and model.
siteOutput :: Prism' FilePath r -> RouteModel r -> r -> Asset LByteString siteOutput :: Prism' FilePath r -> RouteModel r -> r -> SiteOutput r
type EmaStaticSite r = (EmaSite r, SiteOutput r ~ Asset LByteString)