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

Commit 62f38f8

Browse files
authored
Merge pull request #1610 from haskell/mergemains
Deduplicate main for hie/hie-wrapper
2 parents 66291d6 + 7e2db8e commit 62f38f8

File tree

3 files changed

+38
-100
lines changed

3 files changed

+38
-100
lines changed

app/HieWrapper.hs

+2-47
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33
-- https://github.com/alanz/vscode-hie-server
44
module Main where
55

6-
#if __GLASGOW_HASKELL__ < 804
7-
import Data.Semigroup
8-
#endif
96
import Data.List
107
import Data.Foldable
11-
import Data.Version (showVersion)
128
import HIE.Bios
139
import Haskell.Ide.Engine.MonadFunctions
1410
import Haskell.Ide.Engine.Cradle (findLocalCradle)
1511
import Haskell.Ide.Engine.Options
1612
import Haskell.Ide.Engine.Version
17-
import qualified Language.Haskell.LSP.Core as Core
18-
import Options.Applicative.Simple
19-
import qualified Paths_haskell_ide_engine as Meta
2013
import System.Directory
2114
import System.Environment
22-
import qualified System.Log.Logger as L
2315
import System.Process
2416
import System.Info
2517
import System.FilePath
@@ -28,45 +20,8 @@ import System.FilePath
2820

2921
main :: IO ()
3022
main = do
31-
let
32-
numericVersion :: Parser (a -> a)
33-
numericVersion =
34-
infoOption
35-
(showVersion Meta.version)
36-
(long "numeric-version" <>
37-
help "Show only version number")
38-
compiler :: Parser (a -> a)
39-
compiler =
40-
infoOption
41-
hieGhcDisplayVersion
42-
(long "compiler" <>
43-
help "Show only compiler and version supported")
44-
-- Parse the options and run
45-
(global, ()) <-
46-
simpleOptions
47-
hieVersion
48-
"hie-wrapper - Launch the appropriate haskell-ide-engine for a given project"
49-
""
50-
(numericVersion <*> compiler <*> globalOptsParser)
51-
empty
52-
53-
run global
54-
55-
-- ---------------------------------------------------------------------
56-
57-
run :: GlobalOpts -> IO ()
58-
run opts = do
59-
let mLogFileName = optLogFile opts
60-
61-
logLevel = if optDebugOn opts
62-
then L.DEBUG
63-
else L.INFO
64-
65-
Core.setupLogger mLogFileName ["hie"] logLevel
66-
67-
maybe (pure ()) setCurrentDirectory $ projectRoot opts
68-
69-
23+
_opts <- initApp
24+
"hie-wrapper - Launch the appropriate haskell-ide-engine for a given project"
7025
progName <- getProgName
7126
logm $ "run entered for hie-wrapper(" ++ progName ++ ") " ++ hieVersion
7227
d <- getCurrentDirectory

app/MainHie.hs

+3-53
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ module Main where
55

66
import qualified Control.Exception as E
77
import Control.Monad
8-
#if __GLASGOW_HASKELL__ < 808
9-
import Data.Monoid ((<>))
10-
#endif
11-
import Data.Version (showVersion)
128
import qualified Data.Text as T
139
import qualified Data.Text.IO as T
1410
import qualified Data.Yaml as Yaml
@@ -20,15 +16,10 @@ import Haskell.Ide.Engine.Options
2016
import Haskell.Ide.Engine.Scheduler
2117
import Haskell.Ide.Engine.Server
2218
import Haskell.Ide.Engine.Version
23-
import qualified Language.Haskell.LSP.Core as Core
24-
import Options.Applicative.Simple
25-
import qualified Paths_haskell_ide_engine as Meta
2619
import System.Directory
2720
import System.Environment
2821
import System.FilePath
2922
import System.Info
30-
import System.IO
31-
import qualified System.Log.Logger as L
3223

3324
-- ---------------------------------------------------------------------
3425

@@ -84,47 +75,9 @@ plugins includeExamples = pluginDescToIdePlugins allPlugins
8475

