Child pages
  • How to create and use an extension (working example FOR REAL!)
Skip to end of metadata
Go to start of metadata

Example for writing an Extension

This is a fixed version of the Examples in the Guide and the RDoc.

module Loc
  include Extension

  first_time do
    # Define task not specific to any projet.
    desc 'Count lines of code in current project'
    Project.local_task('loc')
  end

  before_define do |project|
    # Define the loc task for this particular project.
    Rake::Task.define_task 'loc' do |task|
      lines = task.prerequisites.map { |path| Dir['#{path}/**/*'] }.flatten.uniq.inject(0) { |total, file| total + File.readlines(file).count }
      puts "Project #{project.name} has #{lines} lines of code"
    end
  end

  after_define do |project|
    # Now that we know all the source directories, add them.
    task('loc'=>project.compile.sources + project.test.sources)
  end

  # To use this method in your project:
  #   loc path_1, path_2
  def loc(*paths)
    task('loc'=>paths)
  end

end

class Buildr::Project
  include Loc
end

define 'testo' do    
  extend Loc    
end

example call

buildr -T
buildr loc
  • No labels