Skip to content

Generate valid Main.lhs files with cabal init. #5810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor Author

@hrhino hrhino Dec 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"lhs" isExtensionOf mainPath

edit: nvm, we need a newer filepath for that.

_ -> 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 <[email protected]> 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