Navigation Menu

Skip to content

Commit

Permalink
fix(godocfx): make extra files optional, filter out third_party (#2985)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbpg committed Oct 8, 2020
1 parent 5a0c4a4 commit f268921
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions internal/godocfx/godocfx_test.go
Expand Up @@ -113,7 +113,7 @@ func TestGoldens(t *testing.T) {
if updateGoldens {
os.RemoveAll(goldenDir)

if err := write(goldenDir, r, extraFiles); err != nil {
if err := write(goldenDir, r); err != nil {
t.Fatalf("write: %v", err)
}

Expand All @@ -128,7 +128,7 @@ func TestGoldens(t *testing.T) {
return
}

if err := write(gotDir, r, extraFiles); err != nil {
if err := write(gotDir, r); err != nil {
t.Fatalf("write: %v", err)
}

Expand Down
10 changes: 5 additions & 5 deletions internal/godocfx/main.go
Expand Up @@ -58,10 +58,10 @@ func main() {
log.Fatalf("%s missing required argument: module path", os.Args[0])
}

extraFiles := []string{
optionalExtraFiles := []string{
"README.md",
}
r, err := parse(flag.Arg(0), extraFiles)
r, err := parse(flag.Arg(0), optionalExtraFiles)
if err != nil {
log.Fatal(err)
}
Expand All @@ -81,12 +81,12 @@ func main() {
os.RemoveAll(*outDir)
}

if err := write(*outDir, r, extraFiles); err != nil {
if err := write(*outDir, r); err != nil {
log.Fatalf("write: %v", err)
}
}

func write(outDir string, r *result, extraFiles []string) error {
func write(outDir string, r *result) error {
if err := os.MkdirAll(outDir, os.ModePerm); err != nil {
return fmt.Errorf("os.MkdirAll: %v", err)
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func write(outDir string, r *result, extraFiles []string) error {
}
}

for _, path := range extraFiles {
for _, path := range r.extraFiles {
src, err := os.Open(filepath.Join(r.module.Dir, path))
if err != nil {
return err
Expand Down
27 changes: 20 additions & 7 deletions internal/godocfx/parse.go
Expand Up @@ -26,6 +26,7 @@ import (
"go/printer"
"go/token"
"log"
"os"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -95,9 +96,10 @@ func (i *item) addChild(c child) {
var onlyGo = []string{"go"}

type result struct {
pages map[string]*page
toc tableOfContents
module *packages.Module
pages map[string]*page
toc tableOfContents
module *packages.Module
extraFiles []string
}

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

// Filter out extra files that don't exist because some modules don't have a
// README.
extraFiles := []string{}
for _, f := range optionalExtraFiles {
if _, err := os.Stat(filepath.Join(module.Dir, f)); err == nil {
extraFiles = append(extraFiles, f)
}
}

toc := buildTOC(module.Path, pkgNames, extraFiles)

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

return &result{
pages: pages,
toc: toc,
module: module,
pages: pages,
toc: toc,
module: module,
extraFiles: extraFiles,
}, nil
}

Expand Down

0 comments on commit f268921

Please sign in to comment.