Commit f00c864e authored by Mikhail Glushenkov's avatar Mikhail Glushenkov

Expose the .cabal pretty-printer as 'cabal format'.

Make it a hidden command for now, because it's not quite ready for general
consumption yet (fields are reordered, comments are not preserved, etc).

See #1504.
parent 1d8bb71e
......@@ -26,6 +26,7 @@ module Distribution.Client.Setup
, freezeCommand, FreezeFlags(..)
, getCommand, unpackCommand, GetFlags(..)
, checkCommand
, formatCommand
, uploadCommand, UploadFlags(..)
, reportCommand, ReportFlags(..)
, runCommand
......@@ -635,6 +636,16 @@ checkCommand = CommandUI {
commandOptions = \_ -> []
}
formatCommand :: CommandUI (Flag Verbosity)
formatCommand = CommandUI {
commandName = "format",
commandSynopsis = "Reformat the .cabal file using the standard style.",
commandDescription = Nothing,
commandUsage = \pname -> "Usage: " ++ pname ++ " format [FILE]\n",
commandDefaultFlags = toFlag normal,
commandOptions = \_ -> []
}
runCommand :: CommandUI (BuildFlags, BuildExFlags)
runCommand = CommandUI {
commandName = "run",
......
......@@ -25,6 +25,7 @@ import Distribution.Client.Setup
, FreezeFlags(..), freezeCommand
, GetFlags(..), getCommand, unpackCommand
, checkCommand
, formatCommand
, updateCommand
, ListFlags(..), listCommand
, InfoFlags(..), infoCommand
......@@ -102,6 +103,10 @@ import Distribution.Client.Utils (determineNumJobs
import Distribution.PackageDescription
( Executable(..) )
import Distribution.PackageDescription.Parse
( readPackageDescription )
import Distribution.PackageDescription.PrettyPrint
( writeGenericPackageDescription )
import Distribution.Simple.Build
( startInterpreter )
import Distribution.Simple.Command
......@@ -117,7 +122,8 @@ import qualified Distribution.Simple.LocalBuildInfo as LBI
import Distribution.Simple.Program (defaultProgramConfiguration)
import qualified Distribution.Simple.Setup as Cabal
import Distribution.Simple.Utils
( cabalVersion, die, notice, info, topHandler, findPackageDesc )
( cabalVersion, die, notice, info, topHandler
, findPackageDesc, tryFindPackageDesc )
import Distribution.Text
( display )
import Distribution.Verbosity as Verbosity
......@@ -220,6 +226,8 @@ mainWorker args = topHandler $
regVerbosity regDistPref
,testCommand `commandAddAction` testAction
,benchmarkCommand `commandAddAction` benchmarkAction
,hiddenCommand $
formatCommand `commandAddAction` formatAction
,hiddenCommand $
upgradeCommand `commandAddAction` upgradeAction
,hiddenCommand $
......@@ -857,6 +865,16 @@ checkAction verbosityFlag extraArgs _globalFlags = do
allOk <- Check.check (fromFlag verbosityFlag)
unless allOk exitFailure
formatAction :: Flag Verbosity -> [String] -> GlobalFlags -> IO ()
formatAction verbosityFlag extraArgs _globalFlags = do
let verbosity = fromFlag verbosityFlag
path <- case extraArgs of
[] -> do cwd <- getCurrentDirectory
tryFindPackageDesc cwd
(p:_) -> return p
pkgDesc <- readPackageDescription verbosity path
-- Uses 'writeFileAtomic' under the hood.
writeGenericPackageDescription path pkgDesc
sdistAction :: (SDistFlags, SDistExFlags) -> [String] -> GlobalFlags -> IO ()
sdistAction (sdistFlags, sdistExFlags) extraArgs _globalFlags = do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment