From 97f3213bb6b5e2839efac43883b25b41fe06ed6c Mon Sep 17 00:00:00 2001 From: Harrison Houghton Date: Thu, 20 Dec 2018 15:21:54 -0500 Subject: [PATCH 1/3] Generate valid Main.lhs files with `cabal init`. Fixes haskell/cabal#5577. --- cabal-install/Distribution/Client/Init.hs | 19 ++++++++++++++----- cabal-install/changelog | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cabal-install/Distribution/Client/Init.hs b/cabal-install/Distribution/Client/Init.hs index 8d43e32b50d..2677e7076f9 100644 --- a/cabal-install/Distribution/Client/Init.hs +++ b/cabal-install/Distribution/Client/Init.hs @@ -32,7 +32,7 @@ import System.Directory ( getCurrentDirectory, doesDirectoryExist, doesFileExist, copyFile , getDirectoryContents, createDirectoryIfMissing ) import System.FilePath - ( (), (<.>), takeBaseName, equalFilePath ) + ( (), (<.>), takeBaseName, takeExtension, equalFilePath ) import Data.Time ( getCurrentTime, utcToLocalTime, toGregorian, localDay, getCurrentTimeZone ) @@ -831,7 +831,7 @@ writeMainHs flags mainPath = do exists <- doesFileExist mainFullPath unless exists $ do message flags $ "Generating " ++ mainPath ++ "..." - writeFileSafe flags mainFullPath mainHs + writeFileSafe flags mainFullPath (mainHs flags) -- | Check that a main file exists. hasMainHs :: InitFlags -> Bool @@ -840,14 +840,23 @@ hasMainHs flags = case mainIs flags of || packageType flags == Flag LibraryAndExecutable) _ -> False --- | Default Main.hs file. Used when no Main.hs exists. -mainHs :: String -mainHs = unlines +-- | Default Main.(l)hs file. Used when no Main.(l)hs exists. +mainHs :: InitFlags -> String +mainHs flags = (unlines . fmap prependPrefix) [ "module Main where" , "" , "main :: IO ()" , "main = putStrLn \"Hello, Haskell!\"" ] + where + prependPrefix "" = "" + prependPrefix line + | isLiterate = "> " ++ line + | otherwise = line + isLiterate = case mainIs flags of + Flag mainPath -> takeExtension mainPath == ".lhs" + _ -> False + -- | Move an existing file, if there is one, and the overwrite flag is -- not set. diff --git a/cabal-install/changelog b/cabal-install/changelog index f7041182d2b..1400351b58e 100644 --- a/cabal-install/changelog +++ b/cabal-install/changelog @@ -14,6 +14,7 @@ error message, but with an increase in run time. (#5647) * v2-test now succeeds when there are no test suites. (#5435) * Add '--lib', '--exe', and '--libandexe' shorthands to init. (#5759) + * init now generates valid `Main.lhs` files. (#5577) 2.4.1.0 Mikhail Glushenkov November 2018 * Add message to alert user to potential package casing errors. (#5635) From b8e87b569b832dcb1ba2809564bbf141b5563a02 Mon Sep 17 00:00:00 2001 From: Mikhail Glushenkov Date: Tue, 15 Jan 2019 22:30:09 +0000 Subject: [PATCH 2/3] Whitespace. --- cabal-install/Distribution/Client/Init.hs | 2 +- cabal-install/changelog | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cabal-install/Distribution/Client/Init.hs b/cabal-install/Distribution/Client/Init.hs index 2677e7076f9..8eff35d42ab 100644 --- a/cabal-install/Distribution/Client/Init.hs +++ b/cabal-install/Distribution/Client/Init.hs @@ -856,7 +856,7 @@ mainHs flags = (unlines . fmap prependPrefix) isLiterate = case mainIs flags of Flag mainPath -> takeExtension mainPath == ".lhs" _ -> False - + -- | Move an existing file, if there is one, and the overwrite flag is -- not set. diff --git a/cabal-install/changelog b/cabal-install/changelog index 1400351b58e..df1d7db43e0 100644 --- a/cabal-install/changelog +++ b/cabal-install/changelog @@ -27,9 +27,9 @@ * Register monolithic packages installed into the store due to a build-tool dependency if they also happen to contain a buildable public lib. (#5379,#5604) - * Fixed a Windows bug where cabal-install tried to copy files - after moving them (#5631). - * 'cabal v2-repl' now works for indefinite (in the Backpack sense) components. (#5619) + * Fixed a Windows bug where cabal-install tried to copy files + after moving them (#5631). + * 'cabal v2-repl' now works for indefinite (in the Backpack sense) components. (#5619) * Set data dir environment variable for tarballs and remote repos (#5469) * Fix monolithic inplace build tool PATH (#5633) * 'cabal init' now supports '-w'/'--with-compiler' flag (#4936, #5654) From 5540cc2c7f5a37402eb1672ee33ef651a2eff0e7 Mon Sep 17 00:00:00 2001 From: Mikhail Glushenkov Date: Tue, 15 Jan 2019 22:32:33 +0000 Subject: [PATCH 3/3] s/fmap/map/ --- cabal-install/Distribution/Client/Init.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cabal-install/Distribution/Client/Init.hs b/cabal-install/Distribution/Client/Init.hs index 8eff35d42ab..16c82adc1e9 100644 --- a/cabal-install/Distribution/Client/Init.hs +++ b/cabal-install/Distribution/Client/Init.hs @@ -842,7 +842,7 @@ hasMainHs flags = case mainIs flags of -- | Default Main.(l)hs file. Used when no Main.(l)hs exists. mainHs :: InitFlags -> String -mainHs flags = (unlines . fmap prependPrefix) +mainHs flags = (unlines . map prependPrefix) [ "module Main where" , "" , "main :: IO ()"