require "core/rake_ext" module Buildr # An inherited attribute gets its value an accessor with the same name. # But if the value is not set, it will obtain a value from the parent, # so setting the value in the parent make it accessible to all the children # that did not override it. module InheritedAttributes class << self private def included(mod) mod.extend(self) end end # :call-seq: # inherited_attr(symbol, default?) # inherited_attr(symbol) { |obj| ... } # # Defines an inherited attribute. The first form can provide a default value # for the top-level object, used if the attribute was not set. The second form # provides a default value by calling the block. # # For example: # inherited_attr :version # inherited_attr :src_dir, "src" # inherited_attr(:created_on) { Time.now } def inherited_attr(symbol, default = nil, &block) block ||= proc { default } attr_accessor symbol define_method "#{symbol}_with_inheritence" do value = send("#{symbol}_without_inheritence") if value.nil? value = parent ? parent.send(symbol) : self.instance_eval(&block) send "#{symbol}=", value end value end alias_method_chain symbol, :inheritence end end # A project definition is where you define all the tasks associated with # the project you're building. # # The project itself will define several life cycle tasks for you. For example, # it automatically creates a compile task that will compile all the source files # found in src/main/java into target/classes, a test task that will compile source # files from src/test/java and run all the JUnit tests found there, and a build # task to compile and then run the tests. # # You use the project definition to enhance these tasks, for example, telling the # compile task which class path dependencies to use. Or telling the project how # to package an artifact, e.g. creating a JAR using package :jar. # # You can also define additional tasks that are executed by project tasks, # or invoked from rake. # # Tasks created by the project are all prefixed with the project name, e.g. # the project foo creates the task foo:compile. If foo contains a sub-project bar, # the later will define the task foo:bar:compile. Since the compile task is # recursive, compiling foo will also compile foo:bar. # # If you run: # rake compile # from the command line, it will execute the compile task of the current project. # # Projects and sub-projects follow a directory heirarchy. The Rakefile is assumed to # reside in the same directory as the top-level project, and each sub-project is # contained in a sub-directory in the same name. For example: # /home/foo # |__ Rakefile # |__ src/main/java # |__ foo # |__ src/main/java # # The default structure of each project is assumed to be: # src # |__main # | |__java <-- Source files to compile # | |__resources <-- Resources to copy # | |__webapp <-- For WARs # |__test # | |__java <-- Source files to compile (tests) # | |__resources <-- Resources to copy (tests) # |__target <-- Packages created here # | |__classes <-- Generated when compiling # | |__test-classes <-- Generated when compiling tests # # You can only define a project once using #define. Afterwards, you # can obtain the project definition using #project. However, when # working with sub-projects, one project may reference another ahead # of its definition: the sub-project definitions are then evaluated # based on their dependencies with each other. Circular dependencies # are not allowed. # # For example: # define "myapp", :version=>"1.1" do # # define "wepapp" do # compile.with project("myapp:beans") # package :war # end # # define "beans" do # compile.with DEPENDS # package :jar # end # end # # puts projects.map(&:name) # => [ "myapp", "myapp:beans", "myapp:webapp" ] # puts project("myapp:webapp").parent.name # => "myapp" # puts project("myapp:webapp").compile.classpath.map(&:to_spec) # => "myapp:myapp-beans:jar:1.1" class Project < Rake::Task class << self # See Buildr#define. def define(name, properties, &block) #:nodoc: # Make sure a sub-project is only defined within the parent project, # to prevent silly mistakes that lead to inconsistencies (e.g. # namespaces will be all out of whack). Rake.application.current_scope == name.split(":")[0...-1] or raise "You can only define a sub project (#{name}) within the definition of its parent project" @projects ||= {} raise "You cannot define the same project (#{name}) more than once" if @projects[name] Project.define_task(name).tap do |project| # Define the project to prevent duplicate definition. @projects[name] = project # Set the project properties first, actions may use them. properties.each { |name, value| project.send "#{name}=", value } if properties project.enhance do |project| @on_define.each { |callback| callback[project] } end if @on_define # Enhance the project using the definition block. project.enhance { project.instance_eval &block } if block # Top-level project? Invoke the project definition. Sub-project? We don't invoke # the project definiton yet (allow project() calls to establish order of evaluation), # but must do so before the parent project's definition is done. if project.parent project.parent.enhance { project.invoke } else project.invoke end end end # See Buildr#project. def project(name) #:nodoc: @projects && @projects[name] or raise "No such project #{name}" @projects[name].tap { |project| project.invoke } end # See Buildr#projects. def projects(*names) #:nodoc: options = names.pop if Hash === names.last rake_check_options options, :in if options @projects ||= {} names = @projects.keys if names.empty? if options && options[:in] parent = @projects[options[:in].to_s] or raise "No such project #{options[:in].to_s}" names.uniq.map { |name| @projects[name] or raise "No such project #{name}" }. select { |project| project.parent == parent }. each { |project| project.invoke }.sort_by(&:name) else names.uniq.map { |name| project(name) or raise "No such project #{name}" }.sort_by(&:name) end end # :call-seq: # clear() # # Discard all project definitions. def clear() @projects.clear if @projects end # :call-seq: # local_task(name) # local_task(name) { |name| ... } # # Defines a local task with an optional execution message. # # A local task is a task that executes a task with the same name, defined in the # current project, the project's with a base directory that is the same as the # current directory. # # Complicated? Try this: # rake build # is the same as: # rake foo:build # But: # cd bar # rake build # is the same as: # rake foo:bar:build # # The optional block is called with the project name when the task executes # and returns a message that, for example "Building project #{name}". def local_task(args, &block) task args do |task| projects = Project.projects.select { |project| project.base_dir == Rake.application.original_dir } if projects.empty? warn "No projects defined for directory #{Rake.application.original_dir}" if verbose else projects.each do |project| puts block.call(project.name) if block && verbose task("#{project.name}:#{task.name}").invoke end end end end # :call-seq: # on_define() { |project| ... } # # The Project class defines minimal behavior, only what is documented here. # To extend its definition, other modules use Project#on_define to incorporate # code called during a new project's definition. # # For example: # # Set the default version of each project to "1.0". # Project.on_define { |project| project.version ||= "1.0" } # # Since each project definition is essentially a task, if you need to do work # at the end of the project definition (after the block is executed), you can # enhance it from within #on_define. def on_define(&block) (@on_define ||= []) << block if block end def warnings() #:nodoc: [].tap do |msgs| msgs << "There are no project definitions in your Rakefile" if @projects.nil? || @projects.empty? # Find all projects that: # * Are referenced but never defined. This is probably a typo. # * Do not have a base directory. (@projects || {}).each do |name, project| msgs << "Project #{name} refers to the directory #{project.base_dir}, which does not exist" unless File.exist?(project.base_dir) end end end def scope_name(scope, task_name) #:nodoc: task_name end end include InheritedAttributes # The project name. For example, "foo" for the top-level project, and "foo:bar" # for its sub-project. attr_reader :name # The parent project if this is a sub-project. attr_reader :parent def initialize(*args) #:nodoc: super split = name.split(":") if split.size > 1 # Get parent project, but do not invoke it's definition to prevent circular # dependencies (it's being invoked right now, so calling project() will fail). @parent = task(split[0...-1].join(":")) raise "No parent project #{split[0...-1].join(":")}" unless @parent && Project === parent end # We only need this because each task (and a project is a task) already has # a @base_dir variable (and base_dir method), and we want it lazily evaluated. # See all the logic that happens when we call base_dir. @base_dir = nil end # :call-seq: # base_dir() => path # # Returns the project's base directory. # # The Rakefile defines top-level project, so it's logical that the top-level project's # base directory is the one in which we find the Rakefile. And each sub-project has # a base directory that is one level down, with the same name as the sub-project. # # For example: # /home/foo/ <-- base_directory of project "foo" # /home/foo/Rakefile <-- builds "foo" # /home/foo/bar <-- sub-project "foo:bar" def base_dir() if @base_dir.nil? if @parent # For sub-project, a good default is a directory in the parent's base_dir, # using the same name as the project. sub_dir = File.join(@parent.base_dir, name.split(":").last) @base_dir = File.exist?(sub_dir) ? sub_dir : @parent.base_dir @base_dir = sub_dir else # For top-level project, a good default is the directory where we found the Rakefile. @base_dir = Dir.pwd end end @base_dir end # :call-seq: # base_dir = dir # # Sets the project's base directory. Allows you to specify a base directory by calling # this accessor, or with the :base_dir property when calling #define. # # You can only set the base directory once for a given project, and only before accessing # the base directory (for example, by calling #file or #path_to). # Set the base directory. Note: you can only do this once for a project, # and only before accessing the base directory. If you try reading the # value with #base_dir, the base directory cannot be set again. def base_dir=(dir) raise "Cannot set base directory twice, or after reading its value" if @base_dir @base_dir = File.expand_path(dir) end # :call-seq: # path_to(*names) => path # # Returns a path from a combination of name, relative to the project's base directory. # Essentially, joins all the supplied names and expands the path relative to #base_dir. # Symbol arguments are converted to paths by calling the attribute accessor on the project. # # Keep in mind that all tasks are defined and executed relative to the Rakefile directory, # so you want to use #path_to to get the actual path within the project as a matter of practice. # # For example: # path_to("foo", "bar") # => /home/project1/foo/bar # path_to("/tmp") # => /tmp # path_to(:base_dir, "foo") # same as path_to("foo") # => /home/project1/foo def path_to(*names) File.expand_path(File.join(names.map { |name| Symbol === name ? send(name) : name.to_s }), base_dir) end alias :_ :path_to # :call-seq: # define(name, properties?) { |project| ... } => project # # Define a new sub-project within this project. See Buildr#define. def define(name, properties = nil, &block) Project.define "#{self.name}:#{name}", properties, &block end # :call-seq: # project(name) => project # # Same as Buildr#project. def project(name) Project.project(name) end # :call-seq: # projects(*names) => projects # # Same as Buildr#projects. def projects(*names) Project.projects(*names) end # :call-seq: # file(path) => Task # file(path=>prereqs) => Task # file(path) { |task| ... } => Task # # Creates and returns a new file task in the project. Similar to calling Rake's # file method, but the path is expanded relative to the project's base directory, # and the task executes in the project's base directory. # # For example: # define "foo" do # define "bar" do # file("src") { ... } # end # end # # puts project("foo:bar").file("src").to_s # => "/home/foo/bar/src" def file(args, &block) task_name, deps = Rake.application.resolve_args(args) deps = [deps] unless deps.respond_to?(:to_ary) Rake::FileTask.define_task(path_to(task_name)=>deps, &block) end # :call-seq: # task(name) => Task # task(name=>prereqs) => Task # task(name) { |task| ... } => Task # # Creates and returns a new task in the project. Similar to calling Rake's task # method, but prefixes the task name with the project name and executes the task # in the project's base directory. # # For example: # define "foo" do # task "doda" # end # # puts project("foo").task("doda").name # => "foo:doda" # # When called from within the project definition, creates a new task if the task # does not already exist. If called from outside the project definition, returns # the named task and raises an exception if the task is not defined. # # As with Rake's task method, calling this method enhances the task with the # prerequisites and optional block. def task(args, &block) task_name, deps = Rake.application.resolve_args(args) if task_name =~ /^:/ Rake.application.instance_eval do scope, @scope = @scope, [] begin Rake::Task.define_task(task_name[1..-1]=>deps, &block) ensure @scope = scope end end elsif Rake.application.current_scope == name.split(":") Rake::Task.define_task(task_name=>deps, &block) else if task = Rake.application.lookup(task_name, name.split(":")) deps = [deps] unless deps.respond_to?(:to_ary) task.enhance deps, &block else full_name = "#{name}:#{task_name}" raise "You cannot define a project task outside the project definition, and no task #{full_name} defined in the project" end end end # :call-seq: # recursive_task(name=>prereqs) { |task| ... } # # Define a recursive task. A recursive task executes itself and the same task # in all the sub-projects. def recursive_task(args, &block) task_name, deps = Rake.application.resolve_args(args) deps = [deps] unless deps.respond_to?(:to_ary) task = Buildr.options.parallel ? multitask(task_name) : task(task_name) Rake.application.lookup(task_name, parent.name.split(":")).enhance [task] if parent task.enhance deps, &block end def execute() #:nodoc: # Reset the namespace, so all tasks are automatically defined in the project's namespace. Rake.application.in_namespace(":#{name}") { super } end end # :call-seq: # define(name, properties?) { |project| ... } => project # # Defines a new project. # # The first argument is the project name. Each project must have a unique name. # For a sub-project, the actual project name is created by prefixing the parent # project's name. # # The second argument is optional and contains a hash or properties that are set # on the project. You can only use properties that are supported by the project # definition, e.g. :group and :version. You can also set these properties from the # project definition. # # You pass a block that is executed in the context of the project definition. # This block is used to define the project and tasks that are part of the project. # Do not perform any work inside the project itself, as it will execute each time # the Rakefile is loaded. Instead, use it to create and extend tasks that are # related to the project. # # For example: # define "foo", :version=>"1.0" do # # define "bar" do # compile.with "org.apache.axis2:axis2:jar:1.1" # end # end # # puts project("foo").version # => "1.0" # puts project("foo:bar").compile.classpath.map(&:to_spec) # => "org.apache.axis2:axis2:jar:1.1" # % rake build # => Compiling 14 source files in foo:bar def define(name, properties = nil, &block) #:yields:project Project.define(name, properties, &block) end # :call-seq: # project(name) => project # # Returns a project definition. # # You cannot reference a project before the project is defined. When working with # sub-projects, the project definition is stored by calling #define, and evaluated # before a call to the parent project's #define method returns. # # However, if you call #project with the name of another sub-project, its definition # is evaluated immediately. So the returned project definition is always complete, # and you can access its definition (e.g. to find files relative to the base directory, # or packages created by that project). # # For example: # define "myapp" do # self.version = "1.1" # # define "webapp" do # # webapp is defined first, but beans is evaluated first # compile.with project("myapp:beans") # package :war # end # # define "beans" do # package :jar # end # end def project(name) Project.project(name) end # :call-seq: # projects(*names) => projects # projects(:in=>parent) => projects # # With no arguments, returns a list of all projects defined so far. With arguments, # returns a list of these projects, fails on undefined projects. # # Like #project, this method evaluates the definition of each project before returning it. # Be advised of circular dependencies. # # Use the :in option if you only want the sub-projects of a given parent project. # # For example: # files = projects.map { |prj| FileList[prj.path_to("src/**/*.java") }.flatten # puts "There are #{files.size} source files in #{projects.size} projects" # # puts projects("myapp:beans", "myapp:webapp").map(&:name) # Same as: # puts projects(:in=>"mayapp").map(&:name) def projects(*names) Project.projects *names end # Add project definition tests. task("check") { |task| task.note *Project.warnings } end