Skip to content

Commit 356bae1

Browse files
committed
Create source archives by running 'setup sdist --output-directory'.
Fixes #403.
1 parent b915e0c commit 356bae1

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed

Cabal/Distribution/Simple/SrcDist.hs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ module Distribution.Simple.SrcDist (
5454
-- * The top level action
5555
sdist,
5656

57-
-- * Actual implemenation of 'sdist', for reuse by 'cabal sdist'
58-
CreateArchiveFun,
59-
sdistWith,
60-
6157
-- ** Parts of 'sdist'
6258
printPackageProblems,
6359
prepareTree,
@@ -123,26 +119,15 @@ sdist :: PackageDescription -- ^information from the tarball
123119
-> [PPSuffixHandler] -- ^ extra preprocessors (includes suffixes)
124120
-> IO ()
125121
sdist pkg mb_lbi flags mkTmpDir pps =
126-
sdistWith pkg mb_lbi flags mkTmpDir pps createArchive
127-
128-
-- |Create a source distribution, parametrised by the createArchive function
129-
-- (for reuse by cabal-install).
130-
sdistWith :: PackageDescription -- ^information from the tarball
131-
-> Maybe LocalBuildInfo -- ^Information from configure
132-
-> SDistFlags -- ^verbosity & snapshot
133-
-> (FilePath -> FilePath) -- ^build prefix (temp dir)
134-
-> [PPSuffixHandler] -- ^extra preprocessors (includes
135-
-- suffixes)
136-
-> CreateArchiveFun
137-
-> IO ()
138-
sdistWith pkg mb_lbi flags mkTmpDir pps createArchiveFun = do
139122

140123
-- When given --list-sources, just output the list of sources to a file.
141124
case (sDistListSources flags) of
142125
Flag path -> withFile path WriteMode $ \outHandle -> do
143126
(ordinary, maybeExecutable) <- listPackageSources verbosity pkg pps
144127
mapM_ (hPutStrLn outHandle) ordinary
145128
mapM_ (hPutStrLn outHandle) maybeExecutable
129+
notice verbosity $ "List of package sources written to file '"
130+
++ path ++ "'"
146131
NoFlag -> do
147132
-- do some QA
148133
printPackageProblems verbosity pkg
@@ -164,7 +149,7 @@ sdistWith pkg mb_lbi flags mkTmpDir pps createArchiveFun = do
164149
withTempDirectory verbosity False tmpTargetDir "sdist." $ \tmpDir -> do
165150
let targetDir = tmpDir </> tarBallName pkg'
166151
generateSourceDir targetDir pkg'
167-
targzFile <- createArchiveFun verbosity pkg' mb_lbi tmpDir targetPref
152+
targzFile <- createArchive verbosity pkg' mb_lbi tmpDir targetPref
168153
notice verbosity $ "Source tarball created: " ++ targzFile
169154

170155
where

cabal-install/Distribution/Client/SrcDist.hs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ module Distribution.Client.SrcDist (
66
) where
77

88

9-
import Distribution.Simple.SrcDist
10-
( CreateArchiveFun, sdistWith )
9+
import Distribution.Client.SetupWrapper
10+
( SetupScriptOptions(..), defaultSetupScriptOptions, setupWrapper )
1111
import Distribution.Client.Tar (createTarGzFile)
1212

1313
import Distribution.Package
1414
( Package(..) )
1515
import Distribution.PackageDescription
1616
( PackageDescription )
17+
import Distribution.PackageDescription.Configuration
18+
( flattenPackageDescription )
1719
import Distribution.PackageDescription.Parse
1820
( readPackageDescription )
1921
import Distribution.Simple.Utils
20-
( defaultPackageDesc, die )
22+
( createDirectoryIfMissingVerbose, defaultPackageDesc
23+
, die, notice, withTempDirectory )
2124
import Distribution.Client.Setup
2225
( SDistFlags(..), SDistExFlags(..), ArchiveFormat(..) )
2326
import Distribution.Simple.Setup
24-
( fromFlag )
25-
import Distribution.Simple.PreProcess (knownSuffixHandlers)
27+
( Flag(..), sdistCommand, flagToList, fromFlag, fromFlagOrDefault )
2628
import Distribution.Simple.BuildPaths ( srcPref)
27-
import Distribution.Simple.Configure(maybeGetPersistBuildConfig)
28-
import Distribution.PackageDescription.Configuration
29-
( flattenPackageDescription )
3029
import Distribution.Simple.Program (requireProgram, simpleProgram, programPath)
3130
import Distribution.Simple.Program.Db (emptyProgramDb)
32-
import Distribution.Text
33-
( display )
31+
import Distribution.Text ( display )
32+
import Distribution.Verbosity (Verbosity)
33+
import Distribution.Version (Version(..), orLaterVersion)
3434

3535
import System.FilePath ((</>), (<.>))
3636
import Control.Monad (when, unless)
@@ -42,15 +42,42 @@ import System.Exit (ExitCode(..))
4242
sdist :: SDistFlags -> SDistExFlags -> IO ()
4343
sdist flags exflags = do
4444
pkg <- return . flattenPackageDescription
45-
=<< readPackageDescription verbosity
46-
=<< defaultPackageDesc verbosity
47-
mb_lbi <- maybeGetPersistBuildConfig distPref
45+
=<< readPackageDescription verbosity
46+
=<< defaultPackageDesc verbosity
47+
let withDir = if isOutDirectory then (\f -> f tmpTargetDir)
48+
else withTempDirectory verbosity False tmpTargetDir "sdist."
49+
-- Otherwise 'withTempDir' fails...
50+
createDirectoryIfMissingVerbose verbosity True tmpTargetDir
51+
withDir $ \tmpDir -> do
52+
let outDir = if isOutDirectory then tmpDir else tmpDir </> tarBallName pkg
53+
flags' = if isOutDirectory then flags
54+
else flags { sDistDirectory = Flag outDir }
4855

49-
sdistWith pkg mb_lbi flags srcPref knownSuffixHandlers createArchive
56+
createDirectoryIfMissingVerbose verbosity True outDir
57+
58+
-- Run 'setup sdist --output-directory=tmpDir'
59+
setupWrapper verbosity setupOpts (Just pkg) sdistCommand (const flags') []
60+
61+
-- And unless we were given --list-sources or --output-directory ourselves,
62+
-- create an archive.
63+
unless (isListSources || isOutDirectory) $
64+
createArchive verbosity pkg tmpDir distPref
5065

5166
where
52-
verbosity = fromFlag (sDistVerbosity flags)
53-
distPref = fromFlag (sDistDistPref flags)
67+
flagEnabled f = not . null . flagToList . f $ flags
68+
69+
isListSources = flagEnabled sDistListSources
70+
isOutDirectory = flagEnabled sDistDirectory
71+
verbosity = fromFlag (sDistVerbosity flags)
72+
distPref = fromFlag (sDistDistPref flags)
73+
tmpTargetDir = fromFlagOrDefault (srcPref distPref) (sDistDirectory flags)
74+
setupOpts = defaultSetupScriptOptions {
75+
-- The '--output-directory' sdist flag was introduced in Cabal 1.12, and
76+
-- '--list-sources' in 1.17.
77+
useCabalVersion = if isListSources
78+
then orLaterVersion $ Version [1,17,0] []
79+
else orLaterVersion $ Version [1,12,0] []
80+
}
5481
format = fromFlag (sDistFormat exflags)
5582
createArchive = case format of
5683
TargzFormat -> createTarGzArchive
@@ -60,16 +87,18 @@ tarBallName :: PackageDescription -> String
6087
tarBallName = display . packageId
6188

6289
-- | Create a tar.gz archive from a tree of source files.
63-
createTarGzArchive :: CreateArchiveFun
64-
createTarGzArchive _verbosity pkg _mlbi tmpDir targetPref = do
90+
createTarGzArchive :: Verbosity -> PackageDescription -> FilePath -> FilePath
91+
-> IO ()
92+
createTarGzArchive verbosity pkg tmpDir targetPref = do
6593
createTarGzFile tarBallFilePath tmpDir (tarBallName pkg)
66-
return tarBallFilePath
94+
notice verbosity $ "Source tarball created: " ++ tarBallFilePath
6795
where
6896
tarBallFilePath = targetPref </> tarBallName pkg <.> "tar.gz"
6997

7098
-- | Create a zip archive from a tree of source files.
71-
createZipArchive :: CreateArchiveFun
72-
createZipArchive verbosity pkg _mlbi tmpDir targetPref = do
99+
createZipArchive :: Verbosity -> PackageDescription -> FilePath -> FilePath
100+
-> IO ()
101+
createZipArchive verbosity pkg tmpDir targetPref = do
73102
let dir = tarBallName pkg
74103
zipfile = targetPref </> dir <.> "zip"
75104
(zipProg, _) <- requireProgram verbosity zipProgram emptyProgramDb
@@ -94,6 +123,6 @@ createZipArchive verbosity pkg _mlbi tmpDir targetPref = do
94123
unless (exitCode == ExitSuccess) $
95124
die $ "Generating the zip file failed "
96125
++ "(zip returned exit code " ++ show exitCode ++ ")"
97-
return zipfile
126+
notice verbosity $ "Source zip archive created: " ++ zipfileAbs
98127
where
99128
zipProgram = simpleProgram "zip"

0 commit comments

Comments
 (0)