Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cabal get --only-package-description #5162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions cabal-install/Distribution/Client/Get.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import Distribution.Package
import Distribution.Simple.Setup
( Flag(..), fromFlag, fromFlagOrDefault, flagToMaybe )
import Distribution.Simple.Utils
( notice, die', info, rawSystemExitCode, writeFileAtomic )
( notice, die', info, warn, rawSystemExitCode, writeFileAtomic )
import Distribution.Verbosity
( Verbosity )
import Distribution.Text(display)
import qualified Distribution.PackageDescription as PD
import Distribution.PackageDescription.PrettyPrint
( writeGenericPackageDescription )

import Distribution.Client.Setup
( GlobalFlags(..), GetFlags(..), RepoContext(..) )
Expand Down Expand Up @@ -98,15 +100,24 @@ get verbosity repoCtxt globalFlags getFlags userTargets = do
unless (null prefix) $
createDirectoryIfMissing True prefix

if useFork
then fork pkgs
else unpack pkgs
if onlyPkgDescr
then do
when useFork $
warn verbosity $
"Ignoring --source-repository for --only-package-description"

mapM_ (unpackOnlyPkgDescr verbosity prefix) pkgs
else
if useFork
then fork pkgs
else unpack pkgs

where
resolverParams sourcePkgDb pkgSpecifiers =
--TODO: add command-line constraint and preference args for unpack
standardInstallPolicy mempty sourcePkgDb pkgSpecifiers

onlyPkgDescr = fromFlagOrDefault False (getOnlyPkgDescr getFlags)
prefix = fromFlagOrDefault "" (getDestDir getFlags)

fork :: [UnresolvedSourcePackage] -> IO ()
Expand Down Expand Up @@ -176,6 +187,22 @@ unpackPackage verbosity prefix pkgid descOverride pkgPath = do
++ " with the latest revision from the index."
writeFileAtomic descFilePath pkgtxt

-- | Write a @pkgId.cabal@ file with the package description to the destination
-- directory, unless one already exists.
unpackOnlyPkgDescr :: Verbosity -> FilePath -> UnresolvedSourcePackage -> IO ()
unpackOnlyPkgDescr verbosity dstDir pkg = do
let pkgFile = dstDir </> display (packageId pkg) <.> "cabal"
existsFile <- doesFileExist pkgFile
when existsFile $ die' verbosity $
"The file \"" ++ pkgFile ++ "\" already exists, not overwriting."
existsDir <- doesDirectoryExist (addTrailingPathSeparator pkgFile)
when existsDir $ die' verbosity $
"A directory \"" ++ pkgFile ++ "\" is in the way, not unpacking."
notice verbosity $ "Writing package description to " ++ pkgFile
case packageDescrOverride pkg of
Just pkgTxt -> writeFileAtomic pkgFile pkgTxt
Nothing ->
writeGenericPackageDescription pkgFile (packageDescription pkg)

-- ------------------------------------------------------------
-- * Forking the source repository
Expand Down
12 changes: 12 additions & 0 deletions cabal-install/Distribution/Client/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ instance Semigroup ReportFlags where

data GetFlags = GetFlags {
getDestDir :: Flag FilePath,
getOnlyPkgDescr :: Flag Bool,
getPristine :: Flag Bool,
getIndexState :: Flag IndexState,
getSourceRepository :: Flag (Maybe RepoKind),
Expand All @@ -1271,6 +1272,7 @@ data GetFlags = GetFlags {
defaultGetFlags :: GetFlags
defaultGetFlags = GetFlags {
getDestDir = mempty,
getOnlyPkgDescr = mempty,
getPristine = mempty,
getIndexState = mempty,
getSourceRepository = mempty,
Expand Down Expand Up @@ -1324,6 +1326,16 @@ getCommand = CommandUI {
(toFlag `fmap` parse))
(flagToList . fmap display))

, option [] ["only-package-description"]
"Unpack only the package description file."
getOnlyPkgDescr (\v flags -> flags { getOnlyPkgDescr = v })
trueArg

, option [] ["package-description-only"]
"A synonym for --only-package-description."
getOnlyPkgDescr (\v flags -> flags { getOnlyPkgDescr = v })
trueArg

, option [] ["pristine"]
("Unpack the original pristine tarball, rather than updating the "
++ ".cabal file with the latest revision from the package archive.")
Expand Down