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

RFC: Common stanzas #4751

Merged
merged 1 commit into from
Dec 13, 2017
Merged
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
18 changes: 18 additions & 0 deletions Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,33 @@ extra-source-files:
-- Generated with 'misc/gen-extra-source-files.sh'
-- Do NOT edit this section manually; instead, run the script.
-- BEGIN gen-extra-source-files
tests/ParserTests/errors/common1.cabal
tests/ParserTests/errors/common1.errors
tests/ParserTests/errors/common2.cabal
tests/ParserTests/errors/common2.errors
tests/ParserTests/errors/common3.cabal
tests/ParserTests/errors/common3.errors
tests/ParserTests/regressions/Octree-0.5.cabal
tests/ParserTests/regressions/Octree-0.5.format
tests/ParserTests/regressions/common.cabal
tests/ParserTests/regressions/common.format
tests/ParserTests/regressions/common2.cabal
tests/ParserTests/regressions/common2.format
tests/ParserTests/regressions/elif.cabal
tests/ParserTests/regressions/elif.format
tests/ParserTests/regressions/elif2.cabal
tests/ParserTests/regressions/elif2.format
tests/ParserTests/regressions/encoding-0.8.cabal
tests/ParserTests/regressions/encoding-0.8.format
tests/ParserTests/regressions/generics-sop.cabal
tests/ParserTests/regressions/generics-sop.format
tests/ParserTests/regressions/haddock-api-2.18.1-check.cabal
tests/ParserTests/regressions/issue-774.cabal
tests/ParserTests/regressions/issue-774.format
tests/ParserTests/regressions/nothing-unicode.cabal
tests/ParserTests/regressions/nothing-unicode.format
tests/ParserTests/regressions/shake.cabal
tests/ParserTests/regressions/shake.format
tests/ParserTests/warnings/bom.cabal
tests/ParserTests/warnings/bool.cabal
tests/ParserTests/warnings/deprecatedfield.cabal
Expand Down
6 changes: 6 additions & 0 deletions Cabal/Distribution/PackageDescription/FieldGrammar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ data TestSuiteStanza = TestSuiteStanza
, _testStanzaBuildInfo :: BuildInfo
}

instance L.HasBuildInfo TestSuiteStanza where
buildInfo = testStanzaBuildInfo

testStanzaTestType :: Lens' TestSuiteStanza (Maybe TestType)
testStanzaTestType f s = fmap (\x -> s { _testStanzaTestType = x }) (f (_testStanzaTestType s))
{-# INLINE testStanzaTestType #-}
Expand Down Expand Up @@ -274,6 +277,9 @@ data BenchmarkStanza = BenchmarkStanza
, _benchmarkStanzaBuildInfo :: BuildInfo
}

instance L.HasBuildInfo BenchmarkStanza where
buildInfo = benchmarkStanzaBuildInfo

