Add some documentation to generate-plugins

This commit is contained in:
Chris Bednarski 2015-10-13 02:38:31 -07:00
parent 9c68f039b3
commit eb8a0bf731
1 changed files with 17 additions and 7 deletions

View File

@ -20,6 +20,9 @@ import (
const target = "command/plugin.go" const target = "command/plugin.go"
func main() { func main() {
// Normally this is run via go:generate from the command folder so we need
// to cd .. first. But when developing it's easier to use go run, so we'll
// support that too.
wd, _ := os.Getwd() wd, _ := os.Getwd()
if filepath.Base(wd) != "packer" { if filepath.Base(wd) != "packer" {
os.Chdir("..") os.Chdir("..")
@ -29,6 +32,7 @@ func main() {
} }
} }
// Collect all of the data we need about plugins we have in the project
builders, err := discoverBuilders() builders, err := discoverBuilders()
if err != nil { if err != nil {
log.Fatalf("Failed to discover builders: %s", err) log.Fatalf("Failed to discover builders: %s", err)
@ -44,6 +48,7 @@ func main() {
log.Fatalf("Failed to discover post processors: %s", err) log.Fatalf("Failed to discover post processors: %s", err)
} }
// Do some simple code generation and templating
output := source output := source
output = strings.Replace(output, "IMPORTS", makeImports(builders, provisioners, postProcessors), 1) output = strings.Replace(output, "IMPORTS", makeImports(builders, provisioners, postProcessors), 1)
output = strings.Replace(output, "BUILDERS", makeMap("Builders", "Builder", builders), 1) output = strings.Replace(output, "BUILDERS", makeMap("Builders", "Builder", builders), 1)
@ -53,22 +58,27 @@ func main() {
// TODO sort the lists of plugins so we are not subjected to random OS ordering of the plugin lists // TODO sort the lists of plugins so we are not subjected to random OS ordering of the plugin lists
// TODO format the file // TODO format the file
// Write our generated code to the command/plugin.go file
file, err := os.Create(target) file, err := os.Create(target)
defer file.Close()
if err != nil { if err != nil {
log.Fatalf("Failed to open %s for writing: %s", target, err) log.Fatalf("Failed to open %s for writing: %s", target, err)
} }
file.WriteString(output)
file.Close() _, err = file.WriteString(output)
if err != nil {
log.Fatalf("Failed writing to %s: %s", target, err)
}
log.Printf("Generated %s", target) log.Printf("Generated %s", target)
} }
type plugin struct { type plugin struct {
Package string Package string // This plugin's package name (iso)
PluginName string PluginName string // Name of plugin (vmware-iso)
TypeName string TypeName string // Type of plugin (builder)
Path string Path string // Path relative to packer root (builder/vmware/iso)
ImportName string ImportName string // PluginName+TypeName (vmwareisobuilder)
} }
// makeMap creates a map named Name with type packer.Name that looks something // makeMap creates a map named Name with type packer.Name that looks something