Skip to content

Commit 64443a6

Browse files
committed
Add a cabal target command
1 parent 9025af5 commit 64443a6

File tree

5 files changed

+220
-2
lines changed

5 files changed

+220
-2
lines changed

cabal-install/cabal-install.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ library
100100
Distribution.Client.CmdRepl
101101
Distribution.Client.CmdRun
102102
Distribution.Client.CmdSdist
103+
Distribution.Client.CmdTarget
103104
Distribution.Client.CmdTest
104105
Distribution.Client.CmdUpdate
105106
Distribution.Client.Compat.Directory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE TupleSections #-}
6+
{-# LANGUAGE ViewPatterns #-}
7+
8+
module Distribution.Client.CmdTarget
9+
( targetCommand
10+
, targetAction
11+
) where
12+
13+
import Distribution.Client.Compat.Prelude
14+
import Prelude ()
15+
16+
import qualified Data.Map as Map
17+
import Distribution.Client.CmdBuild (selectComponentTarget, selectPackageTargets)
18+
import Distribution.Client.CmdErrorMessages
19+
import Distribution.Client.Errors
20+
import Distribution.Client.NixStyleOptions
21+
( NixStyleFlags (..)
22+
, defaultNixStyleFlags
23+
)
24+
import Distribution.Client.ProjectOrchestration
25+
import Distribution.Client.ScriptUtils
26+
( AcceptNoTargets (..)
27+
, TargetContext (..)
28+
, updateContextAndWriteProjectFile
29+
, withContextAndSelectors
30+
)
31+
import Distribution.Client.Setup
32+
( ConfigFlags (..)
33+
, GlobalFlags
34+
)
35+
import Distribution.Client.TargetProblem
36+
( TargetProblem'
37+
)
38+
import Distribution.Simple.Command
39+
( CommandUI (..)
40+
, usageAlternatives
41+
)
42+
import Distribution.Simple.Flag (fromFlagOrDefault)
43+
import Distribution.Simple.Utils
44+
( dieWithException
45+
, wrapText
46+
)
47+
import Distribution.Verbosity
48+
( normal
49+
)
50+
51+
-------------------------------------------------------------------------------
52+
-- Command
53+
-------------------------------------------------------------------------------
54+
55+
targetCommand :: CommandUI (NixStyleFlags ())
56+
targetCommand =
57+
CommandUI
58+
{ commandName = "v2-target"
59+
, commandSynopsis = "List target forms within the project."
60+
, commandUsage = usageAlternatives "v2-target" ["[TARGETS]"]
61+
, commandDescription = Just $ \_ ->
62+
wrapText $
63+
"List targets within a build plan. "
64+
++ "If no [TARGETS] are given 'all' will be used for selecting a build plan.\n\n"
65+
++ "The given target can be;\n"
66+
++ "- a package target (e.g. [pkg:]package)\n"
67+
++ "- a component target (e.g. [package:][ctype:]component)\n"
68+
++ "- all packages (e.g. all)\n"
69+
++ "- components of a particular type (e.g. package:ctypes or all:ctypes)\n"
70+
++ "- a module target: (e.g. [package:][ctype:]module)\n"
71+
++ "- a filepath target: (e.g. [package:][ctype:]filepath)\n"
72+
++ "- a script target: (e.g. path/to/script)\n\n"
73+
++ "The ctypes can be one of: "
74+
++ "libs or libraries, "
75+
++ "exes or executables, "
76+
++ "tests, "
77+
++ "benches or benchmarks, "
78+
++ " and flibs or foreign-libraries."
79+
, commandNotes = Just $ \pname ->
80+
"Examples:\n"
81+
++ " "
82+
++ pname
83+
++ " v2-target all\n"
84+
++ " List all targets of the package in the current directory "
85+
++ "or all packages in the project\n"
86+
++ " "
87+
++ pname
88+
++ " v2-target pkgname\n"
89+
++ " List targets of the package named pkgname in the project\n"
90+
++ " "
91+
++ pname
92+
++ " v2-target ./pkgfoo\n"
93+
++ " List targets of the package in the ./pkgfoo directory\n"
94+
++ " "
95+
++ pname
96+
++ " v2-target cname\n"
97+
++ " List targets of the component named cname in the project\n"
98+
++ " "
99+
, commandDefaultFlags = defaultNixStyleFlags ()
100+
, commandOptions = const []
101+
}
102+
103+
-------------------------------------------------------------------------------
104+
-- Action
105+
-------------------------------------------------------------------------------
106+
107+
targetAction :: NixStyleFlags () -> [String] -> GlobalFlags -> IO ()
108+
targetAction flags@NixStyleFlags{..} ts globalFlags = do
109+
let targetStrings = if null ts then ["all"] else ts
110+
withContextAndSelectors RejectNoTargets Nothing flags targetStrings globalFlags BuildCommand $ \targetCtx ctx targetSelectors -> do
111+
baseCtx <- case targetCtx of
112+
ProjectContext -> return ctx
113+
GlobalContext -> return ctx
114+
ScriptContext path exemeta -> updateContextAndWriteProjectFile ctx path exemeta
115+
116+
buildCtx <-
117+
runProjectPreBuildPhase verbosity baseCtx $ \elaboratedPlan -> do
118+
-- Interpret the targets on the command line as build targets
119+
-- (as opposed to say repl or haddock targets).
120+
targets <-
121+
either (reportBuildTargetProblems verbosity) return $
122+
resolveTargets
123+
selectPackageTargets
124+
selectComponentTarget
125+
elaboratedPlan
126+
Nothing
127+
targetSelectors
128+
129+
let elaboratedPlan' =
130+
pruneInstallPlanToTargets
131+
TargetActionConfigure
132+
targets
133+
elaboratedPlan
134+
elaboratedPlan'' <-
135+
if buildSettingOnlyDeps (buildSettings baseCtx)
136+
then
137+
either (reportCannotPruneDependencies verbosity) return $
138+
pruneInstallPlanToDependencies
139+
(Map.keysSet targets)
140+
elaboratedPlan'
141+
else return elaboratedPlan'
142+
143+
return (elaboratedPlan'', targets)
144+
145+
printPlanTargetForms verbosity buildCtx
146+
where
147+
verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
148+
149+
reportBuildTargetProblems :: Verbosity -> [TargetProblem'] -> IO a
150+
reportBuildTargetProblems verbosity problems =
151+
reportTargetProblems verbosity "target" problems
152+
153+
reportCannotPruneDependencies :: Verbosity -> CannotPruneDependencies -> IO a
154+
reportCannotPruneDependencies verbosity =
155+
dieWithException verbosity . ReportCannotPruneDependencies . renderCannotPruneDependencies

cabal-install/src/Distribution/Client/Main.hs

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ import qualified Distribution.Client.CmdOutdated as CmdOutdated
140140
import qualified Distribution.Client.CmdRepl as CmdRepl
141141
import qualified Distribution.Client.CmdRun as CmdRun
142142
import qualified Distribution.Client.CmdSdist as CmdSdist
143+
import qualified Distribution.Client.CmdTarget as CmdTarget
143144
import qualified Distribution.Client.CmdTest as CmdTest
144145
import qualified Distribution.Client.CmdUpdate as CmdUpdate
145146

@@ -460,6 +461,7 @@ mainWorker args = do
460461
, newCmd CmdExec.execCommand CmdExec.execAction
461462
, newCmd CmdClean.cleanCommand CmdClean.cleanAction
462463
, newCmd CmdSdist.sdistCommand CmdSdist.sdistAction
464+
, newCmd CmdTarget.targetCommand CmdTarget.targetAction
463465
, legacyCmd configureExCommand configureAction
464466
, legacyCmd buildCommand buildAction
465467
, legacyCmd replCommand replAction

cabal-install/src/Distribution/Client/ProjectOrchestration.hs

+57-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ module Distribution.Client.ProjectOrchestration
9090
, pruneInstallPlanToDependencies
9191
, CannotPruneDependencies (..)
9292
, printPlan
93+
, printPlanTargetForms
9394

9495
-- * Build phase: now do it.
9596
, runProjectBuildPhase
@@ -933,7 +934,62 @@ distinctTargetComponents targetsMap =
933934

934935
------------------------------------------------------------------------------
935936
-- Displaying what we plan to do
936-
--
937+
938+
-- | Print available target forms.
939+
printPlanTargetForms
940+
:: Verbosity
941+
-> ProjectBuildContext
942+
-> IO ()
943+
printPlanTargetForms
944+
verbosity
945+
ProjectBuildContext{elaboratedPlanToExecute = elaboratedPlan}
946+
| not (null pkgs) = noticeNoWrap verbosity . unlines $ map showPkgAndReason pkgs
947+
| otherwise = return ()
948+
where
949+
pkgs :: [GenericReadyPackage ElaboratedConfiguredPackage]
950+
pkgs =
951+
sortBy
952+
(compare `on` showPkgAndReason)
953+
(InstallPlan.executionOrder elaboratedPlan)
954+
955+
showPkgAndReason :: ElaboratedReadyPackage -> String
956+
showPkgAndReason (ReadyPackage elab) =
957+
unwords $
958+
filter (not . null) $
959+
[ " -"
960+
, concat . filter (not . null) $
961+
[ prettyShow $ packageName (packageId elab)
962+
, case elabPkgOrComp elab of
963+
ElabPackage _ -> showTargets elab
964+
ElabComponent comp -> ":" ++ showComp elab comp
965+
]
966+
]
967+
968+
showComp :: ElaboratedConfiguredPackage -> ElaboratedComponent -> String
969+
showComp elab comp =
970+
maybe "custom" prettyShow (compComponentName comp)
971+
++ if Map.null (elabInstantiatedWith elab)
972+
then ""
973+
else
974+
" with "
975+
++ intercalate
976+
", "
977+
-- TODO: Abbreviate the UnitIds
978+
[ prettyShow k ++ "=" ++ prettyShow v
979+
| (k, v) <- Map.toList (elabInstantiatedWith elab)
980+
]
981+
982+
showTargets :: ElaboratedConfiguredPackage -> String
983+
showTargets elab
984+
| null (elabBuildTargets elab) = ""
985+
| otherwise =
986+
"("
987+
++ intercalate
988+
", "
989+
[ showComponentTarget (packageId elab) t
990+
| t <- elabBuildTargets elab
991+
]
992+
++ ")"
937993

938994
-- | Print a user-oriented presentation of the install plan, indicating what
939995
-- will be built.

cabal-install/src/Distribution/Client/Setup.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ globalCommand commands =
275275
, "unpack"
276276
, "init"
277277
, "configure"
278+
, "target"
278279
, "build"
279280
, "clean"
280281
, "run"
@@ -327,7 +328,8 @@ globalCommand commands =
327328
, "v1-register"
328329
, "v1-reconfigure"
329330
, -- v2 commands, nix-style
330-
"v2-build"
331+
"v2-target"
332+
, "v2-build"
331333
, "v2-configure"
332334
, "v2-repl"
333335
, "v2-freeze"
@@ -381,6 +383,7 @@ globalCommand commands =
381383
, addCmd "clean"
382384
, par
383385
, startGroup "running and testing"
386+
, addCmd "target"
384387
, addCmd "list-bin"
385388
, addCmd "repl"
386389
, addCmd "run"
@@ -412,6 +415,7 @@ globalCommand commands =
412415
, addCmd "v2-install"
413416
, addCmd "v2-clean"
414417
, addCmd "v2-sdist"
418+
, addCmd "v2-target"
415419
, par
416420
, startGroup "legacy command aliases"
417421
, addCmd "v1-build"

0 commit comments

Comments
 (0)