1+ {-# LANGUAGE RecordWildCards #-}
2+ {-# LANGUAGE LambdaCase #-}
13{-# LANGUAGE CPP #-}
24{-# LANGUAGE TemplateHaskell #-}
35{-# LANGUAGE OverloadedStrings #-}
@@ -9,6 +11,11 @@ import Development.GitRev (gitCommitCount)
911import Options.Applicative.Simple (simpleVersion )
1012import qualified Paths_haskell_language_server as Meta
1113import System.Info
14+ import Data.Version
15+ import Data.Maybe (listToMaybe )
16+ import System.Process
17+ import System.Exit
18+ import Text.ParserCombinators.ReadP
1219
1320hlsVersion :: String
1421hlsVersion =
@@ -24,3 +31,46 @@ hlsVersion =
2431 ]
2532 where
2633 hlsGhcDisplayVersion = compilerName ++ " -" ++ VERSION_ghc
34+
35+ data ProgramsOfInterest = ProgramsOfInterest
36+ { cabalVersion :: Maybe Version
37+ , stackVersion :: Maybe Version
38+ , ghcVersion :: Maybe Version
39+ }
40+
41+ showProgramVersionOfInterest :: ProgramsOfInterest -> String
42+ showProgramVersionOfInterest ProgramsOfInterest {.. } =
43+ unlines
44+ [ concat [" cabal:\t\t " , showVersionWithDefault cabalVersion]
45+ , concat [" stack:\t\t " , showVersionWithDefault stackVersion]
46+ , concat [" ghc:\t\t " , showVersionWithDefault ghcVersion]
47+ ]
48+ where
49+ showVersionWithDefault :: Maybe Version -> String
50+ showVersionWithDefault = maybe (" Not found" ) showVersion
51+
52+ findProgramVersions :: IO ProgramsOfInterest
53+ findProgramVersions = ProgramsOfInterest
54+ <$> findVersionOf " cabal"
55+ <*> findVersionOf " stack"
56+ <*> findVersionOf " ghc"
57+
58+ -- | Find the version of the given program.
59+ -- Assumes the program accepts the cli argument "--numeric-version".
60+ -- If the invocation has a non-zero exit-code, we return 'Nothing'
61+ findVersionOf :: FilePath -> IO (Maybe Version )
62+ findVersionOf tool =
63+ readProcessWithExitCode tool [" --numeric-version" ] " " >>= \ case
64+ (ExitSuccess , sout, _) -> pure $ consumeParser myVersionParser sout
65+ _ -> pure $ Nothing
66+
67+ where
68+ myVersionParser = do
69+ skipSpaces
70+ version <- parseVersion
71+ skipSpaces
72+ pure version
73+
74+ consumeParser :: ReadP a -> String -> Maybe a
75+ consumeParser p input = listToMaybe $ map fst . filter (null . snd ) $ readP_to_S p input
76+
0 commit comments