2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
28 "golang.org/x/tools/imports"
35 func errs2strings(errors []error) []string {
36 strs := make([]string, len(errors))
37 for i := range errors {
38 strs[i] = errors[i].Error()
43 // ExecutePackages runs the generators for every package in 'packages'. 'outDir'
44 // is the base directory in which to place all the generated packages; it
45 // should be a physical path on disk, not an import path. e.g.:
46 // /path/to/home/path/to/gopath/src/
47 // Each package has its import path already, this will be appended to 'outDir'.
48 func (c *Context) ExecutePackages(outDir string, packages Packages) error {
50 for _, p := range packages {
51 if err := c.ExecutePackage(outDir, p); err != nil {
52 errors = append(errors, err)
56 return fmt.Errorf("some packages had errors:\n%v\n", strings.Join(errs2strings(errors), "\n"))
61 type DefaultFileType struct {
62 Format func([]byte) ([]byte, error)
63 Assemble func(io.Writer, *File)
66 func (ft DefaultFileType) AssembleFile(f *File, pathname string) error {
67 klog.V(2).Infof("Assembling file %q", pathname)
68 destFile, err := os.Create(pathname)
72 defer destFile.Close()
75 et := NewErrorTracker(b)
77 if et.Error() != nil {
80 if formatted, err := ft.Format(b.Bytes()); err != nil {
81 err = fmt.Errorf("unable to format file %q (%v).", pathname, err)
82 // Write the file anyway, so they can see what's going wrong and fix the generator.
83 if _, err2 := destFile.Write(b.Bytes()); err2 != nil {
88 _, err = destFile.Write(formatted)
93 func (ft DefaultFileType) VerifyFile(f *File, pathname string) error {
94 klog.V(2).Infof("Verifying file %q", pathname)
95 friendlyName := filepath.Join(f.PackageName, f.Name)
97 et := NewErrorTracker(b)
99 if et.Error() != nil {
102 formatted, err := ft.Format(b.Bytes())
104 return fmt.Errorf("unable to format the output for %q: %v", friendlyName, err)
106 existing, err := ioutil.ReadFile(pathname)
108 return fmt.Errorf("unable to read file %q for comparison: %v", friendlyName, err)
110 if bytes.Compare(formatted, existing) == 0 {
113 // Be nice and find the first place where they differ
115 for i < len(formatted) && i < len(existing) && formatted[i] == existing[i] {
118 eDiff, fDiff := existing[i:], formatted[i:]
119 if len(eDiff) > 100 {
122 if len(fDiff) > 100 {
125 return fmt.Errorf("output for %q differs; first existing/expected diff: \n %q\n %q", friendlyName, string(eDiff), string(fDiff))
128 func assembleGolangFile(w io.Writer, f *File) {
130 fmt.Fprintf(w, "package %v\n\n", f.PackageName)
132 if len(f.Imports) > 0 {
133 fmt.Fprint(w, "import (\n")
134 for i := range f.Imports {
135 if strings.Contains(i, "\"") {
136 // they included quotes, or are using the
137 // `name "path/to/pkg"` format.
138 fmt.Fprintf(w, "\t%s\n", i)
140 fmt.Fprintf(w, "\t%q\n", i)
143 fmt.Fprint(w, ")\n\n")
146 if f.Vars.Len() > 0 {
147 fmt.Fprint(w, "var (\n")
148 w.Write(f.Vars.Bytes())
149 fmt.Fprint(w, ")\n\n")
152 if f.Consts.Len() > 0 {
153 fmt.Fprint(w, "const (\n")
154 w.Write(f.Consts.Bytes())
155 fmt.Fprint(w, ")\n\n")
158 w.Write(f.Body.Bytes())
161 func importsWrapper(src []byte) ([]byte, error) {
162 return imports.Process("", src, nil)
165 func NewGolangFile() *DefaultFileType {
166 return &DefaultFileType{
167 Format: importsWrapper,
168 Assemble: assembleGolangFile,
172 // format should be one line only, and not end with \n.
173 func addIndentHeaderComment(b *bytes.Buffer, format string, args ...interface{}) {
175 fmt.Fprintf(b, "\n// "+format+"\n", args...)
177 fmt.Fprintf(b, "// "+format+"\n", args...)
181 func (c *Context) filteredBy(f func(*Context, *types.Type) bool) *Context {
183 c2.Order = []*types.Type{}
184 for _, t := range c.Order {
186 c2.Order = append(c2.Order, t)
192 // make a new context; inheret c.Namers, but add on 'namers'. In case of a name
193 // collision, the namer in 'namers' wins.
194 func (c *Context) addNameSystems(namers namer.NameSystems) *Context {
199 // Copy the existing name systems so we don't corrupt a parent context
200 c2.Namers = namer.NameSystems{}
201 for k, v := range c.Namers {
205 for name, namer := range namers {
206 c2.Namers[name] = namer
211 // ExecutePackage executes a single package. 'outDir' is the base directory in
212 // which to place the package; it should be a physical path on disk, not an
213 // import path. e.g.: '/path/to/home/path/to/gopath/src/' The package knows its
214 // import path already, this will be appended to 'outDir'.
215 func (c *Context) ExecutePackage(outDir string, p Package) error {
216 path := filepath.Join(outDir, p.Path())
217 klog.V(2).Infof("Processing package %q, disk location %q", p.Name(), path)
218 // Filter out any types the *package* doesn't care about.
219 packageContext := c.filteredBy(p.Filter)
220 os.MkdirAll(path, 0755)
221 files := map[string]*File{}
222 for _, g := range p.Generators(packageContext) {
223 // Filter out types the *generator* doesn't care about.
224 genContext := packageContext.filteredBy(g.Filter)
225 // Now add any extra name systems defined by this generator
226 genContext = genContext.addNameSystems(g.Namers(genContext))
228 fileType := g.FileType()
229 if len(fileType) == 0 {
230 return fmt.Errorf("generator %q must specify a file type", g.Name())
232 f := files[g.Filename()]
234 // This is the first generator to reference this file, so start it.
238 PackageName: p.Name(),
239 Header: p.Header(g.Filename()),
240 Imports: map[string]struct{}{},
244 if f.FileType != g.FileType() {
245 return fmt.Errorf("file %q already has type %q, but generator %q wants to use type %q", f.Name, f.FileType, g.Name(), g.FileType())
249 if vars := g.PackageVars(genContext); len(vars) > 0 {
250 addIndentHeaderComment(&f.Vars, "Package-wide variables from generator %q.", g.Name())
251 for _, v := range vars {
252 if _, err := fmt.Fprintf(&f.Vars, "%s\n", v); err != nil {
257 if consts := g.PackageConsts(genContext); len(consts) > 0 {
258 addIndentHeaderComment(&f.Consts, "Package-wide consts from generator %q.", g.Name())
259 for _, v := range consts {
260 if _, err := fmt.Fprintf(&f.Consts, "%s\n", v); err != nil {
265 if err := genContext.executeBody(&f.Body, g); err != nil {
268 if imports := g.Imports(genContext); len(imports) > 0 {
269 for _, i := range imports {
270 f.Imports[i] = struct{}{}
276 for _, f := range files {
277 finalPath := filepath.Join(path, f.Name)
278 assembler, ok := c.FileTypes[f.FileType]
280 return fmt.Errorf("the file type %q registered for file %q does not exist in the context", f.FileType, f.Name)
284 err = assembler.VerifyFile(f, finalPath)
286 err = assembler.AssembleFile(f, finalPath)
289 errors = append(errors, err)
293 return fmt.Errorf("errors in package %q:\n%v\n", p.Path(), strings.Join(errs2strings(errors), "\n"))
298 func (c *Context) executeBody(w io.Writer, generator Generator) error {
299 et := NewErrorTracker(w)
300 if err := generator.Init(c, et); err != nil {
303 for _, t := range c.Order {
304 if err := generator.GenerateType(c, t, et); err != nil {
308 if err := generator.Finalize(c, et); err != nil {