Skip to content

Commit

Permalink
feat(internal/godocfx): different metadata for different modules (#4297)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbpg authored Jun 22, 2021
1 parent 37607b4 commit 598f5b9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/godocfx/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
cloud.google.com/go/bigquery v1.8.0
cloud.google.com/go/datastore v1.1.0
cloud.google.com/go/storage v1.11.0
github.com/google/go-cmp v0.5.6
github.com/yuin/goldmark v1.3.8
golang.org/x/tools v0.1.3
gopkg.in/yaml.v2 v2.4.0
Expand Down
56 changes: 56 additions & 0 deletions internal/godocfx/godocfx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

_ "cloud.google.com/go/bigquery" // Implicitly required by test.
_ "cloud.google.com/go/storage" // Implicitly required by test.
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/go/packages"
)

var updateGoldens bool
Expand Down Expand Up @@ -198,3 +203,54 @@ func TestHasPrefix(t *testing.T) {
}
}
}

func TestWriteMetadata(t *testing.T) {
now := time.Now()

want := fmt.Sprintf(`update_time {
seconds: %d
nanos: %d
}
name: "cloud.google.com/go"
version: "100.0.0"
language: "go"
`, now.Unix(), now.Nanosecond())

wantAppEngine := fmt.Sprintf(`update_time {
seconds: %d
nanos: %d
}
name: "google.golang.org/appengine/v2"
version: "2.0.0"
language: "go"
stem: "/appengine/docs/standard/go/reference/services/bundled"
`, now.Unix(), now.Nanosecond())

tests := []struct {
path string
version string
want string
}{
{
path: "cloud.google.com/go",
version: "100.0.0",
want: want,
},
{
path: "google.golang.org/appengine/v2",
version: "2.0.0",
want: wantAppEngine,
},
}
for _, test := range tests {
var buf bytes.Buffer
module := &packages.Module{
Path: test.path,
Version: test.version,
}
writeMetadata(&buf, now, module)
if diff := cmp.Diff(test.want, buf.String()); diff != "" {
t.Errorf("writeMetadata(%q) got unexpected diff (-want +got):\n\n%s", test.path, diff)
}
}
}
26 changes: 21 additions & 5 deletions internal/godocfx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"strings"
"time"

"golang.org/x/tools/go/packages"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -245,12 +246,27 @@ func write(outDir string, r *result) error {
}
defer f.Close()
now := time.Now().UTC()
fmt.Fprintf(f, `update_time {
seconds: %d
nanos: %d
writeMetadata(f, now, r.module)
return nil
}

func writeMetadata(w io.Writer, now time.Time, module *packages.Module) {
fmt.Fprintf(w, `update_time {
seconds: %d
nanos: %d
}
name: %q
version: %q
language: "go"`, now.Unix(), now.Nanosecond(), r.module.Path, r.module.Version)
return nil
language: "go"
`, now.Unix(), now.Nanosecond(), module.Path, module.Version)

// Some modules specify a different path to serve from.
// The URL will be /[stem]/[version]/[pkg path relative to module].
// Alternatively, we could plumb this through command line flags.
switch module.Path {
case "google.golang.org/appengine":
fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go111/reference\"\n")
case "google.golang.org/appengine/v2":
fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go/reference/services/bundled\"\n")
}
}

0 comments on commit 598f5b9

Please sign in to comment.