mirror of
https://github.com/srid/ema.git
synced 2024-11-29 09:25:14 +03:00
Support static files outside source directory (#39)
* Try to fix it, WIP * docsx
This commit is contained in:
parent
ccd3649319
commit
feb58f5129
@ -9,6 +9,7 @@
|
||||
- Drop `staticAssets` in favour of `allRoutes` (renamed from `staticRoutes`) returning all routes including both generated and static routes.
|
||||
- Drop `Slug` and use plain `FilePath`. Route encoder and decoder deal directly with the on-disk path of the generated (or static) files.
|
||||
- Make the render function (which `runEma` takes) return a `Asset LByteString` instead of `LByteString` such that it can handle all routes, and handle static files as well as generation of non-HTML content (eg: RSS)
|
||||
- Allow copying static files anywhere on the filesystem
|
||||
- `routeUrl`:
|
||||
- Unicode normalize as well URI encode route URLs
|
||||
- now returns relative URLs (ie. without the leading `/`)
|
||||
|
@ -6,6 +6,9 @@ module Ema.Asset where
|
||||
-- | The type of assets that can be bundled in a static site.
|
||||
data Asset a
|
||||
= -- | A file that is copied as-is from the source directory.
|
||||
--
|
||||
-- Relative paths are assumed relative to the source directory. Absolute
|
||||
-- paths allow copying static files outside of source directory.
|
||||
AssetStatic FilePath
|
||||
| -- | A file whose contents are generated at runtime by user code.
|
||||
AssetGenerated Format a
|
||||
|
@ -45,12 +45,12 @@ generate dest model render = do
|
||||
liftIO $ do
|
||||
createDirectoryIfMissing True (takeDirectory fp)
|
||||
writeFileLBS fp s
|
||||
forM_ staticPaths $ \(_, staticPath) -> do
|
||||
forM_ staticPaths $ \(r, staticPath) -> do
|
||||
liftIO (doesPathExist staticPath) >>= \case
|
||||
True ->
|
||||
-- TODO: In current branch, we don't expect this to be a directory.
|
||||
-- Although the user may pass it, but review before merge.
|
||||
copyDirRecursively staticPath dest
|
||||
copyDirRecursively (encodeRoute r) staticPath dest
|
||||
False ->
|
||||
log LevelWarn $ toText $ "? " <> staticPath <> " (missing)"
|
||||
|
||||
@ -63,25 +63,27 @@ copyDirRecursively ::
|
||||
MonadLoggerIO m,
|
||||
HasCallStack
|
||||
) =>
|
||||
-- | Source file or directory relative to CWD that will be copied
|
||||
-- | Source file path relative to CWD
|
||||
FilePath ->
|
||||
-- | Absolute path to source file to copy.
|
||||
FilePath ->
|
||||
-- | Directory *under* which the source file/dir will be copied
|
||||
FilePath ->
|
||||
m ()
|
||||
copyDirRecursively srcRel destParent =
|
||||
liftIO (doesFileExist srcRel) >>= \case
|
||||
copyDirRecursively srcRel srcAbs destParent = do
|
||||
liftIO (doesFileExist srcAbs) >>= \case
|
||||
True -> do
|
||||
let b = destParent </> srcRel
|
||||
log LevelInfo $ toText $ "C " <> b
|
||||
copyFileCreatingParents srcRel b
|
||||
copyFileCreatingParents srcAbs b
|
||||
False ->
|
||||
liftIO (doesDirectoryExist srcRel) >>= \case
|
||||
liftIO (doesDirectoryExist srcAbs) >>= \case
|
||||
False ->
|
||||
throw $ StaticAssetMissing srcRel
|
||||
throw $ StaticAssetMissing srcAbs
|
||||
True -> do
|
||||
fs <- liftIO $ getDirectoryFiles srcRel ["**"]
|
||||
fs <- liftIO $ getDirectoryFiles srcAbs ["**"]
|
||||
forM_ fs $ \fp -> do
|
||||
let a = srcRel </> fp
|
||||
let a = srcAbs </> fp
|
||||
b = destParent </> srcRel </> fp
|
||||
log LevelInfo $ toText $ "C " <> b
|
||||
copyFileCreatingParents a b
|
||||
|
Loading…
Reference in New Issue
Block a user