Skip to content

Loading…

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

Merged
merged 1 commit into from

2 participants

@23Skidoo
Haskell member

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.

@23Skidoo 23Skidoo 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.
f00c864
@tibbe
Haskell member

:+1:

@23Skidoo
Haskell member

I don't think that this is controversial in the current state, so merging.

@23Skidoo 23Skidoo merged commit 2968b52 into haskell:master

1 check passed

Details default The Travis CI build passed
@23Skidoo 23Skidoo deleted the 23Skidoo:cabal-format branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 3, 2014
  1. @23Skidoo

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

    23Skidoo committed
    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.
Showing with 30 additions and 1 deletion.
  1. +11 −0 cabal-install/Distribution/Client/Setup.hs
  2. +19 −1 cabal-install/Main.hs
View
11 cabal-install/Distribution/Client/Setup.hs
@@ -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",
View
20 cabal-install/Main.hs
@@ -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
@@ -221,6 +227,8 @@ mainWorker args = topHandler $
,testCommand `commandAddAction` testAction
,benchmarkCommand `commandAddAction` benchmarkAction
,hiddenCommand $
+ formatCommand `commandAddAction` formatAction
+ ,hiddenCommand $
upgradeCommand `commandAddAction` upgradeAction
,hiddenCommand $
win32SelfUpgradeCommand`commandAddAction` win32SelfUpgradeAction
@@ -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
Something went wrong with that request. Please try again.