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 2014/05/24 07:15:39 UTC

[2/2] git commit: Add csslint tasks css_lint:xml and css_lint:html that support source code analysis of CSS files.

Add csslint tasks css_lint:xml and css_lint:html that support source code analysis of CSS files.


Project: http://git-wip-us.apache.org/repos/asf/buildr/repo
Commit: http://git-wip-us.apache.org/repos/asf/buildr/commit/62503ff8
Tree: http://git-wip-us.apache.org/repos/asf/buildr/tree/62503ff8
Diff: http://git-wip-us.apache.org/repos/asf/buildr/diff/62503ff8

Branch: refs/heads/master
Commit: 62503ff875664df5e60cd29cc9555113d48c5e6e
Parents: be6c1e8
Author: Peter Donald <pe...@realityforge.org>
Authored: Sat May 24 15:15:23 2014 +1000
Committer: Peter Donald <pe...@realityforge.org>
Committed: Sat May 24 15:15:23 2014 +1000

----------------------------------------------------------------------
 CHANGELOG                        |   2 +
 addon/buildr/css_lint-report.xsl |  83 ++++++++++++++
 addon/buildr/css_lint.rake       | 196 ++++++++++++++++++++++++++++++++++
 doc/more_stuff.textile           |  23 ++++
 4 files changed, 304 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/buildr/blob/62503ff8/CHANGELOG
----------------------------------------------------------------------
diff --git a/CHANGELOG b/CHANGELOG
index 4ce053b..760c4d2 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -3,6 +3,8 @@
           of ruby by reordering blocks.
 * Change: Supply a default xsl file for generating the checkstyle
           report.
+* Added:  Add csslint tasks css_lint:xml and css_lint:html that
+          support source code analysis of CSS files.
 * Added:  Add scss_lint tasks scss_lint:xml and scss_lint:html that
           support source code analysis of SCSS files.
 * Added:  Import 'buildr/custom_pom' addon to make it easier to

http://git-wip-us.apache.org/repos/asf/buildr/blob/62503ff8/addon/buildr/css_lint-report.xsl
----------------------------------------------------------------------
diff --git a/addon/buildr/css_lint-report.xsl b/addon/buildr/css_lint-report.xsl
new file mode 100644
index 0000000..11f174e
--- /dev/null
+++ b/addon/buildr/css_lint-report.xsl
@@ -0,0 +1,83 @@
+<?xml version="1.0"?>
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+  <xsl:template match="/">
+    <html>
+      <head>
+        <title>CssLint Violations</title>
+      </head>
+      <body bgcolor="#FFFFEF">
+        <p>
+          <b>Coding Style Check Results</b>
+        </p>
+        <table border="1" cellspacing="0" cellpadding="2">
+          <tr bgcolor="#CC9966">
+            <th colspan="2">
+              <b>Summary</b>
+            </th>
+          </tr>
+          <tr bgcolor="#F3F3E1">
+            <td>Files with errors</td>
+            <td>
+              <xsl:number level="any" value="count(descendant::file)"/>
+            </td>
+          </tr>
+          <tr bgcolor="#CCF3D0">
+            <td>Total errors</td>
+            <td>
+              <xsl:number level="any" value="count(descendant::issue)"/>
+            </td>
+          </tr>
+        </table>
+        <hr align="left" width="95%" size="1"/>
+        <p>The following are violations of the ScssLint Rules:</p>
+        <p/>
+        <xsl:apply-templates/>
+      </body>
+    </html>
+  </xsl:template>
+
+  <xsl:template match="file[issue]">
+    <table bgcolor="#AFFFFF" width="95%" border="1" cellspacing="0" cellpadding="2">
+      <tr>
+        <th>File:</th>
+        <td>
+          <xsl:value-of select="@name"/>
+        </td>
+      </tr>
+    </table>
+    <table bgcolor="#DFFFFF" width="95%" border="1" cellspacing="0" cellpadding="2">
+      <tr>
+        <th style="width: 4em; padding: 0; margin: 0;">Line</th>
+        <th style="width: 4em; padding: 0; margin: 0;">Column</th>
+        <th style="width: 7em; padding: 0; margin: 0;">Severity</th>
+        <th>Reason</th>
+        <th>Evidence</th>
+      </tr>
+      <xsl:apply-templates select="issue"/>
+    </table>
+    <p/>
+  </xsl:template>
+
+  <xsl:template match="issue">
+    <tr>
+      <td>
+        <xsl:value-of select="@line"/>
+      </td>
+      <td>
+        <xsl:value-of select="@char"/>
+      </td>
+      <td>
+        <xsl:value-of select="@severity"/>
+      </td>
+      <td>
+        <xsl:value-of select="@reason"/>
+      </td>
+      <td>
+        <xsl:value-of select="@evidence"/>
+      </td>
+    </tr>
+  </xsl:template>
+
+</xsl:stylesheet>

