diff --git a/ChangeLog.md b/ChangeLog.md index 55a6593087..4adf861882 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -15,6 +15,7 @@ Other enhancements: * Avoid the duplicate resolving of usage files when parsing `*.hi` files into a set of modules and a collection of resolved usage files. See [#6123](https://github.com/commercialhaskell/stack/pull/6123). +* Add component type flags to the `ide targets` command. Bug fixes: diff --git a/doc/build_command.md b/doc/build_command.md index b8d465f063..bb59f1f1e3 100644 --- a/doc/build_command.md +++ b/doc/build_command.md @@ -144,8 +144,14 @@ supported syntaxes for targets are: `stack build` with no targets specified will build all local packages. -Command `stack ide targets` to get a list of the available targets in your -project. +## Listing targets + +`stack ide targets` lists every available target in your project. + +With one or more component type flags this listing can be restricted. + +`stack ide targets --exes` lists only exe targets. +`stack ide targets --tests --benchmarks` lists test and benchmark targets. ## Controlling what gets built diff --git a/src/Stack/IDE.hs b/src/Stack/IDE.hs index bf5f88ebee..154e15cc0d 100644 --- a/src/Stack/IDE.hs +++ b/src/Stack/IDE.hs @@ -20,11 +20,12 @@ import Stack.Runners import Stack.Types.BuildConfig ( BuildConfig (..), HasBuildConfig (..) ) import Stack.Types.NamedComponent - ( NamedComponent, renderPkgComponent ) + ( NamedComponent, renderPkgComponent, isCExe, isCTest, isCBench ) import Stack.Types.Runner ( Runner ) import Stack.Types.SourceMap - ( ProjectPackage (..), SMWanted (..), ppComponents ) + ( ProjectPackage (..), SMWanted (..), ppComponentsMaybe ) import System.IO ( putStrLn ) +import Data.Tuple (swap) -- Type representing output channel choices for the @stack ide packages@ and -- @stack ide targets@ commands. @@ -47,10 +48,14 @@ idePackagesCmd :: (OutputStream, ListPackagesCmd) -> RIO Runner () idePackagesCmd = withConfig NoReexec . withBuildConfig . uncurry listPackages +compTypes :: (Bool, Bool, Bool) -> NamedComponent -> Bool +compTypes (False, False, False) = const True +compTypes (exe, test, bench) = \x -> (exe && isCExe x) || (test && isCTest x) || (bench && isCBench x) + -- | Function underlying the @stack ide targets@ command. List targets in the -- project. -ideTargetsCmd :: OutputStream -> RIO Runner () -ideTargetsCmd = withConfig NoReexec . withBuildConfig . listTargets +ideTargetsCmd :: ((Bool, Bool, Bool), OutputStream) -> RIO Runner () +ideTargetsCmd = withConfig NoReexec . withBuildConfig . uncurry listTargets . fmap compTypes . swap outputFunc :: HasTerm env => OutputStream -> String -> RIO env () outputFunc OutputLogInfo = prettyInfo . fromString @@ -72,8 +77,8 @@ listPackages stream flag = do mapM_ (outputFunc stream) strs -- | List the targets in the current project. -listTargets :: forall env. HasBuildConfig env => OutputStream -> RIO env () -listTargets stream = do +listTargets :: forall env. HasBuildConfig env => OutputStream -> (NamedComponent -> Bool) -> RIO env () +listTargets stream isCompType = do packages <- view $ buildConfigL.to (smwProject . bcSMWanted) pairs <- concat <$> Map.traverseWithKey toNameAndComponent packages outputFunc stream $ T.unpack $ T.intercalate "\n" $ @@ -84,4 +89,5 @@ listTargets stream = do -> ProjectPackage -> RIO env [(PackageName, NamedComponent)] toNameAndComponent pkgName' = - fmap (map (pkgName', ) . Set.toList) . ppComponents + fmap (map (pkgName', ) . Set.toList) . ppComponentsMaybe (\x -> + if isCompType x then Just x else Nothing) diff --git a/src/Stack/Types/SourceMap.hs b/src/Stack/Types/SourceMap.hs index 23f5588ed5..a956307cf9 100644 --- a/src/Stack/Types/SourceMap.hs +++ b/src/Stack/Types/SourceMap.hs @@ -16,6 +16,7 @@ module Stack.Types.SourceMap , DepPackage (..) , ProjectPackage (..) , ppComponents + , ppComponentsMaybe , ppGPD , ppRoot , ppVersion @@ -174,19 +175,22 @@ ppRoot = parent . ppCabalFP -- | All components available in the given 'ProjectPackage' ppComponents :: MonadIO m => ProjectPackage -> m (Set NamedComponent) -ppComponents pp = do +ppComponents = ppComponentsMaybe Just + +ppComponentsMaybe :: MonadIO m => (NamedComponent -> Maybe NamedComponent) -> ProjectPackage -> m (Set NamedComponent) +ppComponentsMaybe compType pp = do gpd <- ppGPD pp pure $ Set.fromList $ concat - [ maybe [] (const [CLib]) (C.condLibrary gpd) - , go CExe (fst <$> C.condExecutables gpd) - , go CTest (fst <$> C.condTestSuites gpd) - , go CBench (fst <$> C.condBenchmarks gpd) + [ maybe [] (const $ catMaybes [compType CLib]) (C.condLibrary gpd) + , go (compType . CExe) (fst <$> C.condExecutables gpd) + , go (compType . CTest) (fst <$> C.condTestSuites gpd) + , go (compType . CBench) (fst <$> C.condBenchmarks gpd) ] where - go :: (T.Text -> NamedComponent) + go :: (T.Text -> Maybe NamedComponent) -> [C.UnqualComponentName] -> [NamedComponent] - go wrapper = map (wrapper . T.pack . C.unUnqualComponentName) + go wrapper = mapMaybe (wrapper . T.pack . C.unUnqualComponentName) -- | Version for the given 'ProjectPackage ppVersion :: MonadIO m => ProjectPackage -> m Version diff --git a/src/main/Stack/CLI.hs b/src/main/Stack/CLI.hs index 0750def404..e40275585f 100644 --- a/src/main/Stack/CLI.hs +++ b/src/main/Stack/CLI.hs @@ -9,7 +9,7 @@ import Data.Attoparsec.Interpreter ( getInterpreterArgs ) import Data.Char ( toLower ) import qualified Data.List as L import Options.Applicative - ( Parser, ParserFailure, ParserHelp, ParserResult (..), flag + ( Parser, ParserFailure, ParserHelp, ParserResult (..), flag, switch , handleParseResult, help, helpError, idm, long, metavar , overFailure, renderFailure, strArgument, switch ) import Options.Applicative.Help ( errorHelp, stringChunk, vcatChunks ) @@ -318,6 +318,18 @@ commandLineHandler currentDir progName isInterpreter = <> help "Print paths to package Cabal files instead of package \ \names." ) + exeFlag = switch + ( long "exes" + <> help "Include exes." + ) + testFlag = switch + ( long "tests" + <> help "Include tests." + ) + benchFlag = switch + ( long "benchmarks" + <> help "Include benchmarks." + ) in do addCommand' "packages" @@ -326,9 +338,9 @@ commandLineHandler currentDir progName isInterpreter = ((,) <$> outputFlag <*> cabalFileFlag) addCommand' "targets" - "List all available Stack targets." + "List all targets or pick component types to list." ideTargetsCmd - outputFlag + ((,) <$> ((,,) <$> exeFlag <*> testFlag <*> benchFlag) <*> outputFlag) ) init = addCommand'