Skip to content

Commit

Permalink
Add post-checkout-command
Browse files Browse the repository at this point in the history
- Resolve haskell#6684:
- Resolve haskell#6813: Update (and correct) source-repository-package documentation
  • Loading branch information
phadej committed Sep 10, 2020
1 parent 784a43a commit 4ed7261
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Cabal/src/Distribution/Compat/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module Distribution.Compat.Prelude (
partition,

-- * Data.List.NonEmpty
NonEmpty((:|)), foldl1, foldr1,
NonEmpty((:|)), nonEmpty, foldl1, foldr1,
head, tail, last, init,

-- * Data.Foldable
Expand Down Expand Up @@ -187,7 +187,7 @@ import Data.Function (on)
import Data.Functor.Identity (Identity (..))
import Data.Int (Int16, Int32, Int64, Int8)
import Data.List (intercalate, intersperse, isPrefixOf, isSuffixOf, nub, nubBy, partition, sort, sortBy, unfoldr)
import Data.List.NonEmpty (NonEmpty ((:|)), head, init, last, tail)
import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty, head, init, last, tail)
import Data.Map (Map)
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing, listToMaybe, mapMaybe, maybeToList)
import Data.Ord (comparing)
Expand Down
1 change: 0 additions & 1 deletion Cabal/src/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ import qualified Distribution.Simple.HaskellSuite as HaskellSuite

