git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add node flags to psci

Any flags after -- will be be untouched by optparse-applicative. Ones that look like flags will be treated as arguments for node. Not sure how to test this. This also doesn't affect the documentation of the psci command so we still need to figure out where to document this.

commit05d71febd97137a1a114e2571b309a9020d613d7 authorMichael Xavier <michael@michaelxavier.net> dateSun 4 Jan 2015, 16:26 (12 years ago) parentdadcd51
1 file changed+19−10
psci/Main.hs+19−10
psci/Main.hs+19−10
@@ -18,7 +18,7 @@
1818module Main where
1919
2020import Data.Foldable (traverse_)
21−import Data.List (intercalate, isPrefixOf, nub, sortBy, sort)
21+import Data.List (intercalate, isPrefixOf, nub, sortBy, sort, partition)
2222import Data.Maybe (mapMaybe)
2323import Data.Traversable (traverse)
2424import Data.Version (showVersion)
@@ -35,6 +35,7 @@ import Control.Monad.Trans.State.Strict
3535import qualified Control.Monad.Trans.State.Lazy as L
3636
3737import Options.Applicative as Opts
38+import Options.Applicative.Types as Opts
3839
3940import System.Console.Haskeline
4041import System.Directory (createDirectoryIfMissing, getModificationTime, doesFileExist, findExecutable, getHomeDirectory, getCurrentDirectory)
@@ -71,6 +72,7 @@ data PSCiState = PSCiState
7172 , psciImportedModuleNames :: [P.ModuleName]
7273 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
7374 , psciLetBindings :: [P.Expr -> P.Expr]
75+ , psciNodeFlags :: [String]
7476 }
7577
7678-- State helpers
@@ -254,7 +256,7 @@ completion = completeWordWithPrev Nothing " \t\n\r" findCompletions
254256 exports (P.ValueRef ident') = ident == ident'
255257 exports (P.PositionedDeclarationRef _ r) = exports r
256258 exports _ = False
257−
259+
258260 identNames :: [P.Module] -> [String]
259261 identNames ms = nub [ show qual
260262 | P.Module moduleName ds exts <- ms
@@ -362,12 +364,13 @@ handleDeclaration val = do
362364 st <- PSCI $ lift get
363365 let m = createTemporaryModule True st val
364366 e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [(Left P.RebuildAlways, m)]) []
367+ let nodeArgs = psciNodeFlags st ++ [indexFile]
365368 case e of
366369 Left err -> PSCI $ outputStrLn err
367370 Right _ -> do
368371 psciIO $ writeFile indexFile "require('$PSCI').main();"
369372 process <- psciIO findNodeProcess
370− result <- psciIO $ traverse (\node -> readProcessWithExitCode node [indexFile] "") process
373+ result <- psciIO $ traverse (\node -> readProcessWithExitCode node nodeArgs "") process
371374 case result of
372375 Just (ExitSuccess, out, _) -> PSCI $ outputStrLn out
373376 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
@@ -533,7 +536,7 @@ handleCommand Reset = do
533536 modulesOrFirstError <- psciIO $ loadAllModules files
534537 case modulesOrFirstError of
535538 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 [] [])
537540handleCommand (TypeOf val) = handleTypeOf val
538541handleCommand (KindOf typ) = handleKindOf typ
539542handleCommand (Browse moduleName) = handleBrowse moduleName
@@ -557,8 +560,8 @@ loadUserConfig = do
557560-- |
558561-- The PSCI main loop.
559562--
560−loop :: PSCiOptions -> IO ()
561−loop (PSCiOptions singleLineMode files) = do
563+loop :: PSCiOptions -> [String] -> IO ()
564+loop (PSCiOptions singleLineMode files) nflags = do
562565 config <- loadUserConfig
563566 modulesOrFirstError <- loadAllModules files
564567 case modulesOrFirstError of
@@ -566,7 +569,7 @@ loop (PSCiOptions singleLineMode files) = do
566569 Right modules -> do
567570 historyFilename <- getHistoryFilename
568571 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
570573 outputStrLn prologueMessage
571574 traverse_ (mapM_ (runPSCI . handleCommand)) config
572575 go
@@ -594,14 +597,20 @@ psciOptions :: Parser PSCiOptions
594597psciOptions = PSCiOptions <$> singleLineFlag
595598 <*> many inputFile
596599
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+
597607main :: IO ()
598−main = execParser opts >>= loop
608+main = uncurry loop . splitOpts =<< execParser opts
599609 where
600610 opts = info (version <*> helper <*> psciOptions) infoModList
601611 infoModList = fullDesc <> headerInfo <> footerInfo
602612 headerInfo = header "psci - Interactive mode for PureScript"
603613 footerInfo = footer $ "psci " ++ showVersion Paths.version
604−
614+
605615 version :: Parser (a -> a)
606616 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> Opts.help "Show the version number" <> hidden
607−