java-checkstyle-precommit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7f3fe79c21002849f9b84e4293fe51e891a993edb0f1644e5d767cab0a73767d
4
+ data.tar.gz: f765cecde7bba9ecb8e01562c6ac44b55b7664ae303fefa27fbbca356fd1c3a8
5
+ SHA512:
6
+ metadata.gz: 9480d6b281ff399883223ad8e89c234c29fc75070e003bb8041c4cf2f22b60bc5cf7692f88b691a266d617e95b141e8a002d55407ba4aff805cd4521accfbbab
7
+ data.tar.gz: d7e68ee0e2bef54be64402bbab5e05c126c38f9c38f6322452ca102321a029f28f7eb735965767ac54a39162a73c4fa792f74b4363407f48dcf8b700026abb8d
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pre-commit/checks/shell'
4
+ require_relative '../message/extractor'
5
+ require_relative '../message/formatter'
6
+ require_relative '../support/path'
7
+
8
+ module PreCommit
9
+ module Checks
10
+ ##
11
+ # Plugin implementation for pre-commit gem
12
+ #
13
+ # It provides a java checkstyle validation using checkstyle.jar
14
+ # for details see:
15
+ # lib/pre_commit/support/checkstyle
16
+ class Checkstyle < Shell
17
+ ##
18
+ # Function called after pre-commit execution
19
+ # this method receive the +staged_files+ from git
20
+ #
21
+ # @param [String] Standard git ouput with staged files
22
+ def call(staged_files)
23
+ staged_files = staged_files.grep(/\.java$/)
24
+ return if staged_files.empty?
25
+
26
+ output = execute(args(staged_files))
27
+ format(extract(output))
28
+ end
29
+
30
+ def self.description
31
+ 'Runs Checkstyle linter for java code.'
32
+ end
33
+
34
+ private
35
+
36
+ def args(staged_files)
37
+ [
38
+ 'java',
39
+ checkstyle_jar,
40
+ configuration_file,
41
+ staged_files,
42
+ output_format
43
+ ]
44
+ end
45
+
46
+ def checkstyle_jar
47
+ ['-jar', Support::Path.relative_to('checkstyle-6.11-all.jar')]
48
+ end
49
+
50
+ def configuration_file
51
+ config_file ? ['-c', config_file] : []
52
+ end
53
+
54
+ def output_format
55
+ ['-f', 'xml']
56
+ end
57
+
58
+ def alternate_config_file
59
+ Support::Path.relative_to('google_checks.xml')
60
+ end
61
+
62
+ def format(errors)
63
+ @formatter ||= PreCommit::Message::Formatter.new
64
+ @formatter.format errors
65
+ end
66
+
67
+ def extract(data)
68
+ @extractor ||= PreCommit::Message::Extractor.new
69
+ @extractor.extract data
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Domain
4
+ ##
5
+ # Represents a file with style errors.
6
+ #
7
+ class BadFile
8
+ attr_reader :name, :errors
9
+
10
+ ##
11
+ # Instanciate a default bad file.
12
+ #
13
+ # @param [String] The File name
14
+ # @param [Array] The errors
15
+ def initialize(name, errors)
16
+ @name = name
17
+ @errors = errors
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Domain
4
+ ##
5
+ # Represents the Checkstyle
6
+ # @param [Array] of bad files
7
+ class Checkstyle
8
+ attr_reader :bad_files
9
+
10
+ def initialize(bad_files)
11
+ @bad_files = bad_files
12
+ end
13
+
14
+ ##
15
+ # A good checkstyle means no errors.
16
+ #
17
+ def good?
18
+ @bad_files.nil? || @bad_files.empty?
19
+ end
20
+
21
+ ##
22
+ # Factory for Checkstyle without errors
23
+ def self.good
24
+ Domain::Checkstyle.new(nil)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'crack'
4
+ require_relative '../domain/checkstyle'
5
+ require_relative '../domain/bad_file'
6
+
7
+ module PreCommit
8
+ module Message
9
+ ##
10
+ # Responsible for extract error messages from terminal output
11
+ class Extractor
12
+ ##
13
+ # Extract data from a XML formatted +terminal_output+
14
+ #
15
+ # @param terminal_output [String] XML formatted terminal ouput
16
+ # @return [Domain::Checkstyle] The checkstyle
17
+ def extract(terminal_output)
18
+ if blank?(terminal_output) ||
19
+ blank?(xml_content_of(terminal_output))
20
+ return Domain::Checkstyle.good
21
+ end
22
+
23
+ xml_data = Crack::XML.parse(xml_content_of(terminal_output))
24
+ files = xml_data['checkstyle']['file']
25
+
26
+ Domain::Checkstyle.new(extract_bad_file(files))
27
+ end
28
+
29
+ private
30
+
31
+ def xml_content_of(raw_output)
32
+ raw_output[/<(.*)>/m]
33
+ end
34
+
35
+ def extract_bad_file(xml_files)
36
+ return [bad_file(xml_files)] unless xml_files.is_a? Array
37
+ xml_files.map { |e| bad_file(e) }
38
+ end
39
+
40
+ def bad_file(file)
41
+ Domain::BadFile.new(file['name'], extract_errors(file))
42
+ end
43
+
44
+ def extract_errors(file)
45
+ return [] if blank? file['error']
46
+ return [file['error']] unless file['error'].is_a? Array
47
+ file['error']
48
+ end
49
+
50
+ def blank?(value)
51
+ value.nil? || value.empty?
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PreCommit
4
+ module Message
5
+ ##
6
+ # Responsible for format a given output
7
+ class Formatter
8
+ ##
9
+ # Format output for a given +errors+ details
10
+ #
11
+ # @param checkstyle [Domain::Checkstyle] Checkstyle details
12
+ # @return [String] formatted output or nil when has no errors
13
+ # @raise ArgumentError when input is empty
14
+ #
15
+ def format(checkstyle)
16
+ throw ArgumentError.new if checkstyle.nil?
17
+ return nil if checkstyle.good?
18
+
19
+ format_multiple(checkstyle.bad_files)
20
+ end
21
+
22
+ private
23
+
24
+ def format_multiple(files)
25
+ files.reduce('') { |a, e| a + format_single(e) }
26
+ end
27
+
28
+ def format_single(bad_file)
29
+ "File errors: #{bad_file.name} \n" + format_errors(bad_file.errors)
30
+ end
31
+
32
+ def format_errors(errors)
33
+ errors.reduce('') { |a, e| a + line(e) }
34
+ end
35
+
36
+ def line(error)
37
+ " line: #{error['line']}:#{error['column']}"\
38
+ " error: #{error['message']}\n"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Support
4
+ ##
5
+ # Responsible for provide relative paths to support files
6
+ # Files located under: /lib/plugin/pre_commit/support/checkstyle*
7
+ module Path
8
+ ##
9
+ # Return support path relative to a given +file+
10
+ #
11
+ # @param file [String] file name
12
+ # @return [String] formatted path
13
+ def self.relative_to(file)
14
+ File.expand_path(
15
+ "../../../../resources/checkstyle/#{file}",
16
+ __FILE__
17
+ )
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # This is a fork version of the original
5
+ # checkstyle:
6
+ # https://github.com/secondrotation/pre-commit-checkstyle
7
+ #
8
+ module PreCommit
9
+ # Main file: checks/checkstyle.rb
10
+ module Checkstyle
11
+ VERSION = '1.0.0'
12
+ end
13
+ end
@@ -0,0 +1,44 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE module PUBLIC
3
+ "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
4
+ "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
5
+
6
+ <module name = "Checker">
7
+ <property name="charset" value="UTF-8"/>
8
+
9
+ <property name="fileExtensions" value="java, properties, xml"/>
10
+ <!-- Checks for whitespace -->
11
+ <!-- See http://checkstyle.sf.net/config_whitespace.html -->
12
+ <module name="FileTabCharacter">
13
+ <property name="eachLine" value="true"/>
14
+ </module>
15
+
16
+ <module name="TreeWalker">
17
+
18
+ <module name="NeedBraces"/>
19
+ <module name="WhitespaceAround">
20
+ <property name="allowEmptyConstructors" value="true"/>
21
+ <property name="allowEmptyMethods" value="true"/>
22
+ <property name="allowEmptyTypes" value="true"/>
23
+ <property name="allowEmptyLoops" value="true"/>
24
+ <message key="ws.notFollowed"
25
+ value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
26
+ <message key="ws.notPreceded"
27
+ value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>
28
+ </module>
29
+ <module name="EmptyLineSeparator">
30
+ <property name="allowNoEmptyLineBetweenFields" value="true"/>
31
+ </module>
32
+ <module name="Indentation">
33
+ <property name="basicOffset" value="2"/>
34
+ <property name="braceAdjustment" value="0"/>
35
+ <property name="caseIndent" value="2"/>
36
+ <property name="throwsIndent" value="4"/>
37
+ <property name="lineWrappingIndentation" value="4"/>
38
+ <property name="arrayInitIndent" value="2"/>
39
+ </module>
40
+ <module name="EmptyCatchBlock">
41
+ <property name="exceptionVariableName" value="expected"/>
42
+ </module>
43
+ </module>
44
+ </module>
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: java-checkstyle-precommit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aravind Selvamani
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pre-commit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.26'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.26'
27
+ - !ruby/object:Gem::Dependency
28
+ name: crack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.4'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 10.4.2
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '10.4'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 10.4.2
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 3.3.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 3.3.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 0.52.1
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.52.1
103
+ description: Checkstyle linter plugin for pre-commit. Useful for linting Java code.
104
+ email:
105
+ - aaravind281096@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - lib/plugins/pre_commit/checks/checkstyle.rb
111
+ - lib/plugins/pre_commit/domain/bad_file.rb
112
+ - lib/plugins/pre_commit/domain/checkstyle.rb
113
+ - lib/plugins/pre_commit/message/extractor.rb
114
+ - lib/plugins/pre_commit/message/formatter.rb
115
+ - lib/plugins/pre_commit/support/path.rb
116
+ - lib/pre_commit/checkstyle/version.rb
117
+ - lib/resources/checkstyle/checkstyle-6.11-all.jar
118
+ - lib/resources/checkstyle/google_checks.xml
119
+ homepage:
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.0.9
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Checkstyle linter plugin for pre-commit
142
+ test_files: []