on master help sign in
Resource = Identifier with an exists invariant
commit34257df262521e4031c5e19acad3e9ce060c488b authorJasper Van der Jeugt <jaspervdj@gmail.com> dateFri 11 Feb 2011, 23:26 (16 years ago) parent2b9858a
5 files changed+40−27
src/Hakyll/Core/Compiler.hs+5−7 src/Hakyll/Core/ResourceProvider.hs+23−12 src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs+6−3 src/Hakyll/Core/Rules.hs+3−3 src/Hakyll/Core/Run.hs+3−2
src/Hakyll/Core/Compiler.hs+5−7
@@ -88,11 +88,9 @@ getRouteFor = fromJob $ \identifier -> CompilerM $ do
8888-- | Get the resource we are compiling as a string
8989--
9090getResourceString :: Compiler Resource String
91−getResourceString = getIdentifier >>> getResourceString'
92− where
93− getResourceString' = fromJob $ \id' -> CompilerM $ do
94− provider <- compilerResourceProvider <$> ask
95− liftIO $ resourceString provider id'
91+getResourceString = fromJob $ \resource -> CompilerM $ do
92+ provider <- compilerResourceProvider <$> ask
93+ liftIO $ resourceString provider resource
9694
9795-- | Auxiliary: get a dependency
9896--
@@ -141,7 +139,7 @@ requireAll_ :: (Binary a, Typeable a, Writable a)
141139 -> Compiler b [a]
142140requireAll_ pattern = fromDependencies getDeps >>> fromJob requireAll_'
143141 where
144− getDeps = matches pattern . resourceList
142+ getDeps = matches pattern . map unResource . resourceList
145143 requireAll_' = const $ CompilerM $ do
146144 deps <- getDeps . compilerResourceProvider <$> ask
147145 mapM (unCompilerM . getDependency) deps
@@ -174,7 +172,7 @@ cached name (Compiler d j) = Compiler d $ const $ CompilerM $ do
174172 liftIO $ putStrLn $
175173 show identifier ++ ": " ++ if modified then "MODIFIED" else "OK"
176174 if modified
177− then do v <- unCompilerM $ j Resource
175+ then do v <- unCompilerM $ j $ Resource identifier
178176 liftIO $ storeSet store name identifier v
179177 return v
180178 else do v <- liftIO $ storeGet store name identifier
src/Hakyll/Core/ResourceProvider.hs+23−12
@@ -2,6 +2,14 @@
22-- allow Hakyll to get content from resources; the type of resource depends on
33-- the concrete instance.
44--
5+-- A resource is represented by the 'Resource' type. This is basically just a
6+-- newtype wrapper around 'Identifier' -- but it has an important effect: it
7+-- guarantees that a resource with this identifier can be provided by one or
8+-- more resource providers.
9+--
10+-- Therefore, it is not recommended to read files directly -- you should use the
11+-- provided 'Resource' methods.
12+--
513module Hakyll.Core.ResourceProvider
614 ( Resource (..)
715 , ResourceProvider (..)
@@ -22,43 +30,46 @@ import Hakyll.Core.Store
2230
2331-- | A resource
2432--
25−data Resource = Resource
33+-- Invariant: the resource specified by the given identifier must exist
34+--
35+newtype Resource = Resource {unResource :: Identifier}
36+ deriving (Eq, Show, Ord)
2637
2738-- | A value responsible for retrieving and listing resources
2839--
2940data ResourceProvider = ResourceProvider
3041 { -- | A list of all resources this provider is able to provide
31− resourceList :: [Identifier]
42+ resourceList :: [Resource]
3243 , -- | Retrieve a certain resource as string
33− resourceString :: Identifier -> IO String
44+ resourceString :: Resource -> IO String
3445 , -- | Retrieve a certain resource as lazy bytestring
35− resourceLazyByteString :: Identifier -> IO LB.ByteString
46+ resourceLazyByteString :: Resource -> IO LB.ByteString
3647 }
3748
38−-- | Check if a given resource exists
49+-- | Check if a given identifier has a resource
3950--
4051resourceExists :: ResourceProvider -> Identifier -> Bool
41−resourceExists provider = flip elem $ resourceList provider
52+resourceExists provider = flip elem $ map unResource $ resourceList provider
4253
4354-- | Retrieve a digest for a given resource
4455--
45−resourceDigest :: ResourceProvider -> Identifier -> IO [Word8]
56+resourceDigest :: ResourceProvider -> Resource -> IO [Word8]
4657resourceDigest provider = digest MD5 <=< resourceLazyByteString provider
4758
4859-- | Check if a resource was modified
4960--
50−resourceModified :: ResourceProvider -> Identifier -> Store -> IO Bool
51−resourceModified provider identifier store = do
61+resourceModified :: ResourceProvider -> Resource -> Store -> IO Bool
62+resourceModified provider resource store = do
5263 -- Get the latest seen digest from the store
53− lastDigest <- storeGet store itemName identifier
64+ lastDigest <- storeGet store itemName $ unResource resource
5465 -- Calculate the digest for the resource
55− newDigest <- resourceDigest provider identifier
66+ newDigest <- resourceDigest provider resource
5667 -- Check digests
5768 if Just newDigest == lastDigest
5869 -- All is fine, not modified
5970 then return False
6071 -- Resource modified; store new digest
61− else do storeSet store itemName identifier newDigest
72+ else do storeSet store itemName (unResource resource) newDigest
6273 return True
6374 where
6475 itemName = "Hakyll.Core.ResourceProvider.resourceModified"
src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs+6−3
@@ -16,9 +16,12 @@ import Hakyll.Core.Util.File
1616--
1717fileResourceProvider :: IO ResourceProvider
1818fileResourceProvider = do
19+ -- Retrieve a list of identifiers
1920 list <- map parseIdentifier <$> getRecursiveContents False "."
21+
22+ -- Construct a resource provider
2023 return ResourceProvider
21− { resourceList = list
22− , resourceString = readFile . toFilePath
23− , resourceLazyByteString = LB.readFile . toFilePath
24+ { resourceList = map Resource list
25+ , resourceString = readFile . toFilePath . unResource
26+ , resourceLazyByteString = LB.readFile . toFilePath . unResource
2427 }
src/Hakyll/Core/Rules.hs+3−3
@@ -65,9 +65,9 @@ tellCompilers compilers = RulesM $ tell $ RuleSet mempty $
6565compile :: (Binary a, Typeable a, Writable a)
6666 => Pattern -> Compiler Resource a -> Rules
6767compile pattern compiler = RulesM $ do
68− identifiers <- matches pattern . resourceList <$> ask
69− unRulesM $ tellCompilers $ zip identifiers $ repeat $
70− constA Resource >>> compiler
68+ identifiers <- matches pattern . map unResource . resourceList <$> ask
69+ unRulesM $ tellCompilers $ flip map identifiers $ \identifier ->
70+ (identifier, constA (Resource identifier) >>> compiler)
7171
7272-- | Add a compilation rule
7373--
src/Hakyll/Core/Run.hs+3−2
@@ -82,8 +82,9 @@ modified :: ResourceProvider -- ^ Resource provider
8282 -> [Identifier] -- ^ Identifiers to check
8383 -> IO (Set Identifier) -- ^ Modified resources
8484modified provider store ids = fmap S.fromList $ flip filterM ids $ \id' ->
85− if resourceExists provider id' then resourceModified provider id' store
86− else return False
85+ if resourceExists provider id'
86+ then resourceModified provider (Resource id') store
87+ else return False
8788
8889-- | Add a number of compilers and continue using these compilers
8990--