@@ -109,7 +109,7 @@ cabalExecutable :: MaybeT IO FilePath
109
109
cabalExecutable = MaybeT $ findExecutable " cabal"
110
110
111
111
cabalDistDir :: FilePath -> MaybeT IO FilePath
112
- cabalDistDir = findFileUpwards isCabal
112
+ cabalDistDir = findSubdirUpwards isCabal
113
113
where
114
114
-- TODO do old style dist builds work?
115
115
isCabal name = name == " dist-newstyle" || name == " dist"
@@ -143,7 +143,7 @@ stackExecutable :: MaybeT IO FilePath
143
143
stackExecutable = MaybeT $ findExecutable " stack"
144
144
145
145
stackWorkDir :: FilePath -> MaybeT IO FilePath
146
- stackWorkDir = findFileUpwards isStack
146
+ stackWorkDir = findSubdirUpwards isStack
147
147
where
148
148
isStack name = name == " .stack-work"
149
149
@@ -152,33 +152,46 @@ stackYamlDir = findFileUpwards isStack
152
152
where
153
153
isStack name = name == " stack.yaml"
154
154
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
+
155
163
-- | Searches upwards for the first directory containing a file to match
156
164
-- the predicate.
157
165
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
159
173
cnts <-
160
174
liftIO $
161
175
handleJust
162
176
-- Catch permission errors
163
177
(\ (e :: IOError ) -> if isPermissionError e then Just [] else Nothing )
164
178
pure
165
- (findFile p dir)
179
+ (findContent p dir)
166
180
case cnts of
167
181
[]
168
182
| dir' == dir -> fail " No cabal files"
169
- | otherwise -> findFileUpwards p dir'
183
+ | otherwise -> findContentUpwards p dir'
170
184
_ : _ -> return dir
171
185
where
172
186
dir' = takeDirectory dir
173
187
174
188
-- | 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
177
191
b <- doesDirectoryExist dir
178
192
if b then getFiles else pure []
179
193
where
180
- getFiles = filter p <$> getDirectoryContents dir
181
- doesPredFileExist file = doesFileExist $ dir </> file
194
+ getFiles = getDirectoryContents dir >>= filterM p
182
195
183
196
biosWorkDir :: FilePath -> MaybeT IO FilePath
184
197
biosWorkDir = findFileUpwards (" .hie-bios" == )
0 commit comments