Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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