From cad7e40ae59141319f705d06ee6f479fea7705c8 Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Thu, 4 Jul 2019 00:52:35 +0000 Subject: [PATCH] Allow reading from stdin --- app/Main.hs | 30 ++++++++++++++++++++++-------- src/Ormolu.hs | 14 ++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 568e5c4..d2c824b 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -13,6 +13,7 @@ import Options.Applicative import Ormolu import Paths_ormolu (version) import System.Exit (ExitCode (..), exitWith) +import System.IO (hPutStrLn, stderr) import qualified Data.Text.IO as TIO import qualified Data.Yaml as Yaml @@ -26,18 +27,30 @@ main = withPrettyOrmoluExceptions $ do Just path -> do config <- Yaml.decodeFileThrow path return (config <> optConfig) - r <- ormoluFile config optInputFile + r <- case optInputFile of + "-" -> ormoluStdin config + inputFile -> ormoluFile config inputFile + let notForStdin = do + when (optInputFile == "-") $ do + hPutStrLn + stderr + "This feature is not supported when input comes from stdin." + -- 101 is different from all the other exit codes we already use. + exitWith (ExitFailure 101) case optMode of Stdout -> TIO.putStr r - InPlace -> + InPlace -> do + notForStdin TIO.writeFile optInputFile r Check -> do + notForStdin r' <- TIO.readFile optInputFile - when (r /= r') . exitWith $ - ExitFailure 100 -- 100 is different to all the other exit code that - -- are emitted either from an 'OrmoluException' or - -- from 'error' and 'notImplemented'. + when (r /= r') $ + -- 100 is different to all the other exit code that are emitted + -- either from an 'OrmoluException' or from 'error' and + -- 'notImplemented'. + exitWith (ExitFailure 100) ---------------------------------------------------------------------------- -- Command line options parsing. @@ -50,7 +63,7 @@ data Opts = Opts , optConfig :: !Config -- ^ Ormolu 'Config' , optInputFile :: !FilePath - -- ^ Input source file + -- ^ Input source file or stdin ("-") } -- | Mode of operation. @@ -103,7 +116,8 @@ optsParser = Opts <*> configParser <*> (strArgument . mconcat) [ metavar "FILE" - , help "Haskell source file to format" + , value "-" + , help "Haskell source file to format or stdin (default)" ] configParser :: Parser Config diff --git a/src/Ormolu.hs b/src/Ormolu.hs index c50aa4e..b52966c 100644 --- a/src/Ormolu.hs +++ b/src/Ormolu.hs @@ -5,6 +5,7 @@ module Ormolu ( ormolu , ormoluFile + , ormoluStdin , Config (..) , defaultConfig , DynOption (..) @@ -28,6 +29,7 @@ import Ormolu.Utils (showOutputable) import qualified CmdLineParser as GHC import qualified Data.Text as T import qualified GHC +import System.IO (hGetContents, stdin) -- | Format a 'String', return formatted version as 'Text'. -- @@ -77,6 +79,18 @@ ormoluFile ormoluFile cfg path = liftIO (readFile path) >>= ormolu cfg path +-- | Read input from stdin and format it. +-- +-- > ormoluStdin cfg = +-- > liftIO (hGetContents stdin) >>= ormolu cfg "" + +ormoluStdin + :: MonadIO m + => Config -- ^ Ormolu configuration + -> m Text -- ^ Resulting rendition +ormoluStdin cfg = + liftIO (hGetContents stdin) >>= ormolu cfg "" + ---------------------------------------------------------------------------- -- Helpers