Skip to content

Commit 7ba2aa4

Browse files
authored
Re-implement getFilesIn (#165)
1 parent 1cb230b commit 7ba2aa4

3 files changed

Lines changed: 22 additions & 48 deletions

File tree

src/Weeder/Main.hs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# language ApplicativeDo #-}
2+
{-# language ScopedTypeVariables #-}
23
{-# language BlockArguments #-}
34
{-# language FlexibleContexts #-}
45
{-# language NamedFieldPuns #-}
@@ -14,11 +15,11 @@ module Weeder.Main ( main, mainWithConfig, getHieFiles ) where
1415
import Control.Concurrent.Async ( async, link, ExceptionInLinkedThread ( ExceptionInLinkedThread ) )
1516

1617
-- base
17-
import Control.Exception ( Exception, throwIO, displayException, catches, Handler ( Handler ), SomeException ( SomeException ) )
18+
import Control.Exception ( Exception, throwIO, displayException, catches, Handler ( Handler ), SomeException ( SomeException ))
1819
import Control.Concurrent ( getChanContents, newChan, writeChan, setNumCapabilities )
20+
import Data.List
1921
import Control.Monad ( unless, when )
2022
import Data.Foldable
21-
import Data.List ( isSuffixOf )
2223
import Data.Maybe ( isJust, catMaybes )
2324
import Data.Version ( showVersion )
2425
import System.Exit ( ExitCode(..), exitWith )
@@ -28,10 +29,13 @@ import System.IO ( stderr, hPutStrLn )
2829
import qualified TOML
2930

3031
-- directory
31-
import System.Directory ( canonicalizePath, doesDirectoryExist, doesFileExist, doesPathExist, listDirectory, withCurrentDirectory )
32+
import System.Directory ( doesFileExist )
3233

3334
-- filepath
34-
import System.FilePath ( isExtensionOf )
35+
import System.FilePath ( isExtSeparator )
36+
37+
-- glob
38+
import qualified System.FilePath.Glob as Glob
3539

3640
-- ghc
3741
import GHC.Iface.Ext.Binary ( HieFileResult( HieFileResult, hie_file_result ), readHieFileWithVersion )
@@ -234,17 +238,20 @@ mainWithConfig hieExt hieDirectories requireHsFiles weederConfig = handleWeederE
234238
-- Will rethrow exceptions as 'ExceptionInLinkedThread' to the calling thread.
235239
getHieFiles :: String -> [FilePath] -> Bool -> IO [HieFile]
236240
getHieFiles hieExt hieDirectories requireHsFiles = do
237-
hieFilePaths <-
241+
let hiePat = "**/*." <> hieExtNoSep
242+
hieExtNoSep = if isExtSeparator (head hieExt) then tail hieExt else hieExt
243+
244+
hieFilePaths :: [FilePath] <-
238245
concat <$>
239-
traverse ( getFilesIn hieExt )
246+
traverse ( getFilesIn hiePat )
240247
( if null hieDirectories
241248
then ["./."]
242249
else hieDirectories
243250
)
244251

245-
hsFilePaths <-
252+
hsFilePaths :: [FilePath] <-
246253
if requireHsFiles
247-
then getFilesIn ".hs" "./."
254+
then getFilesIn "**/*.hs" "./."
248255
else pure []
249256

250257
hieFileResultsChan <- newChan
@@ -274,43 +281,14 @@ getHieFiles hieExt hieDirectories requireHsFiles = do
274281
-- | Recursively search for files with the given extension in given directory
275282
getFilesIn
276283
:: String
277-
-- ^ Only files with this extension are considered
284+
-- ^ Only files matching this pattern are considered.
278285
-> FilePath
279286
-- ^ Directory to look in
280287
-> IO [FilePath]
281-
getFilesIn ext path = do
282-
exists <-
283-
doesPathExist path
284-
285-
if exists
286-
then do
287-
isFile <-
288-
doesFileExist path
289-
290-
if isFile && ext `isExtensionOf` path
291-
then do
292-
path' <-
293-
canonicalizePath path
294-
295-
return [ path' ]
296-
297-
else do
298-
isDir <-
299-
doesDirectoryExist path
300-
301-
if isDir
302-
then do
303-
cnts <-
304-
listDirectory path
305-
306-
withCurrentDirectory path ( foldMap ( getFilesIn ext ) cnts )
307-
308-
else
309-
return []
310-
311-
else
312-
return []
313-
288+
getFilesIn pat root = do
289+
[result] <- Glob.globDir [Glob.compile pat] root
290+
pure result
291+
314292

315293
-- | Read a .hie file, exiting if it's an incompatible version.
316294
readCompatibleHieFileOrExit :: NameCache -> FilePath -> IO HieFile

test/Main.hs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@ import Data.Maybe
1010
import Algebra.Graph.Export.Dot
1111
import GHC.Types.Name.Occurrence (occNameString)
1212
import System.Directory
13-
import System.Environment (getArgs, withArgs)
1413
import System.FilePath
15-
import System.Process
1614
import System.IO (stderr, hPrint)
1715
import Test.Tasty (TestTree, defaultMain, testGroup)
18-
import Control.Monad (zipWithM_, when)
1916
import Control.Exception ( throwIO, IOException, handle )
20-
import Data.Maybe (isJust)
2117
import Data.List (find, sortOn)
22-
import Data.ByteString (ByteString)
2318
import qualified Data.ByteString.Lazy as LBS
24-
import Data.Text (Text, pack)
19+
import Data.Text (pack)
2520
import Data.Text.Encoding (encodeUtf8)
2621
import Test.Tasty.Golden
2722

weeder.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ library
3131
, filepath ^>= 1.4.2.1
3232
, generic-lens ^>= 2.2.0.0
3333
, ghc ^>= 9.4 || ^>= 9.6 || ^>= 9.8
34+
, Glob ^>= 0.9 || ^>= 0.10
3435
, lens ^>= 5.1 || ^>= 5.2 || ^>= 5.3
3536
, mtl ^>= 2.2.2 || ^>= 2.3
3637
, optparse-applicative ^>= 0.14.3.0 || ^>= 0.15.1.0 || ^>= 0.16.0.0 || ^>= 0.17 || ^>= 0.18.1.0

0 commit comments

Comments
 (0)