Skip to content

Commit 346e3e5

Browse files
committed
Add fieldPAla to factor out newtype wrapper logic.
1 parent 130ecf8 commit 346e3e5

File tree

3 files changed

+93
-77
lines changed

3 files changed

+93
-77
lines changed

cabal-install/Distribution/Client/Init/FileCreators.hs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE OverloadedStrings #-}
12
-----------------------------------------------------------------------------
23
-- |
34
-- Module : Distribution.Client.Init.FileCreators
@@ -50,7 +51,7 @@ import System.Directory
5051
( getCurrentDirectory, doesFileExist, copyFile
5152
, createDirectoryIfMissing )
5253

53-
import Text.PrettyPrint hiding (mode, cat)
54+
import Text.PrettyPrint hiding ((<>), mode, cat)
5455

5556
import Distribution.Client.Init.Defaults
5657
( defaultCabalVersion, myLibModule )
@@ -62,8 +63,12 @@ import Distribution.Client.Init.Types
6263
( InitFlags(..), BuildType(..), PackageType(..) )
6364

6465
import Distribution.CabalSpecVersion
66+
import Distribution.Compat.Newtype
67+
( Newtype )
6568
import Distribution.Deprecated.Text
6669
( display, Text(..) )
70+
import Distribution.Fields.Field
71+
( FieldName )
6772
import Distribution.License
6873
( licenseFromSPDX )
6974
import qualified Distribution.ModuleName as ModuleName
@@ -417,7 +422,7 @@ generateCabalFile fileName c =
417422
[]
418423
False
419424

420-
, fieldP "extra-source-files" (maybeToFlag $ (formatExtraSourceFiles <$> extraSrc c))
425+
, fieldPAla "extra-source-files" formatExtraSourceFiles (maybeToFlag (extraSrc c))
421426
["Extra files to be distributed with the package, such as examples or a README."]
422427
True
423428
]
@@ -439,22 +444,22 @@ generateCabalFile fileName c =
439444

