|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// +build go1.15 |
| 16 | + |
| 17 | +/*Command godocfx generates DocFX YAML for Go code. |
| 18 | +
|
| 19 | +Usage: |
| 20 | +
|
| 21 | + godocfx [flags] path |
| 22 | +
|
| 23 | + cd module && godocfx ./... |
| 24 | + godocfx cloud.google.com/go/... |
| 25 | + godocfx -print cloud.google.com/go/storage/... |
| 26 | + godocfx -out custom/output/dir cloud.google.com/go/... |
| 27 | + godocfx -rm cloud.google.com/go/... |
| 28 | +
|
| 29 | +See: |
| 30 | +* https://dotnet.github.io/docfx/spec/metadata_format_spec.html |
| 31 | +* https://github.com/googleapis/doc-templates |
| 32 | +* https://github.com/googleapis/doc-pipeline |
| 33 | +
|
| 34 | +TODO: |
| 35 | + * Cross link referenced packages. |
| 36 | +*/ |
| 37 | +package main |
| 38 | + |
| 39 | +import ( |
| 40 | + "flag" |
| 41 | + "fmt" |
| 42 | + "log" |
| 43 | + "os" |
| 44 | + "path/filepath" |
| 45 | + "strings" |
| 46 | + "time" |
| 47 | + |
| 48 | + "gopkg.in/yaml.v2" |
| 49 | +) |
| 50 | + |
| 51 | +func main() { |
| 52 | + print := flag.Bool("print", false, "Print instead of save (default false)") |
| 53 | + rm := flag.Bool("rm", false, "Delete out directory before generating") |
| 54 | + outDir := flag.String("out", "obj/api", "Output directory (default obj/api)") |
| 55 | + flag.Parse() |
| 56 | + if flag.NArg() != 1 { |
| 57 | + log.Fatalf("%s missing required argument: module path", os.Args[0]) |
| 58 | + } |
| 59 | + |
| 60 | + pages, toc, module, err := parse(flag.Arg(0)) |
| 61 | + if err != nil { |
| 62 | + log.Fatal(err) |
| 63 | + } |
| 64 | + |
| 65 | + if *print { |
| 66 | + if err := yaml.NewEncoder(os.Stdout).Encode(pages); err != nil { |
| 67 | + log.Fatal(err) |
| 68 | + } |
| 69 | + fmt.Println("----- toc.yaml") |
| 70 | + if err := yaml.NewEncoder(os.Stdout).Encode(toc); err != nil { |
| 71 | + log.Fatal(err) |
| 72 | + } |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if *rm { |
| 77 | + os.RemoveAll(*outDir) |
| 78 | + } |
| 79 | + if err := os.MkdirAll(*outDir, os.ModePerm); err != nil { |
| 80 | + log.Fatalf("os.MkdirAll: %v", err) |
| 81 | + } |
| 82 | + for path, p := range pages { |
| 83 | + // Make the module root page the index. |
| 84 | + if path == module.Path { |
| 85 | + path = "index" |
| 86 | + } |
| 87 | + // Trim the module path from all other paths. |
| 88 | + path = strings.TrimPrefix(path, module.Path+"/") |
| 89 | + path = filepath.Join(*outDir, path+".yml") |
| 90 | + if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { |
| 91 | + log.Fatalf("os.MkdirAll: %v", err) |
| 92 | + } |
| 93 | + f, err := os.Create(path) |
| 94 | + if err != nil { |
| 95 | + log.Fatal(err) |
| 96 | + } |
| 97 | + defer f.Close() |
| 98 | + fmt.Fprintln(f, "### YamlMime:UniversalReference") |
| 99 | + if err := yaml.NewEncoder(f).Encode(p); err != nil { |
| 100 | + log.Fatal(err) |
| 101 | + } |
| 102 | + |
| 103 | + path = filepath.Join(*outDir, "toc.yml") |
| 104 | + f, err = os.Create(path) |
| 105 | + if err != nil { |
| 106 | + log.Fatal(err) |
| 107 | + } |
| 108 | + defer f.Close() |
| 109 | + fmt.Fprintln(f, "### YamlMime:TableOfContent") |
| 110 | + if err := yaml.NewEncoder(f).Encode(toc); err != nil { |
| 111 | + log.Fatal(err) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Write the docuploader docs.metadata file. Not for DocFX. |
| 116 | + // See https://github.com/googleapis/docuploader/issues/11. |
| 117 | + // Example: |
| 118 | + /* |
| 119 | + update_time { |
| 120 | + seconds: 1600048103 |
| 121 | + nanos: 183052000 |
| 122 | + } |
| 123 | + name: "cloud.google.com/go" |
| 124 | + version: "v0.65.0" |
| 125 | + language: "go" |
| 126 | + */ |
| 127 | + path := filepath.Join(*outDir, "docs.metadata") |
| 128 | + f, err := os.Create(path) |
| 129 | + if err != nil { |
| 130 | + log.Fatal(err) |
| 131 | + } |
| 132 | + defer f.Close() |
| 133 | + now := time.Now().UTC() |
| 134 | + fmt.Fprintf(f, `update_time { |
| 135 | + seconds: %d |
| 136 | + nanos: %d |
| 137 | +} |
| 138 | +name: %q |
| 139 | +version: %q |
| 140 | +language: "go"`, now.Unix(), now.Nanosecond(), module.Path, module.Version) |
| 141 | +} |
0 commit comments