浏览代码

Improve the plugin template

Vadim Markovtsev 7 年之前
父节点
当前提交
31179983a8
共有 2 个文件被更改,包括 43 次插入11 次删除
  1. 18 3
      cmd/hercules-generate-plugin/main.go
  2. 25 8
      cmd/hercules-generate-plugin/plugin.template

+ 18 - 3
cmd/hercules-generate-plugin/main.go

@@ -4,6 +4,8 @@ import (
   "flag"
   "flag"
   "fmt"
   "fmt"
   "os"
   "os"
+  "path"
+  "runtime"
   "strings"
   "strings"
   "text/template"
   "text/template"
 
 
@@ -13,6 +15,13 @@ import (
 
 
 //go:generate go run embed.go
 //go:generate go run embed.go
 
 
+var SHLIB_EXT = map[string]string {
+  "window": "dll",
+  "linux": "so",
+  "darwin": "dylib",
+  "freebsd": "dylib",
+}
+
 func main() {
 func main() {
   var outputPath, name, varname, _flag, pkg string
   var outputPath, name, varname, _flag, pkg string
   var printVersion bool
   var printVersion bool
@@ -24,7 +33,7 @@ func main() {
   flag.StringVar(&_flag, "flag", "", "Name of the plugin activation cmdline flag, If not " +
   flag.StringVar(&_flag, "flag", "", "Name of the plugin activation cmdline flag, If not " +
       "specified, inferred from -varname.")
       "specified, inferred from -varname.")
   flag.BoolVar(&printVersion, "version", false, "Print version information and exit.")
   flag.BoolVar(&printVersion, "version", false, "Print version information and exit.")
-  flag.StringVar(&pkg, "package", "contrib", "Name of the package.")
+  flag.StringVar(&pkg, "package", "main", "Name of the package.")
   flag.Parse()
   flag.Parse()
   if printVersion {
   if printVersion {
 		fmt.Printf("Version: 3\nGit:     %s\n", hercules.GIT_HASH)
 		fmt.Printf("Version: 3\nGit:     %s\n", hercules.GIT_HASH)
@@ -38,6 +47,8 @@ func main() {
   splitted := camelcase.Split(name)
   splitted := camelcase.Split(name)
   if outputPath == "" {
   if outputPath == "" {
     outputPath = strings.ToLower(strings.Join(splitted, "_")) + ".go"
     outputPath = strings.ToLower(strings.Join(splitted, "_")) + ".go"
+  } else if !strings.HasSuffix(outputPath, ".go") {
+    panic("-o must end with \".go\"")
   }
   }
   gen := template.Must(template.New("plugin").Parse(PLUGIN_TEMPLATE_SOURCE))
   gen := template.Must(template.New("plugin").Parse(PLUGIN_TEMPLATE_SOURCE))
   outFile, err := os.Create(outputPath)
   outFile, err := os.Create(outputPath)
@@ -49,9 +60,13 @@ func main() {
     varname = strings.ToLower(splitted[0])
     varname = strings.ToLower(splitted[0])
   }
   }
   if _flag == "" {
   if _flag == "" {
-    _flag = varname
+    _flag = strings.Join(splitted, "-")
   }
   }
-  dict := map[string]string{"name": name, "varname": varname, "flag": _flag, "package": pkg}
+  outputBase := path.Base(outputPath)
+  shlib := outputBase[:len(outputBase)-2] + SHLIB_EXT[runtime.GOOS]
+  dict := map[string]string{
+    "name": name, "varname": varname, "flag": _flag, "package": pkg,
+    "output": outputPath, "shlib": shlib}
   err = gen.Execute(outFile, dict)
   err = gen.Execute(outFile, dict)
   if err != nil {
   if err != nil {
     panic(err)
     panic(err)

+ 25 - 8
cmd/hercules-generate-plugin/plugin.template

@@ -1,3 +1,8 @@
+// Hercules plugin
+// How to build: go build -buildmode=plugin {{.output}}
+// This command creates ./{{.shlib}}
+// Usage: hercules -plugin {{.shlib}} -{{.flag}}
+
 package {{.package}}
 package {{.package}}
 
 
 import (
 import (
@@ -16,36 +21,48 @@ type {{.name}}Result struct {
 
 
 // Analysis' name in the graph is usually the same as the type's name, however, does not have to.
 // Analysis' name in the graph is usually the same as the type's name, however, does not have to.
 func ({{.varname}} *{{.name}}) Name() string {
 func ({{.varname}} *{{.name}}) Name() string {
-	return "{{.name}}"
+  return "{{.name}}"
 }
 }
 
 
 // LeafPipelineItem-s normally do not act as intermediate nodes and thus we return an empty slice.
 // LeafPipelineItem-s normally do not act as intermediate nodes and thus we return an empty slice.
 func ({{.varname}} *{{.name}}) Provides() []string {
 func ({{.varname}} *{{.name}}) Provides() []string {
-	return []string{}
+  return []string{}
 }
 }
 
 
 // Requires returns the list of dependencies which must be supplied in Consume().
 // Requires returns the list of dependencies which must be supplied in Consume().
 func ({{.varname}} *{{.name}}) Requires() []string {
 func ({{.varname}} *{{.name}}) Requires() []string {
-	arr := [...]string{/* insert dependencies here */}
-	return arr[:]
+  arr := [...]string{/* insert dependencies here */}
+  return arr[:]
 }
 }
 
 
+// ListConfigurationOptions tells the engine which parameters can be changed through the command
+// line.
 func ({{.varname}} *{{.name}}) ListConfigurationOptions() []hercules.ConfigurationOption {
 func ({{.varname}} *{{.name}}) ListConfigurationOptions() []hercules.ConfigurationOption {
-	return []hercules.ConfigurationOption{}
+  opts := [...]hercules.ConfigurationOption{ /* {
+    Name:        "ParameterName",
+    Description: "Parameter's description.",
+    Flag:        "my-cmdline-flag",
+    Type:        hercules.BoolConfigurationOption,
+    Default:     false}, */
+  }
+  return opts[:]
 }
 }
 
 
+// Flag returns the command line switch which activates the analysis.
 func ({{.varname}} *{{.name}}) Flag() string {
 func ({{.varname}} *{{.name}}) Flag() string {
-	return "{{.flag}}"
+  return "{{.flag}}"
 }
 }
 
 
+// Configure applies the parameters specified in the command line. Map keys correspond to "Name".
 func ({{.varname}} *{{.name}}) Configure(facts map[string]interface{}) {
 func ({{.varname}} *{{.name}}) Configure(facts map[string]interface{}) {
 }
 }
 
 
+// Initialize resets the internal temporary data structures and prepares the object for Consume().
 func ({{.varname}} *{{.name}}) Initialize(repository *git.Repository) {
 func ({{.varname}} *{{.name}}) Initialize(repository *git.Repository) {
 }
 }
 
 
 func ({{.varname}} *{{.name}}) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
 func ({{.varname}} *{{.name}}) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
-	return nil, nil
+  return nil, nil
 }
 }
 
 
 func ({{.varname}} *{{.name}}) Finalize() interface{} {
 func ({{.varname}} *{{.name}}) Finalize() interface{} {
@@ -55,5 +72,5 @@ func ({{.varname}} *{{.name}}) Finalize() interface{} {
 }
 }
 
 
 func init() {
 func init() {
-	hercules.Registry.Register(&{{.name}}{})
+  hercules.Registry.Register(&{{.name}}{})
 }
 }