2021-01-04 23:53:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-05 11:16:34 +00:00
|
|
|
"fmt"
|
2021-01-04 23:53:22 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
2021-01-05 13:23:07 +00:00
|
|
|
"strings"
|
2021-01-05 11:16:34 +00:00
|
|
|
"unicode"
|
2021-01-04 23:53:22 +00:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
2021-01-05 11:16:34 +00:00
|
|
|
// TODO implement dynamic comment token selection
|
|
|
|
|
|
|
|
// TODO With line number find if todo is in a function or struct record the name of the function / struct in `todo{}`
|
|
|
|
|
2021-01-04 23:53:22 +00:00
|
|
|
const (
|
|
|
|
statusDir = "../status-go"
|
|
|
|
ignore = statusDir + "/vendor"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-01-05 11:16:34 +00:00
|
|
|
keywords = []string{"todo", "fixme"}
|
2021-01-05 00:13:06 +00:00
|
|
|
found []todo
|
2021-01-05 13:23:07 +00:00
|
|
|
|
|
|
|
// todoMode tracks if subsequent comment lines should be included in the last to-do's description
|
|
|
|
todoMode = false
|
|
|
|
|
2021-01-04 23:53:22 +00:00
|
|
|
)
|
|
|
|
|
2021-01-05 00:13:06 +00:00
|
|
|
type todo struct {
|
|
|
|
Filepath string
|
|
|
|
Description string
|
2021-01-05 11:44:48 +00:00
|
|
|
LineNumber int
|
2021-01-05 00:13:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 23:53:22 +00:00
|
|
|
func main() {
|
|
|
|
err := processFilesInDir(statusDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spew.Dump(found)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processFilesInDir(dir string) error {
|
2021-01-05 11:16:34 +00:00
|
|
|
r, err := regexp.Compile(buildRegexPattern(keywords))
|
2021-01-04 23:53:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-05 12:06:41 +00:00
|
|
|
lineSplitter, err := regexp.Compile("\n")
|
2021-01-05 11:44:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-04 23:53:22 +00:00
|
|
|
files, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
filepath := dir + "/" + f.Name()
|
|
|
|
|
|
|
|
if filepath == ignore {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.IsDir() {
|
|
|
|
err = processFilesInDir(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isGoFile(f.Name()){
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := ioutil.ReadFile(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:44:48 +00:00
|
|
|
lines := lineSplitter.Split(string(file), -1)
|
|
|
|
for i, l := range lines {
|
2021-01-04 23:53:22 +00:00
|
|
|
|
2021-01-05 12:06:41 +00:00
|
|
|
results := r.FindSubmatch([]byte(l))
|
2021-01-05 11:44:48 +00:00
|
|
|
if results == nil {
|
2021-01-05 13:23:07 +00:00
|
|
|
if len(l) < 3 {
|
|
|
|
todoMode = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if todoMode {
|
|
|
|
l = strings.TrimSpace(l)
|
|
|
|
if l[:2] == "//" {
|
|
|
|
found[len(found)-1].Description += "\n" + l[2:]
|
|
|
|
} else {
|
|
|
|
todoMode = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:44:48 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-01-05 12:06:41 +00:00
|
|
|
found = append(found, todo{filepath, string(results[1]), i+1})
|
2021-01-05 13:23:07 +00:00
|
|
|
todoMode = true
|
|
|
|
|
|
|
|
// TODO Working on getting subsequent lines of a todo
|
2021-01-05 11:44:48 +00:00
|
|
|
}
|
2021-01-04 23:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGoFile(name string) bool {
|
|
|
|
if len(name) < 3 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
last := name[len(name)-3:]
|
|
|
|
return last == ".go"
|
|
|
|
}
|
2021-01-05 11:16:34 +00:00
|
|
|
|
|
|
|
func buildRegexPattern(keywords []string) string {
|
|
|
|
kwp := makeRegexKeywords(keywords)
|
2021-01-05 11:44:48 +00:00
|
|
|
return fmt.Sprintf("//.*((%s)(.*))", kwp)
|
2021-01-05 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 12:06:41 +00:00
|
|
|
func makeRegexKeywords(keywords []string) (out string) {
|
2021-01-05 11:16:34 +00:00
|
|
|
for i, kw := range keywords {
|
|
|
|
for _, r := range kw {
|
|
|
|
lower := unicode.ToLower(r)
|
|
|
|
upper := unicode.ToUpper(r)
|
|
|
|
|
|
|
|
out += fmt.Sprintf("[%s,%s]", string(lower), string(upper))
|
|
|
|
}
|
|
|
|
|
|
|
|
if i+1 < len(keywords) {
|
|
|
|
out += "|"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 12:06:41 +00:00
|
|
|
return
|
2021-01-05 11:16:34 +00:00
|
|
|
}
|