Skip to content

Commit 8c73e51

Browse files
committed
Address review feedback
1 parent ad9ddfd commit 8c73e51

File tree

14 files changed

+113
-107
lines changed

14 files changed

+113
-107
lines changed

hs-bindgen/app/HsBindgen/App.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ parseConfig = Config
162162
<*> parseSelectPredicate
163163
<*> parseProgramSlicing
164164
<*> parsePathStyle
165-
<*> parseFileOverwritePolicy
166165

167166
{-------------------------------------------------------------------------------
168167
Binding specifications

hs-bindgen/app/HsBindgen/Cli/GenTests.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ parseOpts =
5959
exec :: GlobalOpts -> Opts -> IO ()
6060
exec GlobalOpts{..} Opts{..} = do
6161
let artefact = writeTests output
62-
bindgenConfig = toBindgenConfig config uniqueId defHsModuleName
62+
-- AllowFileOverwrite for tests
63+
bindgenConfig = toBindgenConfig config AllowFileOverwrite uniqueId defHsModuleName
6364
void $ hsBindgen tracerConfig bindgenConfig inputs artefact

hs-bindgen/app/HsBindgen/Cli/Info/IncludeGraph.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ data Opts = Opts {
4040
, hsModuleName :: Hs.ModuleName
4141
, output :: Maybe FilePath
4242
, inputs :: [UncheckedHashIncludeArg]
43+
, fileOverwritePolicy :: FileOverwritePolicy
4344
}
4445

4546
parseOpts :: Parser Opts
@@ -50,6 +51,7 @@ parseOpts =
5051
<*> parseHsModuleName
5152
<*> optional parseOutput'
5253
<*> parseInputs
54+
<*> parseFileOverwritePolicy
5355

5456
parseOutput' :: Parser FilePath
5557
parseOutput' = strOption $ mconcat [
@@ -65,6 +67,6 @@ parseOutput' = strOption $ mconcat [
6567

6668
exec :: GlobalOpts -> Opts -> IO ()
6769
exec GlobalOpts{..} Opts{..} = do
68-
let artefact = writeIncludeGraph (fileOverwritePolicy config) output
69-
bindgenConfig = toBindgenConfig config uniqueId hsModuleName
70+
let artefact = writeIncludeGraph output
71+
bindgenConfig = toBindgenConfig config fileOverwritePolicy uniqueId hsModuleName
7072
void $ hsBindgen tracerConfig bindgenConfig inputs artefact

hs-bindgen/app/HsBindgen/Cli/Info/UseDeclGraph.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ data Opts = Opts {
4040
, hsModuleName :: Hs.ModuleName
4141
, output :: Maybe FilePath
4242
, inputs :: [UncheckedHashIncludeArg]
43+
, fileOverwritePolicy :: FileOverwritePolicy
4344
}
4445

4546
parseOpts :: Parser Opts
@@ -50,6 +51,7 @@ parseOpts =
5051
<*> parseHsModuleName
5152
<*> optional parseOutput'
5253
<*> parseInputs
54+
<*> parseFileOverwritePolicy
5355

5456
parseOutput' :: Parser FilePath
5557
parseOutput' = strOption $ mconcat [
@@ -65,6 +67,6 @@ parseOutput' = strOption $ mconcat [
6567

6668
exec :: GlobalOpts -> Opts -> IO ()
6769
exec GlobalOpts{..} Opts{..} = do
68-
let artefact = writeUseDeclGraph (fileOverwritePolicy config) output
69-
bindgenConfig = toBindgenConfig config uniqueId hsModuleName
70+
let artefact = writeUseDeclGraph output
71+
bindgenConfig = toBindgenConfig config fileOverwritePolicy uniqueId hsModuleName
7072
void $ hsBindgen tracerConfig bindgenConfig inputs artefact

hs-bindgen/app/HsBindgen/Cli/Internal/Frontend.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ data Opts = Opts {
3939
, uniqueId :: UniqueId
4040
, hsModuleName :: Hs.ModuleName
4141
, inputs :: [UncheckedHashIncludeArg]
42+
, fileOverwritePolicy :: FileOverwritePolicy
4243
}
4344

4445
parseOpts :: Parser Opts
@@ -48,6 +49,7 @@ parseOpts =
4849
<*> parseUniqueId
4950
<*> parseHsModuleName
5051
<*> parseInputs
52+
<*> parseFileOverwritePolicy
5153

5254
{-------------------------------------------------------------------------------
5355
Execution
@@ -56,5 +58,5 @@ parseOpts =
5658
exec :: GlobalOpts -> Opts -> IO ()
5759
exec GlobalOpts{..} Opts{..} = do
5860
let artefact = ReifiedC >>= liftIO . print
59-
bindgenConfig = toBindgenConfig config uniqueId hsModuleName
61+
bindgenConfig = toBindgenConfig config fileOverwritePolicy uniqueId hsModuleName
6062
hsBindgen tracerConfig bindgenConfig inputs artefact

hs-bindgen/app/HsBindgen/Cli/Preprocess.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ data Opts = Opts {
4747
, outputBindingSpec :: Maybe FilePath
4848
-- NOTE: Inputs (arguments) must be last, options must go before it.
4949
, inputs :: [UncheckedHashIncludeArg]
50+
, fileOverwritePolicy :: FileOverwritePolicy
5051
}
5152
deriving (Generic)
5253

@@ -60,6 +61,7 @@ parseOpts =
6061
<*> parseOutputDirPolicy
6162
<*> optional parseGenBindingSpec
6263
<*> parseInputs
64+
<*> parseFileOverwritePolicy
6365

6466
{-------------------------------------------------------------------------------
6567
Execution
@@ -79,15 +81,15 @@ exec GlobalOpts{..} Opts{..} = do
7981
void $ run $ artefacts
8082
where
8183
bindgenConfig :: BindgenConfig
82-
bindgenConfig = toBindgenConfig config uniqueId hsModuleName
84+
bindgenConfig = toBindgenConfig config fileOverwritePolicy uniqueId hsModuleName
8385

8486
run :: Artefact a -> IO a
8587
run = hsBindgen tracerConfig bindgenConfig inputs
8688

8789
artefacts :: Artefact ()
8890
artefacts = do
89-
writeBindingsMultiple (fileOverwritePolicy config) hsOutputDir
90-
forM_ outputBindingSpec (writeBindingSpec (fileOverwritePolicy config))
91+
writeBindingsMultiple hsOutputDir
92+
forM_ outputBindingSpec writeBindingSpec
9193

9294
{-------------------------------------------------------------------------------
9395
Exception

hs-bindgen/app/HsBindgen/Cli/ToolSupport/Literate.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ info = progDesc $ mconcat [
4848
data Opts = Opts {
4949
input :: FilePath
5050
, output :: FilePath
51+
, fileOverwritePolicy :: FileOverwritePolicy
5152
}
5253
deriving (Show, Eq)
5354

@@ -64,6 +65,8 @@ parseOpts = do
6465

6566
input <- strArgument $ metavar "IN"
6667
output <- strArgument $ metavar "OUT"
68+
69+
fileOverwritePolicy <- parseFileOverwritePolicy
6770
return Opts{..}
6871

6972
{-------------------------------------------------------------------------------
@@ -112,9 +115,9 @@ exec literateOpts = do
112115
Lit{..} <- maybe (throwIO' "cannot parse arguments in literate file") return $
113116
pureParseLit args
114117
let GlobalOpts{..} = globalOpts
115-
bindgenConfig = toBindgenConfig config uniqueId hsModuleName
118+
bindgenConfig = toBindgenConfig config (literateOpts.fileOverwritePolicy ) uniqueId hsModuleName
116119
void $ hsBindgen tracerConfig bindgenConfig inputs $
117-
writeBindings (fileOverwritePolicy config) safety (Just literateOpts.output)
120+
writeBindings safety (Just literateOpts.output)
118121
where
119122
throwIO' :: String -> IO a
120123
throwIO' = throwIO . LiterateFileException literateOpts.input

hs-bindgen/src-internal/HsBindgen.hs

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ module HsBindgen
1616
) where
1717

1818
import Control.Monad (join)
19-
import Control.Monad.Trans.Reader (ask)
2019
import Data.Foldable qualified as Foldable
2120
import Data.Map qualified as Map
2221
import Data.Text qualified as T
23-
import System.Directory (createDirectoryIfMissing, doesFileExist)
24-
import System.FilePath (takeDirectory, (<.>), (</>))
22+
import System.FilePath ((<.>), (</>))
2523

2624
import HsBindgen.Artefact
2725
import 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).
114113
getBindings :: 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).
131130
getBindingsMultiple :: 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.
159157
writeTests :: 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

190176
writeByCategory ::
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

Comments
 (0)