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 | |
| 17 | |
| 18 | """ |
| 19 | Implementation of the rule to call checkstyle |
| 20 | """ |
| 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( |
| 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 | |
| 35 | ctx.actions.write( |
| 36 | output = ctx.outputs.executable, |
| 37 | content = cmd, |
| 38 | ) |
| 39 | |
| 40 | inputs = (ctx.files.srcs + |
| 41 | ctx.files._classpath + |
| 42 | ctx.attr._config.files.to_list() + |
| 43 | ctx.attr._suppressions.files.to_list() + |
| 44 | ctx.attr._java_header.files.to_list()) |
| 45 | |
| 46 | runfiles = ctx.runfiles(files = inputs) |
| 47 | return [DefaultInfo(runfiles = runfiles)] |
| 48 | |
| 49 | |
| 50 | """ |
| 51 | Rule definition for calling checkstyle |
| 52 | """ |
| 53 | _execute_checkstyle_test = rule( |
| 54 | test = True, |
| 55 | attrs = { |
| 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("@guava//jar"), |
| 63 | Label("@commons_logging//jar"), |
| 64 | ]), |
| 65 | "srcs": attr.label_list(allow_files = FileType([".java"])), |
| 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")), |
| 69 | }, |
| 70 | |
| 71 | implementation = _checkstyle_impl, |
| 72 | ) |
| 73 | |
| 74 | """ |
| 75 | Macro to instantiate the checkstyle rule for a given set of sources. |
| 76 | |
| 77 | Args: |
| 78 | name: name of the target to generate. Required. |
| 79 | srcs: list of source file targets to run checkstyle on. Required. |
| 80 | size: test size constraint. Optional, defaults to "small" |
| 81 | """ |
| 82 | def checkstyle_test(name, srcs): |
| 83 | _execute_checkstyle_test(name = name, srcs = srcs, size = "small") |
| 84 | |
| 85 | |