Skip to content
Permalink
Browse files

fix(godocfx): make extra files optional, filter out third_party (#2985)

  • Loading branch information
tbpg committed Oct 8, 2020
1 parent 5a0c4a4 commit f2689214a24b2e325d3e8f54441bb11fbef925f0
Showing with 27 additions and 14 deletions.
  1. +2 −2 internal/godocfx/godocfx_test.go
  2. +5 −5 internal/godocfx/main.go
  3. +20 −7 internal/godocfx/parse.go
@@ -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)
}

@@ -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)
}

@@ -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)
}
@@ -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)
}
@@ -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
@@ -26,6 +26,7 @@ import (
"go/printer"
"go/token"
"log"
"os"
"path/filepath"
"sort"
"strings"
@@ -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.
@@ -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,
@@ -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
}
@@ -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
@@ -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
}

0 comments on commit f268921

Please sign in to comment.