Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit c78cbb0

Browse files
authored
Merge pull request #1297 from maoe/find-all-available-ghcs
install.hs: Make all available GHCs in PATH buildable
2 parents d657504 + fdaf48b commit c78cbb0

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

install.hs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Control.Monad.Extra ( unlessM
1919
)
2020
import Data.Maybe ( isJust )
2121
import System.Directory ( findExecutable
22+
, findExecutables
2223
, listDirectory
2324
)
2425
import System.Environment ( getProgName
@@ -34,6 +35,8 @@ import Data.Maybe ( isNothing
3435
import Data.List ( dropWhileEnd
3536
, intersperse
3637
, intercalate
38+
, isInfixOf
39+
, nubBy
3740
, sort
3841
)
3942
import qualified Data.Text as T
@@ -42,7 +45,9 @@ import Data.Version ( parseVersion
4245
, makeVersion
4346
, showVersion
4447
)
45-
import Data.Function ( (&) )
48+
import Data.Function ( (&)
49+
, on
50+
)
4651
import Text.ParserCombinators.ReadP ( readP_to_S )
4752

4853
type VersionNumber = String
@@ -143,7 +148,7 @@ main = do
143148
forM_ ghcVersions cabalTest
144149

145150
forM_
146-
hieVersions
151+
ghcVersions
147152
(\version -> phony ("cabal-hie-" ++ version) $ do
148153
validateCabalNewInstallIsSupported
149154
need ["submodules"]
@@ -182,7 +187,7 @@ validateCabalNewInstallIsSupported = when isWindowsSystem $ do
182187

183188
configureCabal :: VersionNumber -> Action ()
184189
configureCabal versionNumber = do
185-
ghcPath <- getGhcPath versionNumber >>= \case
190+
ghcPath <- getGhcPathOf versionNumber >>= \case
186191
Nothing -> do
187192
liftIO $ putStrLn $ embedInStars (ghcVersionNotFoundFailMsg versionNumber)
188193
error (ghcVersionNotFoundFailMsg versionNumber)
@@ -193,12 +198,18 @@ configureCabal versionNumber = do
193198
findInstalledGhcs :: IO [(VersionNumber, GhcPath)]
194199
findInstalledGhcs = do
195200
hieVersions <- getHieVersions :: IO [VersionNumber]
196-
mapMaybeM
197-
(\version -> getGhcPath version >>= \case
201+
knownGhcs <- mapMaybeM
202+
(\version -> getGhcPathOf version >>= \case
198203
Nothing -> return Nothing
199204
Just p -> return $ Just (version, p)
200205
)
201206
(reverse hieVersions)
207+
availableGhcs <- getGhcPaths
208+
return
209+
-- nub by version. knownGhcs takes precedence.
210+
$ nubBy ((==) `on` fst)
211+
-- filter out stack provided GHCs
212+
$ filter (not . isInfixOf ".stack" . snd) (knownGhcs ++ availableGhcs)
202213

203214
cabalBuildHie :: VersionNumber -> Action ()
204215
cabalBuildHie versionNumber = do
@@ -515,16 +526,19 @@ getStackGhcPathShake = do
515526
-- First, it is checked whether there is a GHC with the name `ghc-$VersionNumber`.
516527
-- If this yields no result, it is checked, whether the numeric-version of the `ghc`
517528
-- command fits to the desired version.
518-
getGhcPath :: MonadIO m => VersionNumber -> m (Maybe GhcPath)
519-
getGhcPath ghcVersion = liftIO $
520-
findExecutable ("ghc-" ++ ghcVersion) >>= \case
521-
Nothing -> do
522-
findExecutable "ghc" >>= \case
523-
Nothing -> return Nothing
524-
Just p -> do
525-
Stdout version <- cmd p ["--numeric-version"] :: IO (Stdout String)
526-
if ghcVersion == trim version then return $ Just p else return Nothing
527-
p -> return p
529+
getGhcPathOf :: MonadIO m => VersionNumber -> m (Maybe GhcPath)
530+
getGhcPathOf ghcVersion =
531+
liftIO $ findExecutable ("ghc-" ++ ghcVersion) >>= \case
532+
Nothing -> lookup ghcVersion <$> getGhcPaths
533+
path -> return path
534+
535+
-- | Get a list of GHCs that are available in $PATH
536+
getGhcPaths :: MonadIO m => m [(VersionNumber, GhcPath)]
537+
getGhcPaths = liftIO $ do
538+
paths <- findExecutables "ghc"
539+
forM paths $ \path -> do
540+
Stdout version <- cmd path ["--numeric-version"]
541+
return (trim version, path)
528542

529543
-- | Read the local install root of the stack project specified by the VersionNumber
530544
-- Returns the filepath of the local install root.

0 commit comments

Comments
 (0)