Skip to content

Commit 416bbc1

Browse files
committed
Refine helpers to make consuming stdErr output more explicit
1 parent 9612b17 commit 416bbc1

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

ext-common/Ext/Common.hs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Control.Concurrent
77
import Control.Concurrent.MVar
88
import Control.Monad (unless)
99
import Control.Arrow ((>>>))
10+
import qualified GHC.IO.Exception
1011

1112
import qualified Data.ByteString as BS
1213
import qualified Data.ByteString.Lazy as BSL
@@ -373,40 +374,42 @@ ghciThreads = unsafePerformIO $ newMVar []
373374
-- System
374375

375376
bash :: String -> IO String
376-
bash command =
377-
c_ "bash" ["-c", command] ""
377+
bash command = do
378+
(exit, stdOut, stdErr) <- c_ "bash" ["-c", command] ""
379+
pure $ show $ stdErr <> stdOut
378380

379381
bashq :: String -> IO String
380-
bashq command =
381-
cq_ "bash" ["-c", command] ""
382+
bashq command = do
383+
(exit, stdOut, stdErr) <- cq_ "bash" ["-c", command] ""
384+
pure $ show $ stdErr <> stdOut
382385

383386

384-
c_ :: String -> [String] -> String -> IO String
387+
-- Call executable and print output in debug mode
388+
c_ :: String -> [String] -> String -> IO (GHC.IO.Exception.ExitCode, String, String)
385389
c_ bin args input = do
386390
atomicPutStrLnDebug $ "🤖 " <> bin <> " " <> show args <> " " <> input
387391
(exit, stdOut, stdErr) <- System.Process.readProcessWithExitCode bin args input
388-
res <-
389-
if Prelude.length stdErr > 0
390-
then pure stdErr
391-
else pure stdOut
392392
-- This doesn't quite work for debugging because bash will honour the sequence codes of the output
393393
-- which gives us confusing results. I.e. Elm compilation clears the buffer during operation
394394
-- onlyWhen (Prelude.length stdErr > 0) $ atomicPutStrLnDebug $ "└── stdErr: " <> stdErr
395395
-- onlyWhen (Prelude.length stdOut > 0) $ atomicPutStrLnDebug $ "└── stdOut: " <> stdOut
396396
-- This doesn't happen with the show instance as it escapes sequences inside the string
397397
atomicPutStrLnDebug $ "└── " <> show (exit, stdOut, stdErr)
398-
pure $ show (exit, stdOut, stdErr)
398+
pure $ (exit, stdOut, stdErr)
399399

400400

401-
cq_ :: String -> [String] -> String -> IO String
401+
-- Call quiet, don't print output in debug mode
402+
cq_ :: String -> [String] -> String -> IO (GHC.IO.Exception.ExitCode, String, String)
402403
cq_ bin args input = do
403404
atomicPutStrLnDebug $ "🤖 " <> bin <> " " <> show args <> " " <> input
404405
(exit, stdOut, stdErr) <- System.Process.readProcessWithExitCode bin args input
405-
res <-
406-
if Prelude.length stdErr > 0
407-
then pure stdErr
408-
else pure stdOut
409-
pure $ show (exit, stdOut, stdErr)
406+
pure $ (exit, stdOut, stdErr)
407+
408+
409+
execCombineStdOutErr :: String -> [String] -> String -> IO String
410+
execCombineStdOutErr bin args input = do
411+
(exit, stdOut, stdErr) <- c_ bin args input
412+
pure $ stdErr <> stdOut
410413

411414

412415
requireBinary :: String -> IO FilePath

