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?
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.
Related
In my Gemfile I have
gem 'slim', :git => 'git://github.com/brennancheung/slim.git', :branch => 'angularjs_support'
which is a branch of the slim gem required for me to run AngularJS correctly with my views. I've pushed my code to my beanstalk application but am unable to bundle install according to the logs shown below...
sh: git: command not found
Git error: command `git clone 'git://github.com/brennancheung/slim.git'
"/usr/share/ruby/1.9/gems/1.9.1/cache/bundler/git/slim-700ed452e752ccb6baf9de9d0a46fbded8bb2da5"
--bare --no-hardlinks` in directory /var/app/ondeck has failed.
I'm new to Beanstalk and have no idea how to fix this. Any help on how to get bundle to install successfully would be greatly appreciated. Thanks.
Since git is not installed on by default on EC2 instance, you would have to find a workaround solution:
a. Install git on instance with configuration file and command.
It is the most obvious way to solve the problem, although not be the most efficient.
b. Clone slim repository into your project, so it will be deployed together.
Seems that slim is not being actively developed lately, so having the copy in your project might not be a bad idea. It protects you from github.com being down, yet you will have extra files to carry around.
c. Use configuration file and commands to pull the data from github.com directly with http.
Too many files to work with, and also dependency on third party service.
d. Use a combination of above. Clone slim repository and copy files to S3. Use configuration and commands to copy files from S3 to your instance.
It seems like the most elegant and efficient way to solve the problem.
It might look something like:
$ cat .ebextensions/myapp.config
commands:
10-copy-slim-from-s3
command: "aws s3 cp s3://mybucket/slim slim --recursive"
I'm creating a Sinatra app that will pull in data from a remote git repository.
I've taken a look at the ruby-git gem, but I get cannot load such file -- git on the line of require 'git'. The gem is installed and in my Gemfile.
I'm not sure if this is the correct way about going about this, but essentially I want the app to checkout a git repository for pulling in data.
Ended up using the system command:
system("git clone #{$config['repository']} content")
what is the ideal directory structure of a ruby application.
I want it to be deployed on EC2. It should have Gemfile and Rake file for executing rake tasks.
It should contain a lib file for utilities.
Should I just be using the command
newgem --simple test
Take a look at this RailsCast.
'bundle gem simple_test' works great for me.
$ bundle gem simple_test
create simple_test/Gemfile
create simple_test/Rakefile
create simple_test/.gitignore
create simple_test/simple_test.gemspec
create simple_test/lib/simple_test.rb
create simple_test/lib/simple_test/version.rb
Initializating git repo in /Users/sean/dev/foo/simple_test
$
I would like to have ctags generate a TAGS file of all my bundled gems or all the gems under the rvm gemset directory bundler installs its gems. Ideally, a bundle install or bundle update should generate a TAGS file at the last step using a ruby script I'll provide. Afterthat emacs joy.
Is there any kind of a bundler after hook I can use?
You could look at what Tim Pope does in his Hookup project:
https://github.com/tpope/hookup
I'd imagine it wouldn't be too hard to an an extra step after the bundler run.
Personally I just have a good old Makefile in my Ruby project:
.PHONY: tags
tags:
ETAGS=ctags
rm -rf TAGS
ctags -a -e -f TAGS --tag-relative -R app lib vendor
I have a shell script I run in the morning which sets up my dev environment which also runs make tags.
According to https://github.com/bundler/bundler/blob/dd1e11d8f8e869ffab4fc68d4854b27e1f486de4/lib/bundler/source/path.rb, there is the ability to run 'post_install' hooks. It uses meta-programming to deduce the method name, and the gem is supposed to implement that method. Will try and check if this works
My approach has been two pronged:
1) Put a rake task in place that generates tags for all code in the project as well as all required gems:
desc 'Create ctags'
task :tags do
system "ctags -R --language-force=ruby app config lib `rvm gemdir`/gems"
end
2) Using the excellent "foreman" gem (which I was using anyway) to run inotifywait and fire off the rake task if a file changes:
tags: while inotifywait -q -r -e MODIFY --exclude swp$ app/ config/ lib/ ; do bundle exec rake tags; done
If you are not using foreman you can of course just run that line without the first "tags:" part manually in a shell.
say I've got my rails app on github and am deploying the github repo on heroku.
I've got a situation where I have a simple text file with bunch of words (it is in my github repo). I want to insert these words (using a simple ruby program) into a database. Instead of using the tap command, is it possible in heroku to just run my simple ruby program and insert the words into the database...or maybe just show them on the terminal?
maybe confusing but basically I want to know how to run simple ruby script from heroku command line?
With cedar, you can run bash:
heroku run bash
Put your ruby script in a bin directory and git push it to Heroku. Now you can execute a shell command in the heroku console.
For example, if your Ruby script is bin/foo.rb, you can run the following command in the Heroku console:
`ruby bin/foo.rb`
Note the use of backticks.
Since you're talking about a Rails app on Heroku, how about using rails runner:
heroku run bundle exec rails runner ./path/to/script.rb -a <your-app>
Have a look at the RailsGuides for rails runner for more details.
Alternatively, turn that script into a rake task if runner is not your cup of tea (eg, for recurring tasks).
cd /path/to/my/local/repository
heroku console
require 'my_word_importing_script'
Failing that, try a simple Sinatra application as importer.rb?
require 'sinatra'
require 'sequel'
configure do
// connect to the database with sequel
end
get '/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds' do
words = YAML.load(File.join(File.dirname(__FILE__), "my_list_of_words.yaml"))
words.each do |word|
// Your logic for inserting into the database with sequel
end
end
Hitting http://example.com/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds in your browser would kick off the import. Handy for an external cron task.
You would also need a config.ru file in the repo:
require 'importer'
run Sinatra::Application
If you want to run arbitrary local Ruby files on Heroku, check out the blog post at
http://www.22ideastreet.com/debug/run-local-scripts-on-heroku
There are some things to watch out for (long run times, etc.) but it might be useful if you have a file that you haven't checked in that you want to test or run on a Heroku instance.