Skip to content

Commit

Permalink
Add Paths_ module to autogen-modules when cabal-version >= 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Nov 26, 2021
1 parent 34fc7b4 commit f9df468
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Changes in 0.34.6
- Add `Paths_` module to `autogen-modules` when `cabal-version >= 2`

## Changes in 0.34.5
- Compatibility with `aeson-2.*`

Expand All @@ -11,7 +14,7 @@
- Reject empty `then` / `else` sections (see #362)
- Omit conditionals that are always `false` from generated `.cabal` file
(see #404)
- Infer correct `cabal-version` when `Path_` is used with `RebindableSyntax`
- Infer correct `cabal-version` when `Paths_` is used with `RebindableSyntax`
and `OverloadedStrings` or `OverloadedLists` (see #400)
- Do not use indentation from any existing `.cabal` file if it is invalid
(e.g. `0`) (fixes #252)
Expand Down
42 changes: 39 additions & 3 deletions src/Hpack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -676,15 +676,49 @@ verbatimValueToString = \ case
VerbatimBool b -> show b
VerbatimNull -> ""

determineCabalVersion :: Maybe (License SPDX.License) -> Package -> (Package, String)
addPathsModuleToGeneratedModules :: Package -> Version -> Package
addPathsModuleToGeneratedModules pkg cabalVersion
| cabalVersion < makeVersion [2] = pkg
| otherwise = pkg {
packageLibrary = fmap mapLibrary <$> packageLibrary pkg
, packageInternalLibraries = fmap mapLibrary <$> packageInternalLibraries pkg
, packageExecutables = fmap mapExecutable <$> packageExecutables pkg
, packageTests = fmap mapExecutable <$> packageTests pkg
, packageBenchmarks = fmap mapExecutable <$> packageBenchmarks pkg
}
where
pathsModule = pathsModuleFromPackageName (packageName pkg)

mapLibrary :: Library -> Library
mapLibrary lib
| pathsModule `elem` getLibraryModules lib = lib {
libraryGeneratedModules = if pathsModule `elem` generatedModules then generatedModules else pathsModule : generatedModules
}
| otherwise = lib
where
generatedModules = libraryGeneratedModules lib

mapExecutable :: Executable -> Executable
mapExecutable executable
| pathsModule `elem` executableOtherModules executable = executable {
executableGeneratedModules = if pathsModule `elem` generatedModules then generatedModules else pathsModule : generatedModules
}
| otherwise = executable
where
generatedModules = executableGeneratedModules executable

determineCabalVersion :: Maybe (License SPDX.License) -> Package -> (Package, String, Maybe Version)
determineCabalVersion inferredLicense pkg@Package{..} = (
pkg {
packageVerbatim = deleteVerbatimField "cabal-version" packageVerbatim
, packageLicense = formatLicense <$> license
}
, "cabal-version: " ++ fromMaybe inferredCabalVersion verbatimCabalVersion ++ "\n\n"
, "cabal-version: " ++ effectiveCabalVersion ++ "\n\n"
, parseVersion effectiveCabalVersion
)
where
effectiveCabalVersion = fromMaybe inferredCabalVersion verbatimCabalVersion

license = fmap prettyShow <$> (parsedLicense <|> inferredLicense)

parsedLicense = parseLicense <$> packageLicense
Expand Down Expand Up @@ -1184,7 +1218,9 @@ toPackage_ dir (Product g PackageConfig{..}) = do

tell nameWarnings
tell (formatMissingSourceDirs missingSourceDirs)
return (determineCabalVersion inferredLicense pkg)

let (pkg_, renderedCabalVersion, cabalVersion) = determineCabalVersion inferredLicense pkg
return (maybe pkg_ (addPathsModuleToGeneratedModules pkg_) cabalVersion, renderedCabalVersion)
where
nameWarnings :: [String]
packageName_ :: String
Expand Down
70 changes: 70 additions & 0 deletions test/EndToEndSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Paths_foo
|]

context "when cabal-version is >= 2" $ do
it "adds Paths_ to autogen-modules" $ do
[i|
verbatim:
cabal-version: 2.0
library: {}
|] `shouldRenderTo` (library [i|
other-modules:
Paths_foo
autogen-modules:
Paths_foo
|]) { packageCabalVersion = "2.0" }

context "when Paths_ module is listed explicitly under generated-other-modules" $ do
it "adds Paths_ to autogen-modules only once" $ do
[i|
verbatim:
cabal-version: 2.0
library:
generated-other-modules: Paths_foo
|] `shouldRenderTo` (library [i|
other-modules:
Paths_foo
autogen-modules:
Paths_foo
|]) { packageCabalVersion = "2.0" }

context "when Paths_ module is listed explicitly under generated-exposed-modules" $ do
it "adds Paths_ to autogen-modules only once" $ do
[i|
verbatim:
cabal-version: 2.0
library:
generated-exposed-modules: Paths_foo
|] `shouldRenderTo` (library [i|
exposed-modules:
Paths_foo
autogen-modules:
Paths_foo
|]) { packageCabalVersion = "2.0" }

context "when Paths_ is mentioned in a conditional that is always false" $ do
it "does not add Paths_" $ do
[i|
Expand All @@ -100,6 +141,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
OverloadedStrings
other-modules:
Paths_foo
autogen-modules:
Paths_foo
|]) {packageCabalVersion = "2.2"}

context "when Paths_ is mentioned in a conditional that is always false" $ do
Expand Down Expand Up @@ -455,6 +498,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
library
other-modules:
Paths_foo
autogen-modules:
Paths_foo
cxx-options: -Wall
default-language: Haskell2010
|]) {packageCabalVersion = "2.2"}
Expand All @@ -469,6 +514,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
library
other-modules:
Paths_foo
autogen-modules:
Paths_foo
cxx-options: -Wall
default-language: Haskell2010
|]) {packageCabalVersion = "2.2"}
Expand Down Expand Up @@ -671,6 +718,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
system-build-tools:
g++ >= 5.4.0
|] `shouldRenderTo` (executable_ "foo" [i|
autogen-modules:
Paths_foo
build-tools:
g++ >=5.4.0
|]) {packageCabalVersion = "2.0"}
Expand All @@ -690,6 +739,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
executable:
dependencies: foo:bar
|] `shouldRenderTo` (executable_ "foo" [i|
autogen-modules:
Paths_foo
build-depends:
foo:bar
|]) {packageCabalVersion = "3.0"}
Expand Down Expand Up @@ -800,6 +851,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
executable:
cxx-options: -Wall
|] `shouldRenderTo` (executable_ "foo" [i|
autogen-modules:
Paths_foo
cxx-options: -Wall
|]) {packageCabalVersion = "2.2"}