import Control.Exception
( try )
import Data.List.NonEmpty ( nonEmpty )
import Distribution.Utils.Structured ( structuredDecodeOrFailIO, structuredEncode )
import Distribution.Compat.Directory ( listDirectory )
import Data.ByteString.Lazy ( ByteString )
Expand Down
8 changes: 5 additions & 3 deletions Cabal/src/Distribution/Simple/SrcDist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ allSourcesBuildInfo verbosity rip cwd bi pps modules = do
-- file may show up in multiple paths due to a conditional;
-- we need to package all of them. See #367.
in findAllFilesCwdWithExtension cwd suffixes searchDirs file
>>= nonEmpty (notFound module_) return
>>= nonEmpty' (notFound module_) return
| module_ <- modules ++ otherModules bi ]
bootFiles <- sequenceA
[ let file = ModuleName.toFilePath module_
Expand All @@ -462,8 +462,10 @@ allSourcesBuildInfo verbosity rip cwd bi pps modules = do
cmmSources bi ++ asmSources bi ++ jsSources bi

where
nonEmpty x _ [] = x
nonEmpty _ f xs = f xs
nonEmpty' :: b -> ([a] -> b) -> [a] -> b
nonEmpty' x _ [] = x
nonEmpty' _ f xs = f xs

suffixes = ppSuffixes pps ++ ["hs", "lhs", "hsig", "lhsig"]

notFound :: ModuleName -> IO [FilePath]
Expand Down
8 changes: 4 additions & 4 deletions Cabal/src/Distribution/Types/VersionInterval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ insertInterval :: VersionInterval -> VersionIntervals -> VersionIntervals
insertInterval i is = unionVersionIntervals (VersionIntervals [i]) is

validInterval :: (LowerBound, UpperBound) -> Bool
validInterval i@(l, u) = validLower l && validUpper u && nonEmpty i
validInterval i@(l, u) = validLower l && validUpper u && nonEmptyVI i
where
validLower (LowerBound v _) = validVersion v
validUpper NoUpperBound = True
validUpper (UpperBound v _) = validVersion v

-- Check an interval is non-empty
--
nonEmpty :: VersionInterval -> Bool
nonEmpty (_, NoUpperBound ) = True
nonEmpty (LowerBound l lb, UpperBound u ub) =
nonEmptyVI :: VersionInterval -> Bool
nonEmptyVI (_, NoUpperBound ) = True
nonEmptyVI (LowerBound l lb, UpperBound u ub) =
(l < u) || (l == u && lb == InclusiveBound && ub == InclusiveBound)

-- Check an upper bound does not intersect, or even touch a lower bound:
Expand Down
2 changes: 1 addition & 1 deletion cabal-install/src/Distribution/Client/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import Distribution.Client.FetchUtils
import Data.Bits ((.|.))
import Data.List
( maximumBy )
import Data.List.NonEmpty (groupBy, nonEmpty)
import Data.List.NonEmpty (groupBy)
import qualified Data.List as L
import Data.Maybe
( fromJust )
Expand Down
9 changes: 7 additions & 2 deletions cabal-install/src/Distribution/Client/ProjectConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ import Distribution.Simple.InstallDirs
( PathTemplate, fromPathTemplate
, toPathTemplate, substPathTemplate, initialPathTemplateEnv )
import Distribution.Simple.Utils
( die', warn, notice, info, createDirectoryIfMissingVerbose )
( die', warn, notice, info, createDirectoryIfMissingVerbose, rawSystemIOWithEnv )
import Distribution.Client.Utils
( determineNumJobs )
import Distribution.Utils.NubList
Expand Down Expand Up @@ -1172,7 +1172,12 @@ syncAndReadSourcePackagesRemoteRepos verbosity
syncSourceRepos verbosity vcs
[ (repo, repoPath)
| (repo, _, repoPath) <- repoGroupWithPaths ]
-- TODO phadej 2020-06-18 add post-sync script

-- Run post-checkout-command if it is specified
for_ repoGroupWithPaths $ \(repo, _, repoPath) ->
for_ (nonEmpty (srpCommand repo)) $ \(cmd :| args) -> liftIO $ do
exitCode <- rawSystemIOWithEnv verbosity cmd args (Just repoPath) Nothing Nothing Nothing Nothing
unless (exitCode /= ExitSuccess) $ exitWith exitCode

-- But for reading we go through each 'SourceRepo' including its subdir
-- value and have to know which path each one ended up in.
Expand Down
8 changes: 6 additions & 2 deletions cabal-install/src/Distribution/Client/ProjectConfig/Legacy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,9 @@ legacyPackageConfigFieldDescrs =

legacyPackageConfigFGSectionDescrs
:: ( FieldGrammar c g, Applicative (g SourceRepoList)
, c (Identity RepoType), c (List NoCommaFSep FilePathNT String)
, c (Identity RepoType)
, c (List NoCommaFSep FilePathNT String)
, c (NonEmpty' NoCommaFSep Token String)
)
=> [FGSectionDescr g LegacyProjectConfig]
legacyPackageConfigFGSectionDescrs =
Expand Down Expand Up @@ -1253,7 +1255,9 @@ legacyPackageConfigSectionDescrs =

packageRepoSectionDescr
:: ( FieldGrammar c g, Applicative (g SourceRepoList)
, c (Identity RepoType), c (List NoCommaFSep FilePathNT String)
, c (Identity RepoType)
, c (List NoCommaFSep FilePathNT String)
, c (NonEmpty' NoCommaFSep Token String)
)
=> FGSectionDescr g LegacyProjectConfig
packageRepoSectionDescr = FGSectionDescr
Expand Down
9 changes: 9 additions & 0 deletions cabal-install/src/Distribution/Client/Types/SourceRepo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ data SourceRepositoryPackage f = SourceRepositoryPackage
, srpTag :: !(Maybe String)
, srpBranch :: !(Maybe String)
, srpSubdir :: !(f FilePath)
, srpCommand :: ![String]
}
deriving (Generic)

Expand Down Expand Up @@ -88,6 +89,10 @@ srpSubdirLens :: Lens (SourceRepositoryPackage f) (SourceRepositoryPackage g) (f
srpSubdirLens f s = fmap (\x -> s { srpSubdir = x }) (f (srpSubdir s))
{-# INLINE srpSubdirLens #-}

srpCommandLensNE :: Lens' (SourceRepositoryPackage f) (Maybe (NonEmpty String))
srpCommandLensNE f s = fmap (\x -> s { srpCommand = maybe [] toList x }) (f (nonEmpty (srpCommand s)))
{-# INLINE srpCommandLensNE #-}

-------------------------------------------------------------------------------
-- Parser & PPrinter
-------------------------------------------------------------------------------
Expand All @@ -96,6 +101,7 @@ sourceRepositoryPackageGrammar
:: ( FieldGrammar c g, Applicative (g SourceRepoList)
, c (Identity RepoType)
, c (List NoCommaFSep FilePathNT String)
, c (NonEmpty' NoCommaFSep Token String)
)
=> g SourceRepoList SourceRepoList
sourceRepositoryPackageGrammar = SourceRepositoryPackage
Expand All @@ -104,5 +110,8 @@ sourceRepositoryPackageGrammar = SourceRepositoryPackage
<*> optionalFieldAla "tag" Token srpTagLens
<*> optionalFieldAla "branch" Token srpBranchLens
<*> monoidalFieldAla "subdir" (alaList' NoCommaFSep FilePathNT) srpSubdirLens -- note: NoCommaFSep is somewhat important for roundtrip, as "." is there...
<*> fmap (maybe [] toList) pcc
where
pcc = optionalFieldAla "post-checkout-command" (alaNonEmpty' NoCommaFSep Token) srpCommandLensNE
{-# SPECIALIZE sourceRepositoryPackageGrammar :: ParsecFieldGrammar' SourceRepoList #-}
{-# SPECIALIZE sourceRepositoryPackageGrammar :: PrettyFieldGrammar' SourceRepoList #-}
1 change: 1 addition & 0 deletions cabal-install/src/Distribution/Client/VCS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ validatePDSourceRepo repo = do
, srpTag = PD.repoTag repo
, srpBranch = PD.repoBranch repo
, srpSubdir = PD.repoSubdir repo
, srpCommand = mempty
}
where
a ?! e = maybe (Left e) Right a
Expand Down
2 changes: 2 additions & 0 deletions cabal-install/tests/UnitTests/Distribution/Client/Get.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ testUnsupportedRepoType = do
, srpTag = Nothing
, srpBranch = Nothing
, srpSubdir = Proxy
, srpCommand = []
}
repotype = OtherRepoType "baz"

Expand Down Expand Up @@ -184,6 +185,7 @@ testGitFetchFailed =
, srpTag = Nothing
, srpBranch = Nothing
, srpSubdir = Proxy
, srpCommand = []
}
pkgrepos = [(pkgidfoo, [repo])]
e1 <- assertException $
Expand Down
18 changes: 8 additions & 10 deletions cabal-install/tests/UnitTests/Distribution/Client/ProjectConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -735,17 +735,15 @@ instance f ~ [] => Arbitrary (SourceRepositoryPackage f) where
<*> (fmap getShortToken <$> arbitrary)
<*> (fmap getShortToken <$> arbitrary)
<*> (fmap getShortToken <$> shortListOf 3 arbitrary)
<*> (fmap getShortToken <$> shortListOf 3 arbitrary)

shrink (SourceRepositoryPackage x1 x2 x3 x4 x5) =
[ SourceRepositoryPackage
x1'
(getShortToken x2')
(fmap getShortToken x3')
(fmap getShortToken x4')
(fmap getShortToken x5')
| (x1', x2', x3', x4', x5') <- shrink
(x1, ShortToken x2, fmap ShortToken x3, fmap ShortToken x4, fmap ShortToken x5)
]
shrink SourceRepositoryPackage {..} = runShrinker $ pure SourceRepositoryPackage
<*> shrinker srpType
<*> shrinkerAla ShortToken srpLocation
<*> shrinkerAla (fmap ShortToken) srpTag
<*> shrinkerAla (fmap ShortToken) srpBranch
<*> shrinkerAla (fmap ShortToken) srpSubdir
<*> shrinkerAla (fmap ShortToken) srpCommand

instance Arbitrary RemoteRepo where
arbitrary =
Expand Down
2 changes: 2 additions & 0 deletions cabal-install/tests/UnitTests/Distribution/Client/VCS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ prop_cloneRepo vcs mkVCSTestDriver repoRecipe =
, srpTag = Just tagname
, srpBranch = Nothing
, srpSubdir = []
, srpCommand = []
}
verbosity = silent

Expand Down Expand Up @@ -315,6 +316,7 @@ pickSyncTargetSets repoType repoState srcRepoPath dstReposPath =
, srpTag = Just tag
, srpBranch = Nothing
, srpSubdir = Proxy
, srpCommand = []
}
(tag, workingState) = Map.elemAt tagIdx (allTags repoState)
(tagIdx, prng') = randomR (0, Map.size (allTags repoState) - 1) prng
Expand Down
3 changes: 2 additions & 1 deletion changelog.d/documentation
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
synopsis: Documentation improvements
prs: #6971
prs: #6971 #7047
issues: #6813

description: {

Expand Down
3 changes: 3 additions & 0 deletions changelog.d/issue-6664-pre-sdist-command
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
synopsis: Add post-checkout-command to source-package-repository
issues: #6664
prs: #7047
32 changes: 24 additions & 8 deletions doc/cabal-project.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,7 @@ Specifying Packages from Remote Version Control Locations

Starting with Cabal 2.4, there is now a stanza
``source-repository-package`` for specifying packages from an external
version control which supports the following fields:

- :pkg-field:`source-repository:type`
- :pkg-field:`source-repository:location`
- :pkg-field:`source-repository:tag`
- :pkg-field:`source-repository:subdir`

A simple example is shown below:
version control.

.. code-block:: cabal
Expand All @@ -171,6 +164,29 @@ A simple example is shown below:
tag: 3d274c14ca3077c3a081ba7ad57c5182da65c8c1
subdir: cborg
source-repository-package
type: git
location: https://github.com/haskell/network.git
tag: e76fdc753e660dfa615af6c8b6a2ad9ddf6afe70
post-checkout-command: autoreconf -i
cabal-install 3.4 sdists the ``source-repository-package`` repositories and uses resulting tarballs as project packages.
This allows sharing of packages across different projects.

.. cfg-field:: type: VCS kind

.. cfg-field:: location: VCS location (usually URL)

.. cfg-field:: type: VCS tag

.. cfg-field:: subdir: subdirectory list

Use one or more subdirectories of the repository.

.. cfg-field:: post-checkout-command: command

Run command in the checked out repository, prior sdisting.

Global configuration options
----------------------------

Expand Down

0 comments on commit 4ed7261

Please sign in to comment.