Skip to content

Commit 1b9cebd

Browse files
committed
Create findSubdirUpwards
1 parent 7c8e336 commit 1b9cebd

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/Hie/Implicit/Cradle.hs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ cabalExecutable :: MaybeT IO FilePath
109109
cabalExecutable = MaybeT $ findExecutable "cabal"
110110

111111
cabalDistDir :: FilePath -> MaybeT IO FilePath
112-
cabalDistDir = findFileUpwards isCabal
112+
cabalDistDir = findSubdirUpwards isCabal
113113
where
114114
-- TODO do old style dist builds work?
115115
isCabal name = name == "dist-newstyle" || name == "dist"
@@ -143,7 +143,7 @@ stackExecutable :: MaybeT IO FilePath
143143
stackExecutable = MaybeT $ findExecutable "stack"
144144

145145
stackWorkDir :: FilePath -> MaybeT IO FilePath
146-
stackWorkDir = findFileUpwards isStack
146+
stackWorkDir = findSubdirUpwards isStack
147147
where
148148
isStack name = name == ".stack-work"
149149

@@ -152,33 +152,46 @@ stackYamlDir = findFileUpwards isStack
152152
where
153153
isStack name = name == "stack.yaml"
154154

155+
-- | Searches upwards for the first directory containing a subdirectory
156+
-- to match the predicate.
157+
findSubdirUpwards :: (FilePath -> Bool) -> FilePath -> MaybeT IO FilePath
158+
findSubdirUpwards p dir = findContentUpwards p' dir
159+
where p' subdir = do
160+
exists <- doesDirectoryExist $ dir </> subdir
161+
return $ (p subdir) && exists
162+
155163
-- | Searches upwards for the first directory containing a file to match
156164
-- the predicate.
157165
findFileUpwards :: (FilePath -> Bool) -> FilePath -> MaybeT IO FilePath
158-
findFileUpwards p dir = do
166+
findFileUpwards p dir = findContentUpwards p' dir
167+
where p' file = do
168+
exists <- doesFileExist $ dir </> file
169+
return $ (p file) && exists
170+
171+
findContentUpwards :: (FilePath -> IO Bool) -> FilePath -> MaybeT IO FilePath
172+
findContentUpwards p dir = do
159173
cnts <-
160174
liftIO $
161175
handleJust
162176
-- Catch permission errors
163177
(\(e :: IOError) -> if isPermissionError e then Just [] else Nothing)
164178
pure
165-
(findFile p dir)
179+
(findContent p dir)
166180
case cnts of
167181
[]
168182
| dir' == dir -> fail "No cabal files"
169-
| otherwise -> findFileUpwards p dir'
183+
| otherwise -> findContentUpwards p dir'
170184
_ : _ -> return dir
171185
where
172186
dir' = takeDirectory dir
173187

174188
-- | Sees if any file in the directory matches the predicate
175-
findFile :: (FilePath -> Bool) -> FilePath -> IO [FilePath]
176-
findFile p dir = do
189+
findContent :: (FilePath -> IO Bool) -> FilePath -> IO [FilePath]
190+
findContent p dir = do
177191
b <- doesDirectoryExist dir
178192
if b then getFiles else pure []
179193
where
180-
getFiles = filter p <$> getDirectoryContents dir
181-
doesPredFileExist file = doesFileExist $ dir </> file
194+
getFiles = getDirectoryContents dir >>= filterM p
182195

183196
biosWorkDir :: FilePath -> MaybeT IO FilePath
184197
biosWorkDir = findFileUpwards (".hie-bios" ==)

0 commit comments

Comments
 (0)