1
1
mirror of https://github.com/sol/hpack.git synced 2024-10-04 03:38:00 +03:00

Add --silent (close #45)

This commit is contained in:
Simon Hengel 2015-08-30 09:38:05 +08:00
parent 34294f3ea2
commit a849214dfc
3 changed files with 34 additions and 20 deletions

View File

@ -39,5 +39,5 @@ To run `hpack` automatically on modifications to `package.yaml` add the
following to your `~/.vimrc`:
```vim
autocmd BufWritePost package.yaml silent !hpack > /dev/null
autocmd BufWritePost package.yaml silent !hpack --silent
```

View File

@ -2,14 +2,16 @@ module Main (main) where
import Prelude ()
import Prelude.Compat
import Control.Monad.Compat
import Control.DeepSeq
import Control.Exception
import System.IO
import System.IO.Error
import Control.Monad.Compat
import Data.List.Compat
import Data.Version (showVersion)
import Control.DeepSeq
import System.Environment
import System.Exit
import System.IO
import System.IO.Error
import Paths_hpack (version)
import Hpack.Config
@ -29,17 +31,29 @@ header = unlines [
main :: IO ()
main = do
args <- getArgs
if "--version" `elem` args
then putStrLn programVersion
else do
(warnings, name, new) <- run
forM_ warnings $ \warning -> hPutStrLn stderr ("WARNING: " ++ warning)
old <- force . either (const Nothing) (Just . stripHeader) <$> tryJust (guard . isDoesNotExistError) (readFile name)
if (old == Just (lines new)) then do
putStrLn (name ++ " is up-to-date")
else do
(writeFile name $ header ++ new)
putStrLn ("generated " ++ name)
where
stripHeader :: String -> [String]
stripHeader = dropWhile null . dropWhile ("--" `isPrefixOf`) . lines
case args of
["--version"] -> putStrLn programVersion
["--silent"] -> hpack False
[] -> hpack True
_ -> do
hPutStrLn stderr "Usage: hpack [ --version | --silent ]"
exitFailure
hpack :: Bool -> IO ()
hpack verbose = do
(warnings, name, new) <- run
forM_ warnings $ \warning -> hPutStrLn stderr ("WARNING: " ++ warning)
old <- force . either (const Nothing) (Just . stripHeader) <$> tryJust (guard . isDoesNotExistError) (readFile name)
if (old == Just (lines new)) then do
output (name ++ " is up-to-date")
else do
(writeFile name $ header ++ new)
output ("generated " ++ name)
where
stripHeader :: String -> [String]
stripHeader = dropWhile null . dropWhile ("--" `isPrefixOf`) . lines
output :: String -> IO ()
output message
| verbose = putStrLn message
| otherwise = return ()

View File

@ -1,4 +1,4 @@
-- This file has been generated from package.yaml by hpack version 0.5.3.
-- This file has been generated from package.yaml by hpack version 0.5.4.
--
-- see: https://github.com/sol/hpack