Skip to content

Commit 9f209e1

Browse files
committed
Fix overwriting dep tables
1 parent 50d915f commit 9f209e1

File tree

4 files changed

+82
-35
lines changed

4 files changed

+82
-35
lines changed

doc/doc.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import (
1111
"github.com/flowdev/spaghetti-analyzer/data"
1212
)
1313

14-
// FileName is the name of the documentation file (package_dependencies.md).
15-
const FileName = "package_dependencies.md"
14+
const (
15+
// FileName is the name of the documentation file (package_dependencies.md)
16+
FileName = "package_dependencies.md"
17+
// Title is the mark down title of the package dependencies
18+
Title = `# Dependency Table For: `
19+
)
1620

1721
// WriteDocs generates documentation for the packages 'dtPkgs' and writes it to
1822
// files.
@@ -96,7 +100,7 @@ func GenerateTable(
96100
sort.Strings(allCols)
97101

98102
sb := &strings.Builder{}
99-
intro := `# Dependency Table For: ` + path.Join(rootPkg, pattern) + `
103+
intro := Title + path.Join(rootPkg, pattern) + `
100104
101105
| `
102106
sb.WriteString(intro)

main.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
)
2222

2323
func main() {
24-
rc := cut(os.Args[1:])
24+
rc := analyze(os.Args[1:])
2525
if rc != 0 {
2626
os.Exit(rc)
2727
}
2828
}
2929

30-
func cut(args []string) int {
30+
func analyze(args []string) int {
3131
const (
3232
usageShort = " (shorthand)"
3333
defaultRoot = "."
@@ -119,7 +119,7 @@ func cut(args []string) int {
119119
}
120120

121121
if docPkgs != "" {
122-
writeDocumentation(docPkgs, root, rootPkg, noLinks, depMap)
122+
writeDepTables(docPkgs, root, rootPkg, noLinks, depMap)
123123
} else {
124124
log.Print("INFO - No dependency table wanted.")
125125
}
@@ -150,20 +150,6 @@ func writeStatistics(root string, depMap data.DependencyMap) {
150150
}
151151
}
152152

153-
func writeDocumentation(docPkgs, root, rootPkg string, noLinks bool, depMap data.DependencyMap) {
154-
log.Print("INFO - Writing dependency tables:")
155-
dtPkgs := findPackagesWithFileAsSlice(doc.FileName, docPkgs, root, "documentation")
156-
157-
linkDocPkgs := map[string]struct{}{}
158-
if !noLinks {
159-
linkDocPkgs = dirs.FindPkgsWithFile(doc.FileName, dtPkgs, root, true)
160-
for _, p := range dtPkgs {
161-
linkDocPkgs[p] = struct{}{}
162-
}
163-
}
164-
doc.WriteDocs(dtPkgs, depMap, linkDocPkgs, rootPkg, root)
165-
}
166-
167153
func writeDirTree(root, name string, packs []*pkgs.Package) error {
168154
treeFile := filepath.Join(root, tree.File)
169155
log.Printf("INFO - Writing directory tree to file: %s", treeFile)
@@ -180,10 +166,21 @@ func writeDirTree(root, name string, packs []*pkgs.Package) error {
180166
return nil
181167
}
182168

183-
func findPackagesWithFileAsSlice(signalFile, pkgNames, root, pkgType string) []string {
169+
func writeDepTables(docPkgs, root, rootPkg string, noLinks bool, depMap data.DependencyMap) {
170+
log.Print("INFO - Writing dependency tables:")
171+
dtPkgs := findDepTablesAsSlice(docPkgs, root, rootPkg, "documentation")
172+
173+
linkDocPkgs := map[string]struct{}{}
174+
if !noLinks {
175+
linkDocPkgs = dirs.FindDepTables(doc.FileName, doc.Title, dtPkgs, root, rootPkg)
176+
}
177+
doc.WriteDocs(dtPkgs, depMap, linkDocPkgs, rootPkg, root)
178+
}
179+
180+
func findDepTablesAsSlice(pkgNames, root, rootPkg, pkgType string) []string {
184181
var packs []string
185182
if pkgNames == "*" { // find all existing files
186-
pkgMap := dirs.FindPkgsWithFile(signalFile, nil, root, false)
183+
pkgMap := dirs.FindDepTables(doc.FileName, doc.Title, nil, root, rootPkg)
187184
packs = make([]string, 0, len(pkgMap))
188185
for p := range pkgMap {
189186
packs = append(packs, p)

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestCut(t *testing.T) {
5151
root := mustAbs(filepath.Join("testdata", spec.givenRoot))
5252
mustWriteFile(filepath.Join(root, config.File), []byte(spec.givenConfig))
5353
args := []string{"--root", root}
54-
actualReturnCode := cut(args)
54+
actualReturnCode := analyze(args)
5555

5656
if actualReturnCode != spec.expectedReturnCode {
5757
t.Errorf("Expected return code %d but got: %d", spec.expectedReturnCode, actualReturnCode)

x/dirs/dirs.go

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dirs
22

33
import (
4+
"bufio"
45
"fmt"
56
"log"
67
"os"
@@ -37,11 +38,11 @@ func crawlUpAndFindDirOf(startDir string, files ...string) (string, error) {
3738
return "", nil
3839
}
3940

40-
// FindPkgsWithFile is finding packages containing file on disk starting at
41-
// 'root' and adding them to those given in 'startPkgs'.
42-
func FindPkgsWithFile(file string, startPkgs []string, root string, excludeRoot bool) map[string]struct{} {
41+
// FindDepTables is finding packages containing a dependency table on disk
42+
// starting at 'root' and adding them to those given in 'startPkgs'.
43+
func FindDepTables(file, title string, startPkgs []string, root, rootPkg string) map[string]struct{} {
4344
val := struct{}{}
44-
// prefill doc packages from dtPkgs
45+
// prefill doc packages from startPkgs
4546
retPkgs := make(map[string]struct{}, 128)
4647
for _, p := range startPkgs {
4748
retPkgs[p] = val
@@ -56,26 +57,31 @@ func FindPkgsWithFile(file string, startPkgs []string, root string, excludeRoot
5657
log.Printf("WARN - Unable to list directory %q: %v", path, err)
5758
return filepath.SkipDir
5859
}
59-
if excludeRoot && path == root {
60-
return nil // don't add the root 'file'
61-
}
6260

63-
// no valid package starts with '.' and we don't want to search in '.git' and similar
61+
// no valid package starts with '.' and we don't want to search in testdata
6462
if strings.HasPrefix(info.Name(), ".") || info.Name() == "testdata" {
6563
return filepath.SkipDir
6664
}
6765

68-
if _, err := os.Lstat(filepath.Join(path, file)); err == nil {
66+
depFile := filepath.Join(path, file)
67+
if _, err := os.Lstat(depFile); err == nil {
6968
pkg, err := filepath.Rel(root, path)
7069
if err != nil {
7170
log.Printf("WARN - Unable to compute package for %q: %v", path, err)
7271
return nil // sub-directories might work
7372
}
74-
pkg = strings.ReplaceAll(pkg, "\\", "/") // packages like URLs have always '/'s
75-
if pkg == "." {
73+
pattern, err := readPatternFromFile(depFile, title, rootPkg)
74+
if err != nil {
75+
log.Printf("WARN - Problem reading pattern from file %q: %v", depFile, err)
76+
err = nil
77+
}
78+
if pattern == "" {
79+
pattern = strings.ReplaceAll(pkg, "\\", "/") // packages like URLs have always '/'s
80+
}
81+
if pattern == "." {
7682
retPkgs["/"] = val
7783
} else {
78-
retPkgs[pkg] = val
84+
retPkgs[pattern] = val
7985
}
8086
}
8187
return nil
@@ -85,3 +91,43 @@ func FindPkgsWithFile(file string, startPkgs []string, root string, excludeRoot
8591
}
8692
return retPkgs
8793
}
94+
95+
func readPatternFromFile(depFile, prefix, rootPkg string) (string, error) {
96+
lines, err := readFirstLines(depFile, 5)
97+
prefix = strings.ToLower(prefix)
98+
for _, l := range lines {
99+
if strings.HasPrefix(strings.ToLower(l), prefix) {
100+
pattern := l[len(prefix):]
101+
pattern = strings.TrimSpace(pattern)
102+
if strings.HasPrefix(rootPkg, pattern) {
103+
pattern = pattern[len(rootPkg):]
104+
if pattern != "" && pattern[0] == '/' {
105+
pattern = pattern[1:]
106+
}
107+
if pattern == "" {
108+
pattern = "/"
109+
}
110+
}
111+
return pattern, err
112+
}
113+
}
114+
return "", err
115+
}
116+
117+
func readFirstLines(fileName string, n int) ([]string, error) {
118+
file, err := os.Open(fileName)
119+
if err != nil {
120+
return nil, err
121+
}
122+
defer file.Close()
123+
124+
lines := make([]string, 0, n)
125+
scanner := bufio.NewScanner(file)
126+
for i := 0; i < n && scanner.Scan(); i++ {
127+
lines = append(lines, scanner.Text())
128+
}
129+
if err := scanner.Err(); err != nil {
130+
return lines, err
131+
}
132+
return lines, nil
133+
}

0 commit comments

Comments
 (0)