Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | """ |
| 16 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 17 | """ |
| 18 | Implementation of the rule to call checkstyle |
| 19 | """ |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 20 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 21 | def _checkstyle_impl(ctx): |
| 22 | classpath = "" |
| 23 | need_colon = False |
| 24 | for file in ctx.files._classpath: |
| 25 | if need_colon: |
| 26 | classpath += ":" |
| 27 | need_colon = True |
| 28 | classpath += file.path |
| 29 | |
| 30 | cmd = " ".join( |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 31 | ["java -cp %s com.puppycrawl.tools.checkstyle.Main" % classpath] + |
| 32 | ["-c %s" % ctx.attr._config.files.to_list()[0].path] + |
| 33 | [src_file.path for src_file in ctx.files.srcs], |
| 34 | ) |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 35 | |
| 36 | ctx.actions.write( |
| 37 | output = ctx.outputs.executable, |
| 38 | content = cmd, |
| 39 | ) |
| 40 | |
| 41 | inputs = (ctx.files.srcs + |
| 42 | ctx.files._classpath + |
| 43 | ctx.attr._config.files.to_list() + |
| 44 | ctx.attr._suppressions.files.to_list() + |
| 45 | ctx.attr._java_header.files.to_list()) |
| 46 | |
| 47 | runfiles = ctx.runfiles(files = inputs) |
| 48 | return [DefaultInfo(runfiles = runfiles)] |
| 49 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 50 | """ |
| 51 | Rule definition for calling checkstyle |
| 52 | """ |
| 53 | _execute_checkstyle_test = rule( |
| 54 | test = True, |
| 55 | attrs = { |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 56 | "_classpath": attr.label_list(default = [ |
| 57 | Label("@checkstyle//jar"), |
| 58 | Label("@commons_beanutils//jar"), |
| 59 | Label("@commons_cli//jar"), |
| 60 | Label("@commons_collections//jar"), |
| 61 | Label("@antlr//jar"), |
| 62 | Label("@com_google_guava_guava//jar"), |
| 63 | Label("@commons_logging//jar"), |
| 64 | ]), |
Ray Milkey | 16fbcff | 2019-03-26 11:04:34 -0700 | [diff] [blame^] | 65 | "srcs": attr.label_list(allow_files = [".java"]), |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 66 | "_config": attr.label(default = Label("//tools/build/conf:checkstyle_xml")), |
| 67 | "_suppressions": attr.label(default = Label("//tools/build/conf:suppressions_xml")), |
| 68 | "_java_header": attr.label(default = Label("//tools/build/conf:onos_java_header")), |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 69 | }, |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 70 | implementation = _checkstyle_impl, |
| 71 | ) |
| 72 | |
| 73 | """ |
| 74 | Macro to instantiate the checkstyle rule for a given set of sources. |
| 75 | |
| 76 | Args: |
| 77 | name: name of the target to generate. Required. |
| 78 | srcs: list of source file targets to run checkstyle on. Required. |
| 79 | size: test size constraint. Optional, defaults to "small" |
| 80 | """ |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 81 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 82 | def checkstyle_test(name, srcs): |
| 83 | _execute_checkstyle_test(name = name, srcs = srcs, size = "small") |