{-# LANGUAGE OverloadedStrings #-}
module Shelly.Find
( find
, findWhen
, findFold
, findDirFilter
, findDirFilterWhen
, findFoldDirFilter
)
where
import Shelly.Base
import Control.Monad ( foldM )
#if !MIN_VERSION_base(4,13,0)
import Data.Monoid ( mappend )
#endif
import System.PosixCompat.Files ( getSymbolicLinkStatus
, isSymbolicLink
)
import System.Directory ( doesDirectoryExist )
find :: FilePath -> Sh [FilePath]
find :: FilePath -> Sh [FilePath]
find = ([FilePath] -> FilePath -> Sh [FilePath])
-> [FilePath] -> FilePath -> Sh [FilePath]
forall a. (a -> FilePath -> Sh a) -> a -> FilePath -> Sh a
findFold (\[FilePath]
paths FilePath
fp -> [FilePath] -> Sh [FilePath]
forall a. a -> Sh a
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath] -> Sh [FilePath]) -> [FilePath] -> Sh [FilePath]
forall a b. (a -> b) -> a -> b
$ [FilePath]
paths [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [FilePath
fp]) []
findWhen :: (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
findWhen :: (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
findWhen = (FilePath -> Sh Bool)
-> (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
findDirFilterWhen (Sh Bool -> FilePath -> Sh Bool
forall a b. a -> b -> a
const (Sh Bool -> FilePath -> Sh Bool) -> Sh Bool -> FilePath -> Sh Bool
forall a b. (a -> b) -> a -> b
$ Bool -> Sh Bool
forall a. a -> Sh a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True)
findFold :: (a -> FilePath -> Sh a) -> a -> FilePath -> Sh a
findFold :: forall a. (a -> FilePath -> Sh a) -> a -> FilePath -> Sh a
findFold a -> FilePath -> Sh a
folder a
startValue =
(a -> FilePath -> Sh a)
-> a -> (FilePath -> Sh Bool) -> FilePath -> Sh a
forall a.
(a -> FilePath -> Sh a)
-> a -> (FilePath -> Sh Bool) -> FilePath -> Sh a
findFoldDirFilter a -> FilePath -> Sh a
folder a
startValue (Sh Bool -> FilePath -> Sh Bool
forall a b. a -> b -> a
const (Sh Bool -> FilePath -> Sh Bool) -> Sh Bool -> FilePath -> Sh Bool
forall a b. (a -> b) -> a -> b
$ Bool -> Sh Bool
forall a. a -> Sh a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True)
findDirFilter :: (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
findDirFilter :: (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
findDirFilter FilePath -> Sh Bool
filt = (FilePath -> Sh Bool)
-> (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
findDirFilterWhen FilePath -> Sh Bool
filt (Sh Bool -> FilePath -> Sh Bool
forall a b. a -> b -> a
const (Sh Bool -> FilePath -> Sh Bool) -> Sh Bool -> FilePath -> Sh Bool
forall a b. (a -> b) -> a -> b
$ Bool -> Sh Bool
forall a. a -> Sh a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True)
findDirFilterWhen
:: (FilePath -> Sh Bool)
-> (FilePath -> Sh Bool)
-> FilePath
-> Sh [FilePath]
findDirFilterWhen :: (FilePath -> Sh Bool)
-> (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
findDirFilterWhen FilePath -> Sh Bool
dirFilt FilePath -> Sh Bool
fileFilter = ([FilePath] -> FilePath -> Sh [FilePath])
-> [FilePath] -> (FilePath -> Sh Bool) -> FilePath -> Sh [FilePath]
forall a.
(a -> FilePath -> Sh a)
-> a -> (FilePath -> Sh Bool) -> FilePath -> Sh a
findFoldDirFilter [FilePath] -> FilePath -> Sh [FilePath]
filterIt [] FilePath -> Sh Bool
dirFilt
where
filterIt :: [FilePath] -> FilePath -> Sh [FilePath]
filterIt [FilePath]
paths FilePath
fp = do
Bool
yes <- FilePath -> Sh Bool
fileFilter FilePath
fp
[FilePath] -> Sh [FilePath]
forall a. a -> Sh a
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath] -> Sh [FilePath]) -> [FilePath] -> Sh [FilePath]
forall a b. (a -> b) -> a -> b
$ if Bool
yes then [FilePath]
paths [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [FilePath
fp] else [FilePath]
paths
findFoldDirFilter
:: (a -> FilePath -> Sh a) -> a -> (FilePath -> Sh Bool) -> FilePath -> Sh a
findFoldDirFilter :: forall a.
(a -> FilePath -> Sh a)
-> a -> (FilePath -> Sh Bool) -> FilePath -> Sh a
findFoldDirFilter a -> FilePath -> Sh a
folder a
startValue FilePath -> Sh Bool
dirFilter FilePath
dir = do
FilePath
absDir <- FilePath -> Sh FilePath
absPath FilePath
dir
Text -> Sh ()
trace (Text
"find " Text -> Text -> Text
forall a. Monoid a => a -> a -> a
`mappend` FilePath -> Text
toTextIgnore FilePath
absDir)
Bool
filt <- FilePath -> Sh Bool
dirFilter FilePath
absDir
if Bool -> Bool
not Bool
filt
then a -> Sh a
forall a. a -> Sh a
forall (m :: * -> *) a. Monad m => a -> m a
return a
startValue
else do
([FilePath]
rPaths, [FilePath]
aPaths) <- FilePath -> Sh ([FilePath], [FilePath])
lsRelAbs FilePath
dir
(a -> (FilePath, FilePath) -> Sh a)
-> a -> [(FilePath, FilePath)] -> Sh a
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM a -> (FilePath, FilePath) -> Sh a
traverse' a
startValue ([FilePath] -> [FilePath] -> [(FilePath, FilePath)]
forall a b. [a] -> [b] -> [(a, b)]
zip [FilePath]
rPaths [FilePath]
aPaths)
where
traverse' :: a -> (FilePath, FilePath) -> Sh a
traverse' a
acc (FilePath
relativePath, FilePath
absolutePath) = do
Bool
isDir <- IO Bool -> Sh Bool
forall a. IO a -> Sh a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> Sh Bool) -> IO Bool -> Sh Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesDirectoryExist FilePath
absolutePath
Bool
sym <- IO Bool -> Sh Bool
forall a. IO a -> Sh a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> Sh Bool) -> IO Bool -> Sh Bool
forall a b. (a -> b) -> a -> b
$ (FileStatus -> Bool) -> IO FileStatus -> IO Bool
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FileStatus -> Bool
isSymbolicLink (IO FileStatus -> IO Bool) -> IO FileStatus -> IO Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileStatus
getSymbolicLinkStatus FilePath
absolutePath
a
newAcc <- a -> FilePath -> Sh a
folder a
acc FilePath
relativePath
Bool
follow <- (State -> Bool) -> Sh State -> Sh Bool
forall a b. (a -> b) -> Sh a -> Sh b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Bool
sFollowSymlink Sh State
get
if Bool
isDir Bool -> Bool -> Bool
&& (Bool
follow Bool -> Bool -> Bool
|| Bool -> Bool
not Bool
sym)
then (a -> FilePath -> Sh a)
-> a -> (FilePath -> Sh Bool) -> FilePath -> Sh a
forall a.
(a -> FilePath -> Sh a)
-> a -> (FilePath -> Sh Bool) -> FilePath -> Sh a
findFoldDirFilter a -> FilePath -> Sh a
folder a
newAcc FilePath -> Sh Bool
dirFilter FilePath
relativePath
else a -> Sh a
forall a. a -> Sh a
forall (m :: * -> *) a. Monad m => a -> m a
return a
newAcc