@@ -19,7 +19,8 @@ import Data.Foldable (foldrM)
1919import Data.Foldable qualified as Foldable
2020import Data.Map qualified as Map
2121import Data.Text qualified as T
22- import System.Directory (createDirectoryIfMissing , doesFileExist )
22+ import System.Directory (createDirectoryIfMissing , doesDirectoryExist ,
23+ doesFileExist )
2324import System.FilePath (takeDirectory , (<.>) , (</>) )
2425
2526import HsBindgen.Artefact
@@ -82,7 +83,10 @@ hsBindgen
8283
8384 -- Execute file system actions based on FileOverwritePolicy
8485 value <- either throwIO pure result
85- executeFileSystemActions (backendFileOverwrite bindgenBackendConfig) fsActions
86+ executeFileSystemActions
87+ (backendOutputDirPolicy bindgenBackendConfig)
88+ (backendFileOverwrite bindgenBackendConfig)
89+ fsActions
8690 return value
8791 where
8892 tracerConfigSafe :: TracerConfig SafeLevel a
@@ -99,10 +103,23 @@ hsBindgen
99103-------------------------------------------------------------------------------}
100104
101105-- | Execute collected file system actions based on FileOverwritePolicy
102- executeFileSystemActions :: FileOverwritePolicy -> [FileSystemAction ] -> IO ()
103- executeFileSystemActions fop actions = do
106+ executeFileSystemActions :: OutputDirPolicy -> FileOverwritePolicy -> [FileSystemAction ] -> IO ()
107+ executeFileSystemActions outputDirPolicy fop actions = do
108+ -- Get the first directory path that exists if any
109+ mbDirPath <-
110+ foldrM (\ f mbp -> do
111+ case f of
112+ CreateDir path
113+ | Just _ <- mbp -> pure mbp
114+ | otherwise -> do
115+ fileExists <- doesDirectoryExist path
116+ pure $ if fileExists
117+ then Just path
118+ else mbp
119+ _ -> pure mbp
120+ ) Nothing actions
104121 -- Get the first file path that exists if any
105- mbPath <-
122+ mbFilePath <-
106123 foldrM (\ f mbp -> do
107124 case f of
108125 WriteFile _ path _
@@ -112,11 +129,19 @@ executeFileSystemActions fop actions = do
112129 pure $ if fileExists
113130 then Just path
114131 else mbp
132+ _ -> pure mbp
115133 ) Nothing actions
134+ case outputDirPolicy of
135+ DoNotCreateDirStructure
136+ | Just outputDir <- mbDirPath ->
137+ throwIO (OutputDirectoryMissingException outputDir)
138+ _ -> pure ()
116139 case fop of
117140 ProtectExistingFiles
118- | Just path <- mbPath -> throwIO (FileAlreadyExistsException path)
141+ | Just path <- mbFilePath -> throwIO (FileAlreadyExistsException path)
119142 _ -> forM_ actions $ \ case
143+ CreateDir outputDir ->
144+ createDirectoryIfMissing True outputDir
120145 WriteFile _ path content -> do
121146 createDirectoryIfMissing True (takeDirectory path)
122147 case content of
0 commit comments