@@ -16,12 +16,10 @@ module HsBindgen
1616 ) where
1717
1818import Control.Monad (join )
19- import Control.Monad.Trans.Reader (ask )
2019import Data.Foldable qualified as Foldable
2120import Data.Map qualified as Map
2221import Data.Text qualified as T
23- import System.Directory (createDirectoryIfMissing , doesFileExist )
24- import System.FilePath (takeDirectory , (<.>) , (</>) )
22+ import System.FilePath ((<.>) , (</>) )
2523
2624import HsBindgen.Artefact
2725import HsBindgen.Backend
@@ -73,6 +71,7 @@ hsBindgen
7371 -- 4. Artefacts.
7472 withTracerSafe tracerConfigSafe $ \ tracerSafe -> do
7573 runArtefacts
74+ (backendFileOverwrite bindgenBackendConfig)
7675 tracerSafe
7776 tracerUnsafeRef
7877 bootArtefact
@@ -96,19 +95,19 @@ hsBindgen
9695-------------------------------------------------------------------------------}
9796
9897-- | Write the include graph to `STDOUT` or a file.
99- writeIncludeGraph :: FileOverwritePolicy -> Maybe FilePath -> Artefact ()
100- writeIncludeGraph fop mPath = do
98+ writeIncludeGraph :: Maybe FilePath -> Artefact ()
99+ writeIncludeGraph mPath = do
101100 (p, includeGraph) <- IncludeGraph
102- Lift $ write fop " include graph" mPath
103- $ IncludeGraph. dumpMermaid p includeGraph
101+ write " include graph" mPath $
102+ IncludeGraph. dumpMermaid p includeGraph
104103
105104-- | Write @use-decl@ graph to file.
106- writeUseDeclGraph :: FileOverwritePolicy -> Maybe FilePath -> Artefact ()
107- writeUseDeclGraph fop mPath = do
105+ writeUseDeclGraph :: Maybe FilePath -> Artefact ()
106+ writeUseDeclGraph mPath = do
108107 index <- DeclIndex
109108 useDeclGraph <- UseDeclGraph
110- Lift $ write fop " use-decl graph" mPath
111- $ UseDeclGraph. dumpMermaid index useDeclGraph
109+ write " use-decl graph" mPath $
110+ UseDeclGraph. dumpMermaid index useDeclGraph
112111
113112-- | Get bindings (single module).
114113getBindings :: Safety -> Artefact String
@@ -122,10 +121,10 @@ getBindings safety = do
122121-- | Write bindings to file.
123122--
124123-- If no file is given, print to standard output.
125- writeBindings :: FileOverwritePolicy -> Safety -> Maybe FilePath -> Artefact ()
126- writeBindings fop safety mPath = do
124+ writeBindings :: Safety -> Maybe FilePath -> Artefact ()
125+ writeBindings safety mPath = do
127126 bindings <- getBindings safety
128- Lift $ write fop " bindings" mPath bindings
127+ write " bindings" mPath bindings
129128
130129-- | Get bindings (one module per binding category).
131130getBindingsMultiple :: Artefact (ByCategory String )
@@ -136,24 +135,23 @@ getBindingsMultiple = fmap render <$> FinalModules
136135-- Each file contains a different binding category.
137136--
138137-- If no file is given, print to standard output.
139- writeBindingsMultiple :: FileOverwritePolicy -> FilePath -> Artefact ()
140- writeBindingsMultiple fop hsOutputDir = do
138+ writeBindingsMultiple :: FilePath -> Artefact ()
139+ writeBindingsMultiple hsOutputDir = do
141140 moduleBaseName <- FinalModuleBaseName
142141 bindingsByCategory <- getBindingsMultiple
143- Lift $ writeByCategory " bindings" hsOutputDir fop moduleBaseName bindingsByCategory
142+ writeByCategory " bindings" hsOutputDir moduleBaseName bindingsByCategory
144143
145144-- | Write binding specifications to file.
146- writeBindingSpec :: FileOverwritePolicy -> FilePath -> Artefact ()
147- writeBindingSpec fop path = do
145+ writeBindingSpec :: FilePath -> Artefact ()
146+ writeBindingSpec path = do
148147 moduleBaseName <- FinalModuleBaseName
149148 getMainHeaders <- GetMainHeaders
150149 omitTypes <- OmitTypes
151150 hsDecls <- HsDecls
152- tracer <- Lift $ artefactTracer <$> ask
153- traceWith tracer $ RunArtefactWriteFile " binding specifications" path
154151 -- Binding specifications only specify types.
155- liftIO $ genBindingSpec moduleBaseName path fop getMainHeaders omitTypes $
156- fromMaybe [] (Map. lookup BType $ unByCategory hsDecls)
152+ let bindingSpec = genBindingSpec moduleBaseName getMainHeaders omitTypes $
153+ fromMaybe [] (Map. lookup BType $ unByCategory hsDecls)
154+ PendingFileWrite " binding specifications" path (BindingSpecContent bindingSpec)
157155
158156-- | Create test suite in directory.
159157writeTests :: FilePath -> Artefact ()
@@ -171,40 +169,27 @@ writeTests testDir = do
171169 Helpers
172170-------------------------------------------------------------------------------}
173171
174- write :: FileOverwritePolicy -> String -> Maybe FilePath -> String -> ArtefactM ()
175- write _ _ Nothing str = liftIO $ putStrLn str
176- write fop what (Just path) str = do
177- tracer <- artefactTracer <$> ask
178- traceWith tracer $ RunArtefactWriteFile what path
179- liftIO $ do
180- -- Check if file exists and policy forbids overwriting
181- fileExists <- doesFileExist path
182- case fop of
183- ProtectExistingFiles
184- | fileExists ->
185- throwIO (FileAlreadyExistsException path)
186- _ -> do
187- createDirectoryIfMissing True $ takeDirectory path
188- writeFile path str
172+ write :: String -> Maybe FilePath -> String -> Artefact ()
173+ write _ Nothing str = liftIO $ putStrLn str
174+ write what (Just path) str = PendingFileWrite what path (TextContent str)
189175
190176writeByCategory ::
191177 String
192178 -> FilePath
193- -> FileOverwritePolicy
194179 -> Hs. ModuleName
195180 -> ByCategory String
196- -> ArtefactM ()
197- writeByCategory what hsOutputDir fop moduleBaseName =
198- mapM_ (uncurry writeCategory) . Map. toList . unByCategory
181+ -> Artefact ()
182+ writeByCategory what hsOutputDir moduleBaseName =
183+ Foldable. foldl' (>>) ( pure () ) . map (uncurry writeCategory) . Map. toList . unByCategory
199184 where
200- writeCategory :: BindingCategory -> String -> ArtefactM ()
201- writeCategory cat str = do
185+ writeCategory :: BindingCategory -> String -> Artefact ()
186+ writeCategory cat str =
202187 let addSubModule = case cat of
203188 BType -> id
204189 otherCat -> (</> displayBindingCategory otherCat)
205190 path = addSubModule baseFilePath <.> " hs"
206191 whatWithCategory = what ++ " (" ++ show cat ++ " )"
207- write fop whatWithCategory (Just path) str
192+ in write whatWithCategory (Just path) str
208193
209194 baseFilePath :: FilePath
210195 baseFilePath = Foldable. foldl' (</>) " " $
0 commit comments