简体   繁体   中英

How can I update the automatically generated entries of the .cabal and stack.yaml files automatically?

A good part of stack.yaml and project.cabal are generated automatically by cabal init and stack init , respectively.

When I edit the source of the project, some of these entries may need to change drastically.

In particular, the dependencies in stack.yaml and project.cabal and the project.cabal entries other-extensions and exposed-modules should be updated automatically.

How do I do this automatically without clobber?

First of all, the command you are looking for is not stack init , but stack new . Generating stack.yaml should not be your goal, but instead the focus should be on generating project.cabal or package.yaml (if you like hpack ). Reason for this is that the cabal file specificities the actual dependencies as well as all other boilerplate, while stack init uses that info from the cabal file in order to figure out a possible resolver and you can't automate this process with just template interpolation alone. At the same time generating project.cabal and all other files in a new project has a very nice solution in stack .

You can create a repository like this one in your github account (name is important): stack-templates that contains .hsfiles that describe which files need to be generated and how. Then you can use it directly with stack , for example for the above repo I can do:

$ stack new my-new-project github:lehins/lehins-ghc-7

There are various options to the new command, one of which is supplying custom variables, which can be very handy for what you are trying to achieve.

Again, the caveat is that stack.yaml is the only file that is generated by stack without using the template, but that is intentional and it's easy to adjust it later.

Edit

@leftaroundabout pointed out that the question was about updating and not generating the files. The only way I would ever recommend doing such operation is in a declarative way of re-generating file from scratch instead of an imperative approach of mutating the existing file. Not sure about cabal init , but with stack it is just as easy as running stack new just one more time:

$ cd my-new -project
$ stack new --bare --force -p var:newval my-new-project github:lehins/lehins-ghc-7

Trick is you either pass new values for existing variables in the template or modify the template itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM