Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ide target component types #6126

Merged
merged 9 commits into from
May 24, 2023
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
10 changes: 8 additions & 2 deletions doc/build_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 13 additions & 7 deletions src/Stack/IDE.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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" $
Expand All @@ -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)
18 changes: 11 additions & 7 deletions src/Stack/Types/SourceMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Stack.Types.SourceMap
, DepPackage (..)
, ProjectPackage (..)
, ppComponents
, ppComponentsMaybe
, ppGPD
, ppRoot
, ppVersion
Expand Down Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/main/Stack/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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"
Expand All @@ -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'
Expand Down