Skip to content

Commit da68c28

Browse files
ulysses4everhvrmietek
committed
Add cabal get --only-package-description (#8263)
With this option, 'cabal get' writes to the destination directory only the package description already available locally in one of the repository indices. The basename of the file name written to inside the target directory is the package-id rather than only the package name. This is mostly based on #1977 Co-authored-by: Miëtek Bak <[email protected]> Co-authored-by: Artem Pelenitsyn <[email protected]> Co-authored-by: Herbert Valerio Riedel <[email protected]> Co-authored-by: Miëtek Bak <[email protected]>
1 parent 0c4d048 commit da68c28

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

cabal-install/src/Distribution/Client/Get.hs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Distribution.Package
3131
import Distribution.Simple.Setup
3232
( Flag(..), fromFlag, fromFlagOrDefault, flagToMaybe )
3333
import Distribution.Simple.Utils
34-
( notice, die', info, writeFileAtomic )
34+
( notice, die', info, warn, writeFileAtomic )
3535
import qualified Distribution.PackageDescription as PD
3636
import Distribution.Simple.Program
3737
( programName )
@@ -49,8 +49,11 @@ import qualified Distribution.Client.Tar as Tar (extractTarGzFile)
4949
import Distribution.Client.IndexUtils
5050
( getSourcePackagesAtIndexState, TotalIndexState, ActiveRepos )
5151
import Distribution.Solver.Types.SourcePackage
52+
import Distribution.PackageDescription.PrettyPrint
53+
( writeGenericPackageDescription )
5254

5355
import qualified Data.Map as Map
56+
import Control.Monad ( mapM_ )
5457
import System.Directory
5558
( createDirectoryIfMissing, doesDirectoryExist, doesFileExist )
5659
import System.FilePath
@@ -94,16 +97,26 @@ get verbosity repoCtxt _ getFlags userTargets = do
9497
unless (null prefix) $
9598
createDirectoryIfMissing True prefix
9699

97-
if useSourceRepo
98-
then clone pkgs
99-
else unpack pkgs
100+
if onlyPkgDescr
101+
then do
102+
when useSourceRepo $
103+
warn verbosity $
104+
"Ignoring --source-repository for --only-package-description"
105+
106+
mapM_ (unpackOnlyPkgDescr verbosity prefix) pkgs
107+
else
108+
if useSourceRepo
109+
then clone pkgs
110+
else unpack pkgs
100111

101112
where
102113
resolverParams :: SourcePackageDb -> [PackageSpecifier UnresolvedSourcePackage] -> DepResolverParams
103114
resolverParams sourcePkgDb pkgSpecifiers =
104115
--TODO: add command-line constraint and preference args for unpack
105116
standardInstallPolicy mempty sourcePkgDb pkgSpecifiers
106117

118+
onlyPkgDescr = fromFlagOrDefault False (getOnlyPkgDescr getFlags)
119+
107120
prefix :: String
108121
prefix = fromFlagOrDefault "" (getDestDir getFlags)
109122

@@ -189,6 +202,23 @@ unpackPackage verbosity prefix pkgid descOverride pkgPath = do
189202
writeFileAtomic descFilePath pkgtxt
190203

191204

205+
-- | Write a @pkgId.cabal@ file with the package description to the destination
206+
-- directory, unless one already exists.
207+
unpackOnlyPkgDescr :: Verbosity -> FilePath -> UnresolvedSourcePackage -> IO ()
208+
unpackOnlyPkgDescr verbosity dstDir pkg = do
209+
let pkgFile = dstDir </> prettyShow (packageId pkg) <.> "cabal"
210+
existsFile <- doesFileExist pkgFile
211+
when existsFile $ die' verbosity $
212+
"The file \"" ++ pkgFile ++ "\" already exists, not overwriting."
213+
existsDir <- doesDirectoryExist (addTrailingPathSeparator pkgFile)
214+
when existsDir $ die' verbosity $
215+
"A directory \"" ++ pkgFile ++ "\" is in the way, not unpacking."
216+
notice verbosity $ "Writing package description to " ++ pkgFile
217+
case srcpkgDescrOverride pkg of
218+
Just pkgTxt -> writeFileAtomic pkgFile pkgTxt
219+
Nothing ->
220+
writeGenericPackageDescription pkgFile (srcpkgDescription pkg)
221+
192222
-- ------------------------------------------------------------
193223
-- * Cloning packages from their declared source repositories
194224
-- ------------------------------------------------------------

cabal-install/src/Distribution/Client/Setup.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,7 @@ instance Semigroup ReportFlags where
12971297

12981298
data GetFlags = GetFlags {
12991299
getDestDir :: Flag FilePath,
1300+
getOnlyPkgDescr :: Flag Bool,
13001301
getPristine :: Flag Bool,
13011302
getIndexState :: Flag TotalIndexState,
13021303
getActiveRepos :: Flag ActiveRepos,
@@ -1307,6 +1308,7 @@ data GetFlags = GetFlags {
13071308
defaultGetFlags :: GetFlags
13081309
defaultGetFlags = GetFlags {
13091310
getDestDir = mempty,
1311+
getOnlyPkgDescr = mempty,
13101312
getPristine = mempty,
13111313
getIndexState = mempty,
13121314
getActiveRepos = mempty,
@@ -1352,6 +1354,16 @@ getCommand = CommandUI {
13521354
(toFlag `fmap` parsec))
13531355
(flagToList . fmap prettyShow))
13541356

1357+
, option [] ["only-package-description"]
1358+
"Unpack only the package description file."
1359+
getOnlyPkgDescr (\v flags -> flags { getOnlyPkgDescr = v })
1360+
trueArg
1361+
1362+
, option [] ["package-description-only"]
1363+
"A synonym for --only-package-description."
1364+
getOnlyPkgDescr (\v flags -> flags { getOnlyPkgDescr = v })
1365+
trueArg
1366+
13551367
, option [] ["pristine"]
13561368
("Unpack the original pristine tarball, rather than updating the "
13571369
++ ".cabal file with the latest revision from the package archive.")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# cabal v2-update
2+
Downloading the latest package list from test-local-repo
3+
# cabal update
4+
Downloading the latest package list from test-local-repo
5+
# cabal get
6+
Writing package description to criterion-1.1.4.0.cabal
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- 2022-07-03, issue #1954
2+
--
3+
-- Purpose of this test:
4+
-- Make sure that `cabal get --only-package-description` works
5+
6+
import Test.Cabal.Prelude
7+
main = cabalTest $ withRepo "repo" $ do
8+
cabal "update" []
9+
cabal
10+
"get"
11+
[ "criterion", "--only-package-description" ]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: criterion
2+
version: 1.1.4.0
3+
build-type: Simple
4+
cabal-version: >= 1.10
5+
6+
library
7+
build-depends: base, ghc-prim
8+
default-language: Haskell2010

changelog.d/issue-1954

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
synopsis: Add `cabal get --only-package-description`
2+
packages: cabal-install
3+
prs: #1977 #5162 #8263
4+
issues: #1954

changelog.d/issue-7462

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
synopsis: `cabal haddock` now implies `--enable-documentation`
2+
packages: Cabal
3+
issues: #1919
4+
prs: #7827

doc/cabal-package.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,6 +2865,9 @@ The ``get`` command supports the following options:
28652865
``2016-09-24T17:47:48Z``), or ``HEAD`` (default).
28662866
This determines which package versions are available as well as which
28672867
``.cabal`` file revision is selected (unless ``--pristine`` is used).
2868+
``--only-package-description``
2869+
Unpack only the package description file. A synonym,
2870+
``--package-description-only``, is provided for convenience.
28682871
``--pristine``
28692872
Unpack the original pristine tarball, rather than updating the
28702873
``.cabal`` file with the latest revision from the package archive.

0 commit comments

Comments
 (0)