http://git-wip-us.apache.org/repos/asf/buildr/blob/62503ff8/addon/buildr/css_lint.rake
----------------------------------------------------------------------
diff --git a/addon/buildr/css_lint.rake b/addon/buildr/css_lint.rake
new file mode 100644
index 0000000..3d4c587
--- /dev/null
+++ b/addon/buildr/css_lint.rake
@@ -0,0 +1,196 @@
+# 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>css_lint:html</code> and <code>css_lint:xml</code> tasks.
+  # Require explicitly using <code>require "buildr/css_lint"</code>.
+  module CssLint
+    class << self
+
+      def css_lint(output_file, source_paths, options = {})
+        args = []
+        args << 'csslint'
+        args << "--format=#{options[:format]}" if options[:format]
+        args << '--quiet'
+        [:errors, :warnings, :ignore].each do |severity|
+          if options[severity] && !options[severity].empty?
+            args << "--#{severity}=#{options[severity].join(',')}"
+          end
+        end
+        if options[:excludes] && !options[:excludes].empty?
+          args << "--exclude-list=#{options[:excludes].join(',')}"
+        end
+
+        source_paths.each do |source_path|
+          args << source_path.to_s
+        end
+
+        command = args.join(' ')
+        mkdir_p File.dirname(output_file)
+        File.open(output_file, 'wb') do |f|
+          f.write `#{command}`
+        end
+        if 0 != $?.exitstatus
+          error = IO.read(output_file)
+          rm_f output_file
+          raise "Problem running csslint: #{command}\n#{error}"
+        end
+      end
+    end
+
+    class Config
+      def enabled?
+        !self.source_paths.empty?
+      end
+
+      def html_enabled?
+        File.exist?(self.style_file)
+      end
+
+      attr_writer :config_directory
+
+      def config_directory
+        @config_directory || project._(:source, :main, :etc, :scss_lint)
+      end
+
+      attr_writer :report_dir
+
+      def report_dir
+        @report_dir || project._(:reports, :css_lint)
+      end
+
+      attr_writer :excludes
+
+      def excludes
+        @excludes ||= []
+      end
+
+      attr_writer :errors
+
+      def errors
+        @errors ||= []
+      end
+
+      attr_writer :warnings
+
+      def warnings
+        @warnings ||= []
+      end
+
+      attr_writer :ignore
+
+      def ignore
+        @ignore ||= []
+      end
+
+      attr_writer :format
+
+      def format
+        @format || 'csslint-xml'
+      end
+
+      attr_writer :xml_output_file
+
+      def xml_output_file
+        @xml_output_file || "#{self.report_dir}/css_lint.xml"
+      end
+
+      attr_writer :html_output_file
+
+      def html_output_file
+        @html_output_file || "#{self.report_dir}/css_lint.html"
+      end
+
+      attr_writer :style_file
+
+      def style_file
+        unless @style_file
+          project_xsl = "#{self.config_directory}/css_lint-report.xsl"
+          if File.exist?(project_xsl)
+            @style_file = project_xsl
+          else
+            @style_file = "#{File.dirname(__FILE__)}/css_lint-report.xsl"
+          end
+        end
+        @style_file
+      end
+
+      def source_paths
+        unless @source_paths
+          @source_paths = []
+          dir = self.project._(:source, :main, :webapp, :css)
+          @source_paths << dir if File.directory?(dir)
+        end
+        @source_paths
+      end
+
+      protected
+
+      def initialize(project)
+        @project = project
+      end
+
+      attr_reader :project
+
+    end
+
+    module ProjectExtension
+      include Extension
+
+      def css_lint
+        @css_lint ||= Buildr::CssLint::Config.new(project)
+      end
+
+      after_define do |project|
+        if project.css_lint.enabled?
+          desc 'Generate css-lint xml report.'
+          project.task('css_lint:xml') do
+            source_paths = project.css_lint.source_paths.flatten.compact
+            source_paths.each do |path|
+              path.respond_to?(:invoke) ? path.invoke : project.file(path).invoke
+            end
+
+            puts 'CssLint: Analyzing CSS...'
+            Buildr::CssLint.css_lint(project.css_lint.xml_output_file,
+                                     source_paths,
+                                     :format => project.css_lint.format,
+                                     :excludes => project.css_lint.excludes,
+                                     :ignore => project.css_lint.ignore,
+                                     :warnings => project.css_lint.warnings,
+                                     :errors => project.css_lint.errors)
+          end
+
+          if project.css_lint.html_enabled?
+            xml_task = project.task('css_lint:xml')
+            desc 'Generate css_lint html report.'
+            project.task('css_lint:html' => xml_task) do
+              puts "CssLint: Generating report"
+              mkdir_p File.dirname(project.css_lint.html_output_file)
+              Buildr.ant 'css_lint' do |ant|
+                ant.xslt :in => project.css_lint.xml_output_file,
+                         :out => project.css_lint.html_output_file,
+                         :style => project.css_lint.style_file
+              end
+            end
+          end
+        end
+      end
+    end
+  end
+end
+
+class Buildr::Project
+  include Buildr::CssLint::ProjectExtension
+end

