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(godocfx): create Go DocFX YAML generator #2854
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
464f361
feat(godocfx): create Go DocFX YAML generator
tbpg 0d19ef7
chore: update licenses and update license test exceptions
tbpg 4f7599d
fix: Unix seconds
tbpg 9028f86
Move godocfx to internal, add pkgsite license, review updates
tbpg ab1c32b
fix: module path
tbpg 7d59b9f
Merge branch 'master' into godocfx
tbpg 920ef5e
Fix lint and license headers
tbpg 9258013
Build tags?
tbpg File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Fail on any error. | ||
set -eo pipefail | ||
|
||
# Display commands being run. | ||
set -x | ||
|
||
python3 -m pip install gcp-docuploader | ||
|
||
cd github/google-cloud-go/third_party/internal/godocfx | ||
go install | ||
cd - | ||
|
||
if [ -z "$MODULE" ] || [ -z "$VERSION" ] ; then | ||
echo "Must set the MODULE and VERSION environment variables" | ||
exit 1 | ||
fi | ||
|
||
cd $(mktemp -d) | ||
|
||
# Create a module and get the module@version being asked for. | ||
go mod init cloud.google.com/lets/build/some/docs | ||
go get "$MODULE@$VERSION" | ||
|
||
# Generate the YAML and a docs.metadata file. | ||
godocfx "$MODULE/..." | ||
|
||
cd obj/api || exit 4 | ||
|
||
python3 -m docuploader upload \ | ||
--staging-bucket docs-staging-v2-staging \ | ||
--destination-prefix docfx- \ | ||
--credentials "$KOKORO_KEYSTORE_DIR/73713_docuploader_service_account" \ | ||
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
obj/ | ||
site/ | ||
docfxgen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Go DocFX YAML Generator | ||
|
||
This tool generates DocFX YAML for Go modules. | ||
|
||
Only a single module will be processed at once. | ||
|
||
By default, the output files are stored at `./obj/api`. You can convert them to | ||
HTML using [doc-templates](https://github.com/googleapis/doc-templates) and/or | ||
[doc-pipeline](https://github.com/googleapis/doc-pipeline). | ||
|
||
Example usage: | ||
|
||
``` | ||
cd module && godocfx ./... | ||
godocfx cloud.google.com/go/... | ||
godocfx -print cloud.google.com/go/storage/... | ||
godocfx -out custom/output/dir cloud.google.com/go/... | ||
godocfx -rm custom/output/dir cloud.google.com/go/... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module cloud.google.com/go/third_party/internal/godocfx | ||
|
||
go 1.15 | ||
|
||
require ( | ||
cloud.google.com/go/storage v1.11.0 | ||
golang.org/x/tools v0.0.0-20200913032122-97363e29fc9b | ||
gopkg.in/yaml.v2 v2.3.0 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
_ "cloud.google.com/go/storage" // Implicitly required by test. | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
testPath := "cloud.google.com/go/storage" | ||
pages, toc, module, err := parse(testPath) | ||
if err != nil { | ||
t.Fatalf("Parse: %v", err) | ||
} | ||
if got, want := len(toc), 1; got != want { | ||
t.Fatalf("Parse got len(toc) = %d, want %d", got, want) | ||
} | ||
if got, want := len(pages), 1; got != want { | ||
t.Errorf("Parse got len(pages) = %d, want %d", got, want) | ||
} | ||
if got := module.Path; got != testPath { | ||
t.Fatalf("Parse got module = %q, want %q", got, testPath) | ||
} | ||
|
||
page := pages[testPath] | ||
|
||
// Check invariants for every item. | ||
for _, item := range page.Items { | ||
if got := item.UID; got == "" { | ||
t.Errorf("Parse found missing UID: %v", item) | ||
} | ||
|
||
if got, want := item.Langs, []string{"go"}; len(got) != 1 || got[0] != want[0] { | ||
t.Errorf("Parse %v got langs = %v, want %v", item.UID, got, want) | ||
} | ||
} | ||
|
||
// Check there is at least one type, const, variable, and function. | ||
// Note: no method because they aren't printed for Namespaces yet. | ||
wants := []string{"type", "const", "variable", "function"} | ||
for _, want := range wants { | ||
found := false | ||
for _, c := range page.Items { | ||
if c.Type == want { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
t.Errorf("Parse got no %q, want at least one", want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/*Command godocfx generates DocFX YAML for Go code. | ||
|
||
Usage: | ||
|
||
godocfx [flags] path | ||
|
||
cd module && godocfx ./... | ||
godocfx cloud.google.com/go/... | ||
godocfx -print cloud.google.com/go/storage/... | ||
godocfx -out custom/output/dir cloud.google.com/go/... | ||
godocfx -rm cloud.google.com/go/... | ||
|
||
See: | ||
* https://dotnet.github.io/docfx/spec/metadata_format_spec.html | ||
* https://github.com/googleapis/doc-templates | ||
* https://github.com/googleapis/doc-pipeline | ||
|
||
TODO: | ||
* Cross link referenced packages. | ||
*/ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func main() { | ||
print := flag.Bool("print", false, "Print instead of save (default false)") | ||
rm := flag.Bool("rm", false, "Delete out directory before generating") | ||
outDir := flag.String("out", "obj/api", "Output directory (default obj/api)") | ||
flag.Parse() | ||
if flag.NArg() != 1 { | ||
log.Fatalf("%s expected 1 arg, got %d", os.Args[0], flag.NArg()) | ||
} | ||
|
||
pages, toc, module, err := parse(flag.Arg(0)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if *print { | ||
if err := yaml.NewEncoder(os.Stdout).Encode(pages); err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("----- toc.yaml") | ||
if err := yaml.NewEncoder(os.Stdout).Encode(toc); err != nil { | ||
log.Fatal(err) | ||
} | ||
return | ||
} | ||
|
||
if *rm { | ||
os.RemoveAll(*outDir) | ||
} | ||
if err := os.MkdirAll(*outDir, os.ModePerm); err != nil { | ||
log.Fatalf("os.MkdirAll: %v", err) | ||
} | ||
for path, p := range pages { | ||
// Make the module root page the index. | ||
if path == module.Path { | ||
path = "index" | ||
} | ||
// Trim the module path from all other paths. | ||
path = strings.TrimPrefix(path, module.Path+"/") | ||
path = filepath.Join(*outDir, path+".yml") | ||
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { | ||
log.Fatalf("os.MkdirAll: %v", err) | ||
} | ||
f, err := os.Create(path) | ||
codyoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
fmt.Fprintln(f, "### YamlMime:UniversalReference") | ||
if err := yaml.NewEncoder(f).Encode(p); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
path = filepath.Join(*outDir, "toc.yml") | ||
f, err = os.Create(path) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
fmt.Fprintln(f, "### YamlMime:TableOfContent") | ||
if err := yaml.NewEncoder(f).Encode(toc); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// Write the docuploader docs.metadata file. Not for DocFX. | ||
// See https://github.com/googleapis/docuploader/issues/11. | ||
// Example: | ||
/* | ||
update_time { | ||
seconds: 1600048103 | ||
nanos: 183052000 | ||
} | ||
name: "cloud.google.com/go" | ||
version: "v0.65.0" | ||
language: "go" | ||
*/ | ||
path := filepath.Join(*outDir, "docs.metadata") | ||
f, err := os.Create(path) | ||
codyoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
now := time.Now().UTC() | ||
fmt.Fprintf(f, `update_time { | ||
seconds: %d | ||
nanos: %d | ||
} | ||
name: %q | ||
version: %q | ||
language: "go"`, now.Unix(), now.Nanosecond(), module.Path, module.Version) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: could this error message be improved?
%s missing required argument: module path
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.
Done.