From 62877cbba043cecc9a58586eb22299a38036f4e7 Mon Sep 17 00:00:00 2001 From: Maximilian Tagher Date: Fri, 6 Jun 2014 12:51:00 -0700 Subject: [PATCH 1/3] In `yesod init`, ensure words have atleast one character; fixes #550 --- yesod-bin/Scaffolding/Scaffolder.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/yesod-bin/Scaffolding/Scaffolder.hs b/yesod-bin/Scaffolding/Scaffolder.hs index daa76c8c..0ccfa02c 100644 --- a/yesod-bin/Scaffolding/Scaffolder.hs +++ b/yesod-bin/Scaffolding/Scaffolder.hs @@ -15,6 +15,8 @@ import Text.ProjectTemplate (unpackTemplate, receiveFS) import System.IO import Text.Shakespeare.Text (renderTextUrl, textFile) import Network.HTTP.Conduit (withManager, http, parseUrl, responseBody) +import Data.List.Split (splitOn) +import Data.Char (isDigit) prompt :: (String -> Maybe a) -> IO a prompt f = do @@ -68,12 +70,14 @@ validPN c validPN '-' = True validPN _ = False + + scaffold :: Bool -- ^ bare directory instead of a new subdirectory? -> IO () scaffold isBare = do puts $ renderTextUrl undefined $(textFile "input/welcome.cg") project <- prompt $ \s -> - if all validPN s && not (null s) && s /= "test" + if all validPN s && not (null s) && s /= "test" && (not $ any (all isDigit) (splitOn "-" s)) then Just s else Nothing let dir = project From 5b5caf2ad4be8030e4d78dbd98e07f7f247482b1 Mon Sep 17 00:00:00 2001 From: Maximilian Tagher Date: Fri, 6 Jun 2014 13:00:52 -0700 Subject: [PATCH 2/3] Break out the check for numeric-only words into a separate function, with comments --- yesod-bin/Scaffolding/Scaffolder.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/yesod-bin/Scaffolding/Scaffolder.hs b/yesod-bin/Scaffolding/Scaffolder.hs index 0ccfa02c..6f6356a8 100644 --- a/yesod-bin/Scaffolding/Scaffolder.hs +++ b/yesod-bin/Scaffolding/Scaffolder.hs @@ -70,14 +70,18 @@ validPN c validPN '-' = True validPN _ = False - +-- | Cabal separates packages with a hyphen into words. A word can't consist of only digits +-- +-- Fixes +wordsHaveOneCharacter :: String -> Bool +wordsHaveOneCharacter s = not $ any (all isDigit) (splitOn "-" s) scaffold :: Bool -- ^ bare directory instead of a new subdirectory? -> IO () scaffold isBare = do puts $ renderTextUrl undefined $(textFile "input/welcome.cg") project <- prompt $ \s -> - if all validPN s && not (null s) && s /= "test" && (not $ any (all isDigit) (splitOn "-" s)) + if all validPN s && not (null s) && s /= "test" && wordsHaveOneCharacter s then Just s else Nothing let dir = project From 7315a464d7d0efe4e841fb09f42c9665e95cde84 Mon Sep 17 00:00:00 2001 From: Maximilian Tagher Date: Sat, 7 Jun 2014 12:27:34 -0700 Subject: [PATCH 3/3] Use Cabal's Distribution.Text/Distribution.Package to validate package names --- yesod-bin/Scaffolding/Scaffolder.hs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/yesod-bin/Scaffolding/Scaffolder.hs b/yesod-bin/Scaffolding/Scaffolder.hs index 6f6356a8..e3a69faa 100644 --- a/yesod-bin/Scaffolding/Scaffolder.hs +++ b/yesod-bin/Scaffolding/Scaffolder.hs @@ -15,8 +15,9 @@ import Text.ProjectTemplate (unpackTemplate, receiveFS) import System.IO import Text.Shakespeare.Text (renderTextUrl, textFile) import Network.HTTP.Conduit (withManager, http, parseUrl, responseBody) -import Data.List.Split (splitOn) -import Data.Char (isDigit) +import Data.Maybe (isJust) +import Distribution.Text (simpleParse) +import Distribution.Package (PackageName) prompt :: (String -> Maybe a) -> IO a prompt f = do @@ -61,27 +62,15 @@ backendBS Mysql = $(embedFile "hsfiles/mysql.hsfiles") backendBS MongoDB = $(embedFile "hsfiles/mongo.hsfiles") backendBS Simple = $(embedFile "hsfiles/simple.hsfiles") --- | Is the character valid for a project name? -validPN :: Char -> Bool -validPN c - | 'A' <= c && c <= 'Z' = True - | 'a' <= c && c <= 'z' = True - | '0' <= c && c <= '9' = True -validPN '-' = True -validPN _ = False - --- | Cabal separates packages with a hyphen into words. A word can't consist of only digits --- --- Fixes -wordsHaveOneCharacter :: String -> Bool -wordsHaveOneCharacter s = not $ any (all isDigit) (splitOn "-" s) +validPackageName :: String -> Bool +validPackageName s = isJust (simpleParse s :: Maybe PackageName) scaffold :: Bool -- ^ bare directory instead of a new subdirectory? -> IO () scaffold isBare = do puts $ renderTextUrl undefined $(textFile "input/welcome.cg") project <- prompt $ \s -> - if all validPN s && not (null s) && s /= "test" && wordsHaveOneCharacter s + if validPackageName s && s /= "test" then Just s else Nothing let dir = project