benchmarkStanzaBenchmarkType :: Lens' BenchmarkStanza (Maybe BenchmarkType)
benchmarkStanzaBenchmarkType f s = fmap (\x -> s { _benchmarkStanzaBenchmarkType = x }) (f (_benchmarkStanzaBenchmarkType s))
{-# INLINE benchmarkStanzaBenchmarkType #-}
Expand Down
215 changes: 191 additions & 24 deletions Cabal/Distribution/PackageDescription/Parsec.hs

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cabal/Distribution/Parsec/Newtypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ data P sep = P
class Sep sep where
prettySep :: P sep -> [Doc] -> Doc
parseSep
:: P sep -> P.Stream s Identity Char
=> P.Parsec s [PWarning] a
:: P.Stream s Identity Char
=> P sep
-> P.Parsec s [PWarning] a
-> P.Parsec s [PWarning] [a]

instance Sep CommaVCat where
Expand Down
1 change: 1 addition & 0 deletions Cabal/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Support for building with Win32 version 2.6 (#4835).
* Compilation with section splitting is now supported via the
'--enable-split-sections' flag (#4819)
* Support for common stanzas (#4751)
* TODO

2.0.1.1 Mikhail Glushenkov <[email protected]> December 2017
Expand Down
41 changes: 40 additions & 1 deletion Cabal/doc/developing-packages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ Executables
^^^^^^^^^^^

.. pkg-section:: executable name
:synopsis: Exectuable build info section.
:synopsis: Executable build info section.

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

Common stanzas
^^^^^^^^^^^^^^

.. pkg-section:: common name
:synopsis: Common build info section

Starting with Cabal-2.2 it's possible to use common build info stanzas.

::

common deps
build-depends: base ^>= 4.11
ghc-options: -Wall

common test-deps
build-depends: tasty

library
import: deps
exposed-modules: Foo

test-suite tests
import: deps, test-deps
type: exitcode-stdio-1.0
main-is: Tests.hs
build-depends: foo

- You can use `build information`_ fields in common stanzas.

- Common stanzas must be defined before use.

- Common stanzas can import other common stanzas.

- You can import multiple stanzas at once. Stanza names must be separated by commas.

.. Note::

The name `import` was chosen, because there is ``includes`` field.

Source Repositories
^^^^^^^^^^^^^^^^^^^

Expand Down
39 changes: 35 additions & 4 deletions Cabal/tests/ParserTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import qualified Distribution.Types.PackageDescription.Lens as L

tests :: TestTree
tests = testGroup "parsec tests"
[ warningTests
, regressionTests
[ regressionTests
, warningTests
, errorTests
]

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

-------------------------------------------------------------------------------
-- Errors
-------------------------------------------------------------------------------

errorTests :: TestTree
errorTests = testGroup "errors"
[ errorTest "common1.cabal"
, errorTest "common2.cabal"
, errorTest "common3.cabal"
]

errorTest :: FilePath -> TestTree
errorTest fp = cabalGoldenTest "errors" correct $ do
contents <- BS.readFile input
let res = parseGenericPackageDescription contents
let (_, errs, x) = runParseResult res

return $ toUTF8BS $ case x of
Just gpd | null errs ->
"UNXPECTED SUCCESS\n" ++
showGenericPackageDescription gpd
_ ->
unlines $ map show errs
where
input = "tests" </> "ParserTests" </> "errors" </> fp
correct = replaceExtension input "errors"

-------------------------------------------------------------------------------
-- Regressions
-------------------------------------------------------------------------------
Expand All @@ -83,6 +111,8 @@ regressionTests = testGroup "regressions"
, regressionTest "elif.cabal"
, regressionTest "elif2.cabal"
, regressionTest "shake.cabal"
, regressionTest "common.cabal"
, regressionTest "common2.cabal"
]

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

return $ toUTF8BS $ case x of
Just gpd | null errs ->
showGenericPackageDescription gpd
unlines (map show warns)
++ showGenericPackageDescription gpd
_ ->
unlines $ "ERROR" : map show errs
where
Expand Down
29 changes: 29 additions & 0 deletions Cabal/tests/ParserTests/errors/common1.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: common
version: 0
synopsis: Common-stanza demo demo
build-type: Simple
cabal-version: >=2.1

source-repository head
Type: git
Location: https://github.com/hvr/-.git

common windows
if os(windows)
build-depends: Win32

-- Non-existing common stanza
common deps
import: windo
build-depends:
base >=4.10 && <4.11,
containers

library
import: deps

default-language: Haskell2010
exposed-modules: ElseIf

build-depends:
ghc-prim
1 change: 1 addition & 0 deletions Cabal/tests/ParserTests/errors/common1.errors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PError (Position 17 3) "Undefined common stanza imported: windo"
29 changes: 29 additions & 0 deletions Cabal/tests/ParserTests/errors/common2.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: common
version: 0
synopsis: Common-stanza demo demo
build-type: Simple
cabal-version: >=2.1

source-repository head
Type: git
Location: https://github.com/hvr/-.git

-- Used before use
common deps
import: windows
build-depends:
base >=4.10 && <4.11,
containers

common windows
if os(windows)
build-depends: Win32

library
import: deps

default-language: Haskell2010
exposed-modules: ElseIf

build-depends:
ghc-prim
1 change: 1 addition & 0 deletions Cabal/tests/ParserTests/errors/common2.errors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PError (Position 13 3) "Undefined common stanza imported: windows"
31 changes: 31 additions & 0 deletions Cabal/tests/ParserTests/errors/common3.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: common
version: 0
synopsis: Common-stanza demo demo
build-type: Simple
cabal-version: >=2.1

source-repository head
Type: git
Location: https://github.com/hvr/-.git

common windows
if os(windows)
build-depends: Win32

common deps
import: windows
build-depends:
base >=4.10 && <4.11,
containers

-- Duplicate
common deps

library
import: deps

default-language: Haskell2010
exposed-modules: ElseIf

build-depends:
ghc-prim
1 change: 1 addition & 0 deletions Cabal/tests/ParserTests/errors/common3.errors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PError (Position 22 1) "Duplicate common stanza: deps"
3 changes: 3 additions & 0 deletions Cabal/tests/ParserTests/regressions/Octree-0.5.format
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
PWarning PWTLexNBSP (Position 43 3) "Non-breaking space found"
PWarning PWTLexNBSP (Position 41 3) "Non-breaking space found"
PWarning PWTLexNBSP (Position 39 3) "Non-breaking space found"
name: Octree
version: 0.5
license: BSD3
Expand Down
32 changes: 32 additions & 0 deletions Cabal/tests/ParserTests/regressions/common.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: common
version: 0
synopsis: Common-stanza demo demo
build-type: Simple
cabal-version: >=1.10

source-repository head
Type: git
Location: https://github.com/hvr/-.git

common deps
build-depends:
base >=4.10 && <4.11,
containers

library
import: deps

default-language: Haskell2010
exposed-modules: ElseIf

build-depends:
ghc-prim

test-suite tests
import: deps

type: exitcode-stdio-1.0
main-is: Tests.hs

build-depends:
HUnit
25 changes: 25 additions & 0 deletions Cabal/tests/ParserTests/regressions/common.format
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
PWarning PWTUnknownField (Position 26 3) "Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas"
PWarning PWTUnknownField (Position 17 3) "Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas"
PWarning PWTUnknownSection (Position 11 1) "Ignoring section: common. You should set cabal-version: 2.2 or larger to use common stanzas."
name: common
version: 0
synopsis: Common-stanza demo demo
cabal-version: >=1.10
build-type: Simple

source-repository head
type: git
location: https://github.com/hvr/-.git

library
exposed-modules:
ElseIf
default-language: Haskell2010
build-depends:
ghc-prim -any

test-suite tests
type: exitcode-stdio-1.0
main-is: Tests.hs
build-depends:
HUnit -any
Loading