Skip to content

Commit 698101b

Browse files
Provide multiple files to various CLI subcommands (#2169)
* dhall format multiple files * parse stdin as a file argument * dhall freeze multiple files * dhall lint multiple files * Remove unused PossiblyTransitiveInput * More context in CheckFailed * openapi-to-dhall: Fix use of format * Only throw CheckFailed after handling all inputs * Apply suggestions from code review Co-authored-by: Gabriel Gonzalez <Gabriel439@gmail.com> * Better format error message for MultipleChecksFailed * Add missing haddock for CheckFailed * Keep a temporary --inplace flag with a deprecated notice * Remove mentions of '--inplace' in Dhall/Tutorial Co-authored-by: Gabriel Gonzalez <Gabriel439@gmail.com>
1 parent 0b66d50 commit 698101b

File tree

7 files changed

+179
-112
lines changed

7 files changed

+179
-112
lines changed

dhall-openapi/openapi-to-dhall/Main.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ writeDhall path expr = do
7575

7676
let outputMode = Dhall.Util.Write
7777

78-
let input =
79-
Dhall.Util.PossiblyTransitiveInputFile
80-
path
81-
Dhall.Util.NonTransitive
78+
let inputs = pure (Dhall.Util.InputFile path)
79+
80+
let transitivity = Dhall.Util.NonTransitive
8281

8382
let formatOptions = Dhall.Format.Format{..}
8483

dhall/src/Dhall/Format.hs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,24 @@ module Dhall.Format
1111
, format
1212
) where
1313

14-
import Data.Foldable (for_)
15-
import Data.Maybe (fromMaybe)
16-
import Dhall.Pretty (CharacterSet, annToAnsiStyle, detectCharacterSet)
14+
import Data.Foldable (for_)
15+
import Data.List.NonEmpty (NonEmpty)
16+
import Data.Maybe (fromMaybe)
17+
import Dhall.Pretty
18+
( CharacterSet
19+
, annToAnsiStyle
20+
, detectCharacterSet
21+
)
1722
import Dhall.Util
1823
( Censor
1924
, CheckFailed (..)
2025
, Header (..)
26+
, Input (..)
2127
, OutputMode (..)
22-
, PossiblyTransitiveInput (..)
2328
, Transitivity (..)
29+
, handleMultipleChecksFailed
2430
)
2531

26-
import qualified Control.Exception
2732
import qualified Data.Text.IO
2833
import qualified Data.Text.Prettyprint.Doc as Pretty
2934
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
@@ -40,19 +45,21 @@ import qualified System.IO
4045
data Format = Format
4146
{ chosenCharacterSet :: Maybe CharacterSet
4247
, censor :: Censor
43-
, input :: PossiblyTransitiveInput
48+
, transitivity :: Transitivity
49+
, inputs :: NonEmpty Input
4450
, outputMode :: OutputMode
4551
}
4652

4753
-- | Implementation of the @dhall format@ subcommand
4854
format :: Format -> IO ()
49-
format (Format { input = input0, ..}) = go input0
55+
format (Format { inputs = inputs0, transitivity = transitivity0, ..}) =
56+
handleMultipleChecksFailed "format" "formatted" go inputs0
5057
where
5158
go input = do
5259
let directory = case input of
53-
NonTransitiveStandardInput ->
60+
StandardInput ->
5461
"."
55-
PossiblyTransitiveInputFile file _ ->
62+
InputFile file ->
5663
System.FilePath.takeDirectory file
5764

5865
let status = Dhall.Import.emptyStatus directory
@@ -66,16 +73,16 @@ format (Format { input = input0, ..}) = go input0
6673
<> "\n")
6774

6875
(originalText, transitivity) <- case input of
69-
PossiblyTransitiveInputFile file transitivity -> do
76+
InputFile file -> do
7077
text <- Data.Text.IO.readFile file
7178

72-
return (text, transitivity)
73-
74-
NonTransitiveStandardInput -> do
79+
return (text, transitivity0)
80+
StandardInput -> do
7581
text <- Data.Text.IO.getContents
7682

7783
return (text, NonTransitive)
7884

85+
7986
headerAndExpr@(_, parsedExpression) <- Dhall.Util.getExpressionAndHeaderFromStdinText censor originalText
8087

8188
case transitivity of
@@ -84,7 +91,7 @@ format (Format { input = input0, ..}) = go input0
8491
maybeFilepath <- Dhall.Import.dependencyToFile status import_
8592

8693
for_ maybeFilepath $ \filepath ->
87-
go (PossiblyTransitiveInputFile filepath Transitive)
94+
go (InputFile filepath)
8895

8996
NonTransitive ->
9097
return ()
@@ -94,16 +101,16 @@ format (Format { input = input0, ..}) = go input0
94101
let formattedText = Pretty.Text.renderStrict docStream
95102

96103
case outputMode of
97-
Write ->
104+
Write -> do
98105
case input of
99-
PossiblyTransitiveInputFile file _ ->
106+
InputFile file ->
100107
if originalText == formattedText
101108
then return ()
102109
else AtomicWrite.LazyText.atomicWriteFile
103110
file
104111
(Pretty.Text.renderLazy docStream)
105112

