Javran / xmonad-entryhelper

xmonad-entryhelper makes your compiled XMonad config a standalone binary.
MIT License
4 stars 1 forks source link

Suggestion for withLock usage example #1

Open sgf-dma opened 9 years ago

sgf-dma commented 9 years ago

Hi.

Thanks for good module.

And i want to suggest not to use ExitSuccess in the example of withLock you provided in Readme: with default 'mod+q' action xmonad executes xmonad --recompile && xmonad --restart through shell. Thus even if withLock prevents compile from running second time, xmonad will still restart as many times as you press 'mod+q'. It may be harmless, but it is useless either. So i suggest to throw exception instead, then exit code of xmonad --recompile will be non-zero and xmonad --restart won't be executed by shell. Something like this (i use cabal build):

{-# LANGUAGE DeriveDataTypeable #-}

import Control.Exception
import System.FilePath  ((</>))
import System.Directory (getHomeDirectory, copyFile)
import System.Exit

import XMonad
import qualified XMonad.Util.EntryHelper as EH

-- Redefine withHelper to use cabal for building xmonad and throw an
-- exception, if lock file exists. Then '--recompile' will exit with non-zero
-- code and '--restart' won't run (with default 'mod+q' action). The exception
-- will be evaluated, because it's thrown in compile and postCompile will run
-- afterwards.
withHelper :: IO () -> IO ()
withHelper e        = EH.withCustomHelper EH.defaultConfig
            { EH.run = e
            , EH.compile = EH.withLock (throw lockAlreadyExists)
                     . cabalCompile
            , EH.postCompile = cabalInstall
            }

cabalCompile :: Bool -> IO ExitCode
cabalCompile force  = do
let buildCmd = "cabal configure; cabal build"
    cmd      = (if force then "cabal clean; " else "") ++ buildCmd
uninstallSignalHandlers
r <- EH.compileUsingShell cmd
installSignalHandlers
return r

cabalInstall :: ExitCode -> IO ()
cabalInstall ExitSuccess    = do
hd <- getHomeDirectory
xd <- getXMonadDir
copyFile (xd </> "dist/build/xmonad/xmonad") (hd </> "bin/xmonad")
cabalInstall r              = EH.defaultPostCompile r

data LockAlreadyExists  = LockAlreadyExists FilePath
  deriving (Typeable)
-- Default value.
lockAlreadyExists :: LockAlreadyExists
lockAlreadyExists   = LockAlreadyExists ""
instance Show LockAlreadyExists where
show (LockAlreadyExists f)
  | null f      = "Lock file already exists."
  | otherwise   = "Lock file " ++ f ++ " already exists."
instance Exception LockAlreadyExists where
Javran commented 9 years ago

Sorry for my late response and thanks for your suggestion, that does make sense!

I'll take care of it during weekends, but meanwhile pull requests are happily welcomed :)