Skip to content

Commit b8c95ea

Browse files
authored
Merge pull request #4751 from phadej/common-stanzas
RFC: Common stanzas
2 parents f0d688a + bfb1967 commit b8c95ea

22 files changed

+528
-34
lines changed

Cabal/Cabal.cabal

+18
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,33 @@ extra-source-files:
3232
-- Generated with 'misc/gen-extra-source-files.sh'
3333
-- Do NOT edit this section manually; instead, run the script.
3434
-- BEGIN gen-extra-source-files
35+
tests/ParserTests/errors/common1.cabal
36+
tests/ParserTests/errors/common1.errors
37+
tests/ParserTests/errors/common2.cabal
38+
tests/ParserTests/errors/common2.errors
39+
tests/ParserTests/errors/common3.cabal
40+
tests/ParserTests/errors/common3.errors
3541
tests/ParserTests/regressions/Octree-0.5.cabal
42+
tests/ParserTests/regressions/Octree-0.5.format
43+
tests/ParserTests/regressions/common.cabal
44+
tests/ParserTests/regressions/common.format
45+
tests/ParserTests/regressions/common2.cabal
46+
tests/ParserTests/regressions/common2.format
3647
tests/ParserTests/regressions/elif.cabal
48+
tests/ParserTests/regressions/elif.format
3749
tests/ParserTests/regressions/elif2.cabal
50+
tests/ParserTests/regressions/elif2.format
3851
tests/ParserTests/regressions/encoding-0.8.cabal
52+
tests/ParserTests/regressions/encoding-0.8.format
3953
tests/ParserTests/regressions/generics-sop.cabal
54+
tests/ParserTests/regressions/generics-sop.format
4055
tests/ParserTests/regressions/haddock-api-2.18.1-check.cabal
4156
tests/ParserTests/regressions/issue-774.cabal
57+
tests/ParserTests/regressions/issue-774.format
4258
tests/ParserTests/regressions/nothing-unicode.cabal
59+
tests/ParserTests/regressions/nothing-unicode.format
4360
tests/ParserTests/regressions/shake.cabal
61+
tests/ParserTests/regressions/shake.format
4462
tests/ParserTests/warnings/bom.cabal
4563
tests/ParserTests/warnings/bool.cabal
4664
tests/ParserTests/warnings/deprecatedfield.cabal

Cabal/Distribution/PackageDescription/FieldGrammar.hs

+6
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ data TestSuiteStanza = TestSuiteStanza
175175
, _testStanzaBuildInfo :: BuildInfo
176176
}
177177

178+
instance L.HasBuildInfo TestSuiteStanza where
179+
buildInfo = testStanzaBuildInfo
180+
178181
testStanzaTestType :: Lens' TestSuiteStanza (Maybe TestType)
179182
testStanzaTestType f s = fmap (\x -> s { _testStanzaTestType = x }) (f (_testStanzaTestType s))
180183
{-# INLINE testStanzaTestType #-}
@@ -274,6 +277,9 @@ data BenchmarkStanza = BenchmarkStanza
274277
, _benchmarkStanzaBuildInfo :: BuildInfo
275278
}
276279

