Author: donaldp
Date: Sun Oct 30 11:49:16 2011
New Revision: 1195152

URL: http://svn.apache.org/viewvc?rev=1195152&view=rev
Log:
Update the jdepend extension to make it parameterizable like the other analysis 
extensions. Update the documetation to reflect this change

Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/addon/buildr/jdepend.rb
    buildr/trunk/doc/more_stuff.textile

Modified: buildr/trunk/CHANGELOG
URL: 
http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=1195152&r1=1195151&r2=1195152&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Sun Oct 30 11:49:16 2011
@@ -1,4 +1,7 @@
 1.4.7 (Pending)
+* Change: Make it possible to parameterize the JDepend extension and control 
the projects that
+          are included in the analysis and to enable support for loading a per 
project
+          jdepend.properties.
 * Added:  Add a Checkstyle extension.
 * Added:  Add a JavaNCSS extension.
 * Added:  Add a PMD extension.

Modified: buildr/trunk/addon/buildr/jdepend.rb
URL: 
http://svn.apache.org/viewvc/buildr/trunk/addon/buildr/jdepend.rb?rev=1195152&r1=1195151&r2=1195152&view=diff
==============================================================================
--- buildr/trunk/addon/buildr/jdepend.rb (original)
+++ buildr/trunk/addon/buildr/jdepend.rb Sun Oct 30 11:49:16 2011
@@ -14,43 +14,161 @@
 # the License.
 
 module Buildr
-
-  # Addes the <code>jdepend:swing</code>, <code>jdepend:text</code> and 
<code>jdepend:xml</code> tasks.
+  # Addes the <code>projectname:jdepend:swing</code>, 
<code>projectname:jdepend:text</code> and
+  # <code>projectname:jdepend:xml</code> tasks.
+  #
   # Require explicitly using <code>require "buildr/jdepend"</code>.
-  module Jdepend
-
-    REQUIRES = ["jdepend:jdepend:jar:2.9.1"]
+  module JDepend
 
     class << self
 
-      def requires()
-        @requires ||= Buildr.artifacts(REQUIRES).each(&:invoke).map(&:to_s)
+      # The specs for requirements
+      def dependencies
+        [
+          'jdepend:jdepend:jar:2.9.1'
+        ]
       end
 
-      def paths()
-        Project.projects.map(&:compile).each(&:invoke).map(&:target).
-          map(&:to_s).select { |path| File.exist?(path) }.map { |path| 
File.expand_path(path) }
+      def jdepend(output_file, target_paths, options = {})
+        dependencies = (options[:dependencies] || []) + self.dependencies
+        cp = Buildr.artifacts(dependencies).each(&:invoke).map(&:to_s)
+
+        args = []
+        if output_file
+          args << "-file"
+          args << output_file
+        end
+        target_paths.each do |target_path|
+          file(target_path).invoke
+          args << target_path
+        end
+
+        # If no output file then we must be trying to run the swing app
+        command = output_file ? 'jdepend.xmlui.JDepend' : 
'jdepend.swingui.JDepend'
+
+        begin
+          Java::Commands.java command, *(args + [{:classpath => cp, 
:properties => options[:properties], :java_args => options[:java_args]}])
+        rescue => e
+          raise e if options[:fail_on_error]
+        end
       end
-
     end
 
-    namespace "jdepend" do
+    class Config
+      def enabled?
+        !!@enabled
+      end
+
+      attr_writer :enabled
 
-      desc "Runs JDepend on all your projects (Swing UI)"
-      task "swing" do
-        Java::Commands.java "jdepend.swingui.JDepend", paths, 
:classpath=>requires, :name=>"JDepend"
+      def html_enabled?
+        File.exist?(self.style_file)
       end
 
-      desc "Runs JDepend on all your projects (Text UI)"
-      task "text" do
-        Java::Commands.java "jdepend.textui.JDepend", paths, 
:classpath=>requires, :name=>"JDepend"
+      attr_writer :config_directory
+
+      def config_directory
+        @config_directory || project._(:source, :main, :etc, :jdepend)
       end
 
