Using Rake or Gem for building scripts?
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 开发者_如何学Gowant 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?
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/*.rake
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.
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:
namespace :git do
desc "Init, add, initial commit"
task :init do
`git init .`
`git add .`
`git commit -m 'Initial commit'`
end
end
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:
alias new_mysql="rails new $ARGV --database=mysql"
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.
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.
精彩评论