module Shikensu.Utilities
( cleanPath
, commonDirectory
, compilePatterns
, compileParentPath
, compilePathToRoot
, globDir
, io
, mapIO
, replaceSingleDot
, stripPrefix
, takeDirName
) where
import Data.Maybe (fromMaybe)
import Data.Tuple (fst)
import Flow
import Shikensu.Types
import System.FilePath
import qualified Data.List as List (map, stripPrefix)
import qualified System.FilePath.Glob as Glob
cleanPath :: FilePath -> FilePath
cleanPath = normalise .> dropTrailingPathSeparator .> dropDrive .> replaceSingleDot
commonDirectory :: Pattern -> FilePath
commonDirectory pattern = (Glob.compile .> Glob.commonDirectory .> fst) pattern
compilePatterns :: [Pattern] -> [Glob.Pattern]
compilePatterns = List.map Glob.compile
compileParentPath :: FilePath -> Maybe FilePath
compileParentPath dirname =
case dirname of
"" -> Nothing
_ -> Just "../"
compilePathToRoot :: FilePath -> FilePath
compilePathToRoot dirname =
if dirname == "" then
""
else
dirname
|> splitDirectories
|> fmap (\_ -> "..")
|> joinPath
|> addTrailingPathSeparator
globDir :: FilePath -> [Glob.Pattern] -> IO [[FilePath]]
globDir rootDir patterns =
Glob.globDir patterns rootDir
|> fmap (fst)
|> fmap (List.map . List.map . makeRelative $ rootDir)
io :: ([Definition] -> [IO Definition]) -> Dictionary -> IO Dictionary
io fn = sequence . fn
mapIO :: (Definition -> IO Definition) -> Dictionary -> IO Dictionary
mapIO = io . fmap
replaceSingleDot :: String -> String
replaceSingleDot path = if path == "." then "" else path
stripPrefix :: String -> String -> String
stripPrefix prefix target = fromMaybe target (List.stripPrefix prefix target)
takeDirName :: FilePath -> FilePath
takeDirName = replaceSingleDot . takeDirectory