Skip to content
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

Make cabal init create Main.hs if it doesn't exist. #2483

Merged
merged 2 commits into from
Apr 28, 2015

Conversation

cdepillabout
Copy link
Contributor

cabal init will create Main.hs if the following conditions hold:

  • creating an executable (not a library)
  • the mainIs flag has been specified
  • the file the mainIs flag is pointing to doesn't exist

This implements issue #2304.

Now, after doing cabal init, you can immediately do cabal install, cabal run, etc.

`cabal init` will create Main.hs if the following conditions hold:
	- creating an executable (not a library)
	- the mainIs flag has been specified
	- the file the mainIs flag is pointing to doesn't exist
@23Skidoo
Copy link
Member

/cc @byorgey

[ "module Main where"
, ""
, "main :: IO ()"
, "main = putStrLn \"Hello, World!\""
Copy link
Member

Choose a reason for hiding this comment

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

This is our chance for some branding! What funny/witty/clever thing can we have the default Main.hs print out besides "Hello, World!"? (note: I am just debating the color of the bikeshed, this should not be taken too seriously). "Hello, Cabal!"? "Hello, Haskell!"? "Launching missiles..."? ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This sounds great! I'd appreciate some bikeshedding on what it should say. (I don't particularly have any good ideas on what it should say!)

Copy link
Member

Choose a reason for hiding this comment

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

I think "Hello, Haskell!" would be a non-controversial choice =)

@byorgey
Copy link
Member

byorgey commented Mar 24, 2015

I tried this out but it doesn't seem to work. If I run cabal init in an empty directory and select all the defaults other than specifying the projects builds an executable, it does not generate a Main.hs file (although it is listed in the main-is field in the generated .cabal file).

@cdepillabout
Copy link
Contributor Author

If I run cabal init in an empty directory and select all the defaults other than specifying the projects builds an executable, it does not generate a Main.hs

@byorgey, I think you're saying that the Main.hs does not get generated when you are building a library?

I was under the assumption that libraries would not have a Main.hs. In the createMainHs function in my pull request, I'm specifically checking to make sure that the packageType is set to Flag Executable before creating a Main.hs.

Should I remove this check and also create a Main.hs for libraries as well?

@byorgey
Copy link
Member

byorgey commented Apr 7, 2015

@cdepillabout , no, that is not what I was saying. I agree libraries should not have a Main.hs. I was saying that Main.hs does not get generated for executables either.

@cdepillabout
Copy link
Contributor Author

@byorgey, I understand what you're saying. But that's really strange.

When I run cabal init and select all the default choices, it generates a Main.hs for me. Here is an example of using it:

illabout@my-machine ~ $ mkdir testtest
illabout@my-machine ~ $ cd testtest/
illabout@my-machine ~/testtest $ ls
illabout@my-machine ~/testtest $ ~/git/cabal/cabal-install/.cabal-sandbox/bin/cabal init
Package name? [default: testtest]
Package version? [default: 0.1.0.0]
Please choose a license:
   1) GPL-2
   2) GPL-3
   3) LGPL-2.1
   4) LGPL-3
   5) AGPL-3
   6) BSD2
 * 7) BSD3
   8) MIT
   9) ISC
  10) MPL-2.0
  11) Apache-2.0
  12) PublicDomain
  13) AllRightsReserved
  14) Other (specify)
Your choice? [default: BSD3]
Author name?
Maintainer email?
Project homepage URL?
Project synopsis?
Project category:
 * 1) (none)
   2) Codec
   3) Concurrency
   4) Control
   5) Data
   6) Database
   7) Development
   8) Distribution
   9) Game
  10) Graphics
  11) Language
  12) Math
  13) Network
  14) Sound
  15) System
  16) Testing
  17) Text
  18) Web
  19) Other (specify)
Your choice? [default: (none)]   
What does the package build:
   1) Library
   2) Executable
Your choice? 2
What is the main module of the executable:
 * 1) Main.hs (does not yet exist, but will be created)
   2) Main.lhs (does not yet exist, but will be created)
   3) Other (specify)
