Skip to content

Commit

Permalink
Merge pull request #6548 from phadej/man-command
Browse files Browse the repository at this point in the history
Change manpage command to man
  • Loading branch information
phadej authored Feb 21, 2020
2 parents f554761 + 91ac075 commit e75c9f5
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
shell: bash
run: |
cabal v2-build cabal-install:exe:cabal
cp dist-newstyle/build/x86_64-windows/ghc-8.6.5/cabal-install-3.3.0.0/build/cabal/cabal.exe cabal.exe
cp dist-newstyle/build/x86_64-windows/ghc-8.6.5/cabal-install-3.3.0.0/x/cabal/build/cabal/cabal.exe cabal.exe
- name: Smoke test
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion boot/ci-artifacts.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
shell: bash
run: |
cabal v2-build cabal-install:exe:cabal
cp dist-newstyle/build/x86_64-windows/ghc-8.6.5/cabal-install-3.3.0.0/build/cabal/cabal.exe cabal.exe
cp dist-newstyle/build/x86_64-windows/ghc-8.6.5/cabal-install-3.3.0.0/x/cabal/build/cabal/cabal.exe cabal.exe
- name: Smoke test
shell: bash
run: |
Expand Down
3 changes: 3 additions & 0 deletions cabal-install/Distribution/Client/Compat/Process.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import qualified System.Process as P
-- exception. This variant catches \"does not exist\" and
-- \"permission denied\" exceptions and turns them into
-- @ExitFailure@s.
--
-- TODO: this doesn't use 'Distrubution.Compat.Process'.
--
readProcessWithExitCode :: FilePath -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode cmd args input =
P.readProcessWithExitCode cmd args input
Expand Down
48 changes: 45 additions & 3 deletions cabal-install/Distribution/Client/Manpage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,65 @@
module Distribution.Client.Manpage
( -- * Manual page generation
manpage
, manpageCmd
, ManpageFlags
, defaultManpageFlags
, manpageOptions
) where

import Distribution.Client.Compat.Prelude
import Prelude ()

import Distribution.Client.ManpageFlags
import Distribution.Client.Setup (globalCommand)
import Distribution.Compat.Process (createProcess)
import Distribution.Simple.Command
import Distribution.Client.Setup (globalCommand)
import Distribution.Simple.Flag (fromFlagOrDefault)
import System.Exit (exitWith)
import System.IO (hClose, hPutStr)

import Data.Char (toUpper)
import Data.List (intercalate)
import qualified System.Process as Process

data FileInfo = FileInfo String String -- ^ path, description

-------------------------------------------------------------------------------
--
-------------------------------------------------------------------------------

-- | A list of files that should be documented in the manual page.
files :: [FileInfo]
files =
[ (FileInfo "~/.cabal/config" "The defaults that can be overridden with command-line options.")
, (FileInfo "~/.cabal/world" "A list of all packages whose installation has been explicitly requested.")
]

manpageCmd :: String -> [CommandSpec a] -> ManpageFlags -> IO ()
manpageCmd pname commands flags
| fromFlagOrDefault False (manpageRaw flags)
= putStrLn contents
| otherwise
= do
let cmd = "man"
args = ["-l", "-"]

(mb_in, _, _, ph) <- createProcess (Process.proc cmd args)
{ Process.std_in = Process.CreatePipe
, Process.std_out = Process.Inherit
, Process.std_err = Process.Inherit
}

-- put contents
for_ mb_in $ \hin -> do
hPutStr hin contents
hClose hin

-- wait for process to exit, propagate exit code
ec <- Process.waitForProcess ph
exitWith ec
where
contents :: String
contents = manpage pname commands

-- | Produces a manual page with @troff@ markup.
manpage :: String -> [CommandSpec a] -> String
manpage pname commands = unlines $
Expand Down
40 changes: 40 additions & 0 deletions cabal-install/Distribution/Client/ManpageFlags.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
module Distribution.Client.ManpageFlags
( ManpageFlags (..)
, defaultManpageFlags
, manpageOptions,
) where

import Distribution.Client.Compat.Prelude

import Distribution.Simple.Command (OptionField (..), ShowOrParseArgs (..), option)
import Distribution.Simple.Setup (Flag (..), toFlag, trueArg, optionVerbosity)
import Distribution.Verbosity (Verbosity, normal)

data ManpageFlags = ManpageFlags
{ manpageVerbosity :: Flag Verbosity
, manpageRaw :: Flag Bool
} deriving (Eq, Show, Generic)

instance Monoid ManpageFlags where
mempty = gmempty
mappend = (<>)

instance Semigroup ManpageFlags where
(<>) = gmappend

