github.com/pankona/gometalinter@v2.0.11+incompatible/_linters/src/golang.org/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  	"io/ioutil"
    15  	"log"
    16  	"os"
    17  	"path"
    18  	"path/filepath"
    19  	"regexp"
    20  	"sort"
    21  	"strings"
    22  )
    23  
    24  func mustOpen(name string) io.Reader {
    25  	f, err := os.Open(name)
    26  	if err != nil {
    27  		log.Fatal(err)
    28  	}
    29  	return f
    30  }
    31  
    32  func api(base string) string {
    33  	return filepath.Join(os.Getenv("GOROOT"), "api", base)
    34  }
    35  
    36  var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
    37  
    38  func main() {
    39  	var buf bytes.Buffer
    40  	outf := func(format string, args ...interface{}) {
    41  		fmt.Fprintf(&buf, format, args...)
    42  	}
    43  	outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
    44  	outf("package imports\n")
    45  	outf("var stdlib = map[string]string{\n")
    46  	f := io.MultiReader(
    47  		mustOpen(api("go1.txt")),
    48  		mustOpen(api("go1.1.txt")),
    49  		mustOpen(api("go1.2.txt")),
    50  		mustOpen(api("go1.3.txt")),
    51  		mustOpen(api("go1.4.txt")),
    52  		mustOpen(api("go1.5.txt")),
    53  		mustOpen(api("go1.6.txt")),
    54  		mustOpen(api("go1.7.txt")),
    55  		mustOpen(api("go1.8.txt")),
    56  		mustOpen(api("go1.9.txt")),
    57  	)
    58  	sc := bufio.NewScanner(f)
    59  	fullImport := map[string]string{} // "zip.NewReader" => "archive/zip"
    60  	ambiguous := map[string]bool{}
    61  	var keys []string
    62  	for sc.Scan() {
    63  		l := sc.Text()
    64  		has := func(v string) bool { return strings.Contains(l, v) }
    65  		if has("struct, ") || has("interface, ") || has(", method (") {
    66  			continue
    67  		}
    68  		if m := sym.FindStringSubmatch(l); m != nil {
    69  			full := m[1]
    70  			key := path.Base(full) + "." + m[2]
    71  			if exist, ok := fullImport[key]; ok {
    72  				if exist != full {
    73  					ambiguous[key] = true
    74  				}
    75  			} else {
    76  				fullImport[key] = full
    77  				keys = append(keys, key)
    78  			}
    79  		}
    80  	}
    81  	if err := sc.Err(); err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	sort.Strings(keys)
    85  	for _, key := range keys {
    86  		if ambiguous[key] {
    87  			outf("\t// %q is ambiguous\n", key)
    88  		} else {
    89  			outf("\t%q: %q,\n", key, fullImport[key])
    90  		}
    91  	}
    92  	outf("\n")
    93  	for _, sym := range [...]string{"Alignof", "ArbitraryType", "Offsetof", "Pointer", "Sizeof"} {
    94  		outf("\t%q: %q,\n", "unsafe."+sym, "unsafe")
    95  	}
    96  	outf("}\n")
    97  	fmtbuf, err := format.Source(buf.Bytes())
    98  	if err != nil {
    99  		log.Fatal(err)
   100  	}
   101  	err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666)
   102  	if err != nil {
   103  		log.Fatal(err)
   104  	}
   105  }