on master help sign in
Just messing around
commitede51e822f558a2c59d4ab76401b597f8acf59b4 authorJasper Van der Jeugt <m@jaspervdj.be> dateMon 12 Nov 2012, 12:23 (14 years ago) parent86ede74
3 files changed+111−15
src/Hakyll/Core/Dependencies.hs+59 src/Hakyll/Core/Identifier.hs+37−13 src/Hakyll/Core/Identifier/Pattern.hs+15−2
src/Hakyll/Core/Dependencies.hsnew file+59
@@ -0,0 +1,59 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE BangPatterns #-}
3+module Hakyll.Core.Dependencies
4+ (
5+ ) where
6+
7+
8+--------------------------------------------------------------------------------
9+import Data.List (foldl')
10+import Data.Map (Map)
11+import qualified Data.Map as M
12+import Data.Set (Set)
13+import qualified Data.Set as S
14+
15+
16+--------------------------------------------------------------------------------
17+import Hakyll.Core.Identifier
18+import Hakyll.Core.Identifier.Pattern
19+
20+
21+--------------------------------------------------------------------------------
22+data Dependency
23+ = Pattern (Pattern ()) [Identifier ()]
24+ | Identifier (Identifier ())
25+ deriving (Show)
26+
27+
28+--------------------------------------------------------------------------------
29+type DependencyFacts = Map (Identifier ()) [Dependency]
30+
31+
32+--------------------------------------------------------------------------------
33+outOfDate
34+ :: [Identifier ()] -- ^ All known identifiers
35+ -> Set (Identifier ()) -- ^ Initially out-of-date resources
36+ -> DependencyFacts -- ^ Old dependency facts
37+ -> (Set (Identifier ()), DependencyFacts)
38+outOfDate universe ood oldFacts = (ood, oldFacts)
39+
40+
41+--------------------------------------------------------------------------------
42+-- | Determine patterns with changed results
43+changedPatterns
44+ :: [Identifier ()]
45+ -> DependencyFacts
46+ -> (Set (Identifier ()), DependencyFacts)
47+changedPatterns universe facts =
48+ M.foldlWithKey' changed (S.empty, facts) facts
49+ where
50+ changed (!o, !f) id' deps =
51+ let (o', deps') = foldr (changed' id') (o, []) deps
52+ in (o', M.insert id' deps' f)
53+
54+ changed' _ (Identifier i) (o, d) = (o, Identifier i : d)
55+ changed' id' (Pattern p ls) (o, d)
56+ | ls == ls' = (o, Pattern p ls : d)
57+ | otherwise = (S.insert id' o, Pattern p ls' : d)
58+ where
59+ ls' = filterMatches p universe
src/Hakyll/Core/Identifier.hs+37−13
@@ -36,30 +36,36 @@ module Hakyll.Core.Identifier
3636 , fromFilePath
3737 , toFilePath
3838 , castIdentifier
39+ , identifierVersion
40+ , setVersion
3941 ) where
4042
4143
4244--------------------------------------------------------------------------------
43−import Control.DeepSeq (NFData)
44−import Data.List (intercalate)
45−import System.FilePath (dropTrailingPathSeparator, splitPath)
45+import Control.Applicative ((<$>), (<*>))
46+import Control.DeepSeq (NFData (..))
47+import Data.List (intercalate)
48+import System.FilePath (dropTrailingPathSeparator, splitPath)
4649
4750
4851--------------------------------------------------------------------------------
49−import Data.Binary (Binary)
50−import Data.Typeable (Typeable)
51−import GHC.Exts (IsString, fromString)
52+import Data.Binary (Binary (..))
53+import Data.Typeable (Typeable)
54+import GHC.Exts (IsString, fromString)
5255
5356
5457--------------------------------------------------------------------------------
5558-- | An identifier used to uniquely identify a value
56−newtype Identifier a = Identifier {unIdentifier :: String}
57− deriving (Binary, Eq, NFData, Ord, Typeable)
59+data Identifier a = Identifier
60+ { identifierVersion :: Maybe String
61+ , identifierPath :: String
62+ } deriving (Eq, Ord, Typeable)
5863
5964
6065--------------------------------------------------------------------------------
61−instance Show (Identifier a) where
62− show = toFilePath
66+instance Binary (Identifier a) where
67+ put (Identifier v p) = put v >> put p
68+ get = Identifier <$> get <*> get
6369
6470
6571--------------------------------------------------------------------------------
@@ -67,10 +73,23 @@ instance IsString (Identifier a) where
6773 fromString = fromFilePath
6874
6975
76+--------------------------------------------------------------------------------
77+instance NFData (Identifier a) where
78+ rnf (Identifier v p) = rnf v `seq` rnf p `seq` ()
79+
80+
81+--------------------------------------------------------------------------------
82+instance Show (Identifier a) where
83+ show i = case identifierVersion i of
84+ Nothing -> toFilePath i
85+ Just v -> toFilePath i ++ " (" ++ v ++ ")"
86+
87+
7088--------------------------------------------------------------------------------
7189-- | Parse an identifier from a string
7290fromFilePath :: String -> Identifier a
73−fromFilePath = Identifier . intercalate "/" . filter (not . null) . split'
91+fromFilePath = Identifier Nothing .
92+ intercalate "/" . filter (not . null) . split'
7493 where
7594 split' = map dropTrailingPathSeparator . splitPath
7695
@@ -78,11 +97,16 @@ fromFilePath = Identifier . intercalate "/" . filter (not . null) . split'
7897--------------------------------------------------------------------------------
7998-- | Convert an identifier to a relative 'FilePath'
8099toFilePath :: Identifier a -> FilePath
81−toFilePath = unIdentifier
100+toFilePath = identifierPath
82101
83102
84103--------------------------------------------------------------------------------
85104-- | Discard the phantom type parameter of an identifier
86105castIdentifier :: Identifier a -> Identifier b
87−castIdentifier (Identifier x) = Identifier x
106+castIdentifier (Identifier v p) = Identifier v p
88107{-# INLINE castIdentifier #-}
108+
109+
110+--------------------------------------------------------------------------------
111+setVersion :: Maybe String -> Identifier a -> Identifier a
112+setVersion v i = i {identifierVersion = v}
src/Hakyll/Core/Identifier/Pattern.hs+15−2
@@ -46,6 +46,7 @@ module Hakyll.Core.Identifier.Pattern
4646
4747 -- * Manipulating patterns
4848 , complement
49+ , withVersion
4950 , castPattern
5051
5152 -- * Applying patterns
@@ -109,6 +110,7 @@ data Pattern a
109110 | Glob [GlobComponent]
110111 | List [Identifier a] -- TODO Maybe use a set here
111112 | Regex String
113+ | Version (Maybe String)
112114 deriving (Show)
113115
114116
@@ -120,6 +122,7 @@ instance Binary (Pattern a) where
120122 put (Glob g) = putWord8 3 >> put g
121123 put (List is) = putWord8 4 >> put is
122124 put (Regex r) = putWord8 5 >> put r
125+ put (Version v) = putWord8 6 >> put v
123126
124127 get = getWord8 >>= \t -> case t of
125128 0 -> pure Everything
@@ -127,7 +130,8 @@ instance Binary (Pattern a) where
127130 2 -> And <$> get <*> get
128131 3 -> Glob <$> get
129132 4 -> List <$> get
130− _ -> Regex <$> get
133+ 5 -> Regex <$> get
134+ _ -> Version <$> get
131135
132136
133137--------------------------------------------------------------------------------
@@ -177,11 +181,18 @@ fromRegex = Regex
177181-- > complement "foo/bar.html"
178182--
179183-- will match /anything/ except @\"foo\/bar.html\"@
180−--
181184complement :: Pattern a -> Pattern a
182185complement = Complement
183186
184187
188+--------------------------------------------------------------------------------
189+-- | Specify a version, e.g.
190+--
191+-- > "foo/*.markdown" `withVersion` "pdf"
192+withVersion :: Pattern a -> String -> Pattern a
193+withVersion p v = And p $ Version $ Just v
194+
195+
185196--------------------------------------------------------------------------------
186197-- | Discard the phantom type parameter
187198castPattern :: Pattern a -> Pattern b
@@ -191,6 +202,7 @@ castPattern (And x y) = And (castPattern x) (castPattern y)
191202castPattern (Glob g) = Glob g
192203castPattern (List l) = List $ map castIdentifier l
193204castPattern (Regex r) = Regex r
205+castPattern (Version v) = Version v
194206
195207
196208--------------------------------------------------------------------------------
@@ -202,6 +214,7 @@ matches (And x y) i = matches x i && matches y i
202214matches (Glob p) i = isJust $ capture (Glob p) i
203215matches (List l) i = i `elem` l
204216matches (Regex r) i = toFilePath i =~ r
217+matches (Version v) i = identifierVersion i == v
205218
206219
207220--------------------------------------------------------------------------------