Your choice? [default: Main.hs (does not yet exist, but will be created)]
What base language is the package written in:
 * 1) Haskell2010
   2) Haskell98
   3) Other (specify)
Your choice? [default: Haskell2010]
Include documentation on what each field means (y/n)? [default: n]
Source directory:
 * 1) (none)
   2) src
   3) Other (specify)
Your choice? [default: (none)]   

Guessing dependencies...

Generating LICENSE...
Generating Setup.hs...
Generating ChangeLog.md...
Generating Main.hs...
Generating testtest.cabal...

Warning: no synopsis given. You should edit the .cabal file and add one.
You may want to edit the .cabal file and add a Description field.
illabout@my-machine ~/testtest $ ls
ChangeLog.md  LICENSE  Main.hs  Setup.hs  testtest.cabal
illabout@my-machine ~/testtest $ cat Main.hs
module Main where

main :: IO ()
main = putStrLn "Hello, World!"  
illabout@my-machine ~/testtest $ ~/git/cabal/cabal-install/.cabal-sandbox/bin/cabal run
Package has never been configured. Configuring with default flags. If this
fails, please run configure manually.
Warning: The package list for 'hackage.haskell.org' is 32 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Configuring testtest-0.1.0.0...  
Preprocessing executable 'testtest' for testtest-0.1.0.0...
[1 of 1] Compiling Main             ( Main.hs, dist/build/testtest/testtest-tmp/Main.o )
Linking dist/build/testtest/testtest ...
Running testtest...
Hello, World!

Basically, I create an empty directory, run cabal init, and select all the default choices. It creates a Main.hs.

I'm wondering what's going on when you're running cabal and why it's not going through the same code paths?

Looking at my pull request, here are three possible problems I could think of:

  1. I am calling createMainHs from the initCabal function (https://github.com/haskell/cabal/pull/2483/files#diff-a027224d2d3cdb371109d65d698e70bcR114). The createMainHs function will only create the Main.hs file if the packageType flag is set to Executable flag is set and the mainIs flag is set. Could it be that in your case, the mainIs flag is not set for some reason? I was never able to run cabal init interactively and not have the mainIs flag be set.
  2. In my writeMainHs function, I first check to see if the Main.hs file already exists using the doesFileExist function. It's probably unlikely, but maybe this function is returning True even though Main.hs doesn't exist.
  3. Also in my writeMainHs function, I'm writing the contents of the Main.hs file using writeFileSafe. Maybe writeFileSafe is doing something weird? However, looking at the writeFileSafe function, this doesn't seem likely:
-- | Write a file \"safely\", backing up any existing version (unless
--   the overwrite flag is set).
writeFileSafe :: InitFlags -> FilePath -> String -> IO ()
writeFileSafe flags fileName content = do
  moveExistingFile flags fileName
  writeFile fileName content

I'm not sure what else could be the reason that it's working on my system and not on yours. Do you have any idea of things I could take a look at?

@cdepillabout
Copy link
Contributor Author

I changed the Main.hs to print out "Hello, Haskell!" instead of "Hello, World!".

@byorgey
Copy link
Member

byorgey commented Apr 9, 2015

Hmm, I'm not sure. I will try to take another careful look at it soon. It could also be that I was just doing something wrong.

@cdepillabout
Copy link
Contributor Author

Okay, thanks. I really appreciate it.

@freizl
Copy link
Contributor

freizl commented Apr 20, 2015

+1
Works well at ubuntu with option Executable

3.13.0-49-generic #83-Ubuntu SMP Fri Apr 10 20:11:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

@byorgey
Copy link
Member

byorgey commented Apr 28, 2015

Just got around to testing this again, and it works great. Sorry, I don't know what I was doing wrong before!

byorgey added a commit that referenced this pull request Apr 28, 2015
Make `cabal init` create Main.hs if it doesn't exist.
@byorgey byorgey merged commit a3aa3db into haskell:master Apr 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants