diff --git a/yesod-bin/AddHandler.hs b/yesod-bin/AddHandler.hs index 9c59c59b..acd9ba90 100644 --- a/yesod-bin/AddHandler.hs +++ b/yesod-bin/AddHandler.hs @@ -1,13 +1,13 @@ +{-# LANGUAGE PatternGuards #-} module AddHandler (addHandler) where import Prelude hiding (readFile) import System.IO (hFlush, stdout) import Data.Char (isLower, toLower, isSpace) -import Data.List (isPrefixOf, isSuffixOf) +import Data.List (isPrefixOf, isSuffixOf, stripPrefix) import qualified Data.Text as T import qualified Data.Text.IO as TIO import System.Directory (getDirectoryContents, doesFileExist) -import Control.Monad (when) -- strict readFile readFile :: FilePath -> IO String @@ -62,18 +62,18 @@ fixApp :: String -> String -> String fixApp name = unlines . reverse . go . reverse . lines where - l = "import Handler." ++ name + l spaces = "import " ++ spaces ++ "Handler." ++ name - go [] = [l] + go [] = [l ""] go (x:xs) - | "import Handler." `isPrefixOf` x = l : x : xs + | Just y <- stripPrefix "import " x, "Handler." `isPrefixOf` dropWhile (== ' ') y = l (takeWhile (== ' ') y) : x : xs | otherwise = x : go xs fixCabal :: String -> String -> String fixCabal name = unlines . reverse . go . reverse . lines where - l = "import Handler." ++ name + l = " Handler." ++ name go [] = [l] go (x:xs)