Skip to content

Commit

Permalink
Merge pull request #5810 from hrhino/t5577
Browse files Browse the repository at this point in the history
Generate valid Main.lhs files with `cabal init`.
  • Loading branch information
23Skidoo authored Jan 24, 2019
2 parents 97484d8 + 5540cc2 commit b4661c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
19 changes: 14 additions & 5 deletions cabal-install/Distribution/Client/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

Expand Down Expand Up @@ -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
Expand All @@ -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 . map 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.
Expand Down
7 changes: 4 additions & 3 deletions cabal-install/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mikhail.glushenkov@gmail.com> November 2018
* Add message to alert user to potential package casing errors. (#5635)
Expand All @@ -26,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)
Expand Down

0 comments on commit b4661c4

Please sign in to comment.