Rakerake and makeRakefilesFirst example, IFirst example, IIRunning rakeAdditional targetsDynamically building tasksAutomatically building tasksFinal resultCreditThe EndJan 14, 2019Rakerake and makeA program can consist of many source code filesThis is always true in Rails!The files may need to be compiled in a certain orderSome parts of the program may depend on other parts being up to dateA UNIX makefile is a file that describes these dependenciesUNIX make is a program that reads a makefile, determines the correct order in which to update files, and updates themRuby programs are interpreted, not compiled; but...Rails uses metaprogramming to create source files and data files from other filesConsequently, something like make is still neededrake provides the same functionality as make, but is implemented very differentlyRakefilesRakefiles are written in Ruby The following code fragment expresses that a file file_1 depends on files file_2 and file_3file "file_1" => ["file_2", "file_3"]We can use this code fragment with a block that tells what to do with the dependencyfile "file_1" => ["file_2", "file_3"] do # code to create file_1 from file_2 and file_3endA rakefile can consist simply of a number of these blocksLike make, rake looks at the modification dates of files and only updates them as necessaryFirst example, IThis example uses C files as examplesSuppose we have the files main.c, greet.h, and greet.cmain.c is our usual “Hello World” program, but includes greet.h, which specifies a greet method (on greet.c)Our target (the file we want to build) is hello.oWe have the following dependencies:file "main.o" => ["main.c", "greet.h"]file "greet.o" => ["greet.c"]file "hello" => ["main.o", "greet.o"]To create the target, we need to execute these commands:cc -c -o main.o main.ccc -c -o greet.o greet.ccc -o hello main.o greet.oFirst example, IIHere’s the rakefile:file 'main.o' => ["main.c", "greet.h"] do sh "cc -c -o main.o main.c"end file 'greet.o' => ['greet.c'] do sh "cc -c -o greet.o greet.c"end file "hello" => ["main.o", "greet.o"] do sh "cc -o hello main.o greet.o"endRunning rakeThe syntax for running a rake command israke [options ...] [VAR=VALUE] [targets ...] Unless we use the option -f filename , rake will read its commands from a file named rakefileOur target (the thing we want to make) is named "hello" in this file, so (assuming the program on the previous slide is on a file named rakefile), we run rake by saying rake helloAdditional targetsfile targets check modification dates, hence these tasks are only done when neededNon-file tasks are always performedNon-file tasks use the task keyword instead of fileWe can specify a default task, such as "hello", like this:task :default => ["hello"]Other non-file tasks are:clean -- Remove temporary files created during the build processclobber -- Remove all files generated during the build processThe Rake library implements clean and clobber for you, but you have to tell it what files to clean or clobberDo this with FileListsclean and clobber use the lists named CLEAN and CLOBBER, respectivelyExample: CLEAN = FileList["greet.o"]You can use wildcards: CLOBBER = FileList["*.o"]Dynamically building tasksExample:SRC = FileList['*.c']SRC.each do |fn| obj = fn.sub(/\.[^.]*$/, '.o') file obj do sh "cc -c -o #{obj} #{fn}" endendNotes:Remember that Ruby will do substitution in double-quoted stringsThe file list depends on the source files (.c files), because the object files (.o files) may or may not be presentThe dependencies between source and object files are specified elsewhereRake can figure this outAutomatically building tasksRather than dynamically building tasks, it’s usually easier just to generate them automaticallyFor example, In C the object .o files depend on the source .c files, so we can say:rule '.o' => '.c' do |t| sh "cc -c -o #{t.name} #{t.source}"endFinal resultrequire 'rake/clean'CLEAN.include('*.o')CLOBBER.include('hello')task :default => ["hello"]SRC = FileList['*.c']OBJ = SRC.ext('o')rule '.o' => '.c' do |t| sh "cc -c -o #{t.name} #{t.source}"endfile "hello" => OBJ do sh "cc -o hello #{OBJ}"end # File dependencies go here ...file 'main.o' => ['main.c', 'greet.h']file 'greet.o' => ['greet.c']CreditThese slides cover only the most basic use of rakeThe extended example used in these slides is taken from http://docs.rubyrake.org/read/book/1A more comprehensive explanation of rakefiles can be found at http://www.martinfowler.com/articles/rake.htmlThe
View Full Document