如何仅使用脚本创建自制程序公式
我想将一些shell脚本+支持文件打包到一个自制公式中,该公式将这些脚本安装在用户$PATH
上的某个位置.我将自己动手使用配方奶粉.
I want to package up a few shell scripts + support files into a homebrew formula that installs these scripts somewhere on the user $PATH
. I will serve the formula from my own tap.
通读配方食谱的例子似乎在上游库中假设使用cmake或autotools系统.如果我的项目仅包含一些脚本和配置文件怎么办?我应该手动将其复制到公式中的#{prefix}/
吗?
Reading through the formula cookbook the examples seem to assume cmake or autotools system in the upstream library. What if my project only consists of a few scripts and config files? Should I just manually copy those into #{prefix}/
in the Formula?
这里有两种情况:
使用bin.install
将它们安装在bin
下.您可以选择重命名它们,例如删除扩展名:
Install them under bin
using bin.install
. You can optionally rename them, e.g. to strip the extension:
class MyFormula < Formula
# ...
def install
# move 'myscript.sh' under #{prefix}/bin/
bin.install "myscript.sh"
# OR move 'myscript.sh' to #{prefix}/bin/mybettername
bin.install "myscript.sh" => "mybettername"
# OR move *.sh under bin/
bin.install Dir["*.sh"]
end
end
带有支持文件的脚本
这种情况很棘手,因为您需要正确处理所有路径.最简单的方法是将所有内容安装在#{libexec}/
下,然后在#{bin}/
下编写exec
脚本.这是Homebrew公式中非常常见的模式.
Scripts with Support Files
This case is tricky because you need to get all the paths right. The simplest way is to install everything under #{libexec}/
then write exec
scripts under #{bin}/
. That’s a very common pattern in Homebrew formulae.
class MyFormula < Formula
# ...
def install
# Move everything under #{libexec}/
libexec.install Dir["*"]
# Then write executables under #{bin}/
bin.write_exec_script (libexec/"myscript.sh")
end
end
给出一个包含以下内容的tarball(或git repo):
Given a tarball (or a git repo) that contains the following content:
-
script.sh
-
supportfile.txt
script.sh
supportfile.txt
上面的公式将创建以下层次结构:
The above formula will create the following hierarchy:
#{prefix}/
libexec/
script.sh
supportfile.txt
bin/
script.sh
Homebrew使用以下内容创建#{prefix}/bin/script.sh
:
Homebrew creates that #{prefix}/bin/script.sh
with the following content:
#!/bin/bash
exec "#{libexec}/script.sh" "$@"
这意味着您的脚本可以期望在其自己的目录中具有一个支持文件,而不会污染bin/
,并且无需对安装路径进行任何假设(例如,您无需使用诸如../libexec/supportfile.txt
在您的脚本中).
This means that your script can expect to have a support file in its own directory while not polluting bin/
and not making any assumption regarding the install path (e.g. you don’t need to use things like ../libexec/supportfile.txt
in your script).
有关使用Ruby脚本和我的答案 .com/questions/45978097/creating-a-homebrew-formula-for-standalone-application/45984549#45984549>以那个为例,其中包含联机帮助页.
See this answer of mine for an example with a Ruby script and that one for an example with manpages.
请注意,Homebrew还具有其他辅助功能,例如不仅编写exec
脚本,而且编写设置环境变量或执行一个.jar
.
Note Homebrew also have other helpers to e.g. not only write an exec
script but also set environment variables or execute a .jar
.