porras/duct

语言: Ruby

git: https://github.com/porras/duct

Duct允许您在单个文件脚本中嵌入Gemfile
Duct allows you to embed a Gemfile in a single file script
README.md (中文)

Duct Tape, By Evan-Amos, via Wikimedia Commons

管道是捆绑器周围的小包装。它允许您在脚本中嵌入Gemfile。

用例

有时你会编写小脚本来完成不同的任务。有时你会存储它们以备将来使用。有时那些 脚本使用一些宝石来完成他们的任务(数据库访问,Web请求,等等)。所以,过了一段时间,你重复使用它们 并且你不知道你使用了哪个版本的宝石而且你可能遇到麻烦(比如完全成熟的应用程序) 预捆绑日)。当你做明显的解决方法时:创建一个目录来存储脚本和一个 Gemfile和Gemfile.lock。

这是一个很好的解决方案,但感觉有点矫枉过正。 Duct允许您将Gemfile(和Gemfile.lock)嵌入到同一个文件中,因此它是一个与其他文件一起存储的文件,作为要点等。

安装

Duct是一个命令行实用程序,所以只需通过rubygems安装它:

$ gem install duct

用法

首先,在__END__标签之后和@@ Gemfile之后,在脚本的数据部分编写Gemfile 标签:

require 'sinatra'

get '/' do
  'Hello, world!'
end

__END__
@@ Gemfile
source 'https://rubygems.org'

gem 'sinatra'

(这非常像Sinatra内联模板,并且可以 实际上与他们结合)。

然后,使用duct命令运行脚本:

$ duct my_script.rb

默认操作是运行脚本,先检查是否满足依赖关系,然后安装它们(通过 捆绑安装)如果需要的话。请注意,您的脚本数据部分将使用Gemfile.lock的部分进行更新 如果需要(在开发脚本时要小心与编辑器的冲突)。

传递参数

如果您的脚本需要参数,只需在文件名后面传递它们:

$ duct my_script.rb param1 param2

更新捆绑包

您可以运行任何bundle子命令(主要是更新,有或没有gem名称,但也过时,检查, 列表,显示和所有其余的)在它们之前传递它们 文件名:

$ duct update sinatra my_script.rb

请记住,更新捆绑包将更新脚本中的Gemfile.lock部分,因此请记住保存这些更改。

在shebangs中使用Duct

在类Unix操作系统下,您可以指示程序加载程序使用Duct来运行您的脚本。只需将以下shebang放在脚本的第一行(由于此处说明的内容,需要#ruby注释):

#!/usr/bin/env duct # ruby

并为您的脚本添加执行权限:

chmod +x my_script

这使您能够将脚本视为可执行文件,一旦进入$ PATH,就可以直接执行它。

使用脚本数据

在第一个@@ XYZ标签(以及带有其他标签的部分)之前,Duct将忽略数据部分 比@@ Gemfile和@@ Gemfile.lock,因为它是为Sinatra内联模板案例提到的)所以你仍然可以 使用该ruby功能,但您需要确保您的脚本处理并忽略Gemfile和Gemfile.lock 部分,当然。

特约

  1. 叉吧
  2. 创建您的功能分支(git checkout -b my-new-feature)
  3. 提交你的更改(git commit -am'添加一些功能')
  4. 推送到分支(git push origin my-new-feature)
  5. 创建新的Pull请求

执照

根据麻省理工学院许可证发布,版权所有(c)2014 Sergio Gil。

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

Duct Build Status

Duct Tape, By Evan-Amos, via Wikimedia Commons

Duct is a small wrapper around bundler. It allows you to embed a Gemfile in a script.

Usecase

Sometimes you write small scripts to do different tasks. Sometimes you store them for future use. Sometimes those
scripts use some gems to do their task (database access, web requests, whatever). So, after some time, you reuse them
and you don't know which version of which gems did you use and you can run into trouble (like with full fledged apps in
the pre-bundler days). That's when you do the obvious workaround: create a directory to store the script together with a
Gemfile and a Gemfile.lock.

That's a good solution but feels a bit overkill. Duct allows you to embed the Gemfile (and the Gemfile.lock) in the same file, so it's a single file to store together with others, as a gist, etc.

Installation

Duct is a command line utility, so just install it via rubygems:

$ gem install duct

Usage

First, write your Gemfile in the data section of your script, after the __END__ label, and after a @@ Gemfile
label:

require 'sinatra'

get '/' do
  'Hello, world!'
end

__END__
@@ Gemfile
source 'https://rubygems.org'

gem 'sinatra'

(this is very much like the Sinatra inline templates, and can
in fact be combined with them).

Then, run the script using the duct command:

$ duct my_script.rb

The default action is running the script, checking previously whether dependencies are met, and installing them (via
bundle install) if needed. Notice that your script data section will be updated with a section for the Gemfile.lock
if needed (beware of conflicts with your editor while you develop the script).

Passing parameters

If your script expects parameters, just pass them after the filename:

$ duct my_script.rb param1 param2

Updating the bundle

You can run any of the bundle subcommands (mainly update, with or without a gem name, but also outdated, check,
list, show and all the rest) passing them before the
filename:

$ duct update sinatra my_script.rb

Remember that updating the bundle will update the Gemfile.lock section in your script, so remember to save those changes.

Using Duct in shebangs

Under Unix-like operating systems, you can instruct the program loader to use Duct to run your script. Just put the following shebang in the first line of your script (the # ruby comment is needed because of something explained here):

#!/usr/bin/env duct # ruby

and add execute permissions to your script:

chmod +x my_script

This gives you the ability to treat your script as an executable that, once in your $PATH, to execute it directly.

Using the script data

Duct will ignore the part of the data section before the first @@ XYZ label (and the sections with other labels
than @@ Gemfile and @@ Gemfile.lock, as it was mentioned for the Sinatra inline templates case), so you can still
use that ruby feature, but you will need to make sure your script handles and ignores the Gemfile and Gemfile.lock
sections, of course.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Released under the MIT License, Copyright (c) 2014 Sergio Gil.