寻找独立的命令行代码生成器脚本
我正在寻找一个库或命令行脚本,它允许我创建可以从命令行生成的自定义模板。 ruby on Rails 脚手架生成器与我想要做的几乎相同。我什至更喜欢用 Ruby 编写它(但它不能需要 Rails,因为我可能不会在 Ruby 应用程序上使用它)。什么样的脚本已经可用?
I'm looking for a library or command line script that will allow me to create custom templates that I can generate from the command line. The ruby on rails scaffolding generator is almost identical to what I am trying to do. I would even prefer that it be written in Ruby (yet it cannot require Rails because I may not be using it on a Ruby application). What sorts of scripts like this are already available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:https://gomplate.hairyhenderson.ca
丰富的函数集和一个独立的二进制文件(用 Go 编写)
Try this one: https://gomplate.hairyhenderson.ca
Rich set of functions and just a stanalone binary (written in Go)
我也一直在寻找类似的东西 - 还没有找到我所希望的。我使用的两种方法都是可以接受的。但我还是希望能找到真正的东西。
显而易见,但
sed
适用于简单用例适用于中等复杂度用例(如果可以的话)假设机器上存在某个版本的 Python,标准库中的
string.Template
运行良好。您可以编写一个使用此函数的小型 Python 脚本,并且由于它是 Python,因此通常由模板引擎提供的测试/循环等可以很容易地在 Python 代码中处理。我刚刚发现了 Mustache(请参阅 http://mustache.github.io/)。看起来像是一个可靠的、专门构建的解决方案。从其网站来看,Mustache 在 Ruby、JavaScript、Python、Erlang、PHP、Perl、Objective-C、Java、.NET、Android、C++、Go、Lua、ActionScript、ColdFusion、Scala、Clojure、Fantom、CoffeeScript、 D、node.js。如果这些选择适合您的环境,您可以轻松编写或编译 Mustache 支持以使其成为命令行实用程序。
2013 年 10 月 15 日更新
现在我已经使用 Mustache 一段时间了——它是一个很棒的工具,简单但功能强大。
I've also been on the lookout for something like this -- haven't found what I hoped for. The two approaches I've used instead have been acceptable. But I'm still hoping to find the real thing.
obvious, but
sed
for simple use casesfor medium-complexity use cases, if you can assume some version of Python is present on the machine,
string.Template
in the standard library works well. You could write a small Python script that uses this function, and since it is Python, tests / looping etc. that might normally be provided by the template engine could pretty easily be handled in the Python code instead.I've just discovered Mustache (see http://mustache.github.io/). Seems like a solid, purpose-built solution. From its web site, Mustache has implementations in Ruby, JavaScript, Python, Erlang, PHP, Perl, Objective-C, Java, .NET, Android, C++, Go, Lua, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, and node.js. If those choices suit your environment, you could script or compile Mustache support to be a command line utility pretty easily.
UPDATE 15-OCT-2013
Now that I've used Mustache for a while -- it's a great tool, simple but powerful.
对于简单的用例,您可以使用
gettext
中的envsubst
GNU 软件包。基本上,它从
stdin
读取任何(文本)文件,替换环境中所有出现的$VARIABLE
或${VARIABLE}
并写入结果到标准输出
。多一点,少一点。完整文档位于此处。
优点:
缺点:
bash
- 如“参数扩展”。对于更高级的用例,我推荐
j2cli
, “独立”Jinja2 模板引擎,您可以从中安装pip
就像 Python 中一样。代码可以在此处找到(虽然它看起来有点陈旧......)并且文档也在那里。优点:
缺点:
For simple use cases you can use
envsubst
fom thegettext
GNU package.Basically, it reads any (text) file from
stdin
, replaces all occurrences of$VARIABLE
or${VARIABLE}
from environment and writes the result tostdout
. Little more, nothing less.Full documentation is here.
Pros:
Cons:
bash
-like "Parameter Expansion"For more advanced use cases I recommend
j2cli
, a "standalone" Jinja2 templating engine you can install frompip
as it is in Python. Code can be found here (though it looks like a little bit stale...) and documentation is there too.Pros:
Cons:
您可以使用 ESH – 一个基于 shell 的简单模板引擎。它由 POSIX shell 和 awk 编写,大约 290 LoC。
项目自述文件中的示例:
You can use ESH – a simple templating engine based on shell. It’s written ~290 LoC of POSIX shell and awk.
Example from the project’s readme:
实际上,Ruby on Rails 生成器基于一个名为 Thor 的库。它对于创建自定义 CLI 工具以基于模板生成代码非常有用。
Actually the Ruby on Rails generators are based on a library called Thor. It's very useful for creating custom CLI tools for generating code based on templates.