Skip to content

Commit

Permalink
Merge pull request #4889 from haskell/target-package-names
Browse files Browse the repository at this point in the history
Target package names
  • Loading branch information
23Skidoo authored Jan 3, 2018
2 parents d7a88c6 + 8ffed56 commit b5f9914
Show file tree
Hide file tree
Showing 12 changed files with 897 additions and 583 deletions.
23 changes: 13 additions & 10 deletions cabal-install/Distribution/Client/CmdBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ benchAction (applyFlagDefaults -> (configFlags, configExFlags, installFlags, had
-- For the @bench@ command we select all buildable benchmarks,
-- or fail if there are no benchmarks or no buildable benchmarks.
--
selectPackageTargets :: TargetSelector PackageId
selectPackageTargets :: TargetSelector
-> [AvailableTarget k] -> Either TargetProblem [k]
selectPackageTargets targetSelector targets

Expand Down Expand Up @@ -162,17 +162,20 @@ selectPackageTargets targetSelector targets
-- For the @bench@ command we just need to check it is a benchmark, in addition
-- to the basic checks on being buildable etc.
--
selectComponentTarget :: PackageId -> ComponentName -> SubComponentTarget
selectComponentTarget :: SubComponentTarget
-> AvailableTarget k -> Either TargetProblem k
selectComponentTarget pkgid cname subtarget@WholeComponent t
selectComponentTarget subtarget@WholeComponent t
| CBenchName _ <- availableTargetComponentName t
= either (Left . TargetProblemCommon) return $
selectComponentTargetBasic pkgid cname subtarget t
selectComponentTargetBasic subtarget t
| otherwise
= Left (TargetProblemComponentNotBenchmark pkgid cname)
= Left (TargetProblemComponentNotBenchmark (availableTargetPackageId t)
(availableTargetComponentName t))

selectComponentTarget pkgid cname subtarget _
= Left (TargetProblemIsSubComponent pkgid cname subtarget)
selectComponentTarget subtarget t
= Left (TargetProblemIsSubComponent (availableTargetPackageId t)
(availableTargetComponentName t)
subtarget)

-- | The various error conditions that can occur when matching a
-- 'TargetSelector' against 'AvailableTarget's for the @bench@ command.
Expand All @@ -181,13 +184,13 @@ data TargetProblem =
TargetProblemCommon TargetProblemCommon

-- | The 'TargetSelector' matches benchmarks but none are buildable
| TargetProblemNoneEnabled (TargetSelector PackageId) [AvailableTarget ()]
| TargetProblemNoneEnabled TargetSelector [AvailableTarget ()]

-- | There are no targets at all
| TargetProblemNoTargets (TargetSelector PackageId)
| TargetProblemNoTargets TargetSelector

-- | The 'TargetSelector' matches targets but no benchmarks
| TargetProblemNoBenchmarks (TargetSelector PackageId)
| TargetProblemNoBenchmarks TargetSelector

-- | The 'TargetSelector' refers to a component that is not a benchmark
| TargetProblemComponentNotBenchmark PackageId ComponentName
Expand Down
12 changes: 6 additions & 6 deletions cabal-install/Distribution/Client/CmdBuild.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ buildAction (applyFlagDefaults -> (configFlags, configExFlags, installFlags, had
-- For the @build@ command select all components except non-buildable and disabled
-- tests\/benchmarks, fail if there are no such components
--
selectPackageTargets :: TargetSelector PackageId
selectPackageTargets :: TargetSelector
-> [AvailableTarget k] -> Either TargetProblem [k]
selectPackageTargets targetSelector targets

Expand Down Expand Up @@ -159,11 +159,11 @@ selectPackageTargets targetSelector targets
--
-- For the @build@ command we just need the basic checks on being buildable etc.
--
selectComponentTarget :: PackageId -> ComponentName -> SubComponentTarget
selectComponentTarget :: SubComponentTarget
-> AvailableTarget k -> Either TargetProblem k
selectComponentTarget pkgid cname subtarget =
selectComponentTarget subtarget =
either (Left . TargetProblemCommon) Right
. selectComponentTargetBasic pkgid cname subtarget
. selectComponentTargetBasic subtarget


-- | The various error conditions that can occur when matching a
Expand All @@ -173,10 +173,10 @@ data TargetProblem =
TargetProblemCommon TargetProblemCommon

-- | The 'TargetSelector' matches targets but none are buildable
| TargetProblemNoneEnabled (TargetSelector PackageId) [AvailableTarget ()]
| TargetProblemNoneEnabled TargetSelector [AvailableTarget ()]

-- | There are no targets at all
| TargetProblemNoTargets (TargetSelector PackageId)
| TargetProblemNoTargets TargetSelector
deriving (Eq, Show)

reportTargetProblems :: Verbosity -> [TargetProblem] -> IO a
Expand Down
116 changes: 78 additions & 38 deletions cabal-install/Distribution/Client/CmdErrorMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Distribution.Client.TargetSelector
( ComponentKindFilter, componentKind, showTargetSelector )

import Distribution.Package
( packageId, packageName )
( packageId, PackageName, packageName )
import Distribution.Types.ComponentName
( showComponentName )
import Distribution.Solver.Types.OptionalStanza
Expand Down Expand Up @@ -84,13 +84,22 @@ sortGroupOn key = map (\xs@(x:_) -> (key x, xs))
-- Renderering for a few project and package types
--

renderTargetSelector :: TargetSelector PackageId -> String
renderTargetSelector (TargetPackage _ pkgid Nothing) =
"the package " ++ display pkgid
renderTargetSelector :: TargetSelector -> String
renderTargetSelector (TargetPackage _ pkgids Nothing) =
"the " ++ plural (listPlural pkgids) "package" "packages" ++ " "
++ renderListCommaAnd (map display pkgids)

renderTargetSelector (TargetPackage _ pkgid (Just kfilter)) =
renderTargetSelector (TargetPackage _ pkgids (Just kfilter)) =
"the " ++ renderComponentKind Plural kfilter
++ " in the package " ++ display pkgid
++ " in the " ++ plural (listPlural pkgids) "package" "packages" ++ " "
++ renderListCommaAnd (map display pkgids)

renderTargetSelector (TargetPackageNamed pkgname Nothing) =
"the package " ++ display pkgname

renderTargetSelector (TargetPackageNamed pkgname (Just kfilter)) =
"the " ++ renderComponentKind Plural kfilter
++ " in the package " ++ display pkgname

renderTargetSelector (TargetAllPackages Nothing) =
"all the packages in the project"
Expand All @@ -99,20 +108,24 @@ renderTargetSelector (TargetAllPackages (Just kfilter)) =
"all the " ++ renderComponentKind Plural kfilter
++ " in the project"

renderTargetSelector (TargetComponent pkgid CLibName WholeComponent) =
"the library in the package " ++ display pkgid

renderTargetSelector (TargetComponent _pkgid cname WholeComponent) =
"the " ++ showComponentName cname
renderTargetSelector (TargetComponent pkgid cname subtarget) =
renderSubComponentTarget subtarget ++ "the "
++ renderComponentName (packageName pkgid) cname

renderTargetSelector (TargetComponent _pkgid cname (FileTarget filename)) =
"the file " ++ filename ++ " in the " ++ showComponentName cname
renderTargetSelector (TargetComponentUnknown pkgname (Left ucname) subtarget) =
renderSubComponentTarget subtarget ++ "the component " ++ display ucname
++ " in the package " ++ display pkgname

renderTargetSelector (TargetComponent _pkgid cname (ModuleTarget modname)) =
"the module " ++ display modname ++ " in the " ++ showComponentName cname
renderTargetSelector (TargetComponentUnknown pkgname (Right cname) subtarget) =
renderSubComponentTarget subtarget ++ "the "
++ renderComponentName pkgname cname

renderTargetSelector (TargetPackageName pkgname) =
"the package " ++ display pkgname
renderSubComponentTarget :: SubComponentTarget -> String
renderSubComponentTarget WholeComponent = ""
renderSubComponentTarget (FileTarget filename) =
"the file " ++ filename ++ "in "
renderSubComponentTarget (ModuleTarget modname) =
"the module" ++ display modname ++ "in "


renderOptionalStanza :: Plural -> OptionalStanza -> String
Expand All @@ -129,24 +142,35 @@ optionalStanza _ = Nothing

-- | Does the 'TargetSelector' potentially refer to one package or many?
--
targetSelectorPluralPkgs :: TargetSelector a -> Plural
targetSelectorPluralPkgs :: TargetSelector -> Plural
targetSelectorPluralPkgs (TargetAllPackages _) = Plural
targetSelectorPluralPkgs (TargetPackage _ _ _) = Singular
targetSelectorPluralPkgs (TargetComponent _ _ _) = Singular
targetSelectorPluralPkgs (TargetPackageName _) = Singular

-- | Does the 'TargetSelector' refer to
targetSelectorRefersToPkgs :: TargetSelector a -> Bool
targetSelectorRefersToPkgs (TargetAllPackages mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetPackage _ _ mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetComponent _ _ _) = False
targetSelectorRefersToPkgs (TargetPackageName _) = True

targetSelectorFilter :: TargetSelector a -> Maybe ComponentKindFilter
targetSelectorFilter (TargetPackage _ _ mkfilter) = mkfilter
targetSelectorFilter (TargetAllPackages mkfilter) = mkfilter
targetSelectorFilter (TargetComponent _ _ _) = Nothing
targetSelectorFilter (TargetPackageName _) = Nothing
targetSelectorPluralPkgs (TargetPackage _ pids _) = listPlural pids
targetSelectorPluralPkgs (TargetPackageNamed _ _) = Singular
targetSelectorPluralPkgs TargetComponent{} = Singular
targetSelectorPluralPkgs TargetComponentUnknown{} = Singular

-- | Does the 'TargetSelector' refer to packages or to components?
targetSelectorRefersToPkgs :: TargetSelector -> Bool
targetSelectorRefersToPkgs (TargetAllPackages mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetPackage _ _ mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs (TargetPackageNamed _ mkfilter) = isNothing mkfilter
targetSelectorRefersToPkgs TargetComponent{} = False
targetSelectorRefersToPkgs TargetComponentUnknown{} = False

targetSelectorFilter :: TargetSelector -> Maybe ComponentKindFilter
targetSelectorFilter (TargetPackage _ _ mkfilter) = mkfilter
targetSelectorFilter (TargetPackageNamed _ mkfilter) = mkfilter
targetSelectorFilter (TargetAllPackages mkfilter) = mkfilter
targetSelectorFilter TargetComponent{} = Nothing
targetSelectorFilter TargetComponentUnknown{} = Nothing

renderComponentName :: PackageName -> ComponentName -> String
renderComponentName pkgname CLibName = "library " ++ display pkgname
renderComponentName _ (CSubLibName name) = "library " ++ display name
renderComponentName _ (CFLibName name) = "foreign library " ++ display name
renderComponentName _ (CExeName name) = "executable " ++ display name
renderComponentName _ (CTestName name) = "test suite " ++ display name
renderComponentName _ (CBenchName name) = "benchmark " ++ display name

renderComponentKind :: Plural -> ComponentKind -> String
renderComponentKind Singular ckind = case ckind of
Expand Down Expand Up @@ -216,6 +240,18 @@ renderTargetProblemCommon verb (TargetOptionalStanzaDisabledBySolver pkgid cname
where
compkinds = renderComponentKind Plural (componentKind cname)

renderTargetProblemCommon verb (TargetProblemUnknownComponent pkgname ecname) =
"Cannot " ++ verb ++ " the "
++ (case ecname of
Left ucname -> "component " ++ display ucname
Right cname -> renderComponentName pkgname cname)
++ " from the package " ++ display pkgname
++ ", because the package does not contain a "
++ (case ecname of
Left _ -> "component"
Right cname -> renderComponentKind Singular (componentKind cname))
++ " with that name."

renderTargetProblemCommon verb (TargetProblemNoSuchPackage pkgid) =
"Internal error when trying to " ++ verb ++ " the package "
++ display pkgid ++ ". The package is not in the set of available targets "
Expand All @@ -238,7 +274,7 @@ renderTargetProblemCommon verb (TargetProblemNoSuchComponent pkgid cname) =
-- This renders an error message for those cases.
--
renderTargetProblemNoneEnabled :: String
-> TargetSelector PackageId
-> TargetSelector
-> [AvailableTarget ()]
-> String
renderTargetProblemNoneEnabled verb targetSelector targets =
Expand Down Expand Up @@ -300,7 +336,7 @@ renderTargetProblemNoneEnabled verb targetSelector targets =
-- | Several commands have a @TargetProblemNoTargets@ problem constructor.
-- This renders an error message for those cases.
--
renderTargetProblemNoTargets :: String -> TargetSelector PackageId -> String
renderTargetProblemNoTargets :: String -> TargetSelector -> String
renderTargetProblemNoTargets verb targetSelector =
"Cannot " ++ verb ++ " " ++ renderTargetSelector targetSelector
++ " because " ++ reason targetSelector ++ ". "
Expand All @@ -314,15 +350,19 @@ renderTargetProblemNoTargets verb targetSelector =
"it does not contain any components at all"
reason (TargetPackage _ _ (Just kfilter)) =
"it does not contain any " ++ renderComponentKind Plural kfilter
reason (TargetPackageNamed _ Nothing) =
"it does not contain any components at all"
reason (TargetPackageNamed _ (Just kfilter)) =
"it does not contain any " ++ renderComponentKind Plural kfilter
reason (TargetAllPackages Nothing) =
"none of them contain any components at all"
reason (TargetAllPackages (Just kfilter)) =
"none of the packages contain any "
++ renderComponentKind Plural kfilter
reason ts@TargetComponent{} =
error $ "renderTargetProblemNoTargets: " ++ show ts
reason (TargetPackageName _) =
"it does not contain any components at all"
reason ts@TargetComponentUnknown{} =
error $ "renderTargetProblemNoTargets: " ++ show ts

-----------------------------------------------------------
-- Renderering error messages for CannotPruneDependencies
Expand Down
12 changes: 6 additions & 6 deletions cabal-install/Distribution/Client/CmdHaddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ haddockAction (applyFlagDefaults -> (configFlags, configExFlags, installFlags, h
-- depending on the @--executables@ flag we also select all the buildable exes.
-- We do similarly for test-suites, benchmarks and foreign libs.
--
selectPackageTargets :: HaddockFlags -> TargetSelector PackageId
selectPackageTargets :: HaddockFlags -> TargetSelector
-> [AvailableTarget k] -> Either TargetProblem [k]
selectPackageTargets haddockFlags targetSelector targets

Expand Down Expand Up @@ -165,11 +165,11 @@ selectPackageTargets haddockFlags targetSelector targets
-- For the @haddock@ command we just need the basic checks on being buildable
-- etc.
--
selectComponentTarget :: PackageId -> ComponentName -> SubComponentTarget
selectComponentTarget :: SubComponentTarget
-> AvailableTarget k -> Either TargetProblem k
selectComponentTarget pkgid cname subtarget =
selectComponentTarget subtarget =
either (Left . TargetProblemCommon) Right
. selectComponentTargetBasic pkgid cname subtarget
. selectComponentTargetBasic subtarget


-- | The various error conditions that can occur when matching a
Expand All @@ -179,10 +179,10 @@ data TargetProblem =
TargetProblemCommon TargetProblemCommon

-- | The 'TargetSelector' matches targets but none are buildable
| TargetProblemNoneEnabled (TargetSelector PackageId) [AvailableTarget ()]
| TargetProblemNoneEnabled TargetSelector [AvailableTarget ()]

-- | There are no targets at all
| TargetProblemNoTargets (TargetSelector PackageId)
| TargetProblemNoTargets TargetSelector
deriving (Eq, Show)

reportTargetProblems :: Verbosity -> [TargetProblem] -> IO a
Expand Down
17 changes: 9 additions & 8 deletions cabal-install/Distribution/Client/CmdInstall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ installAction (applyFlagDefaults ->
tmpDir
packageSpecifiers

let targetSelectors = TargetPackageName <$> packageNames
let targetSelectors = [ TargetPackageNamed pn Nothing
| pn <- packageNames ]

buildCtx <-
runProjectPreBuildPhase verbosity baseCtx $ \elaboratedPlan -> do
Expand Down Expand Up @@ -215,7 +216,7 @@ symlinkBuiltPackage :: (UnitId -> FilePath) -- ^ A function to get an UnitId's
-- store directory
-> FilePath -- ^ Where to put the symlink
-> ( UnitId
, [(ComponentTarget, [TargetSelector PackageId])] )
, [(ComponentTarget, [TargetSelector])] )
-> IO ()
symlinkBuiltPackage mkSourceBinDir destDir (pkg, components) =
traverse_ (symlinkBuiltExe (mkSourceBinDir pkg) destDir) exes
Expand Down Expand Up @@ -294,7 +295,7 @@ establishDummyProjectBaseContext verbosity cliConfig tmpDir localPackages = do
-- and disabled tests\/benchmarks, fail if there are no such
-- components
--
selectPackageTargets :: TargetSelector PackageId
selectPackageTargets :: TargetSelector
-> [AvailableTarget k] -> Either TargetProblem [k]
selectPackageTargets targetSelector targets

Expand Down Expand Up @@ -327,11 +328,11 @@ selectPackageTargets targetSelector targets
--
-- For the @build@ command we just need the basic checks on being buildable etc.
--
selectComponentTarget :: PackageId -> ComponentName -> SubComponentTarget
selectComponentTarget :: SubComponentTarget
-> AvailableTarget k -> Either TargetProblem k
selectComponentTarget pkgid cname subtarget =
selectComponentTarget subtarget =
either (Left . TargetProblemCommon) Right
. selectComponentTargetBasic pkgid cname subtarget
. selectComponentTargetBasic subtarget


-- | The various error conditions that can occur when matching a
Expand All @@ -341,10 +342,10 @@ data TargetProblem =
TargetProblemCommon TargetProblemCommon

-- | The 'TargetSelector' matches targets but none are buildable
| TargetProblemNoneEnabled (TargetSelector PackageId) [AvailableTarget ()]
| TargetProblemNoneEnabled TargetSelector [AvailableTarget ()]

-- | There are no targets at all
| TargetProblemNoTargets (TargetSelector PackageId)
| TargetProblemNoTargets TargetSelector
deriving (Eq, Show)

reportTargetProblems :: Verbosity -> [TargetProblem] -> IO a
Expand Down
Loading

0 comments on commit b5f9914

Please sign in to comment.