{-# LANGUAGE Safe #-}
module Module.Paths (
PathIOHandler(..),
fixPath,
fixPaths,
) where
import Control.Monad.IO.Class
import Data.List (nub,isSuffixOf)
import System.FilePath
import Base.CompilerError
class PathIOHandler r where
resolveModule :: (MonadIO m, CollectErrorsM m) => r -> FilePath -> FilePath -> m FilePath
isSystemModule :: (MonadIO m, CollectErrorsM m) => r -> FilePath -> FilePath -> m Bool
resolveBaseModule :: (MonadIO m, CollectErrorsM m) => r -> m FilePath
isBaseModule :: (MonadIO m, CollectErrorsM m) => r -> FilePath -> m Bool
zipWithContents :: (MonadIO m, CollectErrorsM m) => r -> FilePath -> [FilePath] -> m [(FilePath,String)]
fixPath :: FilePath -> FilePath
fixPath :: FilePath -> FilePath
fixPath = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl FilePath -> FilePath -> FilePath
(</>) FilePath
"" forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> [FilePath] -> [FilePath]
process [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map FilePath -> FilePath
dropSlash forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
splitPath where
dropSlash :: FilePath -> FilePath
dropSlash FilePath
"/" = FilePath
"/"
dropSlash FilePath
d
| forall a. Eq a => [a] -> [a] -> Bool
isSuffixOf FilePath
"/" FilePath
d = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
tail forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse FilePath
d
| Bool
otherwise = FilePath
d
process :: [FilePath] -> [FilePath] -> [FilePath]
process [FilePath]
rs (FilePath
".":[FilePath]
ds) = [FilePath] -> [FilePath] -> [FilePath]
process [FilePath]
rs [FilePath]
ds
process (FilePath
"..":[FilePath]
rs) (FilePath
"..":[FilePath]
ds) = [FilePath] -> [FilePath] -> [FilePath]
process (FilePath
".."forall a. a -> [a] -> [a]
:FilePath
".."forall a. a -> [a] -> [a]
:[FilePath]
rs) [FilePath]
ds
process (FilePath
"/":[]) (FilePath
"..":[FilePath]
ds) = [FilePath] -> [FilePath] -> [FilePath]
process (FilePath
"/"forall a. a -> [a] -> [a]
:[]) [FilePath]
ds
process (FilePath
_:[FilePath]
rs) (FilePath
"..":[FilePath]
ds) = [FilePath] -> [FilePath] -> [FilePath]
process [FilePath]
rs [FilePath]
ds
process [FilePath]
rs (FilePath
d:[FilePath]
ds) = [FilePath] -> [FilePath] -> [FilePath]
process (FilePath
dforall a. a -> [a] -> [a]
:[FilePath]
rs) [FilePath]
ds
process [FilePath]
rs [FilePath]
_ = forall a. [a] -> [a]
reverse [FilePath]
rs
fixPaths :: [FilePath] -> [FilePath]
fixPaths :: [FilePath] -> [FilePath]
fixPaths = forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map FilePath -> FilePath
fixPath