github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/imports/mkstdlib.go (about)

     1  // +build ignore
     2  
     3  // mkstdlib generates the zstdlib.go file, containing the Go standard
     4  // library API symbols. It's baked into the binary to avoid scanning
     5  // GOPATH in the common case.
     6  package main
     7  
     8  import (
     9  	"bufio"
    10  	"bytes"
    11  	"fmt"
    12  	"go/format"
    13  	"io"
    14  	"log"
    15  	"os"
    16  	"path"
    17  	"path/filepath"
    18  	"regexp"
    19  	"sort"
    20  	"strings"
    21  )
    22  
    23  func mustOpen(name string) io.Reader {
    24  	f, err := os.Open(name)
    25  	if err != nil {
    26  		log.Fatal(err)
    27  	}
    28  	return f
    29  }
    30  
    31  func api(base string) string {
    32  	return filepath.Join(os.Getenv("GOROOT"), "api", base)
    33  }
    34  
    35  var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
    36  
    37  func main() {
    38  	var buf bytes.Buffer
    39  	outf := func(format string, args ...interface{}) {
    40  		fmt.Fprintf(&buf, format, args...)
    41  	}
    42  	outf("// AUTO-GENERATED BY mkstdlib.go\n\n")
    43  	outf("package imports\n")
    44  	outf("var stdlib = map[string]string{\n")
    45  	f := io.MultiReader(
    46  		mustOpen(api("go1.txt")),
    47  		mustOpen(api("go1.1.txt")),
    48  		mustOpen(api("go1.2.txt")),
    49  		mustOpen(api("go1.3.txt")),
    50  		mustOpen(api("go1.4.txt")),
    51  		mustOpen(api("go1.5.txt")),
    52  	)
    53  	sc := bufio.NewScanner(f)
    54  	fullImport := map[string]string{} // "zip.NewReader" => "archive/zip"
    55  	ambiguous := map[string]bool{}
    56  	var keys []string
    57  	for sc.Scan() {
    58  		l := sc.Text()
    59  		has := func(v string) bool { return strings.Contains(l, v) }
    60  		if has("struct, ") || has("interface, ") || has(", method (") {
    61  			continue
    62  		}
    63  		if m := sym.FindStringSubmatch(l); m != nil {
    64  			full := m[1]
    65  			key := path.Base(full) + "." + m[2]
    66  			if exist, ok := fullImport[key]; ok {
    67  				if exist != full {
    68  					ambiguous[key] = true
    69  				}
    70  			} else {
    71  				fullImport[key] = full
    72  				keys = append(keys, key)
    73  			}
    74  		}
    75  	}
    76  	if err := sc.Err(); err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	sort.Strings(keys)
    80  	for _, key := range keys {
    81  		if ambiguous[key] {
    82  			outf("\t// %q is ambiguous\n", key)
    83  		} else {
    84  			outf("\t%q: %q,\n", key, fullImport[key])
    85  		}
    86  	}
    87  	outf("}\n")
    88  	fmtbuf, err := format.Source(buf.Bytes())
    89  	if err != nil {
    90  		log.Fatal(err)
    91  	}
    92  	os.Stdout.Write(fmtbuf)
    93  }