git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add node flags to psci
05d71feMichael Xavier
psci/Main.hs+19−10
@@ -18,7 +18,7 @@
18module Main where
18module Main where
19
19
20import Data.Foldable (traverse_)
20import Data.Foldable (traverse_)
21−import Data.List (intercalate, isPrefixOf, nub, sortBy, sort)
21+import Data.List (intercalate, isPrefixOf, nub, sortBy, sort, partition)
22import Data.Maybe (mapMaybe)
22import Data.Maybe (mapMaybe)
23import Data.Traversable (traverse)
23import Data.Traversable (traverse)
24import Data.Version (showVersion)
24import Data.Version (showVersion)
@@ -35,6 +35,7 @@ import Control.Monad.Trans.State.Strict
35import qualified Control.Monad.Trans.State.Lazy as L
35import qualified Control.Monad.Trans.State.Lazy as L
36
36
37import Options.Applicative as Opts
37import Options.Applicative as Opts
38+import Options.Applicative.Types as Opts
38
39
39import System.Console.Haskeline
40import System.Console.Haskeline
40import System.Directory (createDirectoryIfMissing, getModificationTime, doesFileExist, findExecutable, getHomeDirectory, getCurrentDirectory)
41import System.Directory (createDirectoryIfMissing, getModificationTime, doesFileExist, findExecutable, getHomeDirectory, getCurrentDirectory)
@@ -71,6 +72,7 @@ data PSCiState = PSCiState
71 , psciImportedModuleNames :: [P.ModuleName]
72 , psciImportedModuleNames :: [P.ModuleName]
72 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
73 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
73 , psciLetBindings :: [P.Expr -> P.Expr]
74 , psciLetBindings :: [P.Expr -> P.Expr]
75+ , psciNodeFlags :: [String]
74 }
76 }
75
77
76-- State helpers
78-- State helpers
@@ -254,7 +256,7 @@ completion = completeWordWithPrev Nothing " \t\n\r" findCompletions
254 exports (P.ValueRef ident') = ident == ident'
256 exports (P.ValueRef ident') = ident == ident'
255 exports (P.PositionedDeclarationRef _ r) = exports r
257 exports (P.PositionedDeclarationRef _ r) = exports r
256 exports _ = False
258 exports _ = False
257−
259+
258 identNames :: [P.Module] -> [String]
260 identNames :: [P.Module] -> [String]
259 identNames ms = nub [ show qual
261 identNames ms = nub [ show qual
260 | P.Module moduleName ds exts <- ms
262 | P.Module moduleName ds exts <- ms
@@ -362,12 +364,13 @@ handleDeclaration val = do
362 st <- PSCI $ lift get
364 st <- PSCI $ lift get
363 let m = createTemporaryModule True st val
365 let m = createTemporaryModule True st val
364 e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [(Left P.RebuildAlways, m)]) []
366 e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [(Left P.RebuildAlways, m)]) []
367+ let nodeArgs = psciNodeFlags st ++ [indexFile]
365 case e of
368 case e of
366 Left err -> PSCI $ outputStrLn err
369 Left err -> PSCI $ outputStrLn err
367 Right _ -> do
370 Right _ -> do
368 psciIO $ writeFile indexFile "require('$PSCI').main();"
371 psciIO $ writeFile indexFile "require('$PSCI').main();"
369 process <- psciIO findNodeProcess
372 process <- psciIO findNodeProcess
370− result <- psciIO $ traverse (\node -> readProcessWithExitCode node [indexFile] "") process
373+ result <- psciIO $ traverse (\node -> readProcessWithExitCode node nodeArgs "") process
371 case result of
374 case result of
372 Just (ExitSuccess, out, _) -> PSCI $ outputStrLn out
375 Just (ExitSuccess, out, _) -> PSCI $ outputStrLn out
373 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
376 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
@@ -533,7 +536,7 @@ handleCommand Reset = do
533 modulesOrFirstError <- psciIO $ loadAllModules files
536 modulesOrFirstError <- psciIO $ loadAllModules files
534 case modulesOrFirstError of
537 case modulesOrFirstError of
535 Left err -> psciIO $ print err >> exitFailure
538 Left err -> psciIO $ print err >> exitFailure
536− Right modules -> PSCI . lift $ put (PSCiState files defaultImports modules [])
539+ Right modules -> PSCI . lift $ put (PSCiState files defaultImports modules [] [])
537handleCommand (TypeOf val) = handleTypeOf val
540handleCommand (TypeOf val) = handleTypeOf val
538handleCommand (KindOf typ) = handleKindOf typ
541handleCommand (KindOf typ) = handleKindOf typ
539handleCommand (Browse moduleName) = handleBrowse moduleName
542handleCommand (Browse moduleName) = handleBrowse moduleName
@@ -557,8 +560,8 @@ loadUserConfig = do
557-- |
560-- |
558-- The PSCI main loop.
561-- The PSCI main loop.
559--
562--
560−loop :: PSCiOptions -> IO ()
563+loop :: PSCiOptions -> [String] -> IO ()
561−loop (PSCiOptions singleLineMode files) = do
564+loop (PSCiOptions singleLineMode files) nflags = do
562 config <- loadUserConfig
565 config <- loadUserConfig
563 modulesOrFirstError <- loadAllModules files
566 modulesOrFirstError <- loadAllModules files
564 case modulesOrFirstError of
567 case modulesOrFirstError of
@@ -566,7 +569,7 @@ loop (PSCiOptions singleLineMode files) = do
566 Right modules -> do
569 Right modules -> do
567 historyFilename <- getHistoryFilename
570 historyFilename <- getHistoryFilename
568 let settings = defaultSettings { historyFile = Just historyFilename }
571 let settings = defaultSettings { historyFile = Just historyFilename }
569− flip evalStateT (PSCiState files defaultImports modules []) . runInputT (setComplete completion settings) $ do
572+ flip evalStateT (PSCiState files defaultImports modules [] nflags) . runInputT (setComplete completion settings) $ do
570 outputStrLn prologueMessage
573 outputStrLn prologueMessage
571 traverse_ (mapM_ (runPSCI . handleCommand)) config
574 traverse_ (mapM_ (runPSCI . handleCommand)) config
572 go
575 go
@@ -594,14 +597,20 @@ psciOptions :: Parser PSCiOptions
594psciOptions = PSCiOptions <$> singleLineFlag
597psciOptions = PSCiOptions <$> singleLineFlag
595 <*> many inputFile
598 <*> many inputFile
596
599
600+splitOpts :: PSCiOptions -> (PSCiOptions, [String])
601+splitOpts (opts@PSCiOptions { psciInputFile = fs }) = (opts { psciInputFile = notFlags}, flags)
602+ where
603+ (flags, notFlags) = partition isFlag fs
604+ isFlag ('-':_) = True
605+ isFlag _ = False
606+
597main :: IO ()
607main :: IO ()
598−main = execParser opts >>= loop
608+main = uncurry loop . splitOpts =<< execParser opts
599 where
609 where
600 opts = info (version <*> helper <*> psciOptions) infoModList
610 opts = info (version <*> helper <*> psciOptions) infoModList
601 infoModList = fullDesc <> headerInfo <> footerInfo
611 infoModList = fullDesc <> headerInfo <> footerInfo
602 headerInfo = header "psci - Interactive mode for PureScript"
612 headerInfo = header "psci - Interactive mode for PureScript"
603 footerInfo = footer $ "psci " ++ showVersion Paths.version
613 footerInfo = footer $ "psci " ++ showVersion Paths.version
604−
614+
605 version :: Parser (a -> a)
615 version :: Parser (a -> a)
606 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> Opts.help "Show the version number" <> hidden
616 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> Opts.help "Show the version number" <> hidden
607−