on master help sign in
Format the source of the application as well
commita9f8926c8dd886047d75efc92de7b814d9e7fb8f authorMark Karpov <mark.karpov@tweag.io> dateFri 24 Jan 2020, 16:06 (7 years ago) parentd1c7606
2 files changed+107−92
app/Main.hs+104−90 format.sh+3−2
app/Main.hs+104−90
@@ -1,12 +1,16 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE LambdaCase #-}
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE LambdaCase #-}
33{-# LANGUAGE RecordWildCards #-}
44{-# LANGUAGE TemplateHaskell #-}
55
6−module Main (main) where
6+module Main
7+ ( main,
8+ )
9+where
710
811import Control.Monad
912import Data.List (intercalate, sort)
13+import qualified Data.Text.IO as TIO
1014import Data.Version (showVersion)
1115import Development.GitRev
1216import Options.Applicative
@@ -16,10 +20,8 @@ import Ormolu.Utils (showOutputable)
1620import Paths_ormolu (version)
1721import System.Exit (ExitCode (..), exitWith)
1822import System.IO (hPutStrLn, stderr)
19−import qualified Data.Text.IO as TIO
2023
2124-- | Entry point of the program.
22−
2325main :: IO ()
2426main = withPrettyOrmoluExceptions $ do
2527 Opts {..} <- execParser optsParserInfo
@@ -30,21 +32,24 @@ main = withPrettyOrmoluExceptions $ do
3032 xs -> mapM_ (formatOne' . Just) xs
3133
3234-- | 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 ()
35+formatOne ::
36+ -- | Mode of operation
37+ Mode ->
38+ -- | Configuration
39+ Config ->
40+ -- | File to format or stdin as 'Nothing'
41+ Maybe FilePath ->
42+ IO ()
3943formatOne mode config = \case
4044 Nothing -> do
4145 r <- ormoluStdin config
4246 case mode of
4347 Stdout -> TIO.putStr r
44− _ -> do
45− hPutStrLn stderr
48+ _ -> do
49+ hPutStrLn
50+ stderr
4651 "This feature is not supported when input comes from stdin."
47− -- 101 is different from all the other exit codes we already use.
52+ -- 101 is different from all the other exit codes we already use.
4853 exitWith (ExitFailure 101)
4954 Just inputFile -> do
5055 r <- ormoluFile config inputFile
@@ -64,102 +69,111 @@ formatOne mode config = \case
6469----------------------------------------------------------------------------
6570-- Command line options parsing.
6671
67−data Opts = Opts
68− { optMode :: !Mode
69− -- ^ Mode of operation
70− , optConfig :: !Config
71− -- ^ Ormolu 'Config'
72− , optInputFiles :: ![FilePath]
73− -- ^ Haskell source files to format or stdin (when the list is empty)
74− }
72+data Opts
73+ = Opts
74+ { -- | Mode of operation
75+ optMode :: !Mode,
76+ -- | Ormolu 'Config'
77+ optConfig :: !Config,
78+ -- | Haskell source files to format or stdin (when the list is empty)
79+ optInputFiles :: ![FilePath]
80+ }
7581
7682-- | Mode of operation.
77−
7883data Mode
79− = Stdout -- ^ Output formatted source code to stdout
80− | InPlace -- ^ Overwrite original file
81− | Check -- ^ Exit with non-zero status code if
82− -- source is not already formatted
84+ = -- | Output formatted source code to stdout
85+ Stdout
86+ | -- | Overwrite original file
87+ InPlace
88+ | -- | Exit with non-zero status code if
89+ -- source is not already formatted
90+ Check
8391 deriving (Eq, Show)
8492
8593optsParserInfo :: ParserInfo Opts
86−optsParserInfo = info (helper <*> ver <*> exts <*> optsParser) . mconcat $
87− [ fullDesc
88− , progDesc ""
89− , header ""
90− ]
94+optsParserInfo =
95+ info (helper <*> ver <*> exts <*> optsParser) . mconcat $
96+ [ fullDesc,
97+ progDesc "",
98+ header ""
99+ ]
91100 where
92101 ver :: Parser (a -> a)
93− ver = infoOption verStr . mconcat $
94− [ long "version"
95− , short 'v'
96− , help "Print version of the program"
97− ]
98− verStr = intercalate "\n"
99− [ unwords
100− [ "ormolu"
101− , showVersion version
102− , $gitBranch
103− , $gitHash
102+ ver =
103+ infoOption verStr . mconcat $
104+ [ long "version",
105+ short 'v',
106+ help "Print version of the program"
107+ ]
108+ verStr =
109+ intercalate
110+ "\n"
111+ [ unwords
112+ [ "ormolu",
113+ showVersion version,
114+ $gitBranch,
115+ $gitHash
116+ ],
117+ "using ghc-lib-parser " ++ VERSION_ghc_lib_parser
104118 ]
105− , "using ghc-lib-parser " ++ VERSION_ghc_lib_parser
106− ]
107119 exts :: Parser (a -> a)
108− exts = infoOption displayExts . mconcat $
109− [ long "manual-exts"
110− , help "Display extensions that need to be enabled manually"
111− ]
120+ exts =
121+ infoOption displayExts . mconcat $
122+ [ long "manual-exts",
123+ help "Display extensions that need to be enabled manually"
124+ ]
112125 displayExts = unlines $ sort (showOutputable <$> manualExts)
113126
114127optsParser :: Parser Opts
115−optsParser = Opts
116− <$> (option parseMode . mconcat)
117− [ long "mode"
118− , short 'm'
119− , metavar "MODE"
120− , value Stdout
121− , help "Mode of operation: 'stdout' (default), 'inplace', or 'check'"
122− ]
123− <*> configParser
124− <*> (many . strArgument . mconcat)
125− [ metavar "FILE"
126− , help "Haskell source files to format or stdin (default)"
127− ]
128+optsParser =
129+ Opts
130+ <$> (option parseMode . mconcat)
131+ [ long "mode",
132+ short 'm',
133+ metavar "MODE",
134+ value Stdout,
135+ help "Mode of operation: 'stdout' (default), 'inplace', or 'check'"
136+ ]
137+ <*> configParser
138+ <*> (many . strArgument . mconcat)
139+ [ metavar "FILE",
140+ help "Haskell source files to format or stdin (default)"
141+ ]
128142
129143configParser :: Parser Config
130−configParser = Config
131− <$> (fmap (fmap DynOption) . many . strOption . mconcat)
132− [ long "ghc-opt"
133− , short 'o'
134− , metavar "OPT"
135− , help "GHC options to enable (e.g. language extensions)"
136− ]
137− <*> (switch . mconcat)
138− [ long "unsafe"
139− , short 'u'
140− , help "Do formatting faster but without automatic detection of defects"
141− ]
142− <*> (switch . mconcat)
143− [ long "debug"
144− , short 'd'
145− , help "Output information useful for debugging"
146− ]
147− <*> (switch . mconcat)
148− [ long "tolerate-cpp"
149− , short 'p'
150− , help "Do not fail if CPP pragma is present"
151− ]
152− <*> (switch . mconcat)
153− [ long "check-idempotency"
154− , short 'c'
155− , help "Fail if formatting is not idempotent"
156− ]
144+configParser =
145+ Config
146+ <$> (fmap (fmap DynOption) . many . strOption . mconcat)
147+ [ long "ghc-opt",
148+ short 'o',
149+ metavar "OPT",
150+ help "GHC options to enable (e.g. language extensions)"
151+ ]
152+ <*> (switch . mconcat)
153+ [ long "unsafe",
154+ short 'u',
155+ help "Do formatting faster but without automatic detection of defects"
156+ ]
157+ <*> (switch . mconcat)
158+ [ long "debug",
159+ short 'd',
160+ help "Output information useful for debugging"
161+ ]
162+ <*> (switch . mconcat)
163+ [ long "tolerate-cpp",
164+ short 'p',
165+ help "Do not fail if CPP pragma is present"
166+ ]
167+ <*> (switch . mconcat)
168+ [ long "check-idempotency",
169+ short 'c',
170+ help "Fail if formatting is not idempotent"
171+ ]
157172
158173----------------------------------------------------------------------------
159174-- Helpers
160175
161176-- | Parse 'Mode'.
162−
163177parseMode :: ReadM Mode
164178parseMode = eitherReader $ \case
165179 "stdout" -> Right Stdout
format.sh+3−2
@@ -7,5 +7,6 @@ set -e
77
88export LANG="C.UTF-8"
99
10−ormolu --mode inplace $(find src -type f \( -name "*.hs" -o -name "*.hs-boot" \))
11−ormolu --mode inplace $(find tests -type f -name "*.hs")
10+ormolu -p -m inplace $(find app -type f -name "*.hs")
11+ormolu -p -m inplace $(find src -type f \( -name "*.hs" -o -name "*.hs-boot" \))
12+ormolu -p -m inplace $(find tests -type f -name "*.hs")