Skip to content

Commit b4661c4

Browse files
authored
Merge pull request #5810 from hrhino/t5577
Generate valid Main.lhs files with `cabal init`.
2 parents 97484d8 + 5540cc2 commit b4661c4

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

cabal-install/Distribution/Client/Init.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import System.Directory
3232
( getCurrentDirectory, doesDirectoryExist, doesFileExist, copyFile
3333
, getDirectoryContents, createDirectoryIfMissing )
3434
import System.FilePath
35-
( (</>), (<.>), takeBaseName, equalFilePath )
35+
( (</>), (<.>), takeBaseName, takeExtension, equalFilePath )
3636
import Data.Time
3737
( getCurrentTime, utcToLocalTime, toGregorian, localDay, getCurrentTimeZone )
3838

@@ -831,7 +831,7 @@ writeMainHs flags mainPath = do
831831
exists <- doesFileExist mainFullPath
832832
unless exists $ do
833833
message flags $ "Generating " ++ mainPath ++ "..."
834-
writeFileSafe flags mainFullPath mainHs
834+
writeFileSafe flags mainFullPath (mainHs flags)
835835

836836
-- | Check that a main file exists.
837837
hasMainHs :: InitFlags -> Bool
@@ -840,14 +840,23 @@ hasMainHs flags = case mainIs flags of
840840
|| packageType flags == Flag LibraryAndExecutable)
841841
_ -> False
842842

843-
-- | Default Main.hs file. Used when no Main.hs exists.
844-
mainHs :: String
845-
mainHs = unlines
843+
-- | Default Main.(l)hs file. Used when no Main.(l)hs exists.
844+
mainHs :: InitFlags -> String
845+
mainHs flags = (unlines . map prependPrefix)
846846
[ "module Main where"
847847
, ""
848848
, "main :: IO ()"
849849
, "main = putStrLn \"Hello, Haskell!\""
850850
]
851+
where
852+
prependPrefix "" = ""
853+
prependPrefix line
854+
| isLiterate = "> " ++ line
855+
| otherwise = line
856+
isLiterate = case mainIs flags of
857+
Flag mainPath -> takeExtension mainPath == ".lhs"
858+
_ -> False
859+
851860

852861
-- | Move an existing file, if there is one, and the overwrite flag is
853862
-- not set.

cabal-install/changelog

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
error message, but with an increase in run time. (#5647)
1515
* v2-test now succeeds when there are no test suites. (#5435)
1616
* Add '--lib', '--exe', and '--libandexe' shorthands to init. (#5759)
17+
* init now generates valid `Main.lhs` files. (#5577)
1718

1819
2.4.1.0 Mikhail Glushenkov <[email protected]> November 2018
1920
* Add message to alert user to potential package casing errors. (#5635)
@@ -26,9 +27,9 @@
2627
* Register monolithic packages installed into the store due to a
2728
build-tool dependency if they also happen to contain a buildable
2829
public lib. (#5379,#5604)
29-
* Fixed a Windows bug where cabal-install tried to copy files
30-
after moving them (#5631).
31-
* 'cabal v2-repl' now works for indefinite (in the Backpack sense) components. (#5619)
30+
* Fixed a Windows bug where cabal-install tried to copy files
31+
after moving them (#5631).
32+
* 'cabal v2-repl' now works for indefinite (in the Backpack sense) components. (#5619)
3233
* Set data dir environment variable for tarballs and remote repos (#5469)
3334
* Fix monolithic inplace build tool PATH (#5633)
3435
* 'cabal init' now supports '-w'/'--with-compiler' flag (#4936, #5654)

0 commit comments

Comments
 (0)