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 list-bin support for scripts. #7925

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 16 additions & 33 deletions cabal-install/src/Distribution/Client/CmdListBin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import Prelude ()
import Distribution.Client.CmdErrorMessages
(plural, renderListCommaAnd, renderTargetProblem, renderTargetProblemNoTargets,
renderTargetSelector, showTargetSelector, targetSelectorFilter, targetSelectorPluralPkgs)
import Distribution.Client.DistDirLayout (DistDirLayout (..), ProjectRoot (..))
import Distribution.Client.DistDirLayout (DistDirLayout (..))
import Distribution.Client.NixStyleOptions
(NixStyleFlags (..), defaultNixStyleFlags, nixStyleOptions)
import Distribution.Client.ProjectConfig
(ProjectConfig, projectConfigConfigFile, projectConfigShared, withProjectOrGlobalConfig)
import Distribution.Client.ProjectFlags (ProjectFlags (..))
import Distribution.Client.ProjectOrchestration
import Distribution.Client.ProjectPlanning.Types
import Distribution.Client.ScriptUtils
(AcceptNoTargets(..), TargetContext(..), updateContextAndWriteProjectFile, withContextAndSelectors)
import Distribution.Client.Setup (GlobalFlags (..))
import Distribution.Client.TargetProblem (TargetProblem (..))
import Distribution.Simple.BuildPaths (dllExtension, exeExtension)
Expand All @@ -41,7 +40,6 @@ import Distribution.Types.ComponentName (showComponentName)
import Distribution.Types.UnitId (UnitId)
import Distribution.Types.UnqualComponentName (UnqualComponentName)
import Distribution.Verbosity (silent, verboseStderr)
import System.Directory (getCurrentDirectory)
import System.FilePath ((<.>), (</>))

import qualified Data.Map as Map
Expand Down Expand Up @@ -73,19 +71,18 @@ listbinCommand = CommandUI

listbinAction :: NixStyleFlags () -> [String] -> GlobalFlags -> IO ()
listbinAction flags@NixStyleFlags{..} args globalFlags = do
-- fail early if multiple target selectors specified
target <- case args of
[] -> die' verbosity "One target is required, none provided"
[x] -> return x
_ -> die' verbosity "One target is required, given multiple"

-- configure
(baseCtx, distDirLayout) <- withProjectOrGlobalConfig verbosity ignoreProject globalConfigFlag withProject withoutProject
let localPkgs = localPackages baseCtx

-- elaborate target selectors
targetSelectors <- either (reportTargetSelectorProblems verbosity) return
=<< readTargetSelectors localPkgs (Just ExeKind) [target]
-- fail early if multiple target selectors specified
target <- case args of
[] -> die' verbosity "One target is required, none provided"
[x] -> return x
_ -> die' verbosity "One target is required, given multiple"

-- configure and elaborate target selectors
withContextAndSelectors RejectNoTargets (Just ExeKind) flags [target] globalFlags $ \targetCtx ctx targetSelectors -> do
baseCtx <- case targetCtx of
ProjectContext -> return ctx
GlobalContext -> return ctx
ScriptContext path exemeta -> updateContextAndWriteProjectFile ctx path exemeta

buildCtx <-
runProjectPreBuildPhase verbosity baseCtx $ \elaboratedPlan -> do
Expand Down Expand Up @@ -131,7 +128,7 @@ listbinAction flags@NixStyleFlags{..} args globalFlags = do
Nothing -> die' verbosity "No or multiple targets given..."
Just gpp -> return $ IP.foldPlanPackage
(const []) -- IPI don't have executables
(elaboratedPackage distDirLayout (elaboratedShared buildCtx))
(elaboratedPackage (distDirLayout baseCtx) (elaboratedShared buildCtx))
gpp

case binfiles of
Expand All @@ -140,20 +137,6 @@ listbinAction flags@NixStyleFlags{..} args globalFlags = do
where
defaultVerbosity = verboseStderr silent
verbosity = fromFlagOrDefault defaultVerbosity (configVerbosity configFlags)
ignoreProject = flagIgnoreProject projectFlags
prjConfig = commandLineFlagsToProjectConfig globalFlags flags mempty -- ClientInstallFlags, not needed here
globalConfigFlag = projectConfigConfigFile (projectConfigShared prjConfig)

withProject :: IO (ProjectBaseContext, DistDirLayout)
withProject = do
baseCtx <- establishProjectBaseContext verbosity prjConfig OtherCommand
return (baseCtx, distDirLayout baseCtx)

withoutProject :: ProjectConfig -> IO (ProjectBaseContext, DistDirLayout)
withoutProject config = do
cwd <- getCurrentDirectory
baseCtx <- establishProjectBaseContextWithRoot verbosity (config <> prjConfig) (ProjectRootImplicit cwd) OtherCommand
return (baseCtx, distDirLayout baseCtx)

-- this is copied from
elaboratedPackage
Expand Down
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/ListBin/Script/cabal.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# cabal list-bin
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- fake-package-0 (exe:script) (first run)
8 changes: 8 additions & 0 deletions cabal-testsuite/PackageTests/ListBin/Script/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Test.Cabal.Prelude

main = cabalTest . void $ do
res <- cabal' "list-bin" ["script.hs"]

env <- getTestEnv
cacheDir <- getScriptCacheDirectory $ testCurrentDir env </> "script.hs"
assertOutputContains cacheDir res
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bacchanalia : I don't understand how this assertOutputContains succeeds with above output (that does not contain something resembling a cacheDir). But apparently it does. How?

6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/ListBin/Script/script.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{- cabal:
build-depends: base
-}

main :: IO ()
main = putStrLn "Hello World"
5 changes: 3 additions & 2 deletions changelog.d/pr-7851
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
synopsis: Better support for scripts
packages: cabal-install
prs: #7851
issues: #7842 #6354 #6149.
prs: #7851 #7925
issues: #7842 #7073 #6354 #6149

description: {

Expand All @@ -11,5 +11,6 @@ description: {
- `cabal repl script` added. It will open a repl for script using the cache if available.
- `cabal clean script` added. It will clean the cache for script.
- `cabal clean` will now remove script caches for which there is no marching script.
- `cabal list-bin` now works with scripts

}