{- -----------------------------------------------------------------------------
Copyright 2020 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- -}

-- Author: Kevin P. Barry [ta0kira@gmail.com]

{-# 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