|
| 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 |
0 commit comments