forked from denis-tingaikin/go-header
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.go
146 lines (134 loc) · 3.39 KB
/
analyzer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2020-2022 Denis Tingaikin
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 goheader
import (
"fmt"
"go/ast"
"os"
"os/exec"
"strings"
"time"
)
type Target struct {
Path string
File *ast.File
}
const iso = "2006-01-02 15:04:05 -0700"
func (t *Target) ModTime() (time.Time, error) {
diff, err := exec.Command("git", "diff", t.Path).CombinedOutput()
if err == nil && len(diff) == 0 {
line, err := exec.Command("git", "log", "-1", "--pretty=format:%cd", "--date=iso", "--", t.Path).CombinedOutput()
if err == nil {
return time.Parse(iso, string(line))
}
}
info, err := os.Stat(t.Path)
if err != nil {
return time.Time{}, err
}
return info.ModTime(), nil
}
type Analyzer struct {
values map[string]Value
template string
}
func (a *Analyzer) Analyze(target *Target) Issue {
if a.template == "" {
return NewIssue("Missed template for check")
}
if t, err := target.ModTime(); err == nil {
if t.Year() != time.Now().Year() {
return nil
}
}
file := target.File
var header string
var offset = Location{
Position: 1,
}
if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package {
if strings.HasPrefix(file.Comments[0].List[0].Text, "/*") {
header = (&ast.CommentGroup{List: []*ast.Comment{file.Comments[0].List[0]}}).Text()
} else {
header = file.Comments[0].Text()
offset.Position += 3
}
}
header = strings.TrimSpace(header)
if header == "" {
return NewIssue("Missed header for check")
}
s := NewReader(header)
s.SetOffset(offset)
t := NewReader(a.template)
for !s.Done() && !t.Done() {
templateCh := t.Peek()
if templateCh == '{' {
name := a.readField(t)
if a.values[name] == nil {
return NewIssue(fmt.Sprintf("Template has unknown value: %v", name))
}
if i := a.values[name].Read(s); i != nil {
return i
}
continue
}
sourceCh := s.Peek()
if sourceCh != templateCh {
l := s.Location()
notNextLine := func(r rune) bool {
return r != '\n'
}
actual := s.ReadWhile(notNextLine)
expected := t.ReadWhile(notNextLine)
return NewIssueWithLocation(fmt.Sprintf("Actual: %v\nExpected:%v", actual, expected), l)
}
s.Next()
t.Next()
}
if !s.Done() {
l := s.Location()
return NewIssueWithLocation(fmt.Sprintf("Unexpected string: %v", s.Finish()), l)
}
if !t.Done() {
l := s.Location()
return NewIssueWithLocation(fmt.Sprintf("Missed string: %v", t.Finish()), l)
}
return nil
}
func (a *Analyzer) readField(reader *Reader) string {
_ = reader.Next()
_ = reader.Next()
r := reader.ReadWhile(func(r rune) bool {
return r != '}'
})
_ = reader.Next()
_ = reader.Next()
return strings.ToLower(strings.TrimSpace(r))
}
func New(options ...Option) *Analyzer {
a := &Analyzer{}
for _, o := range options {
o.apply(a)
}
for _, v := range a.values {
err := v.Calculate(a.values)
if err != nil {
panic(err.Error())
}
}
return a
}