module Static.Resources (
htmlImportList
, getResourceSetsForImport
, cleanResourceFiles
, ResourceSpec
, parseSpec
, check
, ImportType(..)
, ResourceSetsForImport(..)
, generateResources
) where
import Static.Resources.Types
import Static.Resources.Spec
import Static.Resources.Checker
import Static.Resources.Generation
import Static.Resources.Import
import Control.Monad
import Control.Monad.Error
import Data.Either.Utils
import Data.Functor
import System.Directory
import Data.List
getResourceSetsForImport :: ImportType -> FilePath -> IO (Either String ResourceSetsForImport)
getResourceSetsForImport it fp = do
putStrLn "Starting static resource generation."
sdir <- getCurrentDirectory
spec <- parseSpec fp
setCurrentDirectory $ reverse $ dropWhile (/= '/') $ reverse $ fp
cleanResourceFiles spec
res <- runErrorT $ do
when (null $ sets spec) $ throwError "No resource sets are defined"
checkRes <- lift $ check spec
when (isLeft checkRes) $ throwError ("Error while checking spec. Error : " ++ fromLeft checkRes)
lift $ generateResources it spec
case res of
Left s -> putStrLn $ "Static resource generation failed. " ++ s
Right (ResourceSetsForImport rs) -> putStrLn $ "Static resource generation done. Generated " ++ show (length rs) ++ " sets."
setCurrentDirectory sdir
return res
where
isLeft (Left _) = True
isLeft _ = False
cleanResourceFiles :: ResourceSpec -> IO ()
cleanResourceFiles spec = do
af <- getDirectoryContents "."
forM_ (af) $ \fn ->
when ((".css" `isSuffixOf` fn || ".js" `isSuffixOf` fn ) && any (\sn -> sn `isPrefixOf` fn ) (name <$> sets spec)) $
removeFile fn