Skip to content

Instantly share code, notes, and snippets.

@notae
Last active February 24, 2016 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notae/6c83423bfa10bd8d0445 to your computer and use it in GitHub Desktop.
Save notae/6c83423bfa10bd8d0445 to your computer and use it in GitHub Desktop.
Notify results of "stack build --file-watch" on OSX
#!/usr/bin/env stack
-- stack --resolver lts-4.0 --install-ghc runghc
module Main where
import Control.Monad (forM_, when)
import Data.List (isInfixOf)
import GHC.IO.Exception (ExitCode (..))
import System.Process (readProcessWithExitCode)
putlog :: String -> IO ()
putlog s = putStrLn $ "osx_notify: " ++ s
notify :: String -> String -> String -> IO ()
notify title subtitle text = do
(ec, stdout, _) <-
readProcessWithExitCode
"osascript"
["-e", "on run argv\ndisplay notification item 3 of argv as text with title item 1 of argv subtitle item 2 of argv\nend run", "--", title, subtitle, text]
""
if ec == ExitSuccess
then putlog $ "Notification Success"
else putlog $ "Notification Error: " ++ stdout
data Color = White | Red | Green | Blue deriving (Show, Eq)
cname :: Color -> String
cname White = "white"
cname Red = "red"
cname Green = "green"
cname Blue = "blue"
anybar :: Color -> IO ()
anybar c = do
(ec, stdout, _) <-
readProcessWithExitCode
"bash"
["-c", "echo -n " ++ cname c ++ " | nc -4u -w0 localhost 1738"]
""
if ec == ExitSuccess
then putlog $ "Anybar notification Success"
else putlog $ "Anybar notification Error: " ++ stdout
watch :: [String] -> IO ()
watch xs = forM_ xs $ \x -> do
putStrLn x
when ("build (" `isInfixOf` x ||
"unregistering" `isInfixOf` x) $ do
putlog "Start to build ..."
notify "stack" "build" "Build started."
anybar Blue
when (x == "ExitSuccess") $ do
putlog "Success"
notify "stack" "build" "Build succeeded."
anybar Green
when (x == " Process exited with code: ExitFailure 1" ||
"exited with: ExitFailure 1" `isInfixOf` x) $ do
putlog "Failure"
notify "stack" "build" "Build failed."
anybar Red
main :: IO ()
main = do
putlog "Started."
input <- lines <$> getContents
watch input
putlog "Terminated."
@notae
Copy link
Author

notae commented Feb 16, 2016

Usage:

stack build --file-watch 2>&1 | osx_notify.hs

This script depends deeply on the messages from "stack build --file-watch".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment