lib/core/project.rb in buildr-1.1.3 vs lib/core/project.rb in buildr-1.2.0

- old
+ new

@@ -1,6 +1,7 @@ require "core/rake_ext" +require "core/common" 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, @@ -63,18 +64,18 @@ # 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 + # buildr 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 + # Projects and sub-projects follow a directory heirarchy. The Buildfile 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 + # |__ Buildfile # |__ src/main/java # |__ foo # |__ src/main/java # # The default structure of each project is assumed to be: @@ -183,16 +184,17 @@ # See Buildr#projects. def projects(*names) #:nodoc: options = names.pop if Hash === names.last rake_check_options options, :scope if options @projects ||= {} + names = names.flatten if options && options[:scope] # We assume parent project is evaluated. if names.empty? - parent = @projects[options[:scope]] or raise "No such project #{options[:scope]}" - @projects.values.select { |project| project.parent == parent }. - each { |project| project.invoke }.sort_by(&:name) + parent = @projects[options[:scope].to_s] or raise "No such project #{options[:scope]}" + @projects.values.select { |project| project.parent == parent }.each { |project| project.invoke }. + map { |project| [project] + projects(:scope=>project) }.flatten.sort_by(&:name) else names.uniq.map { |name| project(name, :scope=>options[:scope]) } end elsif names.empty? # Parent project(s) not evaluated so we don't know all the projects yet. @@ -222,31 +224,26 @@ # 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 + # buildr build # is the same as: - # rake foo:build + # buildr foo:build # But: # cd bar - # rake build + # buildr build # is the same as: - # rake foo:bar:build + # buildr 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 = local_projects - 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 + local_projects do |project| + puts block.call(project.name) if block && verbose + task("#{project.name}:#{task.name}").invoke end end end # :call-seq: @@ -269,15 +266,21 @@ def scope_name(scope, task_name) #:nodoc: task_name end - def local_projects(dir = Rake.application.original_dir) #:nodoc: - dir = File.expand_path(dir) + def local_projects(dir = nil, &block) #:nodoc: + dir = File.expand_path(dir || Rake.application.original_dir) projects = Project.projects.select { |project| project.base_dir == dir } if projects.empty? && dir != Dir.pwd && File.dirname(dir) != dir - local_projects(File.dirname(dir)) + local_projects(File.dirname(dir), &block) + elsif block + if projects.empty? + warn "No projects defined for directory #{Rake.application.original_dir}" if verbose + else + projects.each { |project| block[project] } + end else projects end end @@ -310,26 +313,26 @@ # :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 + # The Buildfile defines top-level project, so it's logical that the top-level project's + # base directory is the one in which we find the Buildfile. 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/Buildfile <-- 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. @base_dir = File.join(@parent.base_dir, name.split(":").last) else - # For top-level project, a good default is the directory where we found the Rakefile. + # For top-level project, a good default is the directory where we found the Buildfile. @base_dir = Dir.pwd end end @base_dir end @@ -355,11 +358,11 @@ # # 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, + # Keep in mind that all tasks are defined and executed relative to the Buildfile 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 @@ -497,11 +500,11 @@ # 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 + parent.task(task_name).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. @@ -529,11 +532,11 @@ # 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 + # the Buildfile is loaded. Instead, use it to create and extend tasks that are # related to the project. # # For example: # define "foo", :version=>"1.0" do # @@ -544,11 +547,11 @@ # # puts project("foo").version # => "1.0" # puts project("foo:bar").compile.classpath.map(&:to_spec) # => "org.apache.axis2:axis2:jar:1.1" - # % rake build + # % buildr build # => Compiling 14 source files in foo:bar def define(name, properties = nil, &block) #:yields:project Project.define(name, properties, &block) end @@ -610,17 +613,9 @@ # puts projects("myapp:beans", "myapp:webapp").map(&:name) # Same as: # puts project("myapp").projects.map(&:name) def projects(*args) Project.projects *args - end - - desc "List all projects defined by this Rakefile" - task "projects" do - wide = projects.map(&:name).map(&:size).max - projects.each do |project| - puts project.comment.blank? ? project.name : ("%-#{wide}s #%s" % [project.name, project.comment]) - end end # Forces all the projects to be evaluated before executing any other task. # If we don't do that, we don't get to have tasks available when running Rake. task("buildr:projects") { projects }