module Hi.FilePath
(
rewritePath
) where
import Hi.Template (untemplate)
import Data.List
import Data.List.Split (splitOn)
import System.FilePath (joinPath, splitPath)
rewritePath :: String -> String -> FilePath -> FilePath
rewritePath packageName moduleName =
replacePackageName . removeFirstPackageName . replaceModuleName . untemplate
where
replacePackageName = replace "package-name" packageName
replaceModuleName = replace "ModuleName" $ moduleNameToFilePath moduleName
removeFirstPackageName :: FilePath -> FilePath
removeFirstPackageName path = go $ splitPath path
where
go :: [FilePath] -> FilePath
go [] = []
go (x:xs)
| x == "package-name/" = joinPath xs
| otherwise = joinPath $ x:xs
moduleNameToFilePath :: String -> FilePath
moduleNameToFilePath = joinPath . splitOn "."
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace a b = foldl1 (++) . intersperse b . splitOn a