You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by do...@apache.org on 2011/10/29 02:13:50 UTC

svn commit: r1190700 - in /buildr/trunk: CHANGELOG addon/buildr/javancss.rake doc/more_stuff.textile

Author: donaldp
Date: Sat Oct 29 00:13:49 2011
New Revision: 1190700

URL: http://svn.apache.org/viewvc?rev=1190700&view=rev
Log:
Add in a javancss extension and some basic documentation

Added:
    buildr/trunk/addon/buildr/javancss.rake
Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/doc/more_stuff.textile

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=1190700&r1=1190699&r2=1190700&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Sat Oct 29 00:13:49 2011
@@ -1,5 +1,6 @@
 1.4.7 (Pending)
 * Added:  Add a Checkstyle extension.
+* Added:  Add a JavaNCSS extension.
 * Change: Parameterize the the directory where the top level cobertura tasks will generate
           reports. Specify using Buildr::Cobertura.report_dir = '....'
 * Fixed:  BUILDR-611 Buildr should not unnecessarily recompile Java files

Added: buildr/trunk/addon/buildr/javancss.rake
URL: http://svn.apache.org/viewvc/buildr/trunk/addon/buildr/javancss.rake?rev=1190700&view=auto
==============================================================================
--- buildr/trunk/addon/buildr/javancss.rake (added)
+++ buildr/trunk/addon/buildr/javancss.rake Sat Oct 29 00:13:49 2011
@@ -0,0 +1,155 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership.  The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+module Buildr
+  # Provides the <code>javancss:html</code> and <code>javancss:xml</code> tasks.
+  # Require explicitly using <code>require "buildr/javancss"</code>.
+  module JavaNCSS
+
+    class << self
+
+      # The specs for requirements
+      def dependencies
+        [
+          'org.codehaus.javancss:javancss:jar:32.53',
+          'javancss:ccl:jar:29.50',
+          'javancss:jhbasic:jar:29.50'
+        ]
+      end
+
+      def javancss(output_file, source_paths, options = {})
+        dependencies = (options[:dependencies] || []) + self.dependencies
+        cp = Buildr.artifacts(dependencies).each(&:invoke).map(&:to_s)
+
+        args = []
+        args << "-all"
+        args << "-xml"
+        args << "-out"
+        args << output_file
+        args << "-recursive"
+        source_paths.each do |source_path|
+          args << source_path
+        end
+
+        begin
+          Java::Commands.java 'javancss.Main', *(args + [{:classpath => cp, :properties => options[:properties], :java_args => options[:java_args]}])
+        rescue => e
+          raise e if options[:fail_on_error]
+        end
+      end
+    end
+
+    class Config
+      def enabled?
+        !!@enabled
+      end
+
+      attr_writer :enabled
+
+      def html_enabled?
+        File.exist?(self.style_file)
+      end
+
+      attr_writer :config_directory
+
+      def config_directory
+        @config_directory || project._(:source, :main, :etc, :javancss)
+      end
+
+      attr_writer :report_dir
+
+      def report_dir
+        @report_dir || project._(:reports, :javancss)
+      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}/javancss.xml"
+      end
+
+      attr_writer :html_output_file
+
+      def html_output_file
+        @html_output_file || "#{self.report_dir}/javancss.html"
+      end
+
+      attr_writer :style_file
+
+      def style_file
+        @style_file || "#{self.config_directory}/javancss2html.xsl"
+      end
+
+      def source_paths
+        @source_paths ||= [self.project.compile.sources, self.project.test.compile.sources]
+      end
+
+      protected
+
+      def initialize(project)
+        @project = project
+      end
+
+      attr_reader :project
+
+    end
+
+    module ProjectExtension
+      include Extension
+
+      def javancss
+        @javancss ||= Buildr::JavaNCSS::Config.new(project)
+      end
+
+      after_define do |project|
+        if project.javancss.enabled?
+          desc "Generate JavaNCSS xml report."
+          project.task("javancss:xml") do
+            puts "JavaNCSS: Analyzing source code..."
+            mkdir_p File.dirname(project.javancss.xml_output_file)
+            Buildr::JavaNCSS.javancss(project.javancss.xml_output_file,
+                                      project.javancss.source_paths.flatten.compact,
+                                      :fail_on_error => project.javancss.fail_on_error?)
+          end
+
+          if project.javancss.html_enabled?
+            xml_task = project.task("javancss:xml")
+            desc "Generate JavaNCSS html report."
+            project.task("javancss:html" => xml_task) do
+              puts "JavaNCSS: Generating report"
+              mkdir_p File.dirname(project.javancss.html_output_file)
+              Buildr.ant "javancss" do |ant|
+                ant.xslt :in => project.javancss.xml_output_file,
+                         :out => project.javancss.html_output_file,
+                         :style => project.javancss.style_file
+              end
+            end
+
+          end
+        end
+      end
+    end
+  end
+end
+
+class Buildr::Project
+  include Buildr::JavaNCSS::ProjectExtension
+end

Modified: buildr/trunk/doc/more_stuff.textile
URL: http://svn.apache.org/viewvc/buildr/trunk/doc/more_stuff.textile?rev=1190700&r1=1190699&r2=1190700&view=diff
==============================================================================
--- buildr/trunk/doc/more_stuff.textile (original)
+++ buildr/trunk/doc/more_stuff.textile Sat Oct 29 00:13:49 2011
@@ -833,6 +833,29 @@ The extension will include the source an
 
 If the xsl file named "checkstyle-report.xsl" is present in the configuration directory then a "checkstyle:html" task will be defined. The name of the xsl file can be overridden by the parameter "checkstyle.style_file".
 
+h2(#javancss). JavaNCSS
+
+Checkstyle is integrated into Buildr through an extension. The extension adds the "javancss:xml" task that generates an xml report and may add a "javancss:html" task if an appropriate xsl is preset. A typical project that uses the extension may look something like;
+
+{% highlight ruby %}
+require 'buildr/javancss'
+
+define "foo" do
+  project.version = "1.0.0"
+
+  define "bar" do ... end
+
+  javancss.enabled = true
+  javancss.config_directory = _('etc/javancss')
+  javancss.source_paths << project('bar')._(:source, :main, :java)
+
+end
+{% endhighlight %}
+
+The extension will include the source and test directories of the project when invoking the javancss tool. These can be added to by the parameters "javancss.source_paths".
+
+By default checkstyle 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(#jaxb_xjc). JAXB Xjc Compiler
 
 Buildr includes an extension that provides the ability to invoke jaxb xjc binding compiler. A typical project that uses the extension may look something like;