defaultManpageFlags :: ManpageFlags
defaultManpageFlags = ManpageFlags
{ manpageVerbosity = toFlag normal
, manpageRaw = toFlag False
}

manpageOptions :: ShowOrParseArgs -> [OptionField ManpageFlags]
manpageOptions _ =
[ optionVerbosity manpageVerbosity (\v flags -> flags { manpageVerbosity = v })
, option "" ["raw"]
"Output raw troff content"
manpageRaw (\v flags -> flags { manpageRaw = v })
trueArg
]
11 changes: 6 additions & 5 deletions cabal-install/Distribution/Client/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ import Distribution.Client.GlobalFlags
( GlobalFlags(..), defaultGlobalFlags
, RepoContext(..), withRepoContext
)
import Distribution.Client.ManpageFlags (ManpageFlags, defaultManpageFlags, manpageOptions)

import Data.List
( deleteFirstsBy )
Expand Down Expand Up @@ -1435,16 +1436,16 @@ uninstallCommand = CommandUI {
commandOptions = \_ -> []
}

manpageCommand :: CommandUI (Flag Verbosity)
manpageCommand :: CommandUI ManpageFlags
manpageCommand = CommandUI {
commandName = "manpage",
commandName = "man",
commandSynopsis = "Outputs manpage source.",
commandDescription = Just $ \_ ->
"Output manpage source to STDOUT.\n",
commandNotes = Nothing,
commandUsage = usageFlags "manpage",
commandDefaultFlags = toFlag normal,
commandOptions = \_ -> [optionVerbosity id const]
commandUsage = usageFlags "man",
commandDefaultFlags = defaultManpageFlags,
commandOptions = manpageOptions
}

runCommand :: CommandUI (BuildFlags, BuildExFlags)
Expand Down
64 changes: 2 additions & 62 deletions cabal-install/Setup.hs
Original file line number Diff line number Diff line change
@@ -1,63 +1,3 @@
import Distribution.PackageDescription ( PackageDescription )
import Distribution.Simple ( defaultMainWithHooks
, simpleUserHooks
, postBuild
, postCopy
, postInst
)
import Distribution.Simple.InstallDirs ( mandir
, CopyDest (NoCopyDest)
)
import Distribution.Simple.LocalBuildInfo ( LocalBuildInfo(..)
, absoluteInstallDirs
)
import Distribution.Simple.Utils ( installOrdinaryFiles
, notice )
import Distribution.Simple.Setup ( buildVerbosity
, copyDest
, copyVerbosity
, fromFlag
, installVerbosity
)
import Distribution.Verbosity ( Verbosity )

import System.IO ( openFile
, IOMode (WriteMode)
)
import System.Process ( runProcess )
import System.FilePath ( (</>) )

-- WARNING to editors of this file:
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- At this moment (Cabal 1.23), whatever you write here must be
-- compatible with ALL Cabal libraries which we support bootstrapping
-- with. This is because pre-setup-depends versions of cabal-install will
-- build Setup.hs against the version of Cabal which MATCHES the library
-- that cabal-install was built against. There is no way of overriding
-- this behavior without bumping the required 'cabal-version' in our
-- Cabal file. Travis will let you know if we fail to install from
-- tarball!

import Distribution.Simple
main :: IO ()
main = defaultMainWithHooks $ simpleUserHooks
{ postBuild = \ _ flags _ lbi ->
buildManpage lbi (fromFlag $ buildVerbosity flags)
, postCopy = \ _ flags pkg lbi ->
installManpage pkg lbi (fromFlag $ copyVerbosity flags) (fromFlag $ copyDest flags)
, postInst = \ _ flags pkg lbi ->
installManpage pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
}

buildManpage :: LocalBuildInfo -> Verbosity -> IO ()
buildManpage lbi verbosity = do
let cabal = buildDir lbi </> "cabal/cabal"
manpage = buildDir lbi </> "cabal/cabal.1"
manpageHandle <- openFile manpage WriteMode
notice verbosity ("Generating manual page " ++ manpage ++ " ...")
_ <- runProcess cabal ["manpage"] Nothing Nothing Nothing (Just manpageHandle) Nothing
return ()

installManpage :: PackageDescription -> LocalBuildInfo -> Verbosity -> CopyDest -> IO ()
installManpage pkg lbi verbosity copy = do
let destDir = mandir (absoluteInstallDirs pkg lbi copy) </> "man1"
installOrdinaryFiles verbosity destDir [(buildDir lbi </> "cabal", "cabal.1")]
main = defaultMain
10 changes: 2 additions & 8 deletions cabal-install/cabal-install.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Author: Cabal Development Team (see AUTHORS file)
Maintainer: Cabal Development Team <cabal-devel@haskell.org>
Copyright: 2003-2019, Cabal Development Team
Category: Distribution
Build-type: Custom
Build-type: Simple
Extra-Source-Files:
README.md bash-completion/cabal bootstrap.sh changelog

