Skip to content

Commit 27a696a

Browse files
committed
Added mainInParentDirectory for easier IHP development
1 parent 80143c7 commit 27a696a

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

ihp-ide/IHP/IDE/Postgres.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ startPostgres = do
1919
shouldInit <- needsDatabaseInit
2020
when shouldInit initDatabase
2121
let args = ["-D", "build/db/state", "-k", currentDir <> "/build/db", "-c", "listen_addresses="]
22-
let params = (Process.proc "postgres" args)
22+
let params = (procDirenvAware "postgres" args)
2323
{ Process.std_in = Process.CreatePipe
2424
, Process.std_out = Process.CreatePipe
2525
, Process.std_err = Process.CreatePipe

ihp-ide/IHP/IDE/Types.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ data ManagedProcess = ManagedProcess
2121
, processHandle :: !ProcessHandle
2222
} deriving (Show)
2323

24+
procDirenvAware :: (?context :: Context) => FilePath -> [String] -> Process.CreateProcess
25+
procDirenvAware command args =
26+
if ?context.wrapWithDirenv
27+
then Process.proc "direnv" (["exec", ".", command] <> args)
28+
else Process.proc command args
29+
2430
createManagedProcess :: CreateProcess -> IO ManagedProcess
2531
createManagedProcess config = do
2632
process <- Process.createProcess config
@@ -128,6 +134,7 @@ data Context = Context
128134
, ghciInChan :: !(Queue.InChan OutputLine) -- ^ Output of the app ghci is written here
129135
, ghciOutChan :: !(Queue.OutChan OutputLine) -- ^ Output of the app ghci is consumed here
130136
, liveReloadClients :: !(IORef (Map UUID Websocket.Connection))
137+
, wrapWithDirenv :: !Bool
131138
}
132139

133140
dispatch :: (?context :: Context) => Action -> IO ()

ihp-ide/exe/IHP/IDE/DevServer.hs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Main (main) where
1+
module Main (main, mainInParentDirectory) where
22

33
import ClassyPrelude
44
import qualified System.Process as Process
@@ -30,9 +30,25 @@ import qualified IHP.FrameworkConfig as FrameworkConfig
3030
import qualified Control.Concurrent.Chan.Unagi as Queue
3131
import IHP.IDE.FileWatcher
3232
import qualified System.Environment as Env
33+
import qualified System.Directory as Directory
34+
35+
mainInParentDirectory :: IO ()
36+
mainInParentDirectory = do
37+
cwd <- Directory.getCurrentDirectory
38+
let projectDir = cwd <> "/../"
39+
Directory.setCurrentDirectory projectDir
40+
41+
Env.setEnv "IHP_LIB" (cwd <> "/ihp-ide/lib/IHP")
42+
Env.setEnv "TOOLSERVER_STATIC" (cwd <> "/ihp-ide/lib/IHP/static")
43+
Env.setEnv "IHP_STATIC" (cwd <> "/lib/IHP/static")
44+
45+
mainWithOptions True
3346

3447
main :: IO ()
35-
main = withUtf8 do
48+
main = mainWithOptions False
49+
50+
mainWithOptions :: Bool -> IO ()
51+
mainWithOptions wrapWithDirenv = withUtf8 do
3652
actionVar <- newEmptyMVar
3753
appStateRef <- emptyAppState >>= newIORef
3854
portConfig <- findAvailablePortConfig
@@ -45,7 +61,7 @@ main = withUtf8 do
4561
logger <- Log.newLogger def
4662
(ghciInChan, ghciOutChan) <- Queue.newChan
4763
liveReloadClients <- newIORef mempty
48-
let ?context = Context { actionVar, portConfig, appStateRef, isDebugMode, logger, ghciInChan, ghciOutChan, liveReloadClients }
64+
let ?context = Context { actionVar, portConfig, appStateRef, isDebugMode, logger, ghciInChan, ghciOutChan, liveReloadClients, wrapWithDirenv }
4965

5066
-- Print IHP Version when in debug mode
5167
when isDebugMode (Log.debug ("IHP Version: " <> Version.ihpVersion))
@@ -215,7 +231,7 @@ startOrWaitPostgres = do
215231
startPostgres
216232
pure ()
217233

218-
startGHCI :: IO ManagedProcess
234+
startGHCI :: (?context :: Context) => IO ManagedProcess
219235
startGHCI = do
220236
let args =
221237
[ "-threaded"
@@ -227,7 +243,8 @@ startGHCI = do
227243
, "-ghci-script", ".ghci" -- Because the previous line ignored default ghci config file locations, we have to manual load our .ghci
228244
, "+RTS", "-A128m", "-n2m", "-H2m", "--nonmoving-gc", "-N"
229245
]
230-
createManagedProcess (Process.proc "ghci" args)
246+
247+
createManagedProcess (procDirenvAware "ghci" args)
231248
{ Process.std_in = Process.CreatePipe
232249
, Process.std_out = Process.CreatePipe
233250
, Process.std_err = Process.CreatePipe

0 commit comments

Comments
 (0)