2017-08-03 00:26:21 +03:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-02-20 02:54:48 +03:00
|
|
|
//go:build ignore
|
2017-08-03 00:26:21 +03:00
|
|
|
|
|
|
|
// The update-readmes.go tool creates or updates README.md files in
|
|
|
|
// the golang.org/x/build tree. It only updates files if they are
|
2021-03-10 00:26:59 +03:00
|
|
|
// missing or were previously generated by this tool. If the file
|
|
|
|
// contains a "<!-- End of auto-generated section -->" comment,
|
|
|
|
// the tool leaves content in the rest of the file unmodified.
|
2017-08-03 00:26:21 +03:00
|
|
|
//
|
|
|
|
// The auto-generated Markdown contains the package doc synopsis
|
2021-03-10 00:26:59 +03:00
|
|
|
// and a link to pkg.go.dev for the API reference.
|
2017-08-03 00:26:21 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/build"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
root, err := build.Import("golang.org/x/build", "", build.FindOnly)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to find golang.org/x/build root: %v", err)
|
|
|
|
}
|
|
|
|
err = filepath.Walk(root.Dir, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rest := strings.TrimPrefix(strings.TrimPrefix(path, root.Dir), "/")
|
|
|
|
switch rest {
|
|
|
|
case "env", "version", "vendor":
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
pkgName := "golang.org/x/build/" + filepath.ToSlash(rest)
|
|
|
|
|
2021-03-10 00:26:59 +03:00
|
|
|
bctx := build.Default
|
|
|
|
bctx.Dir = path // Set Dir since some x/build packages are in nested modules.
|
|
|
|
pkg, err := bctx.Import(pkgName, "", 0)
|
2017-08-03 00:26:21 +03:00
|
|
|
if err != nil {
|
|
|
|
// Skip.
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-15 06:37:22 +03:00
|
|
|
if pkg.Doc == "" {
|
|
|
|
// There's no package comment, so don't create an empty README.
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-03 00:26:21 +03:00
|
|
|
if _, err := os.Stat(filepath.Join(pkg.Dir, "README")); err == nil {
|
|
|
|
// Directory has exiting README; don't touch.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
readmePath := filepath.Join(pkg.Dir, "README.md")
|
2023-09-08 19:42:54 +03:00
|
|
|
exist, err := os.ReadFile(readmePath)
|
2017-08-03 00:26:21 +03:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
// A real error.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
const header = "Auto-generated by x/build/update-readmes.go"
|
|
|
|
if len(exist) > 0 && !bytes.Contains(exist, []byte(header)) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-10 00:26:59 +03:00
|
|
|
var footer []byte
|
|
|
|
if i := bytes.Index(exist, []byte("<!-- End of auto-generated section -->")); i != -1 {
|
|
|
|
footer = exist[i:]
|
|
|
|
}
|
2017-08-03 00:26:21 +03:00
|
|
|
newContents := []byte(fmt.Sprintf(`<!-- %s -->
|
|
|
|
|
2020-12-15 06:37:22 +03:00
|
|
|
[![Go Reference](https://pkg.go.dev/badge/%s.svg)](https://pkg.go.dev/%s)
|
2017-08-03 00:26:21 +03:00
|
|
|
|
|
|
|
# %s
|
|
|
|
|
|
|
|
%s
|
2021-03-10 00:26:59 +03:00
|
|
|
%s`, header, pkgName, pkgName, pkgName, pkg.Doc, footer))
|
2017-08-03 00:26:21 +03:00
|
|
|
if bytes.Equal(exist, newContents) {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-08 19:42:54 +03:00
|
|
|
if err := os.WriteFile(readmePath, newContents, 0644); err != nil {
|
2017-08-03 00:26:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("Wrote %s", readmePath)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|