280+
instance L.HasBuildInfo BenchmarkStanza where
281+
buildInfo = benchmarkStanzaBuildInfo
282+
277283
benchmarkStanzaBenchmarkType :: Lens' BenchmarkStanza (Maybe BenchmarkType)
278284
benchmarkStanzaBenchmarkType f s = fmap (\x -> s { _benchmarkStanzaBenchmarkType = x }) (f (_benchmarkStanzaBenchmarkType s))
279285
{-# INLINE benchmarkStanzaBenchmarkType #-}

Cabal/Distribution/PackageDescription/Parsec.hs

+191-24
Large diffs are not rendered by default.

Cabal/Distribution/Parsec/Newtypes.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ data P sep = P
6363
class Sep sep where
6464
prettySep :: P sep -> [Doc] -> Doc
6565
parseSep
66-
:: P sep -> P.Stream s Identity Char
67-
=> P.Parsec s [PWarning] a
66+
:: P.Stream s Identity Char
67+
=> P sep
68+
-> P.Parsec s [PWarning] a
6869
-> P.Parsec s [PWarning] [a]
6970

7071
instance Sep CommaVCat where

Cabal/changelog

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Support for building with Win32 version 2.6 (#4835).
3030
* Compilation with section splitting is now supported via the
3131
'--enable-split-sections' flag (#4819)
32+
* Support for common stanzas (#4751)
3233
* TODO
3334

3435
2.0.1.1 Mikhail Glushenkov <mikhail.glushenkov@gmail.com> December 2017

Cabal/doc/developing-packages.rst

+40-1
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ Executables
12511251
^^^^^^^^^^^
12521252

12531253
.. pkg-section:: executable name
1254-
:synopsis: Exectuable build info section.
1254+
:synopsis: Executable build info section.
12551255

12561256
Executable sections (if present) describe executable programs contained
12571257
in the package and must have an argument after the section label, which
@@ -2520,6 +2520,45 @@ and outside then they are combined using the following rules.
25202520
else
25212521
Main-is: Main.hs
25222522

2523+
Common stanzas
2524+
^^^^^^^^^^^^^^
2525+
2526+
.. pkg-section:: common name
2527+
:synopsis: Common build info section
2528+
2529+
Starting with Cabal-2.2 it's possible to use common build info stanzas.
2530+
2531+
::
2532+
2533+
common deps
2534+
build-depends: base ^>= 4.11
2535+
ghc-options: -Wall
2536+
2537+
common test-deps
2538+
build-depends: tasty
2539+
2540+
library
2541+
import: deps
2542+
exposed-modules: Foo
2543+
2544+
test-suite tests
2545+
import: deps, test-deps
2546+
type: exitcode-stdio-1.0
2547+
main-is: Tests.hs
2548+
build-depends: foo
2549+
2550+
- You can use `build information`_ fields in common stanzas.
2551+
2552+
- Common stanzas must be defined before use.
2553+
2554+
- Common stanzas can import other common stanzas.
2555+
2556+
- You can import multiple stanzas at once. Stanza names must be separated by commas.
2557+
2558+
.. Note::
2559+
2560+
The name `import` was chosen, because there is ``includes`` field.
2561+
25232562
Source Repositories
25242563
^^^^^^^^^^^^^^^^^^^
25252564

Cabal/tests/ParserTests.hs

+35-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ import qualified Distribution.Types.PackageDescription.Lens as L
2727

2828
tests :: TestTree
2929
tests = testGroup "parsec tests"
30-
[ warningTests
31-
, regressionTests
30+
[ regressionTests
31+
, warningTests
32+
, errorTests
3233
]
3334

3435
-------------------------------------------------------------------------------
@@ -69,6 +70,33 @@ warningTest wt fp = testCase (show wt) $ do
6970
[] -> assertFailure "got no warnings"
7071
_ -> assertFailure $ "got multiple warnings: " ++ show warns
7172

73+
-------------------------------------------------------------------------------
74+
-- Errors
75+
-------------------------------------------------------------------------------
76+
77+
errorTests :: TestTree
78+
errorTests = testGroup "errors"
79+
[ errorTest "common1.cabal"
80+
, errorTest "common2.cabal"
81+
, errorTest "common3.cabal"
82+
]
83+
84+
errorTest :: FilePath -> TestTree
85+
errorTest fp = cabalGoldenTest "errors" correct $ do
86+
contents <- BS.readFile input
87+
let res = parseGenericPackageDescription contents
88+
let (_, errs, x) = runParseResult res
89+
90+
return $ toUTF8BS $ case x of
91+
Just gpd | null errs ->
92+
"UNXPECTED SUCCESS\n" ++
93+
showGenericPackageDescription gpd
94+
_ ->
95+
unlines $ map show errs
96+
where
97+
input = "tests" </> "ParserTests" </> "errors" </> fp
98+
correct = replaceExtension input "errors"
99+
72100
-------------------------------------------------------------------------------
73101
-- Regressions
74102
-------------------------------------------------------------------------------
@@ -83,6 +111,8 @@ regressionTests = testGroup "regressions"
83111
, regressionTest "elif.cabal"
84112
, regressionTest "elif2.cabal"
85113
, regressionTest "shake.cabal"
114+
, regressionTest "common.cabal"
115+
, regressionTest "common2.cabal"
86116
]
87117

88118
regressionTest :: FilePath -> TestTree
@@ -95,11 +125,12 @@ formatGoldenTest :: FilePath -> TestTree
95125
formatGoldenTest fp = cabalGoldenTest "format" correct $ do
96126
contents <- BS.readFile input
97127
let res = parseGenericPackageDescription contents
98-
let (_, errs, x) = runParseResult res
128+
let (warns, errs, x) = runParseResult res
99129

100130
return $ toUTF8BS $ case x of
101131
Just gpd | null errs ->
102-
showGenericPackageDescription gpd
132+
unlines (map show warns)
133+
++ showGenericPackageDescription gpd
103134
_ ->
104135
unlines $ "ERROR" : map show errs
105136
where
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: common
2+
version: 0
3+
synopsis: Common-stanza demo demo
4+
build-type: Simple
5+
cabal-version: >=2.1
6+
7+
source-repository head
8+
Type: git
9+
Location: https://github.com/hvr/-.git
10+
11+
common windows
12+
if os(windows)
13+
build-depends: Win32
14+
15+
-- Non-existing common stanza
16+
common deps
17+
import: windo
18+
build-depends:
19+
base >=4.10 && <4.11,
20+
containers
21+
22+
library
23+
import: deps
24+
25+
default-language: Haskell2010
26+
exposed-modules: ElseIf
27+
28+
build-depends:
29+
ghc-prim
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PError (Position 17 3) "Undefined common stanza imported: windo"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: common
2+
version: 0
3+
synopsis: Common-stanza demo demo
4+
build-type: Simple
5+
cabal-version: >=2.1
6+
7+
source-repository head
8+
Type: git
9+
Location: https://github.com/hvr/-.git
10+
11+
-- Used before use
12+
common deps
13+
import: windows
14+
build-depends:
15+
base >=4.10 && <4.11,
16+
containers
17+
18+
common windows
19+
if os(windows)
20+
build-depends: Win32
21+
22+
library
23+
import: deps
24+
25+
default-language: Haskell2010
26+
exposed-modules: ElseIf
27+
28+
build-depends:
29+
ghc-prim
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PError (Position 13 3) "Undefined common stanza imported: windows"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: common
2+
version: 0
3+
synopsis: Common-stanza demo demo
4+
build-type: Simple
5+
cabal-version: >=2.1
6+
7+
source-repository head
8+
Type: git
9+
Location: https://github.com/hvr/-.git
10+
11+
common windows
12+
if os(windows)
13+
build-depends: Win32
14+
15+
common deps
16+
import: windows
17+
build-depends:
18+
base >=4.10 && <4.11,
19+
containers
20+
21+
-- Duplicate
22+
common deps
23+
24+
library
25+
import: deps
26+
27+
default-language: Haskell2010
28+
exposed-modules: ElseIf
29+
30+
build-depends:
31+
ghc-prim
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PError (Position 22 1) "Duplicate common stanza: deps"

Cabal/tests/ParserTests/regressions/Octree-0.5.format

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
PWarning PWTLexNBSP (Position 43 3) "Non-breaking space found"
2+
PWarning PWTLexNBSP (Position 41 3) "Non-breaking space found"
3+
PWarning PWTLexNBSP (Position 39 3) "Non-breaking space found"
14
name: Octree
25
version: 0.5
36
license: BSD3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: common
2+
version: 0
3+
synopsis: Common-stanza demo demo
4+
build-type: Simple
5+
cabal-version: >=1.10
6+
7+
source-repository head
8+
Type: git
9+
Location: https://github.com/hvr/-.git
10+
11+
common deps
12+
build-depends:
13+
base >=4.10 && <4.11,
14+
containers
15+
16+
library
17+
import: deps
18+
19+
default-language: Haskell2010
20+
exposed-modules: ElseIf
21+
22+
build-depends:
23+
ghc-prim
24+
25+
test-suite tests
26+
import: deps
27+
28+
type: exitcode-stdio-1.0
29+
main-is: Tests.hs
30+
31+
build-depends:
32+
HUnit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
PWarning PWTUnknownField (Position 26 3) "Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas"
2+
PWarning PWTUnknownField (Position 17 3) "Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas"
3+
PWarning PWTUnknownSection (Position 11 1) "Ignoring section: common. You should set cabal-version: 2.2 or larger to use common stanzas."
4+
name: common
5+
version: 0
6+
synopsis: Common-stanza demo demo
7+
cabal-version: >=1.10
8+
build-type: Simple
9+
10+
source-repository head
11+
type: git
12+
location: https://github.com/hvr/-.git
13+
14+
library
15+
exposed-modules:
16+
ElseIf
17+
default-language: Haskell2010
18+
build-depends:
19+
ghc-prim -any
20+
21+
test-suite tests
22+
type: exitcode-stdio-1.0
23+
main-is: Tests.hs
24+
build-depends:
25+
HUnit -any

0 commit comments

Comments
 (0)