Skip to content

Commit ff69985

Browse files
committed
Add package-local build tools to build-tool-depends
(when `cabal-version >= 2`)
1 parent 9444564 commit ff69985

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/Hpack/Render.hs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Hpack.Render (
2323
, Alignment(..)
2424
, CommaStyle(..)
2525
#ifdef TEST
26+
, RenderEnv(..)
2627
, renderConditional
2728
, renderDependencies
2829
, renderLibraryFields
@@ -48,7 +49,18 @@ import Hpack.Render.Hints
4849
import Hpack.Render.Dsl hiding (sortFieldsBy)
4950
import qualified Hpack.Render.Dsl as Dsl
5051

51-
type RenderM = Reader CabalVersion
52+
data RenderEnv = RenderEnv {
53+
renderEnvCabalVersion :: CabalVersion
54+
, renderEnvPackageName :: String
55+
}
56+
57+
type RenderM = Reader RenderEnv
58+
59+
getCabalVersion :: RenderM CabalVersion
60+
getCabalVersion = asks renderEnvCabalVersion
61+
62+
getPackageName :: RenderM String
63+
getPackageName = asks renderEnvPackageName
5264

5365
renderPackage :: [String] -> Package -> String
5466
renderPackage oldCabalFile = renderPackageWith settings headerFieldsAlignment formattingHintsFieldOrder formattingHintsSectionsFieldOrder
@@ -82,7 +94,7 @@ renderPackageWith settings headerFieldsAlignment existingFieldOrder sectionsFiel
8294
customSetup = maybe [] (return . renderCustomSetup) packageCustomSetup
8395

8496
stanzas :: [Element]
85-
stanzas = flip runReader packageCabalVersion $ do
97+
stanzas = flip runReader (RenderEnv packageCabalVersion packageName) $ do
8698
library <- maybe (return []) (fmap return . renderLibrary) packageLibrary
8799
internalLibraries <- renderInternalLibraries packageInternalLibraries
88100
executables <- renderExecutables packageExecutables
@@ -357,21 +369,28 @@ renderBuildTools :: Map BuildTool DependencyVersion -> SystemBuildTools -> Rende
357369
renderBuildTools buildTools systemBuildTools = do
358370
xs <- traverse renderBuildTool $ Map.toList buildTools
359371
return [
360-
Field "build-tools" (CommaSeparatedList $ [x | BuildTools x <- xs] ++ renderSystemBuildTools systemBuildTools)
361-
, Field "build-tool-depends" (CommaSeparatedList [x | BuildToolDepends x <- xs])
372+
Field "build-tools" $ CommaSeparatedList $ [x | BuildTools x <- xs] ++ renderSystemBuildTools systemBuildTools
373+
, Field "build-tool-depends" $ CommaSeparatedList [x | BuildToolDepends x <- xs]
362374
]
363375

364376
data RenderBuildTool = BuildTools String | BuildToolDepends String
365377

366378
renderBuildTool :: (BuildTool, DependencyVersion) -> RenderM RenderBuildTool
367379
renderBuildTool (buildTool, renderVersion -> version) = do
368-
cabalVersion <- ask
380+
cabalVersion <- getCabalVersion
381+
packageName <- getPackageName
382+
let supportsBuildTools = cabalVersion < makeCabalVersion [2]
369383
return $ case buildTool of
370-
LocalBuildTool executable -> BuildTools (executable ++ version)
384+
LocalBuildTool executable
385+
| supportsBuildTools -> BuildTools (executable ++ version)
386+
| otherwise -> BuildToolDepends (packageName ++ ":" ++ executable ++ version)
371387
BuildTool pkg executable
372-
| cabalVersion < makeCabalVersion [2] && pkg == executable && executable `elem` knownBuildTools -> BuildTools (executable ++ version)
388+
| supportsBuildTools && isknownBuildTool pkg executable -> BuildTools (executable ++ version)
373389
| otherwise -> BuildToolDepends (pkg ++ ":" ++ executable ++ version)
374390
where
391+
isknownBuildTool :: String -> String -> Bool
392+
isknownBuildTool pkg executable = pkg == executable && executable `elem` knownBuildTools
393+
375394
knownBuildTools :: [String]
376395
knownBuildTools = [
377396
"alex"

test/EndToEndSpec.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,22 @@ spec = around_ (inTempDirectoryNamed "my-package") $ do
734734
bar ==0.2.0
735735
|]
736736

737+
context "when cabal-version >= 2" $ do
738+
it "adds it to build-tool-depends" $ do
739+
[i|
740+
verbatim:
741+
cabal-version: 2.0
742+
executables:
743+
bar:
744+
build-tools:
745+
- bar
746+
|] `shouldRenderTo` (executable_ "bar" [i|
747+
autogen-modules:
748+
Paths_my_package
749+
build-tool-depends:
750+
my-package:bar
751+
|]) {packageCabalVersion = "2.0"}
752+
737753
context "when the name of a build tool matches a legacy system build tool" $ do
738754
it "adds it to build-tools" $ do
739755
[i|

test/Hpack/RenderSpec.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,19 @@ spec = do
225225
]
226226

227227
describe "renderConditional" $ do
228+
let run = flip runReader (RenderEnv cabalVersion "foo")
229+
228230
it "renders conditionals" $ do
229231
let conditional = Conditional "os(windows)" (section Empty) {sectionDependencies = deps ["Win32"]} Nothing
230-
render defaultRenderSettings 0 (runReader (renderConditional renderEmptySection conditional) cabalVersion) `shouldBe` [
232+
render defaultRenderSettings 0 (run $ renderConditional renderEmptySection conditional) `shouldBe` [
231233
"if os(windows)"
232234
, " build-depends:"
233235
, " Win32"
234236
]
235237

236238
it "renders conditionals with else-branch" $ do
237239
let conditional = Conditional "os(windows)" (section Empty) {sectionDependencies = deps ["Win32"]} (Just $ (section Empty) {sectionDependencies = deps ["unix"]})
238-
render defaultRenderSettings 0 (runReader (renderConditional renderEmptySection conditional) cabalVersion) `shouldBe` [
240+
render defaultRenderSettings 0 (run $ renderConditional renderEmptySection conditional) `shouldBe` [
239241
"if os(windows)"
240242
, " build-depends:"
241243
, " Win32"
@@ -247,7 +249,7 @@ spec = do
247249
it "renders nested conditionals" $ do
248250
let conditional = Conditional "arch(i386)" (section Empty) {sectionGhcOptions = ["-threaded"], sectionConditionals = [innerConditional]} Nothing
249251
innerConditional = Conditional "os(windows)" (section Empty) {sectionDependencies = deps ["Win32"]} Nothing
250-
render defaultRenderSettings 0 (runReader (renderConditional renderEmptySection conditional) cabalVersion) `shouldBe` [
252+
render defaultRenderSettings 0 (run $ renderConditional renderEmptySection conditional) `shouldBe` [
251253
"if arch(i386)"
252254
, " ghc-options: -threaded"
253255
, " if os(windows)"
@@ -258,7 +260,7 @@ spec = do
258260
it "conditionalises both build-depends and mixins" $ do
259261
let conditional = Conditional "os(windows)" (section Empty) {sectionDependencies = [("Win32", depInfo)]} Nothing
260262
depInfo = defaultInfo { dependencyInfoMixins = ["hiding (Blah)"] }
261-
render defaultRenderSettings 0 (runReader (renderConditional renderEmptySection conditional) cabalVersion) `shouldBe` [
263+
render defaultRenderSettings 0 (run $ renderConditional renderEmptySection conditional) `shouldBe` [
262264
"if os(windows)"
263265
, " build-depends:"
264266
, " Win32"

0 commit comments

Comments
 (0)