github.com/bir3/gocompiler@v0.9.2202/src/cmd/gocmd/internal/test/genflags.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"log"
    12  	"os"
    13  	"github.com/bir3/gocompiler/exec"
    14  	"text/template"
    15  
    16  	"github.com/bir3/gocompiler/src/cmd/gocmd/internal/test/internal/genflags"
    17  )
    18  
    19  func main() {
    20  	if err := regenerate(); err != nil {
    21  		log.Fatal(err)
    22  	}
    23  }
    24  
    25  func regenerate() error {
    26  	vetAnalyzers, err := genflags.VetAnalyzers()
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	t := template.Must(template.New("fileTemplate").Parse(fileTemplate))
    32  	tData := map[string][]string{
    33  		"testFlags":	genflags.ShortTestFlags(),
    34  		"vetAnalyzers":	vetAnalyzers,
    35  	}
    36  	buf := bytes.NewBuffer(nil)
    37  	if err := t.Execute(buf, tData); err != nil {
    38  		return err
    39  	}
    40  
    41  	f, err := os.Create("flagdefs.go")
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	cmd := exec.Command("gofmt")
    47  	cmd.Stdin = buf
    48  	cmd.Stdout = f
    49  	cmd.Stderr = os.Stderr
    50  	cmdErr := cmd.Run()
    51  
    52  	if err := f.Close(); err != nil {
    53  		return err
    54  	}
    55  	if cmdErr != nil {
    56  		os.Remove(f.Name())
    57  		return cmdErr
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  const fileTemplate = `// Copyright 2019 The Go Authors. All rights reserved.
    64  // Use of this source code is governed by a BSD-style
    65  // license that can be found in the LICENSE file.
    66  
    67  // Code generated by genflags.go — DO NOT EDIT.
    68  
    69  package test
    70  
    71  // passFlagToTest contains the flags that should be forwarded to
    72  // the test binary with the prefix "test.".
    73  var passFlagToTest = map[string]bool {
    74  {{- range .testFlags}}
    75  	"{{.}}": true,
    76  {{- end }}
    77  }
    78  
    79  var passAnalyzersToVet = map[string]bool {
    80  {{- range .vetAnalyzers}}
    81  	"{{.}}": true,
    82  {{- end }}
    83  }
    84  `