{-# language ViewPatterns #-}
module DhallToCabal.Util
( relativeTo
)
where
import System.FilePath
( dropTrailingPathSeparator, joinPath, normalise, splitDirectories, takeDirectory )
relativeTo
:: FilePath
-> FilePath
-> FilePath
relativeTo =
\ ( splitDirectories . dropTrailingPathSeparator . takeDirectory . normalise -> base ) ->
\ ( splitDirectories . normalise -> path ) ->
joinPath ( go base path )
where
go ( a : as ) ( b : bs )
| a == b = go as bs
| a == "." = go as ( b : bs )
| b == "." = go (a : as) bs
| otherwise = ( ".." <$ ( a : as ) ) ++ ( b : bs )
go [] bs = bs
go as [] = ".." <$ as