You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@buildr.apache.org by Peter Donald <pe...@realityforge.org> on 2010/03/20 00:49:37 UTC

Layouts customized by project attributes

Hi,

My current usage of buildr has resulted in many subprojects. Some of
these subprojects actually have no files as they simply repackaging
jars as OSGi bundles (i.e. jars + more metadata). So I have decided to
have a central "target" directory under which each project has a dir.
i.e. The following snippet uses target/A and target/B as intermediate
dirs of each project

class CentralLayout < Layout::Default
  def initialize(key, top_level = false)
    super()
    prefix = top_level ? '' : '../'
    self[:target] = "#{prefix}target/#{key}"
    self[:target, :main] = "#{prefix}target/#{key}"
  end
end

define 'A', :layout => CentralLayout.new('A', true) do
   ...
   define 'B', :layout => CentralLayout.new('B') do
      ...
   end
end

Unfortunately I have to repeat the name of the project when
constructing the layout. Is there any easy way to avoid this? The only
way that I could think of is to make it an extension which is overkill
atm (However I suspect by the time I reach the 20 or so subprojects it
may be worthwhile).

So is this the way you should do this sort of thing? Or any other suggestions?

-- 
Cheers,

Peter Donald

Re: Layouts customized by project attributes

Posted by Peter Donald <pe...@realityforge.org>.
> Have you considered this simple solution?
>
> def define_with_central_layout(name, &block)
>  define(name, :layout => CentralLayout.new(name), &block)
> end

/me slaps forehead.

err yep - that will work - thanks!

-- 
Cheers,

Peter Donald

Re: Layouts customized by project attributes

Posted by Alex Boisvert <al...@gmail.com>.
On Fri, Mar 19, 2010 at 4:49 PM, Peter Donald <pe...@realityforge.org>wrote:

> Hi,
>
> My current usage of buildr has resulted in many subprojects. Some of
> these subprojects actually have no files as they simply repackaging
> jars as OSGi bundles (i.e. jars + more metadata). So I have decided to
> have a central "target" directory under which each project has a dir.
> i.e. The following snippet uses target/A and target/B as intermediate
> dirs of each project
>
> class CentralLayout < Layout::Default
>  def initialize(key, top_level = false)
>    super()
>    prefix = top_level ? '' : '../'
>    self[:target] = "#{prefix}target/#{key}"
>    self[:target, :main] = "#{prefix}target/#{key}"
>  end
> end
>
> define 'A', :layout => CentralLayout.new('A', true) do
>   ...
>   define 'B', :layout => CentralLayout.new('B') do
>      ...
>   end
> end
>
> Unfortunately I have to repeat the name of the project when
> constructing the layout. Is there any easy way to avoid this? The only
> way that I could think of is to make it an extension which is overkill
> atm (However I suspect by the time I reach the 20 or so subprojects it
> may be worthwhile).
>

Have you considered this simple solution?

def define_with_central_layout(name, &block)
  define(name, :layout => CentralLayout.new(name), &block)
end

alex

Re: Layouts customized by project attributes

Posted by Peter Donald <pe...@realityforge.org>.
On Sat, Mar 20, 2010 at 11:14 AM, Antoine Toulme
<an...@lunar-ocean.com> wrote:
> Ruby can use instance_eval to do this.
>
> Use instance_eval inside a method to which you pass arguments to define
> projects dynamically.

Ok. I don't understand what you are suggesting. What I am trying to do
is have the layout customize itself based on the project to which it
is assigned. To do this the layout object needs to have access to the
project or the project needs to be able to decorate the layout when it
is assigned.

It may be simpler to describe it in a patch (that is untested and
really just to illustrate an idea)

diff --git a/lib/buildr/core/project.rb b/lib/buildr/core/project.rb
index 2cde99b..fd1a053 100644
--- a/lib/buildr/core/project.rb
+++ b/lib/buildr/core/project.rb
@@ -439,7 +439,8 @@ module Buildr

     # Returns the layout associated with this project.
     def layout
-      @layout ||= (parent ? parent.layout : Layout.default).clone
+      self.layout = (parent ? parent.layout : Layout.default).clone
unless @layout
+      @layout
     end

     # :call-seq:
@@ -621,6 +622,9 @@ module Buildr
     def layout=(layout)
       raise 'Cannot set directory layout twice, or after reading its
value' if @layout
       @layout = layout.is_a?(Class) ? layout.new : layout
+      assign_method = "project=".to_sym
+      @layout.send(assign_method, self) if @layout.respond_to? assign_method
+      @layout
     end

     # :call-seq:

-- 
Cheers,

Peter Donald

Re: Layouts customized by project attributes

Posted by Antoine Toulme <an...@lunar-ocean.com>.
Ruby can use instance_eval to do this.

Use instance_eval inside a method to which you pass arguments to define
projects dynamically.

If you are repackaging jars as OSGi, don't forget to check out Eclipse
Orbit!

Thanks,

Antoine

On Fri, Mar 19, 2010 at 16:49, Peter Donald <pe...@realityforge.org> wrote:

> Hi,
>
> My current usage of buildr has resulted in many subprojects. Some of
> these subprojects actually have no files as they simply repackaging
> jars as OSGi bundles (i.e. jars + more metadata). So I have decided to
> have a central "target" directory under which each project has a dir.
> i.e. The following snippet uses target/A and target/B as intermediate
> dirs of each project
>
> class CentralLayout < Layout::Default
>  def initialize(key, top_level = false)
>    super()
>    prefix = top_level ? '' : '../'
>    self[:target] = "#{prefix}target/#{key}"
>    self[:target, :main] = "#{prefix}target/#{key}"
>  end
> end
>
> define 'A', :layout => CentralLayout.new('A', true) do
>   ...
>   define 'B', :layout => CentralLayout.new('B') do
>      ...
>   end
> end
>
> Unfortunately I have to repeat the name of the project when
> constructing the layout. Is there any easy way to avoid this? The only
> way that I could think of is to make it an extension which is overkill
> atm (However I suspect by the time I reach the 20 or so subprojects it
> may be worthwhile).
>
> So is this the way you should do this sort of thing? Or any other
> suggestions?
>
> --
> Cheers,
>
> Peter Donald
>