Moved code out from main into relevant files

This commit is contained in:
Samuel Hawksby-Robinson 2021-01-06 16:38:12 +00:00
parent 41a7931c83
commit ce7a0f5a38
No known key found for this signature in database
GPG Key ID: 64CF99D4A64A1205
3 changed files with 120 additions and 111 deletions

112
main.go
View File

@ -2,9 +2,7 @@ package main
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
"github.com/davecgh/go-spew/spew"
"unicode"
)
@ -19,117 +17,17 @@ const (
var (
keywords = []string{"todo", "fixme"}
// openTodo tracks if subsequent comment lines should be included in the last to-do's description
openTodo *todo
)
type todo struct {
Filepath string
Description string
LineNumber int
RelatedFuncOrType string
filePathSlice []string
}
func (t *todo) Path() []string {
if t.filePathSlice != nil {
return t.filePathSlice
}
sp := strings.Split(t.Filepath, "/")
sp = trimPath(sp)
t.filePathSlice = sp
return t.filePathSlice
}
func main() {
err := processFilesInDir(statusDir)
tf := NewTodoFinder()
err := tf.FindInDir(statusDir)
if err != nil {
panic(err)
}
}
func processFilesInDir(dir string) error {
r, err := regexp.Compile(buildRegexPattern(keywords))
if err != nil {
return err
}
lineSplitter, err := regexp.Compile("\n")
if err != nil {
return err
}
et, err := NewEntityTracker()
if err != nil {
return err
}
tf := NewTodoFinder()
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
}
lines := lineSplitter.Split(string(file), -1)
for i, l := range lines {
et.Track(l)
results := r.FindSubmatch([]byte(l))
if results == nil {
if len(l) < 3 {
openTodo = nil
}
if openTodo != nil {
l = strings.TrimSpace(l)
if l[:2] == "//" {
openTodo.Description += "\n" + l[2:]
} else {
openTodo = nil
}
}
continue
}
td := &todo{
Filepath: filepath,
Description: string(results[1]),
LineNumber: i + 1,
RelatedFuncOrType: et.Current(),
}
tf.AddTodo(td)
openTodo = td
}
}
return nil
spew.Dump(tf.foundTree)
}
func isGoFile(name string) bool {

23
todo.go Normal file
View File

@ -0,0 +1,23 @@
package main
import "strings"
type todo struct {
Filepath string
Description string
LineNumber int
RelatedFuncOrType string
filePathSlice []string
}
func (t *todo) Path() []string {
if t.filePathSlice != nil {
return t.filePathSlice
}
sp := strings.Split(t.Filepath, "/")
sp = trimPath(sp)
t.filePathSlice = sp
return t.filePathSlice
}

View File

@ -1,5 +1,11 @@
package main
import (
"io/ioutil"
"regexp"
"strings"
)
type node struct {
Name string
Type string
@ -7,23 +13,105 @@ type node struct {
Todos []*todo
}
type todoFinder struct {
type TodoFinder struct {
FoundTable []*todo
foundTree *node
// openTodo tracks if subsequent comment lines should be included in the last to-do's description
openTodo *todo
}
func NewTodoFinder() todoFinder {
return todoFinder{
func NewTodoFinder() TodoFinder {
return TodoFinder{
FoundTable: []*todo{},
foundTree: &node{Name: "root", Type: "dir"},
}
}
func (tf *todoFinder) AddTodo(t *todo) {
func (tf *TodoFinder) AddTodo(t *todo) {
tf.FoundTable = append(tf.FoundTable, t)
tf.foundTree.AddToTree(t.Path(), t)
}
func (tf *TodoFinder) FindInDir(dir string) error {
r, err := regexp.Compile(buildRegexPattern(keywords))
if err != nil {
return err
}
lineSplitter, err := regexp.Compile("\n")
if err != nil {
return err
}
et, err := NewEntityTracker()
if err != nil {
return err
}
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 = tf.FindInDir(filepath)
if err != nil {
return err
}
}
if !isGoFile(f.Name()) {
continue
}
file, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
lines := lineSplitter.Split(string(file), -1)
for i, l := range lines {
et.Track(l)
results := r.FindSubmatch([]byte(l))
if results == nil {
if len(l) < 3 {
tf.openTodo = nil
}
if tf.openTodo != nil {
l = strings.TrimSpace(l)
if l[:2] == "//" {
tf.openTodo.Description += "\n" + l[2:]
} else {
tf.openTodo = nil
}
}
continue
}
td := &todo{
Filepath: filepath,
Description: string(results[1]),
LineNumber: i + 1,
RelatedFuncOrType: et.Current(),
}
tf.AddTodo(td)
tf.openTodo = td
}
}
return nil
}
func (n *node) AddToTree(path []string, t *todo) {
if len(path) == 0 {
n.Todos = append(n.Todos, t)