refactor: convert functions in Util.IO to use StrongPath instead of Path (#346)

* refactor: convert functions in Util.IO to use StrongPath instead of Path

* Clean up import in IO

* Updated version of strong-path to the newest one, that doesn't cause version conflicts.

* Tightened types a bit in Util/IO.hs

Co-authored-by: Martin Sosic <sosic.martin@gmail.com>
This commit is contained in:
(´⌣`ʃƪ) 2021-10-28 05:07:26 -07:00 committed by GitHub
parent a7a69ff207
commit 2ce24cdfa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 27 deletions

View File

@ -15,10 +15,8 @@ import Control.Arrow (ArrowChoice (left))
import Control.Monad.IO.Class (liftIO)
import Lib (findWaspFile)
import qualified Parser
import qualified Path as P
import StrongPath (Abs, Dir, Path', fromAbsFile, toFilePath)
import StrongPath (Abs, Dir, Path', fromAbsFile, fromRelFile, toFilePath)
import StrongPath.Operations
import StrongPath.Path (toPathAbsDir)
import System.Directory (doesFileExist, getFileSize)
import Util.IO (listDirectoryDeep)
import qualified Util.Terminal as Term
@ -53,7 +51,7 @@ printInfo :: String -> String -> String
printInfo key value = Term.applyStyles [Term.Cyan] key ++ ": " <> Term.applyStyles [Term.White] value
readDirectorySizeMB :: Path' Abs (Dir WaspProjectDir) -> IO String
readDirectorySizeMB path = (++ " MB") . show . (`div` 1000000) . sum <$> (listDirectoryDeep (toPathAbsDir path) >>= mapM (getFileSize . P.fromRelFile))
readDirectorySizeMB path = (++ " MB") . show . (`div` 1000000) . sum <$> (listDirectoryDeep path >>= mapM (getFileSize . fromRelFile))
readCompileInformation :: Path' Abs (Dir WaspProjectDir) -> IO String
readCompileInformation waspDir = do

View File

@ -14,7 +14,6 @@ import qualified Data.Text.Lazy as TextL
import qualified Data.Text.Lazy.IO as TextL.IO
import StrongPath (Abs, Dir, File', Path', Rel, relfile, (</>))
import qualified StrongPath as SP
import qualified StrongPath.Path as SP.Path
import System.IO.Error (isDoesNotExistError)
import UnliftIO.Exception (catch, throwIO)
import qualified Util.IO
@ -59,8 +58,7 @@ readFiles extCodeDirPath = do
waspignoreFile <- readWaspignoreFile waspignoreFilePath
relFilePaths <-
filter (not . ignores waspignoreFile . SP.toFilePath)
. map SP.Path.fromPathRelFile
<$> Util.IO.listDirectoryDeep (SP.Path.toPathAbsDir extCodeDirPath)
<$> Util.IO.listDirectoryDeep extCodeDirPath
let absFilePaths = map (extCodeDirPath </>) relFilePaths
-- NOTE: We read text from all the files, regardless if they are text files or not, because
-- we don't know if they are a text file or not.

View File

@ -17,7 +17,6 @@ import Generator.Common (ProjectRootDir)
import qualified Parser
import StrongPath (Abs, Dir, File', Path', relfile)
import qualified StrongPath as SP
import qualified StrongPath.Path as SP.Path
import System.Directory (doesFileExist)
import qualified Util.IO
import Wasp (Wasp)
@ -60,7 +59,7 @@ enrichWaspASTBasedOnCompileOptions wasp options = do
findWaspFile :: Path' Abs (Dir WaspProjectDir) -> IO (Maybe (Path' Abs File'))
findWaspFile waspDir = do
files <- map SP.Path.fromPathRelFile . fst <$> Util.IO.listDirectory (SP.Path.toPathAbsDir waspDir)
files <- fst <$> Util.IO.listDirectory waspDir
return $ (waspDir SP.</>) <$> find isWaspFile files
where
isWaspFile path =

View File

@ -1,3 +1,5 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Util.IO
( listDirectoryDeep,
listDirectory,
@ -5,14 +7,12 @@ module Util.IO
where
import Control.Monad (filterM)
import qualified Path as P
import qualified System.Directory as Dir
import StrongPath (Abs, Dir, Dir', File, Path', Rel, basename, parseRelDir, parseRelFile, toFilePath, (</>))
import qualified System.Directory
import qualified System.FilePath as FilePath
import System.IO.Error (isDoesNotExistError)
import UnliftIO.Exception (catch, throwIO)
-- TODO: Convert these to use StrongPath?
-- TODO: write tests.
-- | Lists all files in the directory recursively.
@ -24,38 +24,38 @@ import UnliftIO.Exception (catch, throwIO)
-- >>> listDirectoryDeep "foo/"
-- we should get
-- >>> ["test.txt", "bar/text2.txt"]
listDirectoryDeep :: P.Path P.Abs P.Dir -> IO [P.Path P.Rel P.File]
listDirectoryDeep :: forall d f. Path' Abs (Dir d) -> IO [Path' (Rel d) (File f)]
listDirectoryDeep absDirPath = do
(relFilePaths, relSubDirPaths) <-
listDirectory absDirPath
`catch` \e -> if isDoesNotExistError e then return ([], []) else throwIO e
relSubDirFilesPaths <- mapM (listSubDirDeep . (absDirPath P.</>)) relSubDirPaths
relSubDirFilesPaths <- mapM (listSubDirDeep . (absDirPath </>)) relSubDirPaths
return $ relFilePaths ++ concat relSubDirFilesPaths
where
listSubDirDeep :: P.Path P.Abs P.Dir -> IO [P.Path P.Rel P.File]
listSubDirDeep :: Path' Abs (Dir sd) -> IO [Path' (Rel d) (File f)]
listSubDirDeep subDirPath = do
files <- listDirectoryDeep subDirPath
return $ map (P.dirname subDirPath P.</>) files
return $ map (basename subDirPath </>) files
-- TODO: write tests.
-- | Lists files and directories at top lvl of the directory.
listDirectory :: P.Path P.Abs P.Dir -> IO ([P.Path P.Rel P.File], [P.Path P.Rel P.Dir])
listDirectory :: forall d f. Path' Abs (Dir d) -> IO ([Path' (Rel d) (File f)], [Path' (Rel d) Dir'])
listDirectory absDirPath = do
fpRelItemPaths <- Dir.listDirectory fpAbsDirPath
fpRelItemPaths <- System.Directory.listDirectory fpAbsDirPath
relFilePaths <- filterFiles fpAbsDirPath fpRelItemPaths
relDirPaths <- filterDirs fpAbsDirPath fpRelItemPaths
return (relFilePaths, relDirPaths)
where
fpAbsDirPath :: FilePath
fpAbsDirPath = P.toFilePath absDirPath
fpAbsDirPath = toFilePath absDirPath
filterFiles :: FilePath -> [FilePath] -> IO [P.Path P.Rel P.File]
filterFiles :: FilePath -> [FilePath] -> IO [Path' (Rel d) (File f)]
filterFiles absDir relItems =
filterM (Dir.doesFileExist . (absDir FilePath.</>)) relItems
>>= mapM P.parseRelFile
filterM (System.Directory.doesFileExist . (absDir FilePath.</>)) relItems
>>= mapM parseRelFile
filterDirs :: FilePath -> [FilePath] -> IO [P.Path P.Rel P.Dir]
filterDirs :: FilePath -> [FilePath] -> IO [Path' (Rel d) Dir']
filterDirs absDir relItems =
filterM (Dir.doesDirectoryExist . (absDir FilePath.</>)) relItems
>>= mapM P.parseRelDir
filterM (System.Directory.doesDirectoryExist . (absDir FilePath.</>)) relItems
>>= mapM parseRelDir

View File

@ -39,7 +39,7 @@ packages:
# using the same syntax as the packages field.
# (e.g., acme-missiles-0.3)
extra-deps:
- strong-path-1.0.0.0
- strong-path-1.1.0.0
- path-0.9.0
- path-io-1.6.3