Expand Down Expand Up @@ -126,13 +126,6 @@ Flag lukko
default: True
manual: True

custom-setup
setup-depends:
Cabal >= 2.2,
base,
process >= 1.1.0.1 && < 1.7,
filepath >= 1.3 && < 1.5

executable cabal
main-is: Main.hs
hs-source-dirs: main
Expand Down Expand Up @@ -218,6 +211,7 @@ executable cabal
Distribution.Client.JobControl
Distribution.Client.List
Distribution.Client.Manpage
Distribution.Client.ManpageFlags
Distribution.Client.Nix
Distribution.Client.Outdated
Distribution.Client.PackageHash
Expand Down
15 changes: 1 addition & 14 deletions cabal-install/cabal-install.cabal.pp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
Distribution.Client.JobControl
Distribution.Client.List
Distribution.Client.Manpage
Distribution.Client.ManpageFlags
Distribution.Client.Nix
Distribution.Client.Outdated
Distribution.Client.PackageHash
Expand Down Expand Up @@ -278,11 +279,7 @@
Maintainer: Cabal Development Team <cabal-devel@haskell.org>
Copyright: 2003-2019, Cabal Development Team
Category: Distribution
%if CABAL_FLAG_LIB
Build-type: Simple
%else
Build-type: Custom
%endif
Extra-Source-Files:
README.md bash-completion/cabal bootstrap.sh changelog
Expand Down Expand Up @@ -390,16 +387,6 @@
default: True
manual: True
%if CABAL_FLAG_LIB
%else
custom-setup
setup-depends:
Cabal >= 2.2,
base,
process >= 1.1.0.1 && < 1.7,
filepath >= 1.3 && < 1.5
%endif
#
# Libraries, if CABAL_FLAG_LIB
#
Expand Down
11 changes: 6 additions & 5 deletions cabal-install/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ import Distribution.Client.Sandbox.Types (UseSandbox(..), whenUsingSandbox)
import Distribution.Client.Tar (createTarGzFile)
import Distribution.Client.Types (Password (..))
import Distribution.Client.Init (initCabal)
import Distribution.Client.Manpage (manpage)
import Distribution.Client.Manpage (manpageCmd)
import Distribution.Client.ManpageFlags (ManpageFlags (..))
import qualified Distribution.Client.Win32SelfUpgrade as Win32SelfUpgrade
import Distribution.Client.Utils (determineNumJobs
,relaxEncodingErrors
Expand Down Expand Up @@ -1244,13 +1245,13 @@ actAsSetupAction actAsSetupFlags args _globalFlags =
Make -> Make.defaultMainArgs args
Custom -> error "actAsSetupAction Custom"

manpageAction :: [CommandSpec action] -> Flag Verbosity -> [String] -> Action
manpageAction commands flagVerbosity extraArgs _ = do
let verbosity = fromFlag flagVerbosity
manpageAction :: [CommandSpec action] -> ManpageFlags -> [String] -> Action
manpageAction commands flags extraArgs _ = do
let verbosity = fromFlag (manpageVerbosity flags)
unless (null extraArgs) $
die' verbosity $ "'manpage' doesn't take any extra arguments: " ++ unwords extraArgs
pname <- getProgName
let cabalCmd = if takeExtension pname == ".exe"
then dropExtension pname
else pname
putStrLn $ manpage cabalCmd commands
manpageCmd cabalCmd commands flags
2 changes: 1 addition & 1 deletion cabal-testsuite/PackageTests/Manpage/cabal.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# cabal manpage
# cabal man
2 changes: 1 addition & 1 deletion cabal-testsuite/PackageTests/Manpage/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Test.Cabal.Prelude
main = cabalTest $ do
r <- cabal' "manpage" []
r <- cabal' "man" ["--raw"]
assertOutputContains ".B cabal install" r
assertOutputDoesNotContain ".B cabal manpage" r
2 changes: 1 addition & 1 deletion cabal-testsuite/Test/Cabal/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ cabalG' global_args cmd args = do
-- Sandboxes manage dist dir
| testHaveSandbox env
= install_args
| cmd `elem` ["v1-update", "outdated", "user-config", "manpage", "v1-freeze", "check"]
| cmd `elem` ["v1-update", "outdated", "user-config", "man", "v1-freeze", "check"]
= [ ]
-- new-build commands are affected by testCabalProjectFile
| cmd == "v2-sdist" = [ "--project-file", testCabalProjectFile env ]
Expand Down

0 comments on commit e75c9f5

Please sign in to comment.