Support comments and empty lines (#44)

This commit is contained in:
Juan Paucar 2017-01-28 08:08:37 -05:00 committed by Mark Karpov
parent 64dd271aa5
commit 8c760cff32
3 changed files with 19 additions and 4 deletions

View File

@ -1,4 +1,6 @@
# This is a comment
export PATH=~/.cabal/bin:/usr/local/bin:$PATH export PATH=~/.cabal/bin:/usr/local/bin:$PATH
cabal sandbox delete cabal sandbox delete
cabal sandbox init cabal sandbox init
cabal clean cabal clean

View File

@ -140,6 +140,11 @@ spec = describe "hapistrano" $ do
last (fromRight ltarget) /= '\n' `shouldBe` True last (fromRight ltarget) /= '\n' `shouldBe` True
describe "deploying" $ do describe "deploying" $ do
it "reads a common build script with comments and new lines" $ do
scriptLines <- lines `fmap` IO.readFile "./script/clean-build.sh"
let validBBuildScriptLines = Hap.cleanBuildScript scriptLines
length validBBuildScriptLines `shouldBe` 7
it "a simple deploy" $ it "a simple deploy" $
withSystemTempDirectory "hapistranoDeployTest" $ \tmpDir -> do withSystemTempDirectory "hapistranoDeployTest" $ \tmpDir -> do

View File

@ -8,6 +8,7 @@ module System.Hapistrano
, activateRelease , activateRelease
, currentPath , currentPath
, cleanBuildScript
, defaultSuccessHandler , defaultSuccessHandler
, defaultErrorHandler , defaultErrorHandler
, directoryExists , directoryExists
@ -36,8 +37,8 @@ import Control.Monad.Trans.Either ( left
, right , right
, eitherT ) , eitherT )
import Data.Char (isNumber) import Data.Char (isNumber, isSpace)
import Data.List (intercalate, sortBy, isInfixOf) import Data.List (intercalate, sortBy, isInfixOf, dropWhileEnd)
import Data.Time (getCurrentTime) import Data.Time (getCurrentTime)
import Data.Time.Format (formatTime) import Data.Time.Format (formatTime)
import Data.Time.Locale.Compat (defaultTimeLocale) import Data.Time.Locale.Compat (defaultTimeLocale)
@ -170,7 +171,7 @@ readCurrentLink = do
-- ^ Trims any newlines from the given String -- ^ Trims any newlines from the given String
trim :: String -- ^ String to have trailing newlines stripped trim :: String -- ^ String to have trailing newlines stripped
-> String -- ^ String with trailing newlines removed -> String -- ^ String with trailing newlines removed
trim = reverse . dropWhile (== '\n') . reverse trim = dropWhileEnd isSpace . dropWhile isSpace
-- | Ensure that the initial bare repo exists in the repo directory. Idempotent. -- | Ensure that the initial bare repo exists in the repo directory. Idempotent.
ensureRepositoryPushed :: Hapistrano String ensureRepositoryPushed :: Hapistrano String
@ -330,6 +331,13 @@ restartServerCommand = do
Nothing -> return "No command given for restart action." Nothing -> return "No command given for restart action."
Just cmd -> runCommand (host conf) cmd Just cmd -> runCommand (host conf) cmd
cleanBuildScript :: [String] -> [String]
cleanBuildScript allScriptLines = filter (not . isCommentOrEmpty) allScriptLines
where
isCommentOrEmpty line = isEmpty line || isComment line
isComment line = (head $ trim line) == '#'
isEmpty line = (trim line) == ""
-- | Runs a build script if one is provided. -- | Runs a build script if one is provided.
runBuild :: Release -> Hapistrano Release runBuild :: Release -> Hapistrano Release
runBuild rel = do runBuild rel = do
@ -341,7 +349,7 @@ runBuild rel = do
Just scr -> do Just scr -> do
fl <- liftIO $ readFile scr fl <- liftIO $ readFile scr
buildRelease rel $ lines fl buildRelease rel $ cleanBuildScript $ lines fl
right rel right rel