module Shikensu
( forkDefinition
, list
, listF
, listRelative
, listRelativeF
, makeDefinition
, makeDictionary
, module Shikensu.Internal.Types
) where
import Data.Monoid ((<>))
import Flow
import Shikensu.Internal.Types
import Shikensu.Internal.Utilities
import System.FilePath
import qualified Data.HashMap.Strict as HashMap (empty)
import qualified Data.List as List (concatMap, map, zip)
import qualified System.Directory as Dir (canonicalizePath)
import qualified System.FilePath.Glob as Glob (compile, globDir1)
list :: [String] -> FilePath -> IO Dictionary
list patterns rootDir =
patterns
|> List.map (Glob.compile)
|> List.map (flip Glob.globDir1 $ rootDir)
|> sequence
|> fmap (List.zip patterns)
|> fmap (List.concatMap $ makeDictionary rootDir)
listF :: FilePath -> [String] -> IO Dictionary
listF = flip list
listRelative :: [String] -> FilePath -> IO Dictionary
listRelative patterns relativePath =
Dir.canonicalizePath relativePath >>= list patterns
listRelativeF :: FilePath -> [String] -> IO Dictionary
listRelativeF = flip listRelative
forkDefinition :: FilePath -> Definition -> Definition
forkDefinition newLocalPath def =
Definition
{ basename = takeBaseName newLocalPath
, dirname = takeDirName newLocalPath
, extname = takeExtension newLocalPath
, pattern = (pattern def)
, rootDirname = (rootDirname def)
, workingDirname = (workingDirname def)
, content = (content def)
, metadata = (metadata def)
, parentPath = compileParentPath $ takeDirName newLocalPath
, pathToRoot = compilePathToRoot $ takeDirName newLocalPath
}
makeDefinition :: FilePath -> String -> FilePath -> Definition
makeDefinition rootDirname pattern absolutePath =
let
workingDirname = commonDirectory pattern
rootWorkingDirname = combine rootDirname workingDirname
theAbsolutePath = normalise absolutePath
theLocalPath = dropDrive (stripPrefix rootWorkingDirname theAbsolutePath)
in
Definition
{ basename = takeBaseName theLocalPath
, dirname = takeDirName theLocalPath
, extname = takeExtension theLocalPath
, pattern = pattern
, rootDirname = rootDirname
, workingDirname = workingDirname
, content = Nothing
, metadata = HashMap.empty
, parentPath = compileParentPath $ takeDirName theLocalPath
, pathToRoot = compilePathToRoot $ takeDirName theLocalPath
}
makeDictionary :: FilePath -> (String, [FilePath]) -> Dictionary
makeDictionary rootDirname (pattern, filepaths) =
List.map (makeDefinition rootDirname pattern) filepaths