Class: Rakegen
- Inherits:
-
Rake::TaskLib
- Object
- Rake::TaskLib
- Rakegen
- Defined in:
- lib/rakegen.rb
Instance Attribute Summary collapse
-
#all_files ⇒ Object
Returns the value of attribute all_files.
-
#copy_files ⇒ Object
List of files to be copied.
-
#description ⇒ Object
Returns the value of attribute description.
-
#directories ⇒ Object
readonly
List of directories that need to be created.
-
#excludes ⇒ Object
Returns the value of attribute excludes.
-
#executables ⇒ Object
Returns the value of attribute executables.
-
#files ⇒ Object
Returns the value of attribute files.
-
#name ⇒ Object
Name of the primary Rake task.
-
#source(path = nil) ⇒ Object
If given a path, joins it to @source.
-
#target(path = nil) ⇒ Object
If given a path, joins it to @target.
-
#template_assigns ⇒ Object
variables for use in template processing.
-
#template_files ⇒ Object
List of files to be processed (using, e.g. Erubis).
-
#template_processors ⇒ Object
Hash of extension => lambda.
Instance Method Summary collapse
-
#initialize(task_name = :app) {|_self| ... } ⇒ Rakegen
constructor
Create a generator.
-
#invoke ⇒ Object
Invoke the primary task.
-
#polite_file(args, &block) ⇒ Object
A rake file-based task that assesses neededness of target files based on user confirmation, rather than relative timestamps.
Constructor Details
#initialize(task_name = :app) {|_self| ... } ⇒ Rakegen
Create a generator. The task_name becomes the primary Rake task, which has dependency tasks that get all the work done. You can use a string to supply a namespaced task name, such as “waves:app”.
In the required block, you must define the source and target directories. You may also define a list of excludes, a list of executables, assign variables for templates, and lambdas for processing different kinds of templates, keyed by the file extension.
generator = Rakegen.new do |gen|
gen.source = "some/place"
gen.target = "some/where/else"
gen.excludes = "**/*.yaml"
gen.executables = %w{ bin/console bin/server }
gen.template_assigns[:monkey] = "howler"
gen.template_processors[:erb] = lambda { |src, tgt| MyErb.process(src, tgt) }
end
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rakegen.rb', line 60 def initialize(task_name = :app) @name = task_name @excludes = [] @executables = [] @template_processors = {} @template_processors["erb"] = lambda do |source_file, target_file| require 'erubis' File.open(target_file, 'w') do |trg| File.open(source_file, 'r') do |src| trg.print Erubis::Eruby.new(src.read).evaluate(template_assigns) end end end yield self raise "You must supply a source." unless @source raise "You must supply a target." unless @target Dir.chdir(@source) do @files = Rake::FileList.new("**/*").exclude(*@excludes).to_a @directories = Rake::FileList.new("**/").map { |f| f.chomp("/") }.to_a end @template_files = @template_processors.inject([]) do |tfiles, processor| ext = processor[0] tfiles + @files.select { |f| f =~ /\.#{ext}$/ } end @copy_files = @files - @directories - @template_files @all_files = @files - @template_files + @template_files.map { |f| f.chomp(File.extname(f)) } @target_executables = @executables.map { |f| target(f) } define end |
Instance Attribute Details
#all_files ⇒ Object
Returns the value of attribute all_files.
26 27 28 |
# File 'lib/rakegen.rb', line 26 def all_files @all_files end |
#copy_files ⇒ Object
List of files to be copied
25 26 27 |
# File 'lib/rakegen.rb', line 25 def copy_files @copy_files end |
#description ⇒ Object
Returns the value of attribute description.
13 14 15 |
# File 'lib/rakegen.rb', line 13 def description @description end |
#directories ⇒ Object (readonly)
List of directories that need to be created
22 23 24 |
# File 'lib/rakegen.rb', line 22 def directories @directories end |
#excludes ⇒ Object
Returns the value of attribute excludes.
38 39 40 |
# File 'lib/rakegen.rb', line 38 def excludes @excludes end |
#executables ⇒ Object
Returns the value of attribute executables.
40 41 42 |
# File 'lib/rakegen.rb', line 40 def executables @executables end |
#files ⇒ Object
Returns the value of attribute files.
27 28 29 |
# File 'lib/rakegen.rb', line 27 def files @files end |
#name ⇒ Object
Name of the primary Rake task
11 12 13 |
# File 'lib/rakegen.rb', line 11 def name @name end |
#source(path = nil) ⇒ Object
If given a path, joins it to @source. Otherwise returns @source
117 118 119 |
# File 'lib/rakegen.rb', line 117 def source(path=nil) path ? File.join(@source, path) : @source end |
#target(path = nil) ⇒ Object
If given a path, joins it to @target. Otherwise returns @target
122 123 124 |
# File 'lib/rakegen.rb', line 122 def target(path=nil) path ? File.join(@target, path) : @target end |
#template_assigns ⇒ Object
variables for use in template processing
36 37 38 |
# File 'lib/rakegen.rb', line 36 def template_assigns @template_assigns end |
#template_files ⇒ Object
List of files to be processed (using, e.g. Erubis)
30 31 32 |
# File 'lib/rakegen.rb', line 30 def template_files @template_files end |
#template_processors ⇒ Object
Hash of extension => lambda
33 34 35 |
# File 'lib/rakegen.rb', line 33 def template_processors @template_processors end |
Instance Method Details
#invoke ⇒ Object
Invoke the primary task. In other words, run the generator.
99 100 101 |
# File 'lib/rakegen.rb', line 99 def invoke Rake::Task[name].invoke end |
#polite_file(args, &block) ⇒ Object
A rake file-based task that assesses neededness of target files based on user confirmation, rather than relative timestamps. As with Rake#file, you can do arbitrary stuff in the block:
polite_file "some/target/file" do
str = File.read("/etc/profile").reverse
File.write( "some/target/file", str)
end
112 113 114 |
# File 'lib/rakegen.rb', line 112 def polite_file(args, &block) PoliteFileTask.define_task(args, &block) end |