Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a `stack run` similar to `cabal run`?

Up until recently, I was executing this beauty to build + run a project with stack:

stack build && .stack-work/install/x86_64-linux/lts-4.1/7.10.3/bin/<project-name>

I was told on IRC that this can be simplified to

stack build && stack exec <project-name>

Can this be simplified even more, to

stack run

or at least

stack run <project-name>

?

If I recall correctly this was possible with cabal run.

Edit:

@haoformayor's comment is getting close:

alias b='stack build --fast --ghc-options="-Wall" && stack exec'

Although this still needs the project name, right?

I've also started to get close with

function stack-run () { stack build && stack exec `basename "$PWD"` }

Although this only works if the project name matches with the folder name. Maybe we can query cabal/stack for the first executable entry in the .cabal file? Or Maybe we could do it with sed...

like image 653
Wizek Avatar asked Jan 17 '16 18:01

Wizek


People also ask

What is the difference between stack and cabal?

Stack supports creating projects using templates. It also supports your own custom templates. Stack has built-in hpack support in it. It provides an alternative (IMO, a better) way of writing cabal files using yaml file which is more widely used in the industry.

What is a stack run?

In basketball, a stack is a type of inbounds play where the four offensive players on the court stand close together in a vertical line formation in front of the inbounder. The "stacked" players then simultaneously break out of the line formation by running to different locations on the court.

Where does stack install GHC?

In its default configuration, Stack will simply ignore any system GHC installation and use a sandboxed GHC that it has installed itself. You can find these sandboxed GHC installations in the ghc-* directories in the stack path --programs directory.

Where is Haskell stack installed?

Stack-built files generally go in either the Stack root directory (default: ~/. stack on Unix-like operating systems, or, %LOCALAPPDATA%\Programs\stack on Windows) or ./. stack-work directories local to each project. The Stack root directory holds packages belonging to snapshots and any Stack-installed versions of GHC.


2 Answers

As it's mentioned here http://docs.haskellstack.org/en/stable/README.html#quick-start-guide, you can use stack exec my-project-exe where my-project-exe is the name of the executable in your .cabal file.

like image 97
Govind Krishna Joshi Avatar answered Oct 04 '22 00:10

Govind Krishna Joshi


You can use --exec to tell stack what program should be run after a successful built:

stack build --exec <executable-name>

You can also specify arguments for the executable, e.g.

stack unpack pandoc && cd pandoc*
stack build --exec "pandoc --version"

That's probably the closest you'll get compared to cabal run, since both stack exec and the --exec flag need an executable name. The cleanest variant, however, would be an additional stack-run command, that does stack build --exec <first-executable in .cabal>. It could be worth a feature request on the project's GitHub issue tracker.

like image 24
Zeta Avatar answered Oct 04 '22 00:10

Zeta