-
Notifications
You must be signed in to change notification settings - Fork 0
/
smake.go
161 lines (135 loc) · 3.47 KB
/
smake.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (C) 2023 Shanhu Tech Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package smake
import (
"fmt"
"path"
"path/filepath"
lintpkg "golang.org/x/lint"
"shanhu.io/g/errcode"
"shanhu.io/g/gocheck"
"shanhu.io/g/goload"
"shanhu.io/g/gomod"
"shanhu.io/g/gotags"
"shanhu.io/g/lexing"
)
func smlchkPkg(c *context, pkg *relPkg) []*lexing.Error {
const textHeight = 320 // 20 lines for license notice.
const textWidth = 80
dir := filepath.Join(c.workDir(), filepath.FromSlash(pkg.rel))
return gocheck.ModCheckAll(dir, pkg.abs, textHeight, textWidth)
}
func smlchk(c *context, pkgs []*relPkg) error {
c.logln("smlchk")
for _, pkg := range pkgs {
if errs := smlchkPkg(c, pkg); len(errs) != 0 {
for _, err := range errs {
c.logln(err)
}
return fmt.Errorf("smlchk %q failed", pkg.rel)
}
}
return nil
}
func lint(c *context, pkgs []*relPkg) error {
c.logln("lint")
const minConfidence = 0.8
for _, pkg := range pkgs {
files, err := fileSourceMap(pkg)
if err != nil {
return err
}
l := new(lintpkg.Linter)
ps, err := l.LintFiles(files)
if err != nil {
return err
}
errCount := 0
for _, p := range ps {
if p.Confidence < minConfidence {
continue
}
c.logf("%v: %s\n", p.Position, p.Text)
errCount++
}
if errCount > 0 {
return fmt.Errorf("lint %q failed", pkg.rel)
}
}
return nil
}
func tags(c *context, pkgs []*relPkg) error {
if !c.atModRoot() {
return nil
}
c.logln("tags")
var files []string
for _, pkg := range pkgs {
list := listAbsFiles(pkg.pkg)
files = append(files, list...)
}
return gotags.Write(files, "tags")
}
func listPkgs(c *context) ([]*relPkg, error) {
root := c.modRootDir()
workDir := c.workDir()
modFile := filepath.Join(root, "go.mod")
mod, err := gomod.Parse(modFile)
if err != nil {
return nil, errcode.Annotate(err, "parse go.mod")
}
relPath, err := filepath.Rel(root, workDir)
if err != nil {
return nil, errcode.Annotate(err, "get relative path")
}
relPkg := filepath.ToSlash(relPath)
workPkg := path.Join(mod.Name, relPkg)
scanRes, err := goload.ScanModPkgs(workPkg, workDir, nil)
if err != nil {
return nil, errcode.Annotate(err, "scan packages")
}
return relPkgs(workPkg, scanRes)
}
func smake(c *context) error {
pkgs, err := listPkgs(c)
if err != nil {
return errcode.Annotate(err, "list packages")
}
if len(pkgs) == 0 {
c.logln("no packages found")
return nil
}
installCmd := []string{"go", "install", "-buildvcs=false", "-trimpath"}
if err := c.execPkgs(pkgs, []string{
"gofmt", "-s", "-w", "-l",
}, nil); err != nil {
return err
}
if err := c.execPkgs(pkgs, installCmd, nil); err != nil {
return err
}
if err := smlchk(c, pkgs); err != nil {
return err
}
if err := lint(c, pkgs); err != nil {
return err
}
if err := c.execPkgs(pkgs, []string{
"go", "vet",
}, nil); err != nil {
return err
}
return tags(c, pkgs)
}