简体   繁体   English

如何自动更新 .cabal 和 stack.yaml 文件的自动生成条目?

[英]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. stack.yaml 和 project.cabal 的很大一部分分别由cabal initstack init自动生成。

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.特别是 stack.yaml 和 project.cabal 中的依赖关系以及 project.cabal 条目 other-extensions 和暴露模块应该自动更新。

How do I do this automatically without clobber?如何在没有破坏的情况下自动执行此操作?

First of all, the command you are looking for is not stack init , but stack new .首先,您要查找的命令不是stack init ,而是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 ).生成stack.yaml不应该是您的目标,而是应该将重点放在生成project.cabalpackage.yaml上(如果您喜欢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.原因是 cabal 文件指定了实际依赖项以及所有其他样板文件,而stack init使用来自 cabal 文件的信息来找出可能的解析器,并且您不能仅使用模板插值来自动化此过程. At the same time generating project.cabal and all other files in a new project has a very nice solution in stack .同时在新项目中生成project.cabal和所有其他文件在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.您可以在您的 github 帐户(名称很重要)中创建一个像这样的存储库:包含.hsfiles文件的堆栈模板,其中描述了需要生成哪些文件以及如何生成。 Then you can use it directly with stack , for example for the above repo I can do:然后你可以直接将它与stack一起使用,例如对于上面的 repo,我可以这样做:

$ 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. new命令有多种选项,其中之一是提供自定义变量,这对于您想要实现的目标非常方便。

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.同样,需要注意的是stack.yaml是唯一由stack生成而不使用模板的文件,但这是故意的,以后很容易调整它。

Edit编辑

@leftaroundabout pointed out that the question was about updating and not generating the files. @leftaroundabout 指出问题是关于更新而不是生成文件。 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:不确定cabal init ,但是使用 stack 就像再运行一次stack new一样简单:

$ 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.诀窍是您要么为模板中的现有变量传递新值,要么修改模板本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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