Skip to content

Commit

Permalink
Re-implement getFilesIn (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisx authored Aug 10, 2024
1 parent 1cb230b commit 7ba2aa4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 48 deletions.
62 changes: 20 additions & 42 deletions src/Weeder/Main.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# language ApplicativeDo #-}
{-# language ScopedTypeVariables #-}
{-# language BlockArguments #-}
{-# language FlexibleContexts #-}
{-# language NamedFieldPuns #-}
Expand All @@ -14,11 +15,11 @@ module Weeder.Main ( main, mainWithConfig, getHieFiles ) where
import Control.Concurrent.Async ( async, link, ExceptionInLinkedThread ( ExceptionInLinkedThread ) )

-- base
import Control.Exception ( Exception, throwIO, displayException, catches, Handler ( Handler ), SomeException ( SomeException ) )
import Control.Exception ( Exception, throwIO, displayException, catches, Handler ( Handler ), SomeException ( SomeException ))
import Control.Concurrent ( getChanContents, newChan, writeChan, setNumCapabilities )
import Data.List
import Control.Monad ( unless, when )
import Data.Foldable
import Data.List ( isSuffixOf )
import Data.Maybe ( isJust, catMaybes )
import Data.Version ( showVersion )
import System.Exit ( ExitCode(..), exitWith )
Expand All @@ -28,10 +29,13 @@ import System.IO ( stderr, hPutStrLn )
import qualified TOML

-- directory
import System.Directory ( canonicalizePath, doesDirectoryExist, doesFileExist, doesPathExist, listDirectory, withCurrentDirectory )
import System.Directory ( doesFileExist )

-- filepath
import System.FilePath ( isExtensionOf )
import System.FilePath ( isExtSeparator )

-- glob
import qualified System.FilePath.Glob as Glob

-- ghc
import GHC.Iface.Ext.Binary ( HieFileResult( HieFileResult, hie_file_result ), readHieFileWithVersion )
Expand Down Expand Up @@ -234,17 +238,20 @@ mainWithConfig hieExt hieDirectories requireHsFiles weederConfig = handleWeederE
-- Will rethrow exceptions as 'ExceptionInLinkedThread' to the calling thread.
getHieFiles :: String -> [FilePath] -> Bool -> IO [HieFile]
getHieFiles hieExt hieDirectories requireHsFiles = do
hieFilePaths <-
let hiePat = "**/*." <> hieExtNoSep
hieExtNoSep = if isExtSeparator (head hieExt) then tail hieExt else hieExt

hieFilePaths :: [FilePath] <-
concat <$>
traverse ( getFilesIn hieExt )
traverse ( getFilesIn hiePat )
( if null hieDirectories
then ["./."]
else hieDirectories
)

hsFilePaths <-
hsFilePaths :: [FilePath] <-
if requireHsFiles
then getFilesIn ".hs" "./."
then getFilesIn "**/*.hs" "./."
else pure []

hieFileResultsChan <- newChan
Expand Down Expand Up @@ -274,43 +281,14 @@ getHieFiles hieExt hieDirectories requireHsFiles = do
-- | Recursively search for files with the given extension in given directory
getFilesIn
:: String
-- ^ Only files with this extension are considered
-- ^ Only files matching this pattern are considered.
-> FilePath
-- ^ Directory to look in
-> IO [FilePath]
getFilesIn ext path = do
exists <-
doesPathExist path

if exists
then do
isFile <-
doesFileExist path

if isFile && ext `isExtensionOf` path
then do
path' <-
canonicalizePath path

return [ path' ]

else do
isDir <-
doesDirectoryExist path

if isDir
then do
cnts <-
listDirectory path

withCurrentDirectory path ( foldMap ( getFilesIn ext ) cnts )

else
return []

else
return []

getFilesIn pat root = do
[result] <- Glob.globDir [Glob.compile pat] root
pure result


-- | Read a .hie file, exiting if it's an incompatible version.
readCompatibleHieFileOrExit :: NameCache -> FilePath -> IO HieFile
Expand Down
7 changes: 1 addition & 6 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ import Data.Maybe
import Algebra.Graph.Export.Dot
import GHC.Types.Name.Occurrence (occNameString)
import System.Directory
import System.Environment (getArgs, withArgs)
import System.FilePath
import System.Process
import System.IO (stderr, hPrint)
import Test.Tasty (TestTree, defaultMain, testGroup)
import Control.Monad (zipWithM_, when)
import Control.Exception ( throwIO, IOException, handle )
import Data.Maybe (isJust)
import Data.List (find, sortOn)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS
import Data.Text (Text, pack)
import Data.Text (pack)
import Data.Text.Encoding (encodeUtf8)
import Test.Tasty.Golden

Expand Down
1 change: 1 addition & 0 deletions weeder.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ library
, filepath ^>= 1.4.2.1
, generic-lens ^>= 2.2.0.0
, ghc ^>= 9.4 || ^>= 9.6 || ^>= 9.8
, Glob ^>= 0.9 || ^>= 0.10
, lens ^>= 5.1 || ^>= 5.2 || ^>= 5.3
, mtl ^>= 2.2.2 || ^>= 2.3
, optparse-applicative ^>= 0.14.3.0 || ^>= 0.15.1.0 || ^>= 0.16.0.0 || ^>= 0.17 || ^>= 0.18.1.0
Expand Down

0 comments on commit 7ba2aa4

Please sign in to comment.