Skip to content

Commit

Permalink
Say the help message when given no targets.
Browse files Browse the repository at this point in the history
  • Loading branch information
kindaro committed Sep 6, 2022
1 parent 1bfc094 commit 4dc9ac0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cabal-prettify.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cabal-prettify
version: 0.2.0.8
version: 0.2.1.0
author: Ignat Insarov
maintainer: kindaro@gmail.com
tested-with: GHC == 8.10.7, GHC == 9.0.1, GHC == 9.2.1
Expand All @@ -14,6 +14,7 @@ common commons
, Cabal
, directory
, filepath
, generic-deriving
, mtl
, optparse-applicative
, parsec
Expand All @@ -25,6 +26,7 @@ common commons
default-extensions:
ApplicativeDo
BlockArguments
DeriveGeneric
DuplicateRecordFields
FlexibleContexts
FlexibleInstances
Expand Down
37 changes: 25 additions & 12 deletions executables/cabal-prettify/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import Control.Monad.Trans.Writer
import System.Directory
import Control.Monad
import System.IO.Error
import GHC.Generics (Generic)
import Generics.Deriving.Monoid
import Generics.Deriving.Semigroup

import Distribution.Prettify

Expand Down Expand Up @@ -46,17 +49,27 @@ run Command {..} = do
if and outcomes then exitSuccess else exitWith (ExitFailure 1)

data Targets = Targets
{ thisPackage Bool
, standardInput Bool
{ thisPackage Any
, standardInput Any
, arguments [FilePath]
} deriving (Eq, Ord, Show, Read)

parseTargets Parser Targets
parseTargets = do
thisPackage switch (long "this" <> help "Prettify the configuration file of the package you are in right now.")
standardInput switch (long "filter" <> help "Prettify standard input.")
arguments many (argument str (metavar "{cabal files}"))
pure Targets {..}
} deriving (Eq, Ord, Show, Read, Generic)
instance Semigroup Targets where (<>) = gsappenddefault
instance Monoid Targets where mempty = gmemptydefault

checkFlag Mod FlagFields ( ) Parser ( )
checkFlag = flag' ( )

parseThisPackage, parseStandardInput, parseArguments, parseTargets Parser Targets
parseThisPackage = do
checkFlag (long "this" <> help "Prettify the configuration file of the package you are in right now.")
pure do mempty {thisPackage = Any True}
parseStandardInput = do
checkFlag (long "filter" <> help "Prettify standard input.")
pure do mempty {standardInput = Any True}
parseArguments = do
arguments some (argument (str @FilePath) (metavar "{cabal files}"))
pure do mempty {arguments = arguments}
parseTargets = fmap mconcat do some (parseThisPackage <|> parseStandardInput <|> parseArguments)

data Settings = Settings
{ check Bool
Expand All @@ -77,10 +90,10 @@ data Action = Action
processTargetsWithSettings Targets -> Settings -> IO [Action]
processTargetsWithSettings Targets {..} settings = (fmap catMaybes sequence fmap sequence execWriter) do
say do
whence thisPackage do
whence (getAny thisPackage) do
pathToCabalFile Cabal.defaultPackageDesc Cabal.normal
pure Action {target = Just pathToCabalFile, ..}
say do whence standardInput (pure Action {target = Nothing, ..})
say do whence (getAny standardInput) (pure Action {target = Nothing, ..})
tell do for arguments \ pathToCabalFile Just (pure Action {target = Just pathToCabalFile, ..})

runAction Action IO Bool
Expand Down

0 comments on commit 4dc9ac0

Please sign in to comment.