Skip to content

Commit 017b03f

Browse files
committed
feat: Add signal handling on dataframe executable.
Also ignore project so executable works in source repo.
1 parent 2d6e7ba commit 017b03f

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

app/Main.hs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
14
module Main where
25

6+
import Control.Exception (bracket)
37
import Control.Monad
48
import Data.Time (NominalDiffTime, diffUTCTime, getCurrentTime)
59
import System.Directory (
@@ -9,8 +13,12 @@ import System.Directory (
913
getModificationTime,
1014
getXdgDirectory,
1115
)
16+
import System.Exit (ExitCode (..))
1217
import System.FilePath ((</>))
1318
import System.Process
19+
#ifndef mingw32_HOST_OS
20+
import System.Posix.Signals (Handler(..), installHandler, sigINT)
21+
#endif
1422

1523
main :: IO ()
1624
main = do
@@ -37,15 +45,41 @@ main = do
3745
let command = "cabal"
3846
args =
3947
[ "repl"
48+
, "--ignore-project"
4049
, "-O2"
4150
, "--build-depends"
4251
, "dataframe"
4352
, "--repl-option=-ghci-script=" ++ filepath
4453
]
45-
(_, _, _, processHandle) <- createProcess (proc command args)
54+
let baseCp =
55+
(proc command args)
56+
{ cwd = Just cacheDir
57+
, std_in = Inherit
58+
, std_out = Inherit
59+
, std_err = Inherit
60+
}
61+
#ifdef mingw32_HOST_OS
62+
cp = baseCp {delegate_ctlc = True}
63+
#else
64+
cp = baseCp
65+
#endif
66+
#ifndef mingw32_HOST_OS
67+
-- Unix: ignore Ctrl-C in the wrapper so the child handles it.
68+
bracket (installHandler sigINT Ignore Nothing)
69+
(\old -> installHandler sigINT old Nothing)
70+
(\_ -> runChild cp)
71+
#else
72+
-- Windows: delegate Ctrl-C handling to the child.
73+
runChild cp
74+
#endif
4675

47-
exitCode <- waitForProcess processHandle
48-
pure ()
76+
runChild :: CreateProcess -> IO ()
77+
runChild cp = do
78+
(_, _, _, ph) <- createProcess cp
79+
ec <- waitForProcess ph
80+
case ec of
81+
ExitSuccess -> pure ()
82+
ExitFailure n -> fail ("cabal repl failed with exit code " <> show n)
4983

5084
oneWeek :: NominalDiffTime
5185
oneWeek = 7 * 24 * 60 * 60

dataframe.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ executable dataframe
157157
directory >= 1.3.0.0 && < 2,
158158
filepath >= 1.4 && < 2,
159159
process >= 1.6 && < 2,
160-
time >= 1.12 && < 2
160+
time >= 1.12 && < 2,
161+
unix >= 2 && < 3
161162
hs-source-dirs: app
162163
default-language: Haskell2010
163164
ghc-options: -rtsopts -threaded -with-rtsopts=-N

0 commit comments

Comments
 (0)