module Shikensu.Internal.Utilities
( cleanPath
, commonDirectory
, compilePatterns
, compileParentPath
, compilePathToRoot
, globDir
, replaceSingleDot
, stripPrefix
, takeDirName
) where
import Data.Maybe (fromMaybe)
import Flow
import Shikensu.Types (Pattern)
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)
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