| /* |
| Copyright 2013 Google Inc. All Rights Reserved. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| // Package checkstyle provides helper methods for invoking checkstyle |
| // against GWT's source code. |
| package checkstyle |
| |
| import ( |
| "encoding/xml" |
| "flag" |
| "io" |
| "log" |
| "os" |
| "os/exec" |
| ) |
| |
| var checkstylejar = flag.String("checkstylejar", "../tools/antlib/checkstyle-5.7-all.jar", "path to checkstyle jar") |
| |
| // Error represents a specific instance of an error reported by checkstyle. |
| type Error struct { |
| Line int `xml:"line,attr"` |
| Severity string `xml:"severity,attr"` |
| Message string `xml:"message,attr"` |
| } |
| |
| // File represents a collection of checkstyle errors for a single file. |
| type File struct { |
| Name string `xml:"name,attr"` |
| Errors []Error `xml:"error"` |
| } |
| |
| // ReadXML reads a checkstyle XML report from reader. |
| func ReadXML(reader io.Reader) ([]File, error) { |
| var res struct { |
| Files []File `xml:"file"` |
| } |
| return res.Files, xml.NewDecoder(reader).Decode(&res) |
| } |
| |
| // Run runs checkstyle using the specified config against the specified |
| // list of files and returns the style errors found. |
| func Run(config string, files []string) (outfiles []File, err error) { |
| if len(files) == 0 { |
| return []File{}, nil |
| } |
| |
| cmd := exec.Command("java", append([]string{"-cp", *checkstylejar, |
| "-Dcheckstyle.header.file=eclipse/settings/code-style/google.header", |
| "com.puppycrawl.tools.checkstyle.Main", "-f", "xml", "-c", config}, |
| files...)...) |
| reader, err := cmd.StdoutPipe() |
| if err != nil { |
| return nil, err |
| } |
| defer reader.Close() |
| cmd.Stderr = os.Stderr |
| log.Println("running checkstyle:", cmd.Args) |
| if err = cmd.Start(); err != nil { |
| return nil, err |
| } |
| // TODO(mdempsky): Check cmd.Wait() exit code? |
| defer cmd.Wait() |
| return ReadXML(reader) |
| } |