module Cake.Core (
Rule,
P, (==>),
Act,
cake,
need,
list,
produce,
produce',
use,
overwrote,
debug,
distill,
fileStamp,
cut,
shielded,
Question(..),
Answer(..),
module Control.Applicative,
) where
import Data.Digest.Pure.MD5
import qualified Data.ByteString.Lazy as B
import System.Directory
import System.FilePath
import Control.Applicative
import Control.Monad (when)
import Control.Monad.RWS hiding (put,get)
import qualified Control.Monad.RWS as RWS
import qualified Parsek
import Parsek (completeResults, parse, Parser)
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Binary hiding (put,get)
import System.Exit
import Control.Arrow (second,first)
import Data.DeriveTH
import Data.Binary
import System.IO
data Question = FileContents FilePath
| Listing FilePath String
| Custom [String]
deriving (Eq, Ord)
$( derive makeBinary ''Question )
data Answer = Stamp (Maybe MD5Digest)
| Text [String]
deriving (Eq, Show)
$( derive makeBinary ''Answer )
instance Show Question where
show (FileContents f) = "{"++f++"}"
show (Listing f ext) = "("++f++"/*"++ext++")"
show (Custom c) = show c
type DB = M.Map Question Answer
type Produced = S.Set FilePath
type P = Parser Char
type Rule = P (Act ())
type State = (Produced,Status)
type Written = Dual DB
data Context = Context {ctxHandle :: Handle, ctxRule :: Rule, ctxDB :: DB, ctxProducing :: [Question]}
newtype Act a = Act (RWST Context Written State IO a)
deriving (Functor, Applicative, Monad, MonadIO, MonadState State, MonadWriter Written, MonadReader Context)
data Status = Clean | Dirty
deriving Eq
instance Applicative P where
(<*>) = ap
pure = return
instance Alternative P where
(<|>) = (Parsek.<|>)
empty = Parsek.pzero
(==>) :: P x -> (x -> Act a) -> Rule
p ==> a = (\s -> do a s;return ()) <$> p
databaseFile = ".cake"
logFile = ".cake.log"
cake :: Rule -> Act () -> IO ()
cake rule action = do
e <- doesFileExist databaseFile
oldDB <- if e
then decodeFile databaseFile
else return $ M.empty
newDB <- runAct rule oldDB action
putStrLn $ "Database is:"
forM_ (M.assocs newDB) $ \(k,v) ->
putStrLn $ (show k) ++ " => " ++ (show v)
encodeFile databaseFile newDB
produced :: FilePath -> Act Bool
produced f = do
(ps,_) <- RWS.get
return $ f `S.member` ps
distill :: Question -> Act Answer -> Act Answer
distill q act = do
a1 <- local (modCx q) $ do
debug $ "Starting to answer"
db <- ctxDB <$> ask
let a0 = M.lookup q db
a1 <- shielded act
tell (Dual $ M.singleton q a1)
when (Just a1 /= a0) $ do
clobber
debug $ "Question has not the same answer"
return a1
debug $ "..."
return a1
modCx q (Context {..}) = Context {ctxProducing = q:ctxProducing,..}
refresh :: Question -> Act Answer -> Act Answer
refresh q act = local (modCx q) $ do
debug $ "Overwriting"
a <- shielded act
tell (Dual $ M.singleton q a)
return a
produce :: FilePath -> Act () -> Act ()
produce f a = do
p <- produced f
when (not p) $ do
produce' f a
return ()
produce' :: FilePath -> Act () -> Act Answer
produce' f a = distill (FileContents f) $ do
e <- liftIO $ doesFileExist f
when (not e) clobber
a
modify $ first $ S.insert f
fileStamp f
list directory extension = do
Text x <- distill (Listing directory extension) $ do
files <- filter (filt . takeExtension) <$> liftIO (getDirectoryContents directory)
return $ Text (map (directory </>) files)
return x
where filt = if null extension then const True else (== '.':extension)
use f = do
distill (FileContents f) (fileStamp f)
overwrote f = refresh (FileContents f) (fileStamp f)
shielded :: Act a -> Act a
shielded a = do
(ps,s) <- RWS.get
RWS.put (ps,Clean)
x <- a
(ps',_) <- RWS.get
RWS.put (ps',s)
return x
runAct :: Rule -> DB -> Act () -> IO DB
runAct r db (Act act) = do
h <- openFile logFile WriteMode
(_a,Dual db) <- evalRWST act (Context h r db []) (S.empty,Clean)
hClose h
return db
findRule :: FilePath -> Act (Maybe (Act ()))
findRule f = do
r <- ctxRule <$> ask
let rs = parse r completeResults f
case rs of
Right [x] -> return (Just x)
Right _ -> fail $ "More than one rule for file " ++ f
Left e -> do
debug $ "No rule for file: " ++ f
return Nothing
debug :: String -> Act ()
debug x = do
h <- ctxHandle <$> ask
ps <- ctxProducing <$> ask
(_,s) <- RWS.get
let st = case s of
Clean -> "O"
Dirty -> "X"
liftIO $ hPutStrLn h $ "cake: " ++ st ++ " "++ concat (map (++": ") $ reverse $ map show ps) ++ x
fileStamp f = liftIO $ do
e <- doesFileExist f
Stamp <$> if e
then Just <$> md5 <$> B.readFile f
else return Nothing
clobber = RWS.modify $ second $ const Dirty
cut x = do
(_,s) <- RWS.get
case s of
Clean -> debug $ "Clean state; skipping."
Dirty -> x
need :: FilePath -> Act ()
need f = do
debug $ "Need: " ++ f
r <- findRule f
case r of
Nothing -> do
e <- liftIO $ doesFileExist f
when (not e) $ fail $ "No rule to create " ++ f
debug $ "using existing file"
use f
return ()
Just a -> a