From d87499deb5fa559d8d33b36c682312e9b2fe0afd Mon Sep 17 00:00:00 2001 From: Luigy Leon Date: Thu, 18 Feb 2016 17:57:31 -0500 Subject: [PATCH] [yesod-bin] improve stack detection for 'stack keter' The following will now use stack: * `stack query` succeeds from current directory instead of searching that a `stack.yaml` exists * `STACK_YAML` or `STACK_EXE`(set by `stack exec`) environment variables are set --- yesod-bin/Keter.hs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/yesod-bin/Keter.hs b/yesod-bin/Keter.hs index 3cf021e1..12c1a5c9 100644 --- a/yesod-bin/Keter.hs +++ b/yesod-bin/Keter.hs @@ -6,11 +6,12 @@ module Keter import Data.Yaml import qualified Data.HashMap.Strict as Map import qualified Data.Text as T +import System.Environment (getEnvironment) import System.Exit import System.Process import Control.Monad import System.Directory hiding (findFiles) -import Data.Maybe (mapMaybe) +import Data.Maybe (mapMaybe,isJust,maybeToList) import Data.Monoid import System.FilePath (()) import qualified Codec.Archive.Tar as Tar @@ -48,6 +49,8 @@ keter cabal noBuild noCopyTo buildArgs = do _ -> return value Just _ -> error $ ketercfg ++ " is not an object" + env' <- getEnvironment + cwd' <- getCurrentDirectory files <- getDirectoryContents "." project <- case mapMaybe (T.stripSuffix ".cabal" . T.pack) files of @@ -74,11 +77,22 @@ keter cabal noBuild noCopyTo buildArgs = do collapse' (x:xs) = x : collapse' xs collapse' [] = [] - unless noBuild $ if elem "stack.yaml" files - then do run "stack" ["clean"] - createDirectoryIfMissing True "./dist/bin" + stackQueryRunSuccess <- do + (ec,_,_) <- readProcessWithExitCode "stack" ["query"] "" + return (ec == ExitSuccess) + + let inStackExec = isJust $ lookup "STACK_EXE" env' + mStackYaml = lookup "STACK_YAML" env' + useStack = inStackExec || isJust mStackYaml || stackQueryRunSuccess + + unless noBuild $ if useStack + then do let stackYaml = maybeToList $ fmap ("--stack-yaml="<>) mStackYaml + localBinPath = cwd' "dist/bin" + run "stack" $ stackYaml <> ["clean"] + createDirectoryIfMissing True localBinPath run "stack" - ((words "--local-bin-path ./dist/bin build --copy-bins") + (stackYaml + <> ["--local-bin-path",localBinPath,"build","--copy-bins"] <> buildArgs) else do run cabal ["clean"] run cabal ["configure"]