module Development.Duplo.FileList where
import Control.Lens hiding (Action)
import Control.Lens.TH (makeLenses)
import Control.Monad (liftM, msum)
import Data.Maybe (Maybe(..), catMaybes)
import Development.Shake
import Development.Shake.FilePath ((</>))
import System.FilePath.Posix (makeRelative)
data File = File { _filePath :: FilePath
, _fileBase :: FilePath
} deriving (Show)
type Copy = (FilePath, FilePath)
defaultFile :: File
defaultFile = File { _filePath = ""
, _fileBase = ""
}
makeLenses ''File
makeFiles :: FilePath -> [FilePath] -> [File]
makeFiles = fmap . makeFile
makeFile :: FilePath -> FilePath -> File
makeFile base relPath =
setPath absPath
where
constructor = defaultFile { _fileBase = base }
absPath = base </> relPath
setPath = (constructor &) . (filePath .~)
collapseFileLists :: [[File]] -> Action [File]
collapseFileLists = liftM catMaybes . mapM collapseFileList
collapseFileList :: [File] -> Action (Maybe File)
collapseFileList = liftM msum . mapM collapseFile
collapseFile :: File -> Action (Maybe File)
collapseFile file = do
let path = file ^. filePath
doesExist <- doesFileExist path
return $ if doesExist then Just file else Nothing
toCopies :: FilePath -> [File] -> [Copy]
toCopies base = fmap $ toCopy base
toCopy :: FilePath -> File -> Copy
toCopy targetBase file =
(from, to)
where
path = file ^. filePath
base = file ^. fileBase
relPath = makeRelative base path
from = path
to = targetBase </> relPath