8576
main :: IO ()
8677
main = do
87-
let
88-
numericVersion :: Parser (a -> a)
89-
numericVersion =
90-
infoOption
91-
(showVersion Meta.version)
92-
(long "numeric-version" <>
93-
help "Show only version number")
94-
compiler :: Parser (a -> a)
95-
compiler =
96-
infoOption
97-
hieGhcDisplayVersion
98-
(long "compiler" <>
99-
help "Show only compiler and version supported")
100-
-- Parse the options and run
101-
(global, ()) <-
102-
simpleOptions
103-
hieVersion
104-
"haskell-ide-engine - Provide a common engine to power any Haskell IDE"
105-
""
106-
(numericVersion <*> compiler <*> globalOptsParser)
107-
empty
108-
109-
run global
110-
111-
-- ---------------------------------------------------------------------
112-
113-
run :: GlobalOpts -> IO ()
114-
run opts = do
115-
hSetBuffering stderr LineBuffering
116-
let mLogFileName = optLogFile opts
117-
118-
logLevel = if optDebugOn opts
119-
then L.DEBUG
120-
else L.INFO
121-
122-
Core.setupLogger mLogFileName ["hie", "hie-bios"] logLevel
123-
12478
origDir <- getCurrentDirectory
125-
126-
maybe (pure ()) setCurrentDirectory $ projectRoot opts
127-
79+
opts <- initApp
80+
"haskell-ide-engine - Provide a common engine to power any Haskell IDE"
12881
progName <- getProgName
12982
args <- getArgs
13083

@@ -199,10 +152,7 @@ run opts = do
199152
-- ---------------------------------------------------------------------
200153

201154
getCradleInfo :: FilePath -> IO (Either Yaml.ParseException Cradle)
202-
getCradleInfo currentDir = do
203-
let dummyCradleFile = currentDir </> "File.hs"
204-
cradleRes <- E.try (findLocalCradle dummyCradleFile)
205-
return cradleRes
155+
getCradleInfo currentDir = E.try $ findLocalCradle $ currentDir </> "File.hs"
206156

207157
-- ---------------------------------------------------------------------
208158

src/Haskell/Ide/Engine/Options.hs

+33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
{-# LANGUAGE CPP #-}
22
module Haskell.Ide.Engine.Options where
33

4+
#if __GLASGOW_HASKELL__ < 808
5+
import Data.Monoid ((<>))
6+
#endif
7+
import Data.Version (showVersion)
8+
import Haskell.Ide.Engine.Version
9+
import qualified Language.Haskell.LSP.Core as Core
410
import Options.Applicative.Simple
11+
import qualified Paths_haskell_ide_engine as Meta
12+
import System.Directory
13+
import System.IO
14+
import qualified System.Log.Logger as L
15+
import Data.Foldable
516

617
data GlobalOpts = GlobalOpts
718
{ optDebugOn :: Bool
@@ -15,6 +26,28 @@ data GlobalOpts = GlobalOpts
1526
, optFiles :: [FilePath]
1627
} deriving (Show)
1728

29+
-- | Introduced as the common prefix of app/HieWrapper.hs/main and app/MainHie.hs/main
30+
initApp :: String -> IO GlobalOpts
31+
initApp namedesc = do
32+
hSetBuffering stderr LineBuffering
33+
let numericVersion :: Parser (a -> a)
34+
numericVersion = infoOption (showVersion Meta.version)
35+
(long "numeric-version" <> help "Show only version number")
36+
compiler :: Parser (a -> a)
37+
compiler = infoOption hieGhcDisplayVersion
38+
(long "compiler" <> help "Show only compiler and version supported")
39+
-- Parse the options and run
40+
(opts, ()) <- simpleOptions
41+
hieVersion
42+
namedesc
43+
""
44+
(numericVersion <*> compiler <*> globalOptsParser)
45+
empty
46+
Core.setupLogger (optLogFile opts) ["hie", "hie-bios"]
47+
$ if optDebugOn opts then L.DEBUG else L.INFO
48+
traverse_ setCurrentDirectory $ projectRoot opts
49+
return opts
50+
1851
globalOptsParser :: Parser GlobalOpts
1952
globalOptsParser = GlobalOpts
2053
<$> switch

0 commit comments

Comments
 (0)