extra/Lamdera/CLI/Live.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,39 +466,39 @@ editors :: FilePath -> [IO (Maybe (B.Builder, EditorOpenIO))]
466466
editors projectRoot =
467467
[ detectEditor "custom-*nix"
468468
(Dir.doesFileExist (projectRoot </> "openEditor.sh"))
469-
(\file row column -> Ext.Common.cq_ (projectRoot </> "openEditor.sh") [file, T.unpack row, T.unpack column] "")
469+
(\file row column -> Ext.Common.execCombineStdOutErr (projectRoot </> "openEditor.sh") [file, T.unpack row, T.unpack column] "")
470470

471471
, detectEditor "custom-windows"
472472
(do
473473
exists <- Dir.doesFileExist (projectRoot </> "openEditor.bat")
474474
pure $ exists && ostype == Windows
475475
)
476-
(\file row column -> Ext.Common.cq_ (projectRoot </> "openEditor.bat") [file, T.unpack row, T.unpack column] "")
476+
(\file row column -> Ext.Common.execCombineStdOutErr (projectRoot </> "openEditor.bat") [file, T.unpack row, T.unpack column] "")
477477

478478
, detectExecutable "code-insiders"
479479
(\executablePath file row column -> do
480-
Ext.Common.c_ executablePath [ "-g", file <> ":" <> T.unpack row <> ":" <> T.unpack column] ""
480+
Ext.Common.execCombineStdOutErr executablePath [ "-g", file <> ":" <> T.unpack row <> ":" <> T.unpack column] ""
481481
)
482482

483483
, detectExecutable "code"
484484
(\executablePath file row column -> do
485-
Ext.Common.c_ executablePath [ "-g", file <> ":" <> T.unpack row <> ":" <> T.unpack column] ""
485+
Ext.Common.execCombineStdOutErr executablePath [ "-g", file <> ":" <> T.unpack row <> ":" <> T.unpack column] ""
486486
)
487487

488488
, detectEditor "intellij-ce"
489489
(Dir.doesDirectoryExist "/Applications/IntelliJ IDEA CE.app")
490490
(\file row column -> do
491491
let column_ :: Int = column & readMaybeText & withDefault 1
492492
-- IntelliJ seems to number it's columns from 1 index
493-
Ext.Common.cq_ "open" ["-na", "IntelliJ IDEA CE.app", "--args", "--line", T.unpack row, "--column", show (column_ - 1), file] ""
493+
Ext.Common.execCombineStdOutErr "open" ["-na", "IntelliJ IDEA CE.app", "--args", "--line", T.unpack row, "--column", show (column_ - 1), file] ""
494494
)
495495

496496
, detectEditor "intellij"
497497
(Dir.doesDirectoryExist "/Applications/IntelliJ IDEA.app")
498498
(\file row column -> do
499499
let column_ :: Int = column & readMaybeText & withDefault 1
500500
-- IntelliJ seems to number it's columns from 1 index
501-
Ext.Common.cq_ "open" ["-na", "IntelliJ IDEA.app", "--args", "--line", T.unpack row, "--column", show (column_ - 1), file] ""
501+
Ext.Common.execCombineStdOutErr "open" ["-na", "IntelliJ IDEA.app", "--args", "--line", T.unpack row, "--column", show (column_ - 1), file] ""
502502
)
503503
]
504504

extra/Lamdera/Injection.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ esbuildIncluder root esbuildPath includesPath = do
386386
Nothing -> do
387387
Lamdera.debug_ "Building elm-pkg-js-includes.js"
388388
-- packaged <- Ext.Common.cq_ esbuildPath [ includesPath, "--bundle", "--global-name=elmPkgJsIncludes" ] ""
389-
packaged <- Ext.Common.cq_ esbuildPath [ includesPath, "--bundle", "--minify", "--global-name=elmPkgJsIncludes" ] ""
389+
(exit, packaged, stdErr) <- Ext.Common.cq_ esbuildPath [ includesPath, "--bundle", "--minify", "--global-name=elmPkgJsIncludes" ] ""
390390
packaged
391391
& Ext.Common.stringToBuilder
392392
-- & debugHaskell "minified elmpkgjs"

0 commit comments

Comments
 (0)