-      desc "Runs JDepend on all your projects (XML output to jdepend.xml)"
-      task "xml" do
-        Java::Commands.java "jdepend.xmlui.JDepend", "-file", "jdepend.xml", 
paths, :classpath=>requires, :name=>"JDepend"
-        puts "Created jdepend.xml"
+      attr_writer :report_dir
+
+      def report_dir
+        @report_dir || project._(:reports, :jdepend)
+      end
+
+      attr_writer :fail_on_error
+
+      def fail_on_error?
+        @fail_on_error.nil? ? false : @fail_on_error
+      end
+
+      attr_writer :xml_output_file
+
+      def xml_output_file
+        @xml_output_file || "#{self.report_dir}/jdepend.xml"
+      end
+
+      attr_writer :html_output_file
+
+      def html_output_file
+        @html_output_file || "#{self.report_dir}/jdepend.html"
+      end
+
+      attr_writer :style_file
+
+      def style_file
+        @style_file || "#{self.config_directory}/jdepend.xsl"
+      end
+
+      def target_paths
+        @target_paths ||= [self.project.compile.target, 
self.project.test.compile.target]
+      end
+
+      def to_options
+        {
+            :fail_on_error => project.jdepend.fail_on_error?,
+            # Set user home so that jdepend.properties will be loaded from 
there if present
+            :properties => { 'user.home' => project.jdepend.config_directory }
+        }
+      end
+
+      protected
+
+      def initialize(project)
+        @project = project
+      end
+
+      attr_reader :project
+
+    end
+
+    module ProjectExtension
+      include Extension
+
+      def jdepend
+        @jdepend ||= Buildr::JDepend::Config.new(project)
+      end
+
+      after_define do |project|
+        if project.jdepend.enabled?
+          desc "Generate JDepend xml report."
+          project.task("jdepend:xml") do
+            puts "JDepend: Analyzing source code..."
+            mkdir_p File.dirname(project.jdepend.xml_output_file)
+            Buildr::JDepend.jdepend(project.jdepend.xml_output_file,
+                                    
project.jdepend.target_paths.flatten.compact,
+                                    project.jdepend.to_options)
+          end
+
+          desc "Run JDepend with Swing UI."
+          project.task("jdepend:swing") do
+            puts "JDepend: Analyzing source code..."
+            Buildr::JDepend.jdepend(nil,
+                                    
project.jdepend.target_paths.flatten.compact,
+                                    project.jdepend.to_options)
+          end
+
+          if project.jdepend.html_enabled?
+            xml_task = project.task("jdepend:xml")
+            desc "Generate JDepend html report."
+            project.task("jdepend:html" => xml_task) do
+              puts "JDepend: Generating report"
+              mkdir_p File.dirname(project.jdepend.html_output_file)
+              Buildr.ant "jdepend" do |ant|
+                ant.xslt :in => project.jdepend.xml_output_file,
+                         :out => project.jdepend.html_output_file,
+                         :style => project.jdepend.style_file
+              end
+            end
+
+          end
+        end
       end
     end
   end
 end
+
+class Buildr::Project
+  include Buildr::JDepend::ProjectExtension
+end

Modified: buildr/trunk/doc/more_stuff.textile
URL: 
http://svn.apache.org/viewvc/buildr/trunk/doc/more_stuff.textile?rev=1195152&r1=1195151&r2=1195152&view=diff
==============================================================================
--- buildr/trunk/doc/more_stuff.textile (original)
+++ buildr/trunk/doc/more_stuff.textile Sun Oct 30 11:49:16 2011
@@ -738,7 +738,7 @@ h4. Buildr plugin for IDEA
 
 Also, check out the "Buildr plugin for 
IDEA":http://www.digitalsanctum.com/buildr-plug-in/ (IDEA 7 and later).  Once 
installed, open your project with IDEA.  If IDEA finds that you have Buildr 
installed and finds a buildfile in the project's directory, it will show all 
the tasks available for that project.  To run a task, double-click it.  When 
the task completes, IDEA will show the results in the Buildr Output window.
 
