Skip to content
Snippets Groups Projects
Commit 5073063d authored by BasLaa's avatar BasLaa Committed by Mergify
Browse files

Return empty default when git fails

Add maybe in guess functions

Adjust type in NonInteractive

Change unit tests

Fix whitespace

Abstract guessing and remove comments

Simplify guess functions

Return default for cabal init author and name when git fails
parent 0bedc4ac
No related branches found
No related tags found
No related merge requests found
...@@ -355,14 +355,10 @@ licensePrompt flags = getLicense flags $ do ...@@ -355,14 +355,10 @@ licensePrompt flags = getLicense flags $ do
else fmap prettyShow knownLicenses else fmap prettyShow knownLicenses
authorPrompt :: Interactive m => InitFlags -> m String authorPrompt :: Interactive m => InitFlags -> m String
authorPrompt flags = getAuthor flags $ do authorPrompt flags = getAuthor flags $ guessAuthorName >>= promptOrDefault "Author name"
name <- guessAuthorName
promptStr "Author name" (DefaultPrompt name)
emailPrompt :: Interactive m => InitFlags -> m String emailPrompt :: Interactive m => InitFlags -> m String
emailPrompt flags = getEmail flags $ do emailPrompt flags = getEmail flags $ guessAuthorEmail >>= promptOrDefault "Maintainer email"
email' <- guessAuthorEmail
promptStr "Maintainer email" (DefaultPrompt email')
homepagePrompt :: Interactive m => InitFlags -> m String homepagePrompt :: Interactive m => InitFlags -> m String
homepagePrompt flags = getHomepage flags $ homepagePrompt flags = getHomepage flags $
...@@ -467,3 +463,6 @@ srcDirsPrompt flags = getSrcDirs flags $ do ...@@ -467,3 +463,6 @@ srcDirsPrompt flags = getSrcDirs flags $ do
True True
return [dir] return [dir]
promptOrDefault :: Interactive m => String -> Maybe String -> m String
promptOrDefault s = maybe (promptStr s MandatoryPrompt) (promptStr s . DefaultPrompt)
...@@ -274,14 +274,16 @@ licenseHeuristics :: Interactive m => InitFlags -> m SpecLicense ...@@ -274,14 +274,16 @@ licenseHeuristics :: Interactive m => InitFlags -> m SpecLicense
licenseHeuristics flags = getLicense flags $ guessLicense flags licenseHeuristics flags = getLicense flags $ guessLicense flags
-- | The author's name. Prompt, or try to guess from an existing -- | The author's name. Prompt, or try to guess from an existing
-- darcs repo. -- git repo.
authorHeuristics :: Interactive m => InitFlags -> m String authorHeuristics :: Interactive m => InitFlags -> m String
authorHeuristics flags = getAuthor flags guessAuthorEmail authorHeuristics flags = guessAuthorEmail >>=
maybe (getAuthor flags $ return "") (getAuthor flags . return)
-- | The author's email. Prompt, or try to guess from an existing -- | The author's email. Prompt, or try to guess from an existing
-- darcs repo. -- git repo.
emailHeuristics :: Interactive m => InitFlags -> m String emailHeuristics :: Interactive m => InitFlags -> m String
emailHeuristics flags = getEmail flags guessAuthorName emailHeuristics flags = guessAuthorName >>=
maybe (getEmail flags $ return "") (getEmail flags . return)
-- | Prompt for a homepage URL for the package. -- | Prompt for a homepage URL for the package.
homepageHeuristics :: Interactive m => InitFlags -> m String homepageHeuristics :: Interactive m => InitFlags -> m String
......
...@@ -151,18 +151,23 @@ guessSourceDirectories flags = do ...@@ -151,18 +151,23 @@ guessSourceDirectories flags = do
True -> ["src"] True -> ["src"]
-- | Guess author and email using git configuration options. -- | Guess author and email using git configuration options.
guessAuthorName :: Interactive m => m String guessAuthorName :: Interactive m => m (Maybe String)
guessAuthorName = guessGitInfo "user.name" guessAuthorName = guessGitInfo "user.name"
guessAuthorEmail :: Interactive m => m String guessAuthorEmail :: Interactive m => m (Maybe String)
guessAuthorEmail = guessGitInfo "user.email" guessAuthorEmail = guessGitInfo "user.email"
guessGitInfo :: Interactive m => String -> m String guessGitInfo :: Interactive m => String -> m (Maybe String)
guessGitInfo target = do guessGitInfo target = do
info <- readProcessWithExitCode "git" ["config", "--local", target] "" localInfo <- readProcessWithExitCode "git" ["config", "--local", target] ""
if null $ snd' info if null $ snd' localInfo
then trim . snd' <$> readProcessWithExitCode "git" ["config", "--global", target] "" then do
else return . trim $ snd' info globalInfo <- readProcessWithExitCode "git" ["config", "--global", target] ""
case fst' globalInfo of
ExitSuccess -> return $ Just (trim $ snd' globalInfo)
_ -> return Nothing
else return $ Just (trim $ snd' localInfo)
where where
fst' (x, _, _) = x
snd' (_, x, _) = x snd' (_, x, _) = x
...@@ -44,7 +44,9 @@ tests _v _initFlags comp pkgIx srcDb = ...@@ -44,7 +44,9 @@ tests _v _initFlags comp pkgIx srcDb =
} }
inputs = inputs =
-- createProject stuff -- createProject stuff
[ "True" [ "Foobar"
, "foobar@qux.com"
, "True"
, "[\"quxTest/Main.hs\"]" , "[\"quxTest/Main.hs\"]"
-- writeProject stuff -- writeProject stuff
-- writeLicense -- writeLicense
......
...@@ -73,7 +73,9 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject" ...@@ -73,7 +73,9 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
, dependencies = Flag [] , dependencies = Flag []
} }
inputs = NEL.fromList inputs = NEL.fromList
[ "True" ["Foobar"
, "foobar@qux.com"
, "True"
, "[\"quxTest/Main.hs\"]" , "[\"quxTest/Main.hs\"]"
] ]
...@@ -149,8 +151,11 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject" ...@@ -149,8 +151,11 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
, dependencies = Flag [] , dependencies = Flag []
} }
inputs = NEL.fromList inputs = NEL.fromList
[ "Foobar"
, "foobar@qux.com"
-- extra sources -- extra sources
[ "[\"CHANGELOG.md\"]" , "[\"CHANGELOG.md\"]"
-- lib other modules -- lib other modules
, "False" , "False"
-- exe other modules -- exe other modules
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment