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

View File

@ -22,7 +22,7 @@ import Ema.Route.Encoder (
applyRouteEncoder,
checkRouteEncoderGivenRoute,
)
import Ema.Site (EmaSite (siteOutput))
import Ema.Site (EmaSite (siteOutput), EmaStaticSite)
import Optics.Core (review)
import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist, doesFileExist, doesPathExist)
import System.FilePath (takeDirectory, (</>))
@ -37,7 +37,7 @@ log = logWithoutLoc "ema.generate"
-}
generateSiteFromModel ::
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.
FilePath ->
-- | The model data used to generate assets.
@ -61,7 +61,7 @@ generateSiteFromModel dest model =
-- | Like `generateSiteFromModel` but without buffering or error handling.
generateSiteFromModel' ::
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 ->
RouteModel r ->
-- | List of generated files.

View File

@ -6,7 +6,7 @@ module Ema.Route.Lib.Folder (
import Data.Text qualified as T
import Ema.Route.Class (IsRoute (..))
import Ema.Route.Encoder (RouteEncoder, mapRouteEncoder)
import Ema.Site (EmaSite (..))
import Ema.Site (EmaSite (..), EmaStaticSite)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import Optics.Core (coercedTo, prism', (%))
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
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
siteInput cliAct =
siteInput @r cliAct

View File

@ -13,7 +13,7 @@ module Ema.Route.Lib.Multi (
import Data.SOP (I (..), NP (..), NS (..))
import Ema.Route.Class (IsRoute (..))
import Ema.Route.Encoder
import Ema.Site (EmaSite (..))
import Ema.Site (EmaSite (..), EmaStaticSite)
import Optics.Core (equality, iso, prism', (%))
{- | 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 {}
instance
( EmaSite r
, EmaSite (MultiRoute rs)
( EmaStaticSite r
, EmaStaticSite (MultiRoute rs)
, SiteArg (MultiRoute rs) ~ NP I (MultiSiteArg rs)
, RouteModel (MultiRoute rs) ~ NP I (MultiModel rs)
) =>

View File

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

View File

@ -2,6 +2,7 @@
module Ema.Site (
EmaSite (..),
EmaStaticSite,
) where
import Control.Monad.Logger (MonadLoggerIO)
@ -42,6 +43,9 @@ class IsRoute r => EmaSite r where
-- By default Ema sites have no site arguments.
type SiteArg r = ()
type SiteOutput r :: Type
type SiteOutput r = Asset LByteString
{- Get the model's time-varying value as a `Dynamic`.
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))
-- | 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)