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
feat(internal/godocfx): add support for other modules #4290
Changes from all commits
c2aa309
fd89a07
8e1266e
37b6704
c137e7e
fb0628e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ const wantEntries = 5 | |
|
||
type fakeIC struct{} | ||
|
||
func (f fakeIC) get(prefix string, since time.Time) (entries []indexEntry, last time.Time, err error) { | ||
func (f fakeIC) get(prefixes []string, since time.Time) (entries []indexEntry, last time.Time, err error) { | ||
e := indexEntry{Timestamp: since.Add(24 * time.Hour)} | ||
return []indexEntry{e}, e.Timestamp, nil | ||
} | ||
|
@@ -49,7 +49,7 @@ func (f *fakeTS) put(context.Context, time.Time) error { | |
func TestNewModules(t *testing.T) { | ||
ic := fakeIC{} | ||
ts := &fakeTS{} | ||
entries, err := newModules(context.Background(), ic, ts, "cloud.google.com") | ||
entries, err := newModules(context.Background(), ic, ts, []string{"cloud.google.com"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe include an entry with explicit versioning and maybe something that exercises the filter paths? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test really just makes sure the paging logic works for the index. It doesn't exercise the prefix logic, which is a gap in coverage. Can try to figure out how to add a test for it (need to refactor the HTTP calls) if you feel strongly. |
||
if err != nil { | ||
t.Fatalf("newModules got err: %v", err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,42 +58,43 @@ func main() { | |
outDir := flag.String("out", "obj/api", "Output directory (default obj/api)") | ||
projectID := flag.String("project", "", "Project ID to use. Required when using -new-modules.") | ||
newMods := flag.Bool("new-modules", false, "Process all new modules with the given prefix. Uses timestamp in Datastore. Stores results in $out/$mod.") | ||
// TODO: flag to set output URL path | ||
|
||
log.SetPrefix("[godocfx] ") | ||
|
||
flag.Parse() | ||
if flag.NArg() != 1 { | ||
if flag.NArg() == 0 { | ||
log.Fatalf("%s missing required argument: module path/prefix", os.Args[0]) | ||
} | ||
|
||
mod := flag.Arg(0) | ||
modNames := flag.Args() | ||
var mods []indexEntry | ||
if *newMods { | ||
if *projectID == "" { | ||
log.Fatal("Must set -project when using -new-modules") | ||
} | ||
var err error | ||
mods, err = newModules(context.Background(), indexClient{}, &dsTimeSaver{projectID: *projectID}, mod) | ||
mods, err = newModules(context.Background(), indexClient{}, &dsTimeSaver{projectID: *projectID}, modNames) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} else { | ||
modPath := mod | ||
version := "latest" | ||
if strings.Contains(mod, "@") { | ||
parts := strings.Split(mod, "@") | ||
if len(parts) != 2 { | ||
log.Fatal("module arg expected only one '@'") | ||
for _, mod := range modNames { | ||
modPath := mod | ||
version := "latest" | ||
if strings.Contains(mod, "@") { | ||
parts := strings.Split(mod, "@") | ||
if len(parts) != 2 { | ||
log.Fatal("module arg expected only one '@'") | ||
} | ||
modPath = parts[0] | ||
version = parts[1] | ||
} | ||
modPath = parts[0] | ||
version = parts[1] | ||
} | ||
modPath = strings.TrimSuffix(modPath, "/...") // No /... needed. | ||
mods = []indexEntry{ | ||
{ | ||
modPath = strings.TrimSuffix(modPath, "/...") // No /... needed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unclear to me end to end how module strings are moving through the system, do you ever need to sanitize trailing comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean by trailing comments. This comes from a command line argument. This makes sure that if the user did include the |
||
mods = append(mods, indexEntry{ | ||
Path: modPath, | ||
Version: version, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -105,21 +106,19 @@ func main() { | |
return | ||
} | ||
// Create a temp module so we can get the exact version asked for. | ||
tempDir, err := ioutil.TempDir("", "godocfx-*") | ||
workingDir, err := ioutil.TempDir("", "godocfx-*") | ||
if err != nil { | ||
log.Fatalf("ioutil.TempDir: %v", err) | ||
} | ||
runCmd(tempDir, "go", "mod", "init", "cloud.google.com/go/lets-build-some-docs") | ||
// Use a fake module that doesn't start with cloud.google.com/go. | ||
runCmd(workingDir, "go", "mod", "init", "cloud.google.com/lets-build-some-docs") | ||
for _, m := range mods { | ||
log.Printf("Processing %s@%s", m.Path, m.Version) | ||
|
||
path := *outDir | ||
// If we have more than one module, we need a more specific out path. | ||
if len(mods) > 1 { | ||
path = filepath.Join(path, fmt.Sprintf("%s@%s", m.Path, m.Version)) | ||
} | ||
if err := process(m, tempDir, path, *print); err != nil { | ||
log.Printf("Failed to process %v", m) | ||
// Always output to specific directory. | ||
path := filepath.Join(*outDir, fmt.Sprintf("%s@%s", m.Path, m.Version)) | ||
if err := process(m, workingDir, path, *print); err != nil { | ||
log.Printf("Failed to process %v: %v", m, err) | ||
} | ||
log.Printf("Done with %s@%s", m.Path, m.Version) | ||
} | ||
|
@@ -138,22 +137,23 @@ func runCmd(dir, name string, args ...string) error { | |
return nil | ||
} | ||
|
||
func process(mod indexEntry, tempDir, outDir string, print bool) error { | ||
func process(mod indexEntry, workingDir, outDir string, print bool) error { | ||
// Be sure to get the module and run the module loader in the tempDir. | ||
if err := runCmd(tempDir, "go", "mod", "tidy"); err != nil { | ||
return err | ||
if err := runCmd(workingDir, "go", "mod", "tidy"); err != nil { | ||
return fmt.Errorf("go mod tidy error: %v", err) | ||
} | ||
if err := runCmd(tempDir, "go", "get", mod.Path+"@"+mod.Version); err != nil { | ||
return err | ||
if err := runCmd(workingDir, "go", "get", "-d", "-t", mod.Path+"/...@"+mod.Version); err != nil { | ||
return fmt.Errorf("go get %s@%s: %v", mod.Path, mod.Version, err) | ||
} | ||
|
||
log.Println("Starting to parse") | ||
optionalExtraFiles := []string{} | ||
filter := []string{ | ||
"cloud.google.com/go/analytics", | ||
"cloud.google.com/go/area120", | ||
"cloud.google.com/go/gsuiteaddons", | ||
} | ||
r, err := parse(mod.Path+"/...", tempDir, optionalExtraFiles, filter) | ||
r, err := parse(mod.Path+"/...", workingDir, optionalExtraFiles, filter) | ||
if err != nil { | ||
return fmt.Errorf("parse: %v", err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are overlapping prefixes an issue that may need consideration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't think so. We won't process duplicate modules because we don't process each prefix independently -- if a module matches any prefix, it's included.