module Ribosome.File where

import qualified Data.Text as Text
import System.Directory (canonicalizePath, getHomeDirectory)

canonicalPathWithHome :: FilePath -> FilePath -> FilePath
canonicalPathWithHome :: FilePath -> FilePath -> FilePath
canonicalPathWithHome FilePath
home FilePath
path =
  Text -> FilePath
forall a. ToString a => a -> FilePath
toString (Text -> Text -> Text -> Text
Text.replace Text
"~" (FilePath -> Text
forall a. ToText a => a -> Text
toText FilePath
home) (FilePath -> Text
forall a. ToText a => a -> Text
toText FilePath
path))

canonicalPath :: MonadIO m => FilePath -> m FilePath
canonicalPath :: FilePath -> m FilePath
canonicalPath FilePath
path = do
  FilePath
home <- IO FilePath -> m FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
getHomeDirectory
  return $ FilePath -> FilePath -> FilePath
canonicalPathWithHome FilePath
home FilePath
path

canonicalPaths :: MonadIO m => [FilePath] -> m [FilePath]
canonicalPaths :: [FilePath] -> m [FilePath]
canonicalPaths [FilePath]
paths = do
  FilePath
home <- IO FilePath -> m FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
getHomeDirectory
  let withHome :: [FilePath]
withHome = (FilePath -> FilePath) -> [FilePath] -> [FilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FilePath -> FilePath -> FilePath
canonicalPathWithHome FilePath
home) [FilePath]
paths
  IO [FilePath] -> m [FilePath]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [FilePath] -> m [FilePath]) -> IO [FilePath] -> m [FilePath]
forall a b. (a -> b) -> a -> b
$ (FilePath -> IO FilePath) -> [FilePath] -> IO [FilePath]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM FilePath -> IO FilePath
canonicalizePath [FilePath]
withHome