@@ -5,10 +5,12 @@ module Obelisk.Command where
55import Control.Monad
66import Data.List
77import Data.Monoid
8+import qualified Data.Text as T
89import Options.Applicative
910import System.Environment
1011import System.FilePath
1112import System.IO
13+import System.IO.Temp (withSystemTempDirectory)
1214import System.Posix.Process
1315
1416import Obelisk.Command.Project
@@ -48,7 +50,7 @@ initSource = foldl1 (<|>)
4850
4951data ObCommand
5052 = ObCommand_Init InitSource
51− | ObCommand_Dev
53+ | ObCommand_Dev FilePath
5254 | ObCommand_Thunk ThunkCommand
5355 | ObCommand_Repl FilePath
5456 | ObCommand_Watch FilePath
@@ -56,7 +58,7 @@ data ObCommand
5658obCommand :: Parser ObCommand
5759obCommand = hsubparser $ mconcat
5860 [ command "init" $ info (ObCommand_Init <$> initSource) $ progDesc "Initialize an Obelisk project"
59− , command "dev" $ info (pure ObCommand_Dev) $ progDesc "Run current project in development mode"
61+ , command "dev" $ info (ObCommand_Dev <$> (strArgument (action "directory"))) $ progDesc "Run current project in development mode"
6062 , command "thunk" $ info (ObCommand_Thunk <$> thunkCommand) $ progDesc "Manipulate thunk directories"
6163 , command "repl" $ info (ObCommand_Repl <$> (strArgument (action "directory"))) $ progDesc "Open an interactive interpreter"
6264 , command "watch" $ info (ObCommand_Watch <$> (strArgument (action "directory")))$ progDesc "Watch directory for changes and update interactive interpreter"
@@ -131,7 +133,24 @@ main = do
131133ob :: ObCommand -> IO ()
132134ob = \case
133135 ObCommand_Init source -> initProject source
134− ObCommand_Dev -> putStrLn "Dev!"
136+ ObCommand_Dev _ -> do
137+ freePort <- getFreePort
138+ let dotGhci = unlines
139+ [ ":set args --port " <> show freePort
140+ , ":set -ibackend/src:common/src:frontend/src"
141+ , ":add Common.Api Backend.App Frontend.App"
142+ , ":module + Control.Concurrent Obelisk.Widget.Run Common.Api Frontend.App Backend.App"
143+ ]
144+ testCmd = unlines
145+ [ "backendId <- forkIO backend"
146+ , "let conf = defRunConfig { _runConfig_redirectPort = " <> show freePort <> "}"
147+ , "runWidget conf frontend"
148+ , "killThread backendId"
149+ ]
150+ withSystemTempDirectory "ob-ghci" $ \fp -> do
151+ let dotGhciPath = fp </> ".ghci"
152+ writeFile dotGhciPath dotGhci
153+ runDev dotGhciPath $ Just testCmd
135154 ObCommand_Thunk tc -> case tc of
136155 ThunkCommand_Update thunks -> mapM_ updateThunkToLatest thunks
137156 ThunkCommand_Unpack thunks -> mapM_ unpackThunk thunks