summaryrefslogtreecommitdiff
path: root/src/Hakyll
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2011-02-11 23:26:54 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2011-02-11 23:26:54 +0100
commit34257df262521e4031c5e19acad3e9ce060c488b (patch)
tree9316ed7bf401bc0a75b5a832890aca0f213cea06 /src/Hakyll
parent2b9858a8f9212219718625b7c5891bcb11cbaefb (diff)
downloadhakyll-34257df262521e4031c5e19acad3e9ce060c488b.tar.gz
Resource = Identifier with an exists invariant
Diffstat (limited to 'src/Hakyll')
-rw-r--r--src/Hakyll/Core/Compiler.hs12
-rw-r--r--src/Hakyll/Core/ResourceProvider.hs35
-rw-r--r--src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs9
-rw-r--r--src/Hakyll/Core/Rules.hs6
-rw-r--r--src/Hakyll/Core/Run.hs5
5 files changed, 40 insertions, 27 deletions
diff --git a/src/Hakyll/Core/Compiler.hs b/src/Hakyll/Core/Compiler.hs
index bbb5737..056ef32 100644
--- a/src/Hakyll/Core/Compiler.hs
+++ b/src/Hakyll/Core/Compiler.hs
@@ -88,11 +88,9 @@ getRouteFor = fromJob $ \identifier -> CompilerM $ do
-- | Get the resource we are compiling as a string
--
getResourceString :: Compiler Resource String
-getResourceString = getIdentifier >>> getResourceString'
- where
- getResourceString' = fromJob $ \id' -> CompilerM $ do
- provider <- compilerResourceProvider <$> ask
- liftIO $ resourceString provider id'
+getResourceString = fromJob $ \resource -> CompilerM $ do
+ provider <- compilerResourceProvider <$> ask
+ liftIO $ resourceString provider resource
-- | Auxiliary: get a dependency
--
@@ -141,7 +139,7 @@ requireAll_ :: (Binary a, Typeable a, Writable a)
-> Compiler b [a]
requireAll_ pattern = fromDependencies getDeps >>> fromJob requireAll_'
where
- getDeps = matches pattern . resourceList
+ getDeps = matches pattern . map unResource . resourceList
requireAll_' = const $ CompilerM $ do
deps <- getDeps . compilerResourceProvider <$> ask
mapM (unCompilerM . getDependency) deps
@@ -174,7 +172,7 @@ cached name (Compiler d j) = Compiler d $ const $ CompilerM $ do
liftIO $ putStrLn $
show identifier ++ ": " ++ if modified then "MODIFIED" else "OK"
if modified
- then do v <- unCompilerM $ j Resource
+ then do v <- unCompilerM $ j $ Resource identifier
liftIO $ storeSet store name identifier v
return v
else do v <- liftIO $ storeGet store name identifier
diff --git a/src/Hakyll/Core/ResourceProvider.hs b/src/Hakyll/Core/ResourceProvider.hs
index 980f001..dcd4af0 100644
--- a/src/Hakyll/Core/ResourceProvider.hs
+++ b/src/Hakyll/Core/ResourceProvider.hs
@@ -2,6 +2,14 @@
-- allow Hakyll to get content from resources; the type of resource depends on
-- the concrete instance.
--
+-- A resource is represented by the 'Resource' type. This is basically just a
+-- newtype wrapper around 'Identifier' -- but it has an important effect: it
+-- guarantees that a resource with this identifier can be provided by one or
+-- more resource providers.
+--
+-- Therefore, it is not recommended to read files directly -- you should use the
+-- provided 'Resource' methods.
+--
module Hakyll.Core.ResourceProvider
( Resource (..)
, ResourceProvider (..)
@@ -22,43 +30,46 @@ import Hakyll.Core.Store
-- | A resource
--
-data Resource = Resource
+-- Invariant: the resource specified by the given identifier must exist
+--
+newtype Resource = Resource {unResource :: Identifier}
+ deriving (Eq, Show, Ord)
-- | A value responsible for retrieving and listing resources
--
data ResourceProvider = ResourceProvider
{ -- | A list of all resources this provider is able to provide
- resourceList :: [Identifier]
+ resourceList :: [Resource]
, -- | Retrieve a certain resource as string
- resourceString :: Identifier -> IO String
+ resourceString :: Resource -> IO String
, -- | Retrieve a certain resource as lazy bytestring
- resourceLazyByteString :: Identifier -> IO LB.ByteString
+ resourceLazyByteString :: Resource -> IO LB.ByteString
}
--- | Check if a given resource exists
+-- | Check if a given identifier has a resource
--
resourceExists :: ResourceProvider -> Identifier -> Bool
-resourceExists provider = flip elem $ resourceList provider
+resourceExists provider = flip elem $ map unResource $ resourceList provider
-- | Retrieve a digest for a given resource
--
-resourceDigest :: ResourceProvider -> Identifier -> IO [Word8]
+resourceDigest :: ResourceProvider -> Resource -> IO [Word8]
resourceDigest provider = digest MD5 <=< resourceLazyByteString provider
-- | Check if a resource was modified
--
-resourceModified :: ResourceProvider -> Identifier -> Store -> IO Bool
-resourceModified provider identifier store = do
+resourceModified :: ResourceProvider -> Resource -> Store -> IO Bool
+resourceModified provider resource store = do
-- Get the latest seen digest from the store
- lastDigest <- storeGet store itemName identifier
+ lastDigest <- storeGet store itemName $ unResource resource
-- Calculate the digest for the resource
- newDigest <- resourceDigest provider identifier
+ newDigest <- resourceDigest provider resource
-- Check digests
if Just newDigest == lastDigest
-- All is fine, not modified
then return False
-- Resource modified; store new digest
- else do storeSet store itemName identifier newDigest
+ else do storeSet store itemName (unResource resource) newDigest
return True
where
itemName = "Hakyll.Core.ResourceProvider.resourceModified"
diff --git a/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs b/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs
index 7343855..2f040b3 100644
--- a/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs
+++ b/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs
@@ -16,9 +16,12 @@ import Hakyll.Core.Util.File
--
fileResourceProvider :: IO ResourceProvider
fileResourceProvider = do
+ -- Retrieve a list of identifiers
list <- map parseIdentifier <$> getRecursiveContents False "."
+
+ -- Construct a resource provider
return ResourceProvider
- { resourceList = list
- , resourceString = readFile . toFilePath
- , resourceLazyByteString = LB.readFile . toFilePath
+ { resourceList = map Resource list
+ , resourceString = readFile . toFilePath . unResource
+ , resourceLazyByteString = LB.readFile . toFilePath . unResource
}
diff --git a/src/Hakyll/Core/Rules.hs b/src/Hakyll/Core/Rules.hs
index 78cbac7..137dc2c 100644
--- a/src/Hakyll/Core/Rules.hs
+++ b/src/Hakyll/Core/Rules.hs
@@ -65,9 +65,9 @@ tellCompilers compilers = RulesM $ tell $ RuleSet mempty $
compile :: (Binary a, Typeable a, Writable a)
=> Pattern -> Compiler Resource a -> Rules
compile pattern compiler = RulesM $ do
- identifiers <- matches pattern . resourceList <$> ask
- unRulesM $ tellCompilers $ zip identifiers $ repeat $
- constA Resource >>> compiler
+ identifiers <- matches pattern . map unResource . resourceList <$> ask
+ unRulesM $ tellCompilers $ flip map identifiers $ \identifier ->
+ (identifier, constA (Resource identifier) >>> compiler)
-- | Add a compilation rule
--
diff --git a/src/Hakyll/Core/Run.hs b/src/Hakyll/Core/Run.hs
index 7e6851f..2b0ff5d 100644
--- a/src/Hakyll/Core/Run.hs
+++ b/src/Hakyll/Core/Run.hs
@@ -82,8 +82,9 @@ modified :: ResourceProvider -- ^ Resource provider
-> [Identifier] -- ^ Identifiers to check
-> IO (Set Identifier) -- ^ Modified resources
modified provider store ids = fmap S.fromList $ flip filterM ids $ \id' ->
- if resourceExists provider id' then resourceModified provider id' store
- else return False
+ if resourceExists provider id'
+ then resourceModified provider (Resource id') store
+ else return False
-- | Add a number of compilers and continue using these compilers
--