module Development.Cake3.Types where
import Control.Applicative
import Data.Maybe
import Data.Monoid
import Data.Data
import Data.Typeable
import Data.Foldable (Foldable(..), foldl')
import qualified Data.List as L
import Data.List hiding(foldr, foldl')
import qualified Data.Map as M
import Data.Map (Map)
import qualified Data.Set as S
import Data.Set (Set)
import System.FilePath.Wrapper
data Variable = Variable {
vname :: String
, vval :: Maybe String
} deriving(Show, Eq, Ord, Data, Typeable)
type Command = [CommandPiece]
data CommandPiece = CmdStr String | CmdFile File
deriving (Show, Eq, Ord, Data, Typeable)
return_text x = return [CmdStr x]
return_file f = return [CmdFile f]
data Flag = Phony | Intermediate
deriving(Show,Eq,Ord, Data, Typeable)
data Recipe = Recipe {
rtgt :: Set File
, rsrc :: Set File
, rcmd :: [Command]
, rvars :: Set Variable
, rloc :: String
, rflags :: Set Flag
} deriving(Show, Eq, Ord, Data, Typeable)
emptyRecipe :: String -> Recipe
emptyRecipe loc = Recipe mempty mempty mempty mempty loc mempty
addPrerequisites :: Set File -> Recipe -> Recipe
addPrerequisites p r = r { rsrc = p`mappend`(rsrc r)}
addPrerequisite :: File -> Recipe -> Recipe
addPrerequisite f = addPrerequisites (S.singleton f)
type Target = Set File
groupSet :: (Ord k, Ord x, Foldable t) => (x -> Set k) -> t x -> Map k (Set x)
groupSet keys s = foldl' f' mempty s where
f' m x = foldl' ins m (keys x) where
ins m k = M.insertWith mappend k (S.singleton x) m
groupRecipes :: (Foldable t) => t Recipe -> Map File (Set Recipe)
groupRecipes = groupSet rtgt
flattern :: [Set x] -> [x]
flattern = concat . map S.toList
applyPlacement' :: (Eq x) => [File] -> Map File x -> [x]
applyPlacement' pl m =
let placed = nub $ catMaybes $ L.map (\k -> M.lookup k m) pl
all = L.map snd $ M.toList m
in placed ++ (all \\ placed)
applyPlacement :: (Foldable t) => [File] -> t Recipe -> [Recipe]
applyPlacement pl rs = flattern $ applyPlacement' pl (groupRecipes rs)
transformRecipes :: (Applicative m) => (Recipe -> m (Set Recipe)) -> Set Recipe -> m (Set Recipe)
transformRecipes f m = S.foldl' f' (pure mempty) m where
f' a r = mappend <$> (f r) <*> a
transformRecipesM_ :: (Monad m, Foldable t) => (Recipe -> m ()) -> t Recipe -> m ()
transformRecipesM_ f rs = foldl' (\a r -> a >> f r) (return mempty) rs
queryVariables :: (Foldable t) => t Recipe -> Set Variable
queryVariables rs = foldl' (\a r -> a`mappend`(rvars r)) mempty rs
queryVariablesE :: (Foldable t) => t Recipe -> Either String (Set Variable)
queryVariablesE rs = check where
vs = queryVariables rs
bads = M.filter (\s -> (S.size s) /= 1) (groupSet (\v -> S.singleton (vname v)) vs)
check | (M.size bads) > 0 = Left "Some variables share same name"
| otherwise = Right vs
queryTargets :: (Foldable t) => t Recipe -> Set File
queryTargets rs = foldl' (\a r -> a`mappend`(rtgt r)) mempty rs
var :: String -> Maybe String -> Variable
var n v = Variable n v
makevar
:: String
-> String
-> Variable
makevar n v = var n (Just v)
extvar :: String -> Variable
extvar n = var n Nothing
make = extvar "MAKE"