From e3615d2f55569f0d279490953031b78e062bb872 Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Sun, 11 Oct 2026 11:11:59 +0200 Subject: [PATCH] Add --list-targets[=PATTERN] to list the available targets Closes #784. --- CHANGES.txt | 1 + docs/CommandLine.md | 1 + src/Development/Shake/Internal/Args.hs | 28 +++++++++++++------- src/Development/Shake/Internal/Core/Rules.hs | 2 +- src/Test/Targets.hs | 10 +++++++ 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 126545407..86f033217 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ Changelog for Shake (* = breaking change) + #784, add --list-targets to list the available targets #860, add specialise pragmas (maybe 10% faster) Test with GHC 9.14, preserving exception call stacks on GHC 9.12 and later 0.19.9, released 2026-01-19 diff --git a/docs/CommandLine.md b/docs/CommandLine.md index 67b568de9..82789d82d 100755 --- a/docs/CommandLine.md +++ b/docs/CommandLine.md @@ -8,6 +8,7 @@ There are a number of different modes in which Shake can run, which have an over * `-h, --help` -- print the a message about supported command line flags and exit. * `-v, --version, --numeric-version` -- print version information and exit. This will always be the Shake version number as available on Hackage. +* `--list-targets[=PATTERN]` -- print the available targets (those matching `PATTERN`, if given) along with any documentation from `withTargetDocs`, and exit. * `--demo` -- run in demo mode, which creates a demo project and compiles it, producing [this output](Demo.md). * `-f FILE, --file=FILE, --makefile=FILE` -- read in `FILE` as a Makefile or [Ninja project](Ninja.md). By default will look for `build.ninja` and use that if it exists. * `-t TOOL, --tool=TOOL` -- Ninja-compatible tools such as `compdb`, only valid when operating on a Ninja project. diff --git a/src/Development/Shake/Internal/Args.hs b/src/Development/Shake/Internal/Args.hs index 6593f95ca..a6199e159 100644 --- a/src/Development/Shake/Internal/Args.hs +++ b/src/Development/Shake/Internal/Args.hs @@ -15,6 +15,7 @@ import Development.Shake.Internal.CompactUI import Development.Shake.Internal.Demo import Development.Shake.Internal.Core.Action import Development.Shake.FilePath +import Development.Shake.Internal.FilePattern import Development.Shake.Internal.Rules.File import Development.Shake.Internal.Progress import Development.Shake.Database @@ -148,6 +149,7 @@ shakeArgsOptionsWith baseOpts userOptions rules = do changeDirectory = listToMaybe [x | ChangeDirectory x <- flagsExtra] printDirectory = lastDef False [x | PrintDirectory x <- flagsExtra] shareRemoves = [x | ShareRemove x <- flagsExtra] + listTargets = [x | ListTargets x <- flagsExtra] oshakeOpts = foldl' (flip ($)) baseOpts flagsShake lintInside <- mapM canonicalizePath $ shakeLintInside oshakeOpts let shakeOpts = oshakeOpts {shakeLintInside = map (toStandard . addTrailingPathSeparator) lintInside @@ -158,18 +160,21 @@ shakeArgsOptionsWith baseOpts userOptions rules = do } let putWhen v msg = when (shakeVerbosity oshakeOpts >= v) $ shakeOutput oshakeOpts v msg let putWhenLn v msg = putWhen v $ msg ++ "\n" + -- run the rules as simply as we can, to find the targets and help suffix + let collectTargets = do + rs <- rules shakeOpts [] [] + case rs of + Just (_, rs) -> do + xs <- getTargets shakeOpts rs + helpSuffix <- getHelpSuffix shakeOpts rs + evaluate $ force (xs, helpSuffix) + _ -> pure ([], []) + let showTarget (a, b) = a ++ maybe "" (" - " ++) b let showHelp long = do progName <- getProgName (targets, helpSuffix) <- if not long then pure ([], []) else - handleSynchronous (\e -> do putWhenLn Info $ "Failure to collect targets: " ++ show e; pure ([], [])) $ do - -- run the rules as simply as we can - rs <- rules shakeOpts [] [] - case rs of - Just (_, rs) -> do - xs <- getTargets shakeOpts rs - helpSuffix <- getHelpSuffix shakeOpts rs - evaluate $ force ([" - " ++ a ++ maybe "" (" - " ++) b | (a,b) <- xs], helpSuffix) - _ -> pure ([], []) + handleSynchronous (\e -> do putWhenLn Info $ "Failure to collect targets: " ++ show e; pure ([], [])) $ + first (map ((" - " ++) . showTarget)) <$> collectTargets changes<- pure $ let as = shakeOptionsFields baseOpts bs = shakeOptionsFields oshakeOpts @@ -190,6 +195,9 @@ shakeArgsOptionsWith baseOpts userOptions rules = do if Help `elem` flagsExtra then showHelp True + else if not $ null listTargets then do + targets <- filter ((?==*) listTargets . fst) . fst <$> collectTargets + unless (null targets) $ putWhen Error $ intercalate "\n" $ map showTarget targets else if Version `elem` flagsExtra then putWhenLn Info $ "Shake build system, version " ++ shakeVersionString else if NumericVersion `elem` flagsExtra then @@ -279,6 +287,7 @@ data Extra = ChangeDirectory FilePath | NumericVersion | PrintDirectory Bool | Help + | ListTargets FilePattern | Sleep | NoTime | Exception @@ -336,6 +345,7 @@ shakeOptsEx = ,opts $ Option "" ["lint-fsatrace"] (optArg "DIR" $ \x s -> s{shakeLint=Just LintFSATrace, shakeLintInside=shakeLintInside s ++ [fromMaybe "." x]}) "Use fsatrace to do validation [in current dir]." ,opts $ Option "" ["lint-ignore"] (reqArg "PATTERN" $ \x s -> s{shakeLintIgnore=shakeLintIgnore s ++ [x]}) "Ignore any lint errors in these patterns." ,opts $ Option "" ["no-lint"] (noArg $ \s -> s{shakeLint=Nothing}) "Turn off --lint." + ,extr $ Option "" ["list-targets"] (optArg "PATTERN" $ \x -> [ListTargets $ fromMaybe "//" x]) "List the available targets [matching PATTERN] and exit." ,opts $ Option "" ["live"] (optArg "FILE" $ \x s -> s{shakeLiveFiles=shakeLiveFiles s ++ [fromMaybe "live.txt" x]}) "List the files that are live [to live.txt]." ,opts $ Option "m" ["metadata"] (reqArg "PREFIX" $ \x s -> s{shakeFiles=x}) "Prefix for storing metadata files." ,extr $ Option "" ["numeric-version"] (noArg [NumericVersion]) "Print just the version number and exit." diff --git a/src/Development/Shake/Internal/Core/Rules.hs b/src/Development/Shake/Internal/Core/Rules.hs index 2a49a8aaf..66875e71b 100644 --- a/src/Development/Shake/Internal/Core/Rules.hs +++ b/src/Development/Shake/Internal/Core/Rules.hs @@ -189,7 +189,7 @@ instance (Semigroup a, Monoid a) => Monoid (Rules a) where addUserRule :: Typeable a => a -> Rules () addUserRule r = newRules mempty{userRules = TMap.singleton $ UserRuleVersioned False $ UserRule r} --- | Register a target, as available when passing @--help@ or through 'getTargets'. +-- | Register a target, as available when passing @--help@ or @--list-targets@, or through 'getTargets'. -- Called automatically by rules such as 'Development.Shake.phony' and -- 'Development.Shake.%>' - to avoid that use 'withoutTargets'. -- To add documentation to a target use 'withTargetDocs'. diff --git a/src/Test/Targets.hs b/src/Test/Targets.hs index 7cbbb97c5..947733ef9 100644 --- a/src/Test/Targets.hs +++ b/src/Test/Targets.hs @@ -3,6 +3,8 @@ module Test.Targets(main) where import Development.Shake import Development.Shake.Internal.Core.Rules (getHelpSuffix) import Test.Type +import System.Environment +import qualified System.IO.Extra as IO main :: IO () -> IO () main _sleeper = do @@ -11,6 +13,14 @@ main _sleeper = do helpSuffix <- getHelpSuffix shakeOptions rules helpSuffix === ["Don't Panic", "Know where your towel is"] + let listTargets args = lines . fst <$> IO.captureOutput (withArgs args $ shakeArgs shakeOptions rules) + xs <- listTargets ["--list-targets"] + xs === [a ++ maybe "" (" - " ++) b | (a,b) <- expected] + xs <- listTargets ["--list-targets=file1*", "--quiet"] + xs === ["file1", "file10", "file11 - a great file", "file12 - awesome files", "file13 - awesome files"] + xs <- listTargets ["--list-targets=phony*", "--list-targets=Foo"] + xs === ["phony1 - A phony target", "phony2 - Builds something really good", "Foo - bad docs"] + rules :: Rules () rules = do withTargetDocs "A phony target" $ phony "phony1" $ pure ()