-h2(#cobertura_emma_jdepend). Cobertura, Emma, JDepend
+h2(#cobertura_emma). Cobertura, Emma
 
 You can use "Cobertura":http://cobertura.sourceforge.net/ or 
"Emma":http://emma.sourceforge.net/ to instrument your code, run the tests and 
create a test coverage report in either HTML or XML format.
 
@@ -783,20 +783,11 @@ end
 
 The @cobertura:check@ task supports all of the configuration parameters 
allowed by the @cobertura-check@ Ant task (as "documented 
here":http://cobertura.sourceforge.net/anttaskreference.html).  Configuration 
parameters are "Ruby-ized" (as demonstrated in the example above).
 
-You can use "JDepend":http://clarkware.com/software/JDepend.html on to 
generate design quality metrics.  There are three tasks this time, the eye 
candy one:
-
-{% highlight sh %}
-$ buildr jdepend:swing
-{% endhighlight %}
-
-The other two tasks are @jdepend:text@ and @jdepend:xml@.
-
 We want Buildr to load fast, and not everyone cares for these tasks, so we 
don't include them by default.  If you want to use one of them, you need to 
require it explicitly.  The proper way to do it in Ruby:
 
 {% highlight ruby %}
 require 'buildr/java/cobertura'
 require 'buildr/java/emma'
-require 'buildr/jdepend'
 {% endhighlight %}
 
 You may want to add those to the Buildfile.  Alternatively, you can use these 
tasks for all your projects without modifying the Buildfile.  One convenient 
method is to add these lines to the @buildr.rb@ file in the @.buildr@ directory 
under your home directory.
@@ -804,7 +795,6 @@ You may want to add those to the Buildfi
 Another option is to require it from the command line (@--require@ or @-r@), 
for example:
 
 {% highlight sh %}
-$ buildr --require buildr/jdepend jdepend:swing
 $ buildr -rbuildr/java/cobertura cobertura:html
 {% endhighlight %}
 
@@ -856,6 +846,29 @@ The extension will include the source an
 
 By default javancss will look for all configuration files in the 
src/main/etc/javancss directory but this can be overriden by the setting the 
"javancss.config_directory" parameter. The "javancss:xml" task will be defined 
if the "javancss.enabled" property is set to true. If the xsl file named 
"javancss2html.xsl" is present in the configuration directory then a 
"javancss:html" task will be defined. The name of the xsl file can be 
overridden by the parameter "javancss.style_file".
 
+h2(#jdepend). JDepend
+
+"JDepend":http://clarkware.com/software/JDepend.html is integrated into Buildr 
through an extension. The extension adds the "jdepend:xml" task that generates 
an xml report, "jdepend:swing" that shows a Swing UI, and may add a 
"jdepend:html" task if an appropriate xsl is present. A typical project that 
uses the extension may look something like;
+
+{% highlight ruby %}
+require 'buildr/jdepend'
+
+define "foo" do
+  project.version = "1.0.0"
+
+  define "bar" do ... end
+
+  jdepend.enabled = true
+  jdepend.config_directory = _('etc/jdepend')
+  jdepend.target_paths << project('bar').compile.target
+
+end
+{% endhighlight %}
+
+The extension will include the compiled source and test directories of the 
project when invoking the JDepend tool. These can be added to by the parameters 
"jdepend.target_paths".
+
+By default JDepend will look for all configuration files in the 
src/main/etc/jdepend directory but this can be overriden by the setting the 
"jdepend.config_directory" parameter. The "jdepend:xml" and "jdepend:swing" 
task will be defined if the "jdepend.enabled" property is set to true. If a 
"jdepend.properties" is included in the configuration directory then jdepend 
will load it during the analysis. If the xsl file named "jdepend.xsl" is 
present in the configuration directory then a "jdepend:html" task will be 
defined. The name of the xsl file can be overridden by the parameter 
"jdepend.style_file".
+
 h2(#pmd). PMD
 
 PMD is integrated into Buildr through an extension. The extension adds the 
"pmd:rule:xml" and "pmd:rule:html" tasks. A typical project that uses the 
extension may look something like;


Reply via email to