Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 84 | List<File> fileList = input.subList(2, input.size() - 1).stream() |
| 85 | .map(File::new) |
| 86 | .collect(Collectors.toList()); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 87 | int errorCounter = checker.process(fileList); |
| 88 | if (errorCounter > 0) { |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 89 | context.output("CHECKSTYLE ERROR"); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 90 | } |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 91 | |
| 92 | listener.await(); |
| 93 | } catch (CheckstyleException | InterruptedException e) { |
| 94 | throw new RuntimeException(e); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 95 | } finally { |
| 96 | checker.destroy(); |
| 97 | } |
| 98 | |
Brian O'Connor | fc247f4 | 2016-05-23 15:25:34 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 101 | static class StringAuditor implements AuditListener { |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 102 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 103 | private final BuckTaskContext context; |
| 104 | private CountDownLatch finishedLatch = new CountDownLatch(1); |
| 105 | private String baseDir = ""; |
| 106 | private String project = ""; |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 107 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 108 | StringAuditor(BuckTaskContext context) { |
| 109 | this.context = context; |
| 110 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 111 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 112 | public void setBaseDir(String base) { |
| 113 | this.baseDir = base; |
| 114 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 115 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 116 | public void setProjectName(String projectName) { |
| 117 | this.project = projectName; |
| 118 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 119 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 120 | public void await() throws InterruptedException { |
| 121 | finishedLatch.await(); |
| 122 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 123 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public void auditStarted(AuditEvent evt) { |
| 126 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 127 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public void auditFinished(AuditEvent evt) { |
| 130 | finishedLatch.countDown(); |
| 131 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 132 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public void fileStarted(AuditEvent evt) { |
| 135 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 136 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public void fileFinished(AuditEvent evt) { |
| 139 | } |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 140 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 141 | @Override |
| 142 | public void addError(AuditEvent evt) { |
| 143 | switch (evt.getSeverityLevel()) { |
| 144 | case ERROR: |
| 145 | StringBuilder output = new StringBuilder(); |
| 146 | String fileName = evt.getFileName(); |
| 147 | if (!isNullOrEmpty(baseDir)) { |
| 148 | int index = fileName.indexOf(baseDir); |
| 149 | if (index >= 0) { |
| 150 | fileName = fileName.substring(index + baseDir.length() + 1); |
| 151 | if (!isNullOrEmpty(project)) { |
| 152 | output.append(project).append(':'); |
| 153 | } |
Brian O'Connor | fc247f4 | 2016-05-23 15:25:34 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 156 | output.append(fileName).append(':').append(evt.getLine()); |
| 157 | if (evt.getColumn() > 0) { |
| 158 | output.append(':').append(evt.getColumn()); |
| 159 | } |
| 160 | output.append(": ").append(evt.getMessage()); |
| 161 | context.output(output.toString()); |
| 162 | break; |
| 163 | case IGNORE: |
| 164 | case INFO: |
| 165 | case WARNING: |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void addException(AuditEvent evt, Throwable throwable) { |
| 173 | addError(evt); |
| 174 | context.output(throwable.getMessage()); |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 178 | } |