Skip to content

Commit

Permalink
Resolve component targets using readBuildTargets
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Dec 30, 2023
1 parent 00e0e7b commit 7228a98
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions cabal-add.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ library
build-depends:
base <5,
bytestring <0.13,
Cabal >=3.8 && <3.11,
Cabal-syntax >=3.8 && <3.11,
containers <0.8,
mtl <2.4
Expand Down
13 changes: 13 additions & 0 deletions src/Distribution/Client/Add.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ import Distribution.PackageDescription (
componentNameString,
unUnqualComponentName,
)
import Distribution.PackageDescription.Configuration (flattenPackageDescription)
import Distribution.PackageDescription.Parsec (
parseGenericPackageDescription,
parseGenericPackageDescriptionMaybe,
runParseResult,
)
import Distribution.Parsec (Position (..), eitherParsec, showPError)
import Distribution.Simple.BuildTarget (BuildTarget (BuildTargetComponent), readUserBuildTargets, resolveBuildTargets)

-- | Just a newtype wrapper, since @Cabal-syntax@ does not provide any.
newtype CommonStanza = CommonStanza {unCommonStanza :: ByteString}
Expand Down Expand Up @@ -216,6 +218,14 @@ parseCabalFile fileName contents = do

pure (fields, packDescr)

readBuildTarget :: PackageDescription -> String -> Maybe ComponentName
readBuildTarget pkg targetStr = do
let (_, utargets) = readUserBuildTargets [targetStr]
[utarget] <- pure utargets
let (_, btargets) = resolveBuildTargets pkg [(utarget, False)]
[BuildTargetComponent btarget] <- pure btargets
pure btarget

-- | Resolve a raw component name.
resolveComponent
:: MonadError String m
Expand All @@ -227,6 +237,9 @@ resolveComponent
-- ^ Component name (default component if 'Nothing').
-> m (Either CommonStanza ComponentName)
-- ^ Resolved component.
resolveComponent _ (_, gpd) (Just component)
| Just cmp <- readBuildTarget (flattenPackageDescription gpd) component =
pure $ Right cmp
resolveComponent
fileName
(extractCommonStanzas -> commonStanzas, extractComponentNames -> componentNames)
Expand Down
86 changes: 86 additions & 0 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,90 @@ executable baz
|]
}

caseTestTarget1 :: TestTree
caseTestTarget1 =
mkTest $
CabalAddTest
{ catName = "test target 1"
, catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"]
, catInput =
[s|
name: dummy
version: 0.1.0.0
cabal-version: 2.0
build-type: Simple

common baz
language: Haskell2010

test-suite baz
type: exitcode-stdio-1.0
main-is: Main.hs
build-depends:
base >=4.15 && <5
|]
, catOutput =
[s|
name: dummy
version: 0.1.0.0
cabal-version: 2.0
build-type: Simple

common baz
language: Haskell2010

test-suite baz
type: exitcode-stdio-1.0
main-is: Main.hs
build-depends:
foo < 1 && >0.7,
quux < 1,
base >=4.15 && <5
|]
}

caseTestTarget2 :: TestTree
caseTestTarget2 =
mkTest $
CabalAddTest
{ catName = "test target 2"
, catArgs = ["-c", "test:baz", "foo < 1 && >0.7", "quux < 1"]
, catInput =
[s|
name: dummy
version: 0.1.0.0
cabal-version: 2.0
build-type: Simple

common baz
language: Haskell2010

test-suite baz
type: exitcode-stdio-1.0
main-is: Main.hs
build-depends:
base >=4.15 && <5
|]
, catOutput =
[s|
name: dummy
version: 0.1.0.0
cabal-version: 2.0
build-type: Simple

common baz
language: Haskell2010

test-suite baz
type: exitcode-stdio-1.0
main-is: Main.hs
build-depends:
foo < 1 && >0.7,
quux < 1,
base >=4.15 && <5
|]
}

caseCommonStanzaTarget1 :: TestTree
caseCommonStanzaTarget1 =
mkTest $
Expand Down Expand Up @@ -1210,6 +1294,8 @@ main =
, caseExecutableTarget2
, caseExecutableTarget3
, caseExecutableTarget4
, caseTestTarget1
, caseTestTarget2
, caseCommonStanzaTarget1
, caseCommonStanzaTarget2
, caseTwoSpacesInStanza
Expand Down

0 comments on commit 7228a98

Please sign in to comment.