使用 Rake 或 Gem 构建脚本?
我想构建为我实现自动化的脚本。
这是我想要做的一个例子:
- 创建新的rails应用程序 (rails new application_name --database=mysql)
- 跳转到文件夹
- 初始化 git (git init; git add .; git commit -m "first commit"; git remote add名称地址;git推送名称master)
- 创建heroku项目(heroku创建;git推送herokumaster)
- 等等......
我有很多这样的脚本(不仅仅是rails相关的)我想构建。
这些步骤应该使用 Rake 还是 Gem 进行编码?
据我了解,rake 通常是从当前文件夹的 Rakefile 中获取任务。如果我想让操作通用,是不是创建一个 gem 更好?
问题是我希望能够从我所在的任何目录中调用它。
每个目录的优点和缺点是什么?
I want to build scripts that automatize things for me.
Here is an example of what I want to do:
- Create new rails app (rails new application_name --database=mysql)
- Jump to folder
- Initialize git (git init; git add .; git commit -m "first commit"; git remote add name address; git push name master)
- Create heroku project (heroku create; git push heroku master)
- Etc...
I've got a lot of such scripts (not just rails related) I want to build.
Should these kind of steps be coded with Rake or Gem?
From what I have understood rake is usually getting the tasks from the current folder's Rakefile. If i want to make operations universal, is it better to create a gem?
The thing is that I want to be able to call it from whatever directory I'm in.
What are the pros and cons with each?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用耙子或宝石就可以了。如果你想集中你的 rake 任务(假设你使用的是 OSX 或某些 Linux/*nix 变体),你可以在你的主目录中创建它们:
如果在没有 Rakefile 的目录中,Rake 将在那里查找要运行的任务。
另外,请考虑(再次,如果您在某种 *nix 平台上)为您的命令创建 shell 别名。
编辑:
Rails 工作特有的另一个考虑因素是利用应用程序模板。这是一个很好的截屏视频的链接。
Going with rake or a gem is fine. If you want to centralize your rake tasks (assuming you are on OSX or Some Linux/*nix variant) you can create them in your home directory:
Rake will look there for tasks to run if in a directory w/ no Rakefile.
Also, consider (again, if you are on some sort of *nix platform) just creating shell aliases to your commands.
Edit:
Another consideration specific to your Rails work is to leverage Application Templates. Here is a link to a good screencast.
你想要的一些东西可以用 shell 别名来完成,一些用 gems,一些用 rake 来完成。
根据 Brian 的建议,我将以下内容添加到 ~/.rake/git.rake:
然后在任何目录中我都可以运行“rake git:init”,它将完成所有初始设置。远程添加有点困难,因为远程名称将是一个变量(可以通过 shell 变量或 Readline 提示提供)。
为了创建 Rails 应用程序,我将向 ~/.bash_profile 添加别名:
然后运行“new_mysql myRailsProject”。
在大多数情况下,我认为运行一堆命令行脚本将是一个 shell 别名,而不是一个 Rake 任务。
Some of what you want could be accomplished with shell aliases, some with gems, some with rake.
Using Brian's suggestion, I added the following to ~/.rake/git.rake:
Then in any directory I can run "rake git:init" and it will do all the intial setup. The remote add is a little harder because the remote name would be a variable (could be provided via a shell variable or a Readline prompt).
For creating a rails app, I'd add an alias to ~/.bash_profile:
Then run "new_mysql myRailsProject".
For the most part I would think just running a bunch of command line scripts would be a shell alias rather than a Rake task.
你应该使用 rake 任务来做这些事情。贝茨有一个截屏视频,展示如何完成您想要完成的任务。基本上,您创建一个自定义任务,然后您可以调用 rake my_task ,它将执行您的脚本。
You should be using rake tasks for this stuff. Bates has a screencast showing how to accomplish what your trying to get done. Basically you create a custom task and after you can call rake my_task and it will execute your script.