on master help sign in
Support formatting multiple files at once
3a093e6mrkkrp
app/Main.hs+40−28
@@ -23,30 +23,43 @@ import qualified Data.Text.IO as TIO
23main :: IO ()
23main :: IO ()
24main = withPrettyOrmoluExceptions $ do
24main = withPrettyOrmoluExceptions $ do
25 Opts {..} <- execParser optsParserInfo
25 Opts {..} <- execParser optsParserInfo
26− r <- case optInputFile of
26+ let formatOne' = formatOne optMode optConfig
27+ case optInputFiles of
28+ [] -> formatOne' Nothing
29+ ["-"] -> formatOne' Nothing
30+ xs -> mapM_ (formatOne' . Just) xs
31+
32+-- | Format a single input.
33+
34+formatOne
35+ :: Mode -- ^ Mode of operation
36+ -> Config -- ^ Configuration
37+ -> Maybe FilePath -- ^ File to format or stdin as 'Nothing'
38+ -> IO ()
39+formatOne mode config = \case
40+ Nothing -> do
27− "-" -> ormoluStdin optConfig
41+ r <- ormoluStdin config
28− inputFile -> ormoluFile optConfig inputFile
42+ case mode of
29− let notForStdin = do
43+ Stdout -> TIO.putStr r
30− when (optInputFile == "-") $ do
44+ _ -> do
31− hPutStrLn
45+ hPutStrLn stderr
32− stderr
33− "This feature is not supported when input comes from stdin."
46+ "This feature is not supported when input comes from stdin."
34 -- 101 is different from all the other exit codes we already use.
47 -- 101 is different from all the other exit codes we already use.
35− exitWith (ExitFailure 101)
48+ exitWith (ExitFailure 101)
49+ Just inputFile -> do
50+ r <- ormoluFile config inputFile
36− case optMode of
51+ case mode of
37− Stdout ->
52+ Stdout ->
38− TIO.putStr r
53+ TIO.putStr r
39− InPlace -> do
54+ InPlace ->
40− notForStdin
41− TIO.writeFile optInputFile r
55+ TIO.writeFile inputFile r
42− Check -> do
56+ Check -> do
43− notForStdin
44− r' <- TIO.readFile optInputFile
57+ r' <- TIO.readFile inputFile
45− when (r /= r') $
58+ when (r /= r') $
46− -- 100 is different to all the other exit code that are emitted
59+ -- 100 is different to all the other exit code that are emitted
47− -- either from an 'OrmoluException' or from 'error' and
60+ -- either from an 'OrmoluException' or from 'error' and
48− -- 'notImplemented'.
61+ -- 'notImplemented'.
49− exitWith (ExitFailure 100)
62+ exitWith (ExitFailure 100)
50
63
51----------------------------------------------------------------------------
64----------------------------------------------------------------------------
52-- Command line options parsing.
65-- Command line options parsing.
@@ -56,8 +69,8 @@ data Opts = Opts
56 -- ^ Mode of operation
69 -- ^ Mode of operation
57 , optConfig :: !Config
70 , optConfig :: !Config
58 -- ^ Ormolu 'Config'
71 -- ^ Ormolu 'Config'
59− , optInputFile :: !FilePath
72+ , optInputFiles :: ![FilePath]
60− -- ^ Input source file or stdin ("-")
73+ -- ^ Haskell source files to format or stdin (when the list is empty)
61 }
74 }
62
75
63-- | Mode of operation.
76-- | Mode of operation.
@@ -108,10 +121,9 @@ optsParser = Opts
108 , help "Mode of operation: 'stdout', 'inplace', or 'check'"
121 , help "Mode of operation: 'stdout', 'inplace', or 'check'"
109 ]
122 ]
110 <*> configParser
123 <*> configParser
111− <*> (strArgument . mconcat)
124+ <*> (many . strArgument . mconcat)
112 [ metavar "FILE"
125 [ metavar "FILE"
113− , value "-"
114− , help "Haskell source file to format or stdin (default)"
126+ , help "Haskell source files to format or stdin (default)"
115 ]
127 ]
116
128
117configParser :: Parser Config
129configParser :: Parser Config