|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +-- | This module is based on the hie-wrapper.sh script in |
| 3 | +-- https://github.com/alanz/vscode-hie-server |
| 4 | +module Main where |
| 5 | + |
| 6 | +#if __GLASGOW_HASKELL__ < 804 |
| 7 | +import Data.Semigroup |
| 8 | +#endif |
| 9 | +import Data.Foldable |
| 10 | +import Data.List |
| 11 | +import Data.Version (showVersion) |
| 12 | +import HIE.Bios |
| 13 | +import Ide.Cradle (findLocalCradle, logm) |
| 14 | +import Ide.Options |
| 15 | +import Ide.Version |
| 16 | +import qualified Language.Haskell.LSP.Core as Core |
| 17 | +import Options.Applicative.Simple |
| 18 | +import qualified Paths_ide as Meta |
| 19 | +import System.Directory |
| 20 | +import System.Environment |
| 21 | +import System.FilePath |
| 22 | +import System.Info |
| 23 | +import qualified System.Log.Logger as L |
| 24 | +import System.Process |
| 25 | + |
| 26 | +-- --------------------------------------------------------------------- |
| 27 | + |
| 28 | +main :: IO () |
| 29 | +main = do |
| 30 | + let |
| 31 | + numericVersion :: Parser (a -> a) |
| 32 | + numericVersion = |
| 33 | + infoOption |
| 34 | + (showVersion Meta.version) |
| 35 | + (long "numeric-version" <> |
| 36 | + help "Show only version number") |
| 37 | + compiler :: Parser (a -> a) |
| 38 | + compiler = |
| 39 | + infoOption |
| 40 | + hieGhcDisplayVersion |
| 41 | + (long "compiler" <> |
| 42 | + help "Show only compiler and version supported") |
| 43 | + -- Parse the options and run |
| 44 | + (global, ()) <- |
| 45 | + simpleOptions |
| 46 | + hieVersion |
| 47 | + "haskell-ide-wrapper - Launch the appropriate haskell-ide for a given project" |
| 48 | + "" |
| 49 | + (numericVersion <*> compiler <*> globalOptsParser) |
| 50 | + empty |
| 51 | + |
| 52 | + run global |
| 53 | + |
| 54 | +-- --------------------------------------------------------------------- |
| 55 | + |
| 56 | +run :: GlobalOpts -> IO () |
| 57 | +run opts = do |
| 58 | + let mLogFileName = optLogFile opts |
| 59 | + |
| 60 | + logLevel = if optDebugOn opts |
| 61 | + then L.DEBUG |
| 62 | + else L.INFO |
| 63 | + |
| 64 | + Core.setupLogger mLogFileName ["hie"] logLevel |
| 65 | + |
| 66 | + maybe (pure ()) setCurrentDirectory $ projectRoot opts |
| 67 | + |
| 68 | + |
| 69 | + progName <- getProgName |
| 70 | + logm $ "run entered for haskell-ide-wrapper(" ++ progName ++ ") " ++ hieVersion |
| 71 | + d <- getCurrentDirectory |
| 72 | + logm $ "Current directory:" ++ d |
| 73 | + logm $ "Operating system:" ++ os |
| 74 | + args <- getArgs |
| 75 | + logm $ "args:" ++ show args |
| 76 | + |
| 77 | + -- Get the cabal directory from the cradle |
| 78 | + cradle <- findLocalCradle (d </> "File.hs") |
| 79 | + let dir = cradleRootDir cradle |
| 80 | + logm $ "Cradle directory:" ++ dir |
| 81 | + setCurrentDirectory dir |
| 82 | + |
| 83 | + ghcVersion <- getProjectGhcVersion cradle |
| 84 | + logm $ "Project GHC version:" ++ ghcVersion |
| 85 | + |
| 86 | + let |
| 87 | + hieBin = "haskell-ide-" ++ ghcVersion |
| 88 | + backupHieBin = |
| 89 | + case dropWhileEnd (/='.') ghcVersion of |
| 90 | + [] -> "haskell-ide" |
| 91 | + xs -> "haskell-ide-" ++ init xs |
| 92 | + candidates' = [hieBin, backupHieBin, "haskell-ide"] |
| 93 | + candidates = map (++ exeExtension) candidates' |
| 94 | + |
| 95 | + logm $ "haskell-ide exe candidates :" ++ show candidates |
| 96 | + |
| 97 | + mexes <- traverse findExecutable candidates |
| 98 | + |
| 99 | + case asum mexes of |
| 100 | + Nothing -> logm $ "cannot find any haskell-ide exe, looked for:" ++ intercalate ", " candidates |
| 101 | + Just e -> do |
| 102 | + logm $ "found haskell-ide exe at:" ++ e |
| 103 | + logm $ "args:" ++ show args |
| 104 | + logm "launching ....\n\n\n" |
| 105 | + callProcess e args |
| 106 | + logm "done" |
| 107 | + |
| 108 | +-- --------------------------------------------------------------------- |
0 commit comments