func registerPlugins(src string, plugins features.Plugins) error { fset := token.NewFileSet() file := filepath.Join(src, importFile) f, err := parser.ParseFile(fset, file, nil, 0) if err != nil { return err } for _, m := range plugins.Packages() { astutil.AddNamedImport(fset, f, "_", m) } var buf bytes.Buffer err = printer.Fprint(&buf, fset, f) if err != nil { return err } // Save generated code file err = ioutil.WriteFile(file, buf.Bytes(), os.FileMode(0660)) if err != nil { return err } // run `go generate` for all plugins return goGenerate(plugins) }
// PrepareBuild prepares a custombuild.Builder for generating a custom binary using // plugins. A call to Build on the returned builder will generate the binary. // If error is not nil, the returned Builder should be ignored. func PrepareBuild(plugins features.Plugins, pullLatest bool) (custombuild.Builder, error) { // create builder builder, err := custombuild.NewUnready(CaddyPackage, gen(plugins), plugins.Packages()) if err != nil { return builder, err } builder.UseNetworkForAll(pullLatest) // if true, run go get -u for all dependencies before each build err = builder.Setup() if err != nil { // not useful, clear assets go builder.Teardown() return builder, err } // necessary to ensure import "github.com/mholt/caddy..." is referencing // this code. err = builder.SetImportPath(CaddyPackage) if err != nil { // not useful, clear assets go builder.Teardown() return builder, err } // set subpackage builder.SubPackage = "caddy" return builder, nil }