module Shikensu
( list
, listF
, listRelative
, listRelativeF
, forkDefinition
, makeDefinition
, makeDictionary
) where
import Flow
import Shikensu.Internal.Utilities
import Shikensu.Types
import System.FilePath
import qualified Data.HashMap.Strict as HashMap (empty)
import qualified Data.List as List (concat, map, zip)
import qualified System.Directory as Dir (canonicalizePath)
list :: [Pattern] -> FilePath -> IO Dictionary
list patterns rootDir =
patterns
|> compilePatterns
|> globDir rootDir
|> fmap (List.zip patterns)
|> fmap (List.map (uncurry . makeDictionary $ rootDir))
|> fmap (List.concat)
listF :: FilePath -> [Pattern] -> IO Dictionary
listF = flip list
listRelative :: [Pattern] -> FilePath -> IO Dictionary
listRelative patterns relativePath =
Dir.canonicalizePath relativePath >>= list patterns
listRelativeF :: FilePath -> [Pattern] -> 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 -> Pattern -> FilePath -> Definition
makeDefinition _rootDirname _pattern _workspacePath =
let
workingDir = cleanPath . (commonDirectory) $ _pattern
localPath = cleanPath . (stripPrefix workingDir) . cleanPath $ _workspacePath
in
Definition
{ basename = takeBaseName localPath
, dirname = takeDirName localPath
, extname = takeExtension localPath
, pattern = _pattern
, rootDirname = dropTrailingPathSeparator _rootDirname
, workingDirname = workingDir
, content = Nothing
, metadata = HashMap.empty
, parentPath = compileParentPath $ takeDirName localPath
, pathToRoot = compilePathToRoot $ takeDirName localPath
}
makeDictionary :: FilePath -> Pattern -> [FilePath] -> Dictionary
makeDictionary _rootDirname _pattern =
List.map (makeDefinition _rootDirname _pattern)