From b6de905ee44d39eb25dd8eba06426d4fac74229b Mon Sep 17 00:00:00 2001 From: Jordon Bedwell Date: Fri, 4 Dec 2015 06:46:44 -0600 Subject: [PATCH] Fix: #4219: Add CodeClimate Platform. --- .codeclimate.yml | 46 +++++++++++++++++++++++++++ .gitignore | 1 + .rubocop.yml | 27 ++++++++++++++++ Rakefile | 37 ++++++++++++++++++++++ lib/jekyll/utils.rb | 1 + lib/jekyll/utils/ansi.rb | 59 +++++++++++++++++++++++++++++++++++ lib/jekyll/utils/platforms.rb | 3 +- test/test_ansi.rb | 23 ++++++++++++++ 8 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 .codeclimate.yml create mode 100644 .rubocop.yml create mode 100644 lib/jekyll/utils/ansi.rb create mode 100644 test/test_ansi.rb diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 00000000..29ed62d0 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,46 @@ +engines: + rubocop: + enabled: true + checks: + Rubocop/Style/SpaceInsideBrackets: { enabled: false } + Rubocop/Style/BracesAroundHashParameters: { enabled: false} + Rubocop/Style/EmptyLinesAroundAccessModifier: { enabled: false } + Rubocop/Style/EmptyLinesAroundModuleBody: { enabled: false } + Rubocop/Lint/FormatParameterMismatch: { enabled: false } + Rubocop/Lint/UselessAccessModifier: { enabled: false } + Rubocop/Lint/AssignmentInCondition: { enabled: false } + Rubocop/Style/SpaceAroundOperators: { enabled: false } + Rubocop/Style/AlignParameters: { enabled: false } + Rubocop/Style/SignalException: { enabled: false } + Rubocop/Style/Documentation: { enabled: false } + Rubocop/Style/DoubleNegation: { enabled: false } + Rubocop/Style/UnneededCapitalW: { enabled: false } + Rubocop/Style/IfUnlessModifier: { enabled: false } + Rubocop/Style/RescueModifier: { enabled: false } + Rubocop/Style/RegexpLiteral: { enabled: false } + Rubocop/Style/GuardClause: { enabled: false } + Rubocop/Style/FileName: { enabled: false } + fixme: + enabled: false +exclude_paths: +- .rubocop.yml +- .codeclimate.yml +- .travis.yml +- .gitignore +- .rspec + +- Gemfile.lock +- CHANGELOG.md +- readme.md +- README.md +- Readme.md +- ReadMe.md +- COPYING +- LICENSE + +- test/**/* +- script/**/* +- spec/**/* +ratings: + paths: + - lib/**/* diff --git a/.gitignore b/.gitignore index d75a4982..6441a147 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ tmp/* .jekyll-metadata /vendor /test/source/file_name.txt +.analysis diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..66b8552d --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,27 @@ +Style/IndentHash: { EnforcedStyle: align_braces } +Style/StringLiteralsInInterpolation: { EnforcedStyle: double_quotes } +Style/StringLiterals: { EnforcedStyle: none } +Metrics/MethodLength: { Max: 24 } +Metrics/ClassLength: { Max: 240 } +Metrics/ModuleLength: { Max: 240 } +Metrics/LineLength: { Max: 112 } + +Style/HashSyntax: + UseHashRocketsWithSymbolValues: false + SupportedStyles: [ruby19 ruby19_no_mixed_keys, hash_rockets] + EnforcedStyle: ruby19 + +Style/MultilineOperationIndentation: + EnforcedStyle: indented + SupportedStyles: + - indented + +Style/PercentLiteralDelimiters: + PreferredDelimiters: + '%q': '{}' + '%Q': '{}' + '%r': '!!' + '%s': '()' + '%w': '()' + '%W': '()' + '%x': '()' diff --git a/Rakefile b/Rakefile index e189f0b9..3596270b 100644 --- a/Rakefile +++ b/Rakefile @@ -329,3 +329,40 @@ namespace :docs do sh "mv #{docs_name}-#{version}.gem pkg" end end + +task :analysis do + require "jekyll/utils/ansi" + require "open3" + + cmd = [ + "docker", "run", "--rm", "--env=CODE_PATH=#{Dir.pwd}", \ + "--volume='#{Dir.pwd}:/code'", "--volume=/var/run/docker.sock:/var/run/docker.sock", \ + "--volume=/tmp/cc:/tmp/cc", "-i", "codeclimate/codeclimate", "analyze" + ] + + ansi = Jekyll::Utils::Ansi + file = File.open(".analysis", "w+") + Open3.popen3(cmd.shelljoin) do |_, out, err, _| + while data = out.gets + file.write data + if data =~ /\A==/ + $stdout.print ansi.yellow(data) + + elsif data !~ %r!\A[0-9\-]+! + $stdout.puts data + + else + h, d = data.split(":", 2) + $stdout.print ansi.cyan(h) + $stdout.print ":", d + end + end + + while data = err.gets + file.write data + $stderr.print ansi.red(data) + end + end + + file.close +end diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index 7d2490a6..339c860a 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -1,6 +1,7 @@ module Jekyll module Utils extend self autoload :Platforms, 'jekyll/utils/platforms' + autoload :Ansi, "jekyll/utils/ansi" # Constants for use in #slugify SLUGIFY_MODES = %w{raw default pretty} diff --git a/lib/jekyll/utils/ansi.rb b/lib/jekyll/utils/ansi.rb new file mode 100644 index 00000000..05f38429 --- /dev/null +++ b/lib/jekyll/utils/ansi.rb @@ -0,0 +1,59 @@ +# Frozen-string-literal: true +# Copyright: 2015 Jekyll - MIT License +# Encoding: utf-8 + +module Jekyll + module Utils + module Ansi + extend self + + ESCAPE = format("%c", 27) + MATCH = /#{ESCAPE}\[(?:\d+)(?:;\d+)*(j|k|m|s|u|A|B|G)|\e\(B\e\[m/ix.freeze + COLORS = { + :red => 31, + :green => 32, + :black => 30, + :magenta => 35, + :yellow => 33, + :white => 37, + :blue => 34, + :cyan => 36 + } + + # Strip ANSI from the current string. It also strips cursor stuff, + # well some of it, and it also strips some other stuff that a lot of + # the other ANSI strippers don't. + + def strip(str) + str.gsub MATCH, "" + end + + # + + def has?(str) + !!(str =~ MATCH) + end + + # Reset the color back to the default color so that you do not leak any + # colors when you move onto the next line. This is probably normally + # used as part of a wrapper so that we don't leak colors. + + def reset(str = "") + @ansi_reset ||= format("%c[0m", 27) + "#{@ansi_reset}#{str}" + end + + # SEE: `self::COLORS` for a list of methods. They are mostly + # standard base colors supported by pretty much any xterm-color, we do + # not need more than the base colors so we do not include them. + # Actually... if I'm honest we don't even need most of the + # base colors. + + COLORS.each do |color, num| + define_method color do |str| + "#{format("%c", 27)}[#{num}m#{str}#{reset}" + end + end + end + end +end diff --git a/lib/jekyll/utils/platforms.rb b/lib/jekyll/utils/platforms.rb index 83858a07..abc1598c 100644 --- a/lib/jekyll/utils/platforms.rb +++ b/lib/jekyll/utils/platforms.rb @@ -1,6 +1,7 @@ module Jekyll module Utils - module Platforms extend self + module Platforms + extend self # Provides jruby? and mri? which respectively detect these two types of # tested Engines we support, in the future we might probably support the diff --git a/test/test_ansi.rb b/test/test_ansi.rb new file mode 100644 index 00000000..b209e237 --- /dev/null +++ b/test/test_ansi.rb @@ -0,0 +1,23 @@ +require "helper" + +class TestAnsi < JekyllUnitTest + context nil do + setup do + @subject = Jekyll::Utils::Ansi + end + + Jekyll::Utils::Ansi::COLORS.each do |color, val| + should "respond_to? #{color}" do + assert @subject.respond_to?(color) + end + end + + should "be able to strip colors" do + assert_equal @subject.strip(@subject.yellow(@subject.red("hello"))), "hello" + end + + should "be able to detect colors" do + assert @subject.has?(@subject.yellow("hello")) + end + end +end