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

Deduplicate main for hie/hie-wrapper #1610

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 2 additions & 47 deletions app/HieWrapper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@
-- https://github.com/alanz/vscode-hie-server
module Main where

#if __GLASGOW_HASKELL__ < 804
import Data.Semigroup
#endif
import Data.List
import Data.Foldable
import Data.Version (showVersion)
import HIE.Bios
import Haskell.Ide.Engine.MonadFunctions
import Haskell.Ide.Engine.Cradle (findLocalCradle)
import Haskell.Ide.Engine.Options
import Haskell.Ide.Engine.Version
import qualified Language.Haskell.LSP.Core as Core
import Options.Applicative.Simple
import qualified Paths_haskell_ide_engine as Meta
import System.Directory
import System.Environment
import qualified System.Log.Logger as L
import System.Process
import System.Info
import System.FilePath
Expand All @@ -28,45 +20,8 @@ import System.FilePath

main :: IO ()
main = do
let
numericVersion :: Parser (a -> a)
numericVersion =
infoOption
(showVersion Meta.version)
(long "numeric-version" <>
help "Show only version number")
compiler :: Parser (a -> a)
compiler =
infoOption
hieGhcDisplayVersion
(long "compiler" <>
help "Show only compiler and version supported")
-- Parse the options and run
(global, ()) <-
simpleOptions
hieVersion
"hie-wrapper - Launch the appropriate haskell-ide-engine for a given project"
""
(numericVersion <*> compiler <*> globalOptsParser)
empty

run global

-- ---------------------------------------------------------------------

run :: GlobalOpts -> IO ()
run opts = do
let mLogFileName = optLogFile opts

logLevel = if optDebugOn opts
then L.DEBUG
else L.INFO

Core.setupLogger mLogFileName ["hie"] logLevel

maybe (pure ()) setCurrentDirectory $ projectRoot opts


_opts <- initApp
"hie-wrapper - Launch the appropriate haskell-ide-engine for a given project"
progName <- getProgName
logm $ "run entered for hie-wrapper(" ++ progName ++ ") " ++ hieVersion
d <- getCurrentDirectory
Expand Down
56 changes: 3 additions & 53 deletions app/MainHie.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ module Main where

import qualified Control.Exception as E
import Control.Monad
#if __GLASGOW_HASKELL__ < 808
import Data.Monoid ((<>))
#endif
import Data.Version (showVersion)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Yaml as Yaml
Expand All @@ -20,15 +16,10 @@ import Haskell.Ide.Engine.Options
import Haskell.Ide.Engine.Scheduler
import Haskell.Ide.Engine.Server
import Haskell.Ide.Engine.Version
import qualified Language.Haskell.LSP.Core as Core
import Options.Applicative.Simple
import qualified Paths_haskell_ide_engine as Meta
import System.Directory
import System.Environment
import System.FilePath
import System.Info
import System.IO
import qualified System.Log.Logger as L

-- ---------------------------------------------------------------------

Expand Down Expand Up @@ -84,47 +75,9 @@ plugins includeExamples = pluginDescToIdePlugins allPlugins

main :: IO ()
main = do
let
numericVersion :: Parser (a -> a)
numericVersion =
infoOption
(showVersion Meta.version)
(long "numeric-version" <>
help "Show only version number")
compiler :: Parser (a -> a)
compiler =
infoOption
hieGhcDisplayVersion
(long "compiler" <>
help "Show only compiler and version supported")
-- Parse the options and run
(global, ()) <-
simpleOptions
hieVersion
"haskell-ide-engine - Provide a common engine to power any Haskell IDE"
""
(numericVersion <*> compiler <*> globalOptsParser)
empty

run global

-- ---------------------------------------------------------------------

run :: GlobalOpts -> IO ()
run opts = do
hSetBuffering stderr LineBuffering
let mLogFileName = optLogFile opts

logLevel = if optDebugOn opts
then L.DEBUG
else L.INFO

Core.setupLogger mLogFileName ["hie", "hie-bios"] logLevel

origDir <- getCurrentDirectory

maybe (pure ()) setCurrentDirectory $ projectRoot opts

opts <- initApp
"haskell-ide-engine - Provide a common engine to power any Haskell IDE"
progName <- getProgName
args <- getArgs

Expand Down Expand Up @@ -199,10 +152,7 @@ run opts = do
-- ---------------------------------------------------------------------

getCradleInfo :: FilePath -> IO (Either Yaml.ParseException Cradle)
getCradleInfo currentDir = do
let dummyCradleFile = currentDir </> "File.hs"
cradleRes <- E.try (findLocalCradle dummyCradleFile)
return cradleRes
getCradleInfo currentDir = E.try $ findLocalCradle $ currentDir </> "File.hs"

-- ---------------------------------------------------------------------

Expand Down
35 changes: 35 additions & 0 deletions src/Haskell/Ide/Engine/Options.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
{-# LANGUAGE CPP #-}
module Haskell.Ide.Engine.Options where

#if __GLASGOW_HASKELL__ < 804
import Data.Semigroup
#endif
#if __GLASGOW_HASKELL__ < 808
import Data.Monoid ((<>))
#endif
import Data.Version (showVersion)
import Haskell.Ide.Engine.Version
import qualified Language.Haskell.LSP.Core as Core
import Options.Applicative.Simple
import qualified Paths_haskell_ide_engine as Meta
import System.Directory
import System.IO
import qualified System.Log.Logger as L
import Data.Foldable

data GlobalOpts = GlobalOpts
{ optDebugOn :: Bool
Expand All @@ -15,6 +29,27 @@ data GlobalOpts = GlobalOpts
, optFiles :: [FilePath]
} deriving (Show)

initApp :: String -> IO GlobalOpts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is now happening out of context, it would be nice if there was some documentation what this function does, e.g. initialises logger, searches for some project root, etc...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead reestablish context, such as by saying in a comment that this is the common prefix of hie and hie-wrapper?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense, too. Still, what it roughly does to set-up the application would be nice as well.

initApp namedesc = do
hSetBuffering stderr LineBuffering
let numericVersion :: Parser (a -> a)
numericVersion = infoOption (showVersion Meta.version)
(long "numeric-version" <> help "Show only version number")
compiler :: Parser (a -> a)
compiler = infoOption hieGhcDisplayVersion
(long "compiler" <> help "Show only compiler and version supported")
-- Parse the options and run
(opts, ()) <- simpleOptions
hieVersion
namedesc
""
(numericVersion <*> compiler <*> globalOptsParser)
empty
Core.setupLogger (optLogFile opts) ["hie", "hie-bios"]
$ if optDebugOn opts then L.DEBUG else L.INFO
traverse_ setCurrentDirectory $ projectRoot opts
return opts

globalOptsParser :: Parser GlobalOpts
globalOptsParser = GlobalOpts
<$> switch
Expand Down