Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 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 | package org.onosproject.checkstyle; |
| 17 | |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 18 | import com.puppycrawl.tools.checkstyle.Checker; |
| 19 | import com.puppycrawl.tools.checkstyle.ConfigurationLoader; |
| 20 | import com.puppycrawl.tools.checkstyle.DefaultConfiguration; |
| 21 | import com.puppycrawl.tools.checkstyle.PropertiesExpander; |
| 22 | import com.puppycrawl.tools.checkstyle.api.AuditEvent; |
| 23 | import com.puppycrawl.tools.checkstyle.api.AuditListener; |
| 24 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
| 25 | import com.puppycrawl.tools.checkstyle.api.Configuration; |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 26 | import org.onosproject.buckdaemon.BuckTask; |
| 27 | import org.onosproject.buckdaemon.BuckTaskContext; |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 28 | |
| 29 | import java.io.File; |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.concurrent.CountDownLatch; |
| 32 | import java.util.stream.Collectors; |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 33 | |
Brian O'Connor | fc247f4 | 2016-05-23 15:25:34 -0700 | [diff] [blame] | 34 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 35 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Buck task for executing checkstyle on the specified project files. |
| 38 | */ |
| 39 | public class CheckstyleRunner implements BuckTask { |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 40 | |
| 41 | private final Configuration config; |
| 42 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 43 | public CheckstyleRunner(String configLocation, String suppressionLocation) { |
| 44 | try { |
| 45 | // create a configuration |
| 46 | DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader |
| 47 | .loadConfiguration(configLocation, new PropertiesExpander(System.getProperties())); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 48 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 49 | // add the suppression file to the configuration |
| 50 | DefaultConfiguration suppressions = new DefaultConfiguration("SuppressionFilter"); |
| 51 | suppressions.addAttribute("file", suppressionLocation); |
| 52 | config.addChild(suppressions); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 53 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 54 | this.config = config; |
| 55 | } catch (CheckstyleException e) { |
| 56 | throw new RuntimeException(e); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 57 | } |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void execute(BuckTaskContext context) { |
| 62 | List<String> input = context.input(); |
| 63 | if (input.size() < 3 || input.get(2).length() == 0) { |
| 64 | return; |
| 65 | } |
| 66 | String project = input.get(0); |
| 67 | String baseDir = input.get(1); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 68 | |
| 69 | // create a listener for output |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 70 | StringAuditor listener = new StringAuditor(context); |
Brian O'Connor | fc247f4 | 2016-05-23 15:25:34 -0700 | [diff] [blame] | 71 | listener.setProjectName(project); |
| 72 | listener.setBaseDir(baseDir); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 73 | |
| 74 | // create Checker object and run it |
| 75 | final Checker checker = new Checker(); |
| 76 | final ClassLoader moduleClassLoader = Checker.class.getClassLoader(); |
| 77 | checker.setModuleClassLoader(moduleClassLoader); |
| 78 | |
| 79 | try { |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 80 | checker.configure(config); |
| 81 | checker.addListener(listener); |
| 82 | |
| 83 | // run Checker |
Yuta HIGUCHI | 9312a80 | 2017-06-12 20:01:27 -0700 | [diff] [blame] | 84 | List<File> fileList = input.subList(2, input.size()).stream() |
Thomas Vachuska | 73436b5 | 2017-03-22 19:50:47 -0700 | [diff] [blame] | 85 | .filter(s -> !s.contains("/:")) // Yes, fighting a hack with a hack. |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 86 | .map(File::new) |
| 87 | .collect(Collectors.toList()); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 88 | int errorCounter = checker.process(fileList); |
| 89 | if (errorCounter > 0) { |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 90 | context.output("CHECKSTYLE ERROR"); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 91 | } |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 92 | |
| 93 | listener.await(); |
| 94 | } catch (CheckstyleException | InterruptedException e) { |
Ray Milkey | 8df94b8 | 2016-11-16 11:03:32 -0800 | [diff] [blame] | 95 | e.printStackTrace(); //dump exeception to stderr |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 96 | throw new RuntimeException(e); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 97 | } finally { |
| 98 | checker.destroy(); |
| 99 | } |
| 100 | |
Brian O'Connor | fc247f4 | 2016-05-23 15:25:34 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 103 | static class StringAuditor implements AuditListener { |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 104 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 105 | private final BuckTaskContext context; |
| 106 | private CountDownLatch finishedLatch = new CountDownLatch(1); |
| 107 | private String baseDir = ""; |
| 108 | private String project = ""; |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 109 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 110 | StringAuditor(BuckTaskContext context) { |
| 111 | this.context = context; |
| 112 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 113 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 114 | public void setBaseDir(String base) { |
| 115 | this.baseDir = base; |
| 116 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 117 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 118 | public void setProjectName(String projectName) { |
| 119 | this.project = projectName; |
| 120 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 121 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 122 | public void await() throws InterruptedException { |
| 123 | finishedLatch.await(); |
| 124 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 125 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public void auditStarted(AuditEvent evt) { |
| 128 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 129 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void auditFinished(AuditEvent evt) { |
| 132 | finishedLatch.countDown(); |
| 133 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 134 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public void fileStarted(AuditEvent evt) { |
| 137 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 138 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public void fileFinished(AuditEvent evt) { |
| 141 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 142 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public void addError(AuditEvent evt) { |
| 145 | switch (evt.getSeverityLevel()) { |
| 146 | case ERROR: |
| 147 | StringBuilder output = new StringBuilder(); |
| 148 | String fileName = evt.getFileName(); |
| 149 | if (!isNullOrEmpty(baseDir)) { |
| 150 | int index = fileName.indexOf(baseDir); |
| 151 | if (index >= 0) { |
| 152 | fileName = fileName.substring(index + baseDir.length() + 1); |
| 153 | if (!isNullOrEmpty(project)) { |
| 154 | output.append(project).append(':'); |
| 155 | } |
Brian O'Connor | fc247f4 | 2016-05-23 15:25:34 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 158 | output.append(fileName).append(':').append(evt.getLine()); |
| 159 | if (evt.getColumn() > 0) { |
| 160 | output.append(':').append(evt.getColumn()); |
| 161 | } |
| 162 | output.append(": ").append(evt.getMessage()); |
| 163 | context.output(output.toString()); |
| 164 | break; |
| 165 | case IGNORE: |
| 166 | case INFO: |
| 167 | case WARNING: |
| 168 | default: |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void addException(AuditEvent evt, Throwable throwable) { |
| 175 | addError(evt); |
| 176 | context.output(throwable.getMessage()); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 180 | } |