http://git-wip-us.apache.org/repos/asf/buildr/blob/62503ff8/doc/more_stuff.textile
----------------------------------------------------------------------
diff --git a/doc/more_stuff.textile b/doc/more_stuff.textile
index dbb5c8f..d453dc3 100644
--- a/doc/more_stuff.textile
+++ b/doc/more_stuff.textile
@@ -1059,6 +1059,29 @@ The method compile_jaxb accepts either an array of files or a single file as the
 * <tt>:keep_content</tt>: By default the generated directory will be deleted. If <tt>true</tt> is specified for this parameter the directory will not be deleted.
 * <tt>:package</tt>: The package in which the source is generated.
 
+h2(#css_lint). CssLint
+
+"CssLint":https://github.com/CSSLint/csslint is integrated into Buildr through an extension. The extension adds the "css_lint:xml" task to generate an xml report listing css lint violations and a "css_lint:html" task for a html variant of the same data. It is expected that a project that makes use of css linting will have installed the csslint using node. A typical project that uses the extension may look something like;
+
+{% highlight ruby %}
+require 'buildr/css_lint'
+
+define "foo" do
+  project.version = "1.0.0"
+
+  define "bar" do ... end
+
+  css_lint.source_paths.concat([some_generated_dir])
+  css_lint.ignore.concat(%w(box-sizing font-sizes adjoining-classes))
+end
+{% endhighlight %}
+
+By default css_lint will look for the xsl file in the src/main/etc/css_lint directory but this can be overriden by the setting the "css_lint.config_directory" parameter. The "css_lint:xml" task will be defined if the source_paths is not empty. The rules can be passed to the task using the 'ignores', 'errors' and 'warnings' parameters.
+
+The extension will lint the css files in the "_(:source, :main, :webapp, :css)" directory by default. The set of source directories linted can be controlled by the "css_lint.source_paths" parameter.
+
+If the xsl file named "css_lint-report.xsl" is present in the configuration directory then that will be used in the "css_lint:html" task otherwise a default xsl included with buildr will be used. The name of the xsl file can be overridden by the parameter "css_lint.style_file".
+
 h2(#scss_lint). ScssLint
 
 "ScssLint":https://github.com/causes/scss-lint is integrated into Buildr through an extension. The extension adds the "scss_lint:xml" task to generate an xml report listing scss lint violations and a "scss_lint:html" task for a html variant of the same data. A buildr project that makes uses of the extension is expected to have added 'scss_lint' gem to the projects Gemfile by adding a line such as;