Skip to content

Commit e9ff7a0

Browse files
authored
feat(internal/gensnippets): add license header and region tags (#3924)
1 parent 1e0efd8 commit e9ff7a0

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

internal/gensnippets/main.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package main
2222
import (
2323
"bytes"
2424
"flag"
25+
"fmt"
2526
"go/format"
2627
"go/printer"
2728
"go/token"
@@ -30,6 +31,7 @@ import (
3031
"os"
3132
"path/filepath"
3233
"strings"
34+
"time"
3335

3436
"cloud.google.com/go/internal/godocfx/pkgload"
3537
"cloud.google.com/go/third_party/go/doc"
@@ -81,37 +83,39 @@ func processExamples(pkg *doc.Package, fset *token.FileSet, trimPrefix, outDir s
8183
trimmed := strings.TrimPrefix(pkg.ImportPath, trimPrefix)
8284
outDir = filepath.Join(outDir, trimmed)
8385

86+
regionTag := "generated" + strings.ReplaceAll(trimmed, "/", "_")
87+
8488
// Note: variables and constants don't have examples.
8589

8690
for _, f := range pkg.Funcs {
8791
dir := filepath.Join(outDir, f.Name)
88-
if err := writeExamples(dir, f.Examples, fset); err != nil {
92+
if err := writeExamples(dir, f.Examples, fset, regionTag); err != nil {
8993
return err
9094
}
9195
}
9296

9397
for _, t := range pkg.Types {
9498
dir := filepath.Join(outDir, t.Name)
95-
if err := writeExamples(dir, t.Examples, fset); err != nil {
99+
if err := writeExamples(dir, t.Examples, fset, regionTag); err != nil {
96100
return err
97101
}
98102
for _, f := range t.Funcs {
99103
fDir := filepath.Join(dir, f.Name)
100-
if err := writeExamples(fDir, f.Examples, fset); err != nil {
104+
if err := writeExamples(fDir, f.Examples, fset, regionTag); err != nil {
101105
return err
102106
}
103107
}
104108
for _, m := range t.Methods {
105109
mDir := filepath.Join(dir, m.Name)
106-
if err := writeExamples(mDir, m.Examples, fset); err != nil {
110+
if err := writeExamples(mDir, m.Examples, fset, regionTag); err != nil {
107111
return err
108112
}
109113
}
110114
}
111115
return nil
112116
}
113117

114-
func writeExamples(outDir string, exs []*doc.Example, fset *token.FileSet) error {
118+
func writeExamples(outDir string, exs []*doc.Example, fset *token.FileSet, regionTag string) error {
115119
if len(exs) == 0 {
116120
// Nothing to do.
117121
return nil
@@ -148,9 +152,46 @@ func writeExamples(outDir string, exs []*doc.Example, fset *token.FileSet) error
148152
if err := os.MkdirAll(dir, 0755); err != nil {
149153
return err
150154
}
151-
if err := os.WriteFile(filename, []byte(s), 0644); err != nil {
155+
156+
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
157+
if err != nil {
158+
return err
159+
}
160+
defer f.Close()
161+
if _, err := f.WriteString(header()); err != nil {
162+
return err
163+
}
164+
tag := regionTag + "_" + ex.Name
165+
// Include an extra newline to keep separate from the package declaration.
166+
if _, err := fmt.Fprintf(f, "// [START %v]\n\n", tag); err != nil {
167+
return err
168+
}
169+
if _, err := f.WriteString(s); err != nil {
170+
return err
171+
}
172+
if _, err := fmt.Fprintf(f, "// [END %v]\n", tag); err != nil {
152173
return err
153174
}
154175
}
155176
return nil
156177
}
178+
179+
func header() string {
180+
return fmt.Sprintf(licenseHeader, time.Now().Year())
181+
}
182+
183+
const licenseHeader string = `// Copyright %v Google LLC
184+
//
185+
// Licensed under the Apache License, Version 2.0 (the "License");
186+
// you may not use this file except in compliance with the License.
187+
// You may obtain a copy of the License at
188+
//
189+
// http://www.apache.org/licenses/LICENSE-2.0
190+
//
191+
// Unless required by applicable law or agreed to in writing, software
192+
// distributed under the License is distributed on an "AS IS" BASIS,
193+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
194+
// See the License for the specific language governing permissions and
195+
// limitations under the License.
196+
197+
`

0 commit comments

Comments
 (0)