Skip to content

Commit 7671eb2

Browse files
committed
Merge pull request #424 from markus1189/reduce-duplication
Reduce duplication in file resolver functions
2 parents 7e1b0fb + dbd9036 commit 7671eb2

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

src/Path/IO.hs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,33 +72,35 @@ resolveFile x y =
7272
where fp = toFilePath x FP.</> y
7373
Just fp -> return fp
7474

75+
-- Internal helper to define resolveDirMaybe and resolveFileMaybe in one
76+
resolveCheckParse :: (Functor m, MonadIO m)
77+
=> (FilePath -> IO Bool) -- check if file/dir does exist
78+
-> (FilePath -> m a) -- parse into absolute file/dir
79+
-> Path Abs Dir
80+
-> FilePath
81+
-> m (Maybe a)
82+
resolveCheckParse check parse x y = do
83+
let fp = toFilePath x FP.</> y
84+
exists <- liftIO $ check fp
85+
if exists
86+
then do
87+
canonic <- liftIO $ canonicalizePath fp
88+
fmap Just (parse canonic)
89+
else return Nothing
90+
7591
-- | Appends a stringly-typed relative path to an absolute path, and then
7692
-- canonicalizes it. If the path doesn't exist (and therefore cannot
7793
-- be canonicalized, 'Nothing' is returned).
7894
resolveDirMaybe :: (MonadIO m,MonadThrow m)
7995
=> Path Abs Dir -> FilePath -> m (Maybe (Path Abs Dir))
80-
resolveDirMaybe x y = do
81-
let fp = toFilePath x FP.</> y
82-
exists <- liftIO $ doesDirectoryExist fp
83-
if exists
84-
then do
85-
dir <- liftIO $ canonicalizePath fp
86-
liftM Just (parseAbsDir dir)
87-
else return Nothing
96+
resolveDirMaybe = resolveCheckParse doesDirectoryExist parseAbsDir
8897

8998
-- | Appends a stringly-typed relative path to an absolute path, and then
9099
-- canonicalizes it. If the path doesn't exist (and therefore cannot
91100
-- be canonicalized, 'Nothing' is returned).
92101
resolveFileMaybe :: (MonadIO m,MonadThrow m)
93102
=> Path Abs Dir -> FilePath -> m (Maybe (Path Abs File))
94-
resolveFileMaybe x y = do
95-
let fp = toFilePath x FP.</> y
96-
exists <- liftIO $ doesFileExist fp
97-
if exists
98-
then do
99-
file <- liftIO $ canonicalizePath fp
100-
liftM Just (parseAbsFile file)
101-
else return Nothing
103+
resolveFileMaybe = resolveCheckParse doesFileExist parseAbsFile
102104

103105
-- | List objects in a directory, excluding "@.@" and "@..@". Entries are not sorted.
104106
listDirectory :: (MonadIO m,MonadThrow m) => Path Abs Dir -> m ([Path Abs Dir],[Path Abs File])

src/Stack/Package.hs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -724,40 +724,34 @@ buildLogPath package' = do
724724
]
725725
return $ stack </> $(mkRelDir "logs") </> fp
726726

727+
-- Internal helper to define resolveFileOrWarn and resolveDirOrWarn
728+
resolveOrWarn :: (MonadLogger m, MonadIO m, MonadReader (Path Abs File) m)
729+
=> Text
730+
-> (Path Abs Dir -> String -> m (Maybe a))
731+
-> FilePath.FilePath
732+
-> m (Maybe a)
733+
resolveOrWarn subject resolver path =
734+
do cwd <- getWorkingDir
735+
file <- ask
736+
dir <- asks parent
737+
result <- resolver dir path
738+
when (isNothing result) $
739+
$logWarn ("Warning: " <> subject <> " listed in " <>
740+
T.pack (maybe (FL.toFilePath file) FL.toFilePath (stripDir cwd file)) <>
741+
" file does not exist: " <>
742+
T.pack path)
743+
return result
744+
727745
-- | Resolve the file, if it can't be resolved, warn for the user
728746
-- (purely to be helpful).
729747
resolveFileOrWarn :: (MonadThrow m,MonadIO m,MonadLogger m,MonadReader (Path Abs File) m)
730748
=> FilePath.FilePath
731749
-> m (Maybe (Path Abs File))
732-
resolveFileOrWarn y =
733-
do cwd <- getWorkingDir
734-
file <- ask
735-
dir <- asks parent
736-
result <- resolveFileMaybe dir y
737-
case result of
738-
Nothing ->
739-
$logWarn ("Warning: File listed in " <>
740-
T.pack (maybe (FL.toFilePath file) FL.toFilePath (stripDir cwd file)) <>
741-
" file does not exist: " <>
742-
T.pack y)
743-
_ -> return ()
744-
return result
750+
resolveFileOrWarn = resolveOrWarn "File" resolveFileMaybe
745751

746752
-- | Resolve the directory, if it can't be resolved, warn for the user
747753
-- (purely to be helpful).
748754
resolveDirOrWarn :: (MonadThrow m,MonadIO m,MonadLogger m,MonadReader (Path Abs File) m)
749755
=> FilePath.FilePath
750756
-> m (Maybe (Path Abs Dir))
751-
resolveDirOrWarn y =
752-
do cwd <- getWorkingDir
753-
file <- ask
754-
dir <- asks parent
755-
result <- resolveDirMaybe dir y
756-
case result of
757-
Nothing ->
758-
$logWarn ("Warning: Directory listed in " <>
759-
T.pack (maybe (FL.toFilePath file) FL.toFilePath (stripDir cwd file)) <>
760-
" file does not exist: " <>
761-
T.pack y)
762-
_ -> return ()
763-
return result
757+
resolveDirOrWarn = resolveOrWarn "Directory" resolveDirMaybe

0 commit comments

Comments
 (0)