440445
generateBuildInfo :: BuildType -> InitFlags -> [PrettyField [String]]
441446
generateBuildInfo buildType c' = catMaybes
442-
[ fieldP "other-modules" (formatOtherModules <$> maybeToFlag otherMods)
447+
[ fieldPAla "other-modules" formatOtherModules (maybeToFlag otherMods)
443448
[ case buildType of
444449
LibBuild -> "Modules included in this library but not exported."
445450
ExecBuild -> "Modules included in this executable, other than Main."]
446451
True
447452

448-
, fieldP "other-extensions" (maybeToFlag (formatOtherExtensions <$> otherExts c))
453+
, fieldPAla "other-extensions" formatOtherExtensions (maybeToFlag (otherExts c))
449454
["LANGUAGE extensions used by modules in this package."]
450455
True
451456

452-
, fieldP "build-depends" (maybeToFlag (formatDependencyList <$> buildDependencies))
457+
, fieldPAla "build-depends" formatDependencyList (maybeToFlag buildDependencies)
453458
["Other library packages from which modules are imported."]
454459
True
455460

456-
, fieldP "hs-source-dirs"
457-
(maybeToFlag (formatHsSourceDirs <$> case buildType of
461+
, fieldPAla "hs-source-dirs" formatHsSourceDirs
462+
(maybeToFlag (case buildType of
458463
LibBuild -> sourceDirs c
459464
ExecBuild -> applicationDirs c))
460465
["Directories containing source files."]
@@ -494,16 +499,16 @@ generateCabalFile fileName c =
494499

495500
-- | Construct a 'PrettyField' from a field that can be automatically
496501
-- converted to a 'Doc' via 'display'.
497-
field :: Text t =>
498-
String
502+
field :: Text t
503+
=> FieldName
499504
-> Flag t
500505
-> [String]
501506
-> Bool
502507
-> Maybe (PrettyField [String])
503508
field fieldName fieldContentsFlag = fieldS fieldName (display <$> fieldContentsFlag)
504509

505510
-- | Construct a 'PrettyField' from a 'String' field.
506-
fieldS :: String -- ^ Name of the field
511+
fieldS :: FieldName -- ^ Name of the field
507512
-> Flag String -- ^ Field contents
508513
-> [String] -- ^ Comment to explain the field
509514
-> Bool -- ^ Should the field be included (commented out) even if blank?
@@ -512,16 +517,28 @@ generateCabalFile fileName c =
512517

513518
-- | Construct a 'PrettyField' from a Flag which can be 'pretty'-ied.
514519
fieldP :: Pretty a
515-
=> String
520+
=> FieldName
516521
-> Flag a
517522
-> [String]
518523
-> Bool
519524
-> Maybe (PrettyField [String])
520525
fieldP fieldName fieldContentsFlag fieldComments includeField =
521-
fieldD fieldName (pretty <$> fieldContentsFlag) fieldComments includeField
526+
fieldPAla fieldName Identity fieldContentsFlag fieldComments includeField
527+
528+
-- | Construct a 'PrettyField' from a flag which can be 'pretty'-ied, wrapped in newtypeWrapper.
529+
fieldPAla
530+
:: (Pretty b, Newtype a b)
531+
=> FieldName
532+
-> (a -> b)
533+
-> Flag a
534+
-> [String]
535+
-> Bool
536+
-> Maybe (PrettyField [String])
537+
fieldPAla fieldName newtypeWrapper fieldContentsFlag fieldComments includeField =
538+
fieldD fieldName (pretty . newtypeWrapper <$> fieldContentsFlag) fieldComments includeField
522539

523540
-- | Construct a 'PrettyField' from a 'Doc' Flag.
524-
fieldD :: String -- ^ Name of the field
541+
fieldD :: FieldName -- ^ Name of the field
525542
-> Flag Doc -- ^ Field contents
526543
-> [String] -- ^ Comment to explain the field
527544
-> Bool -- ^ Should the field be included (commented out) even if blank?
@@ -551,23 +568,23 @@ generateCabalFile fileName c =
551568
fieldSWithContents fieldName fieldContents fieldComments
552569

553570
-- | Optionally produce a field with no content (depending on flags).
554-
fieldSEmptyContents :: String
571+
fieldSEmptyContents :: FieldName
555572
-> [String]
556573
-> Bool
557574
-> Maybe (PrettyField [String])
558575
fieldSEmptyContents fieldName fieldComments includeField
559576
| not includeField || (minimal c == Flag True) =
560577
Nothing
561578
| otherwise =
562-
Just (PrettyFieldCommentedOut (map ("-- " ++) fieldComments) (toUTF8BS fieldName))
579+
Just (PrettyFieldCommentedOut (map ("-- " ++) fieldComments) fieldName)
563580

564581
-- | Produce a field with content.
565-
fieldSWithContents :: String
582+
fieldSWithContents :: FieldName
566583
-> Doc
567584
-> [String]
568585
-> PrettyField [String]
569586
fieldSWithContents fieldName fieldContents fieldComments =
570-
PrettyField (map ("-- " ++) fieldComments) (toUTF8BS fieldName) fieldContents
587+
PrettyField (map ("-- " ++) fieldComments) fieldName fieldContents
571588

572589
executableStanza :: PrettyField [String]
573590
executableStanza = PrettySection [] (toUTF8BS "executable") [exeName] $ catMaybes
@@ -582,7 +599,7 @@ generateCabalFile fileName c =
582599

583600
libraryStanza :: PrettyField [String]
584601
libraryStanza = PrettySection [] (toUTF8BS "library") [] $ catMaybes
585-
[ fieldP "exposed-modules" (maybeToFlag (formatExposedModules <$> exposedModules c))
602+
[ fieldPAla "exposed-modules" formatExposedModules (maybeToFlag (exposedModules c))
586603
["Modules exported by the library."]
587604
True
588605
]
@@ -600,16 +617,16 @@ generateCabalFile fileName c =
600617
["The interface type and version of the test suite."]
601618
True
602619

603-
, fieldP "hs-source-dirs"
604-
(maybeToFlag (formatHsSourceDirs <$> testDirs c))
620+
, fieldPAla "hs-source-dirs" formatHsSourceDirs
621+
(maybeToFlag (testDirs c))
605622
["Directories containing source files."]
606623
True
607624

608625
, fieldS "main-is" (Flag testFile)
609626
["The entrypoint to the test suite."]
610627
True
611628

612-
, fieldP "build-depends" (maybeToFlag (formatDependencyList <$> dependencies c))
629+
, fieldPAla "build-depends" formatDependencyList (maybeToFlag (dependencies c))
613630
["Test dependencies."]
614631
True
615632
]

tests/fixtures/init/lib-and-exe-golden.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ executable foo
2525
base ^>=4.13.0.0,
2626
containers ^>=5.7.0.0,
2727
unordered-containers ^>=2.7.0.0,
28-
foo -any
28+
foo
2929

3030
hs-source-dirs: app
3131
default-language: Haskell2010
Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
cabal-version: 2.4
22

3-
-- Initial package description
4-
-- 'lib-exe-and-test-with-comments-golden.cabal' generated by 'cabal init'.
5-
-- For further documentation, see http://haskell.org/cabal/users-guide/
6-
3+
-- Initial package description 'lib-exe-and-test-with-comments-golden.cabal' generated by
4+
-- 'cabal init'. For further documentation, see:
5+
-- http://haskell.org/cabal/users-guide/
6+
--
77
-- The name of the package.
88
name: foo
99

10-
-- The package version. See the Haskell package versioning policy (PVP)
11-
-- for standards guiding when and how versions should be incremented.
10+
-- The package version.
11+
-- See the Haskell package versioning policy (PVP) for standards
12+
-- guiding when and how versions should be incremented.
1213
-- https://pvp.haskell.org
1314
-- PVP summary: +-+------- breaking API changes
1415
-- | | +----- non-breaking API additions
@@ -17,13 +18,11 @@ version: 3.2.1
1718

1819
-- A short (one-line) description of the package.
1920
synopsis: The foo package
20-
2121
-- A longer description of the package.
2222
-- description:
2323

2424
-- URL for the project homepage or repository.
2525
homepage: https://github.com/foo/foo
26-
2726
-- A URL where users can report bugs.
2827
-- bug-reports:
2928

@@ -33,73 +32,73 @@ license: NONE
3332
-- The package author(s).
3433
author: me
3534

36-
-- An email address to which users can send suggestions, bug reports, and
37-
-- patches.
35+
-- An email address to which users can send suggestions, bug reports, and patches.
3836
maintainer: me@me.me
39-
4037
-- A copyright notice.
4138
-- copyright:
42-
4339
category: SomeCat
4440

45-
-- Extra files to be distributed with the package, such as examples or a
46-
-- README.
41+
-- Extra files to be distributed with the package, such as examples or a README.
4742
extra-source-files: CHANGELOG.md
4843

49-
5044
library
51-
-- Modules exported by the library.
52-
exposed-modules: A, B
45+
-- Modules exported by the library.
46+
exposed-modules:
47+
A
48+
B
5349

54-
-- Modules included in this library but not exported.
55-
-- other-modules:
50+
-- Modules included in this library but not exported.
51+
-- other-modules:
52+
-- LANGUAGE extensions used by modules in this package.
53+
-- other-extensions:
5654

57-
-- LANGUAGE extensions used by modules in this package.
58-
-- other-extensions:
55+
-- Other library packages from which modules are imported.
56+
build-depends:
57+
base ^>=4.13.0.0,
58+
containers ^>=5.7.0.0,
59+
unordered-containers ^>=2.7.0.0
5960

60-
-- Other library packages from which modules are imported.
61-
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
62-
63-
-- Directories containing source files.
64-
hs-source-dirs: src
65-
66-
-- Base language which the package is written in.
67-
default-language: Haskell2010
61+
-- Directories containing source files.
62+
hs-source-dirs: src
6863

64+
-- Base language which the package is written in.
65+
default-language: Haskell2010
6966

7067
executable foo
71-
-- .hs or .lhs file containing the Main module.
72-
main-is: Main.hs
73-
74-
-- Modules included in this executable, other than Main.
75-
-- other-modules:
68+
-- .hs or .lhs file containing the Main module.
69+
main-is: Main.hs
70+
-- Modules included in this executable, other than Main.
71+
-- other-modules:
72+
-- LANGUAGE extensions used by modules in this package.
73+
-- other-extensions:
7674

77-
-- LANGUAGE extensions used by modules in this package.
78-
-- other-extensions:
75+
-- Other library packages from which modules are imported.
76+
build-depends:
77+
base ^>=4.13.0.0,
78+
containers ^>=5.7.0.0,
79+
unordered-containers ^>=2.7.0.0
7980

80-
-- Other library packages from which modules are imported.
81-
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
82-
83-
-- Directories containing source files.
84-
hs-source-dirs: app
85-
86-
-- Base language which the package is written in.
87-
default-language: Haskell2010
81+
-- Directories containing source files.
82+
hs-source-dirs: app
8883

84+
-- Base language which the package is written in.
85+
default-language: Haskell2010
8986

9087
test-suite foo-test
91-
-- Base language which the package is written in.
92-
default-language: Haskell2010
93-
94-
-- The interface type and version of the test suite.
95-
type: exitcode-stdio-1.0
88+
-- Base language which the package is written in.
89+
default-language: Haskell2010
9690

97-
-- The directory where the test specifications are found.
98-
hs-source-dirs: tests
91+
-- The interface type and version of the test suite.
92+
type: exitcode-stdio-1.0
9993

100-
-- The entrypoint to the test suite.
101-
main-is: MyLibTest.hs
94+
-- Directories containing source files.
95+
hs-source-dirs: tests
10296

103-
-- Test dependencies.
104-
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
97+
-- The entrypoint to the test suite.
98+
main-is: MyLibTest.hs
10599

100+
-- Test dependencies.
101+
build-depends:
102+
base ^>=4.13.0.0,
103+
containers ^>=5.7.0.0,
104+
unordered-containers ^>=2.7.0.0

0 commit comments

Comments
 (0)