Skip to content

Commit f268921

Browse files
authored
fix(godocfx): make extra files optional, filter out third_party (#2985)
1 parent 5a0c4a4 commit f268921

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

internal/godocfx/godocfx_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func TestGoldens(t *testing.T) {
113113
if updateGoldens {
114114
os.RemoveAll(goldenDir)
115115

116-
if err := write(goldenDir, r, extraFiles); err != nil {
116+
if err := write(goldenDir, r); err != nil {
117117
t.Fatalf("write: %v", err)
118118
}
119119

@@ -128,7 +128,7 @@ func TestGoldens(t *testing.T) {
128128
return
129129
}
130130

131-
if err := write(gotDir, r, extraFiles); err != nil {
131+
if err := write(gotDir, r); err != nil {
132132
t.Fatalf("write: %v", err)
133133
}
134134

internal/godocfx/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func main() {
5858
log.Fatalf("%s missing required argument: module path", os.Args[0])
5959
}
6060

61-
extraFiles := []string{
61+
optionalExtraFiles := []string{
6262
"README.md",
6363
}
64-
r, err := parse(flag.Arg(0), extraFiles)
64+
r, err := parse(flag.Arg(0), optionalExtraFiles)
6565
if err != nil {
6666
log.Fatal(err)
6767
}
@@ -81,12 +81,12 @@ func main() {
8181
os.RemoveAll(*outDir)
8282
}
8383

84-
if err := write(*outDir, r, extraFiles); err != nil {
84+
if err := write(*outDir, r); err != nil {
8585
log.Fatalf("write: %v", err)
8686
}
8787
}
8888

89-
func write(outDir string, r *result, extraFiles []string) error {
89+
func write(outDir string, r *result) error {
9090
if err := os.MkdirAll(outDir, os.ModePerm); err != nil {
9191
return fmt.Errorf("os.MkdirAll: %v", err)
9292
}
@@ -123,7 +123,7 @@ func write(outDir string, r *result, extraFiles []string) error {
123123
}
124124
}
125125

126-
for _, path := range extraFiles {
126+
for _, path := range r.extraFiles {
127127
src, err := os.Open(filepath.Join(r.module.Dir, path))
128128
if err != nil {
129129
return err

internal/godocfx/parse.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"go/printer"
2727
"go/token"
2828
"log"
29+
"os"
2930
"path/filepath"
3031
"sort"
3132
"strings"
@@ -95,9 +96,10 @@ func (i *item) addChild(c child) {
9596
var onlyGo = []string{"go"}
9697

9798
type result struct {
98-
pages map[string]*page
99-
toc tableOfContents
100-
module *packages.Module
99+
pages map[string]*page
100+
toc tableOfContents
101+
module *packages.Module
102+
extraFiles []string
101103
}
102104

103105
// parse parses the directory into a map of import path -> page and a TOC.
@@ -106,7 +108,7 @@ type result struct {
106108
// to packages.Load as-is.
107109
//
108110
// extraFiles is a list of paths relative to the module root to include.
109-
func parse(glob string, extraFiles []string) (*result, error) {
111+
func parse(glob string, optionalExtraFiles []string) (*result, error) {
110112
config := &packages.Config{
111113
Mode: packages.NeedName | packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedModule,
112114
Tests: true,
@@ -138,6 +140,7 @@ func parse(glob string, extraFiles []string) (*result, error) {
138140
// The uncompiled test package shows up as "foo_test [foo.test]".
139141
if strings.HasSuffix(id, ".test") ||
140142
strings.Contains(id, "internal") ||
143+
strings.Contains(id, "third_party") ||
141144
(strings.Contains(id, " [") && !strings.Contains(id, "_test [")) {
142145
continue
143146
}
@@ -176,6 +179,15 @@ func parse(glob string, extraFiles []string) (*result, error) {
176179
}
177180
sort.Strings(pkgNames)
178181

182+
// Filter out extra files that don't exist because some modules don't have a
183+
// README.
184+
extraFiles := []string{}
185+
for _, f := range optionalExtraFiles {
186+
if _, err := os.Stat(filepath.Join(module.Dir, f)); err == nil {
187+
extraFiles = append(extraFiles, f)
188+
}
189+
}
190+
179191
toc := buildTOC(module.Path, pkgNames, extraFiles)
180192

181193
// Once the files are grouped by package, process each package
@@ -353,9 +365,10 @@ func parse(glob string, extraFiles []string) (*result, error) {
353365
}
354366

355367
return &result{
356-
pages: pages,
357-
toc: toc,
358-
module: module,
368+
pages: pages,
369+
toc: toc,
370+
module: module,
371+
extraFiles: extraFiles,
359372
}, nil
360373
}
361374

0 commit comments

Comments
 (0)