Expand All @@ -815,6 +868,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
condition: True
cxx-options: -Wall
|] `shouldRenderTo` (executable_ "foo" [i|
autogen-modules:
Paths_foo
if true
if true
if true
Expand All @@ -829,6 +884,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
- foo.cc
- cxxbits/*.cc
|] `shouldRenderTo` (executable_ "foo" [i|
autogen-modules:
Paths_foo
cxx-sources:
foo.cc
cxxbits/bar.cc
Expand Down Expand Up @@ -979,6 +1036,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
library:
signatures: Foo
|] `shouldRenderTo` (library_ [i|
autogen-modules:
Paths_foo
signatures:
Foo
|]) {packageCabalVersion = "2.0"}
Expand Down Expand Up @@ -1008,6 +1067,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
|] `shouldRenderTo` (library [i|
other-modules:
Paths_foo
autogen-modules:
Paths_foo
build-depends:
foo
mixins:
Expand All @@ -1026,6 +1087,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Foo
other-modules:
Paths_foo
autogen-modules:
Paths_foo
hs-source-dirs:
src
|]
Expand Down Expand Up @@ -1055,6 +1118,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
visibility: public
other-modules:
Paths_foo
autogen-modules:
Paths_foo
|]) {packageCabalVersion = "3.0"}

context "when inferring modules" $ do
Expand Down Expand Up @@ -1239,6 +1304,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Paths_foo
Bar
autogen-modules:
Paths_foo
Foo
Bar
|]) {packageCabalVersion = "2.0"}
Expand All @@ -1260,6 +1326,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Paths_foo
Other
autogen-modules:
Paths_foo
Exposed
Other
|]) {packageCabalVersion = "2.0"}
Expand All @@ -1277,6 +1344,8 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
|] `shouldRenderTo` (library [i|
other-modules:
Paths_foo
autogen-modules:
Paths_foo
hs-source-dirs:
src
if os(windows)
Expand Down Expand Up @@ -1340,6 +1409,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Paths_foo
Foo
autogen-modules:
Paths_foo
Foo
|]) {packageCabalVersion = "2.0"}

Expand Down

0 comments on commit f9df468

Please sign in to comment.