1
+ {-# LANGUAGE RecordWildCards #-}
2
+ {-# LANGUAGE LambdaCase #-}
1
3
{-# LANGUAGE CPP #-}
2
4
{-# LANGUAGE TemplateHaskell #-}
3
5
{-# LANGUAGE OverloadedStrings #-}
@@ -9,6 +11,11 @@ import Development.GitRev (gitCommitCount)
9
11
import Options.Applicative.Simple (simpleVersion )
10
12
import qualified Paths_haskell_language_server as Meta
11
13
import System.Info
14
+ import Data.Version
15
+ import Data.Maybe (listToMaybe )
16
+ import System.Process
17
+ import System.Exit
18
+ import Text.ParserCombinators.ReadP
12
19
13
20
hlsVersion :: String
14
21
hlsVersion =
@@ -24,3 +31,46 @@ hlsVersion =
24
31
]
25
32
where
26
33
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