106-
NonTransitiveStandardInput -> do
113+
StandardInput -> do
107114
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
108115

109116
Pretty.Terminal.renderIO
@@ -112,12 +119,10 @@ format (Format { input = input0, ..}) = go input0
112119
then (fmap annToAnsiStyle docStream)
113120
else (Pretty.unAnnotateS docStream))
114121

115-
Check ->
116-
if originalText == formattedText
117-
then return ()
118-
else do
119-
let command = "format"
120-
121-
let modified = "formatted"
122+
return (Right ())
122123

123-
Control.Exception.throwIO CheckFailed{..}
124+
Check ->
125+
return $
126+
if originalText == formattedText
127+
then Right ()
128+
else Left CheckFailed{..}

dhall/src/Dhall/Freeze.hs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Dhall.Freeze
2222
) where
2323

2424
import Data.Foldable (for_)
25+
import Data.List.NonEmpty (NonEmpty)
2526
import Data.Maybe (fromMaybe)
2627
import Dhall.Pretty (CharacterSet, detectCharacterSet)
2728
import Dhall.Syntax
@@ -34,9 +35,10 @@ import Dhall.Util
3435
( Censor
3536
, CheckFailed (..)
3637
, Header (..)
38+
, Input (..)
3739
, OutputMode (..)
38-
, PossiblyTransitiveInput (..)
3940
, Transitivity (..)
41+
, handleMultipleChecksFailed
4042
)
4143
import System.Console.ANSI (hSupportsANSI)
4244

@@ -140,7 +142,8 @@ data Intent
140142
-- | Implementation of the @dhall freeze@ subcommand
141143
freeze
142144
:: OutputMode
143-
-> PossiblyTransitiveInput
145+
-> Transitivity
146+
-> NonEmpty Input
144147
-> Scope
145148
-> Intent
146149
-> Maybe CharacterSet
@@ -152,30 +155,32 @@ freeze = freezeWithManager Dhall.Import.defaultNewManager
152155
freezeWithManager
153156
:: IO Dhall.Import.Manager
154157
-> OutputMode
155-
-> PossiblyTransitiveInput
158+
-> Transitivity
159+
-> NonEmpty Input
156160
-> Scope
157161
-> Intent
158162
-> Maybe CharacterSet
159163
-> Censor
160164
-> IO ()
161-
freezeWithManager newManager outputMode input0 scope intent chosenCharacterSet censor = go input0
165+
freezeWithManager newManager outputMode transitivity0 inputs scope intent chosenCharacterSet censor =
166+
handleMultipleChecksFailed "freeze" "frozen" go inputs
162167
where
163168
go input = do
164169
let directory = case input of
165-
NonTransitiveStandardInput ->
170+
StandardInput ->
166171
"."
167-
PossiblyTransitiveInputFile file _ ->
172+
InputFile file ->
168173
System.FilePath.takeDirectory file
169174

170175
let status = Dhall.Import.emptyStatusWithManager newManager directory
171176

172177
(originalText, transitivity) <- case input of
173-
PossiblyTransitiveInputFile file transitivity -> do
178+
InputFile file -> do
174179
text <- Text.IO.readFile file
175180

176-
return (text, transitivity)
181+
return (text, transitivity0)
177182

178-
NonTransitiveStandardInput -> do
183+
StandardInput -> do
179184
text <- Text.IO.getContents
180185

181186
return (text, NonTransitive)
@@ -190,7 +195,7 @@ freezeWithManager newManager outputMode input0 scope intent chosenCharacterSet c
190195
maybeFilepath <- Dhall.Import.dependencyToFile status import_
191196

192197
for_ maybeFilepath $ \filepath ->
193-
go (PossiblyTransitiveInputFile filepath Transitive)
198+
go (InputFile filepath)
194199

195200
NonTransitive ->
196201
return ()
@@ -210,31 +215,29 @@ freezeWithManager newManager outputMode input0 scope intent chosenCharacterSet c
210215
let unAnnotated = Pretty.unAnnotateS stream
211216

212217
case input of
213-
PossiblyTransitiveInputFile file _ ->
218+
InputFile file ->
214219
if originalText == modifiedText
215220
then return ()
216221
else
217222
AtomicWrite.LazyText.atomicWriteFile
218223
file
219224
(Pretty.Text.renderLazy unAnnotated)
220225

221-
NonTransitiveStandardInput -> do
226+
StandardInput -> do
222227
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
223228
if supportsANSI
224229
then
225230
Pretty.renderIO System.IO.stdout (Dhall.Pretty.annToAnsiStyle <$> stream)
226231
else
227232
Pretty.renderIO System.IO.stdout unAnnotated
228233

229-
Check ->
230-
if originalText == modifiedText
231-
then return ()
232-
else do
233-
let command = "freeze"
234-
235-
let modified = "frozen"
234+
return (Right ())
236235

237-
Exception.throwIO CheckFailed{..}
236+
Check ->
237+
return $
238+
if originalText == modifiedText
239+
then Right ()
240+
else Left CheckFailed{..}
238241

239242
{-| Slightly more pure version of the `freeze` function
240243

0 commit comments

Comments
 (0)