Have you ever thought why ‘rake’ is available in the shell or windows cmd, after "gem install rake"? What if you want write your who gem that could let you say hello to he world from console? Let’s better ask rubygems itself 🙂
But, before we dive into the code, there is a little warm up. The gem specification, which contains the meta date of the gem, have some entries related the executables:
Executables A list of files in the package that are applications.
Bindir The directory containing the application files, if any.
Is tell us to looking up a file named rake, as executable, in ‘bin’ folder. Just as above, we could find rake file(or rake.bat if you are on windows), which start with #!/usr/bin/ruby, in you bin folder after you install rake gem. It told us all this is done by rubygems, or to be more specific, by gem install.
So let’s try to find out if there is a file called something like install in the rubygems package. Lucky enough, there is truly a installer.rb there!
I think the author is kind enough to show all the gem installation logic in this single method. Here we only need to focus on line36, oh yes, it is rubygems responsibility to generate executable and put it in the bindir. Of cause, there are much more magic happens behind this simple line of "generate_bin", I bet you will be pleased to learn it by your self:)
Most people treat Rakefile as the rake task definition, but it’s rarely documented, as far as I know. Let once again ask the source code for confirmation 🙂
Rake start with run method, which is quite simple
Obviously, load_rakefile is suspicious, which is a method basically invoke raw_load_rakefile:
Although it is a pretty long method, we only need to focus the first line for our goal:
And let’s see what find_rakefile_location could explain:
find_rakefile_location find rake definition files from the current directory to all it parent directories. Not so hard to understand, right?
have_rakefile is the method for detecting if the rake definition file is exist in the current path from a candidate list @rakefiles:
Finally, we found our rake definition files: @rakefiles, which is an array:
Haha , we got our answer from the source code:) It turns out we are not limited with only Rakefile, but also other three forms. It also explains why the Rakefile always located in the root path of a project, because rake will eventually reach definition files all the way up from all directories under the project path in which we execute the rake method.
Freeze rails version for you rails app is a very common job:
rake rails:freeze:gems #Freeze to the system rails gem version on your machine
rake rails:freeze:edge #Freeze to the lasted rails version from web
To better understand, read the source code in railsrailtieslibtasksframework.rake