plugin.template 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Hercules plugin "{{.name}}"
  2. //
  3. // How to build: execute "make" *OR*
  4. //
  5. // 1. Update the Protocol Buffers definition as needed, regenerate {{.protogo}}
  6. //
  7. // PATH=$PATH:$GOPATH/bin protoc --gogo_out={{.outdir}} --proto_path={{.outdir}} {{.proto}}
  8. //
  9. // 2. Build {{.shlib}}
  10. //
  11. // go build -buildmode=plugin -linkshared {{.output}} {{.protogo}}
  12. //
  13. // Step (1) requires GoGo Protobuf https://github.com/gogo/protobuf
  14. //
  15. // Usage:
  16. //
  17. // hercules --plugin {{.shlib}} --{{.flag}}
  18. // hercules --plugin {{.shlib}} -help
  19. package {{.package}}
  20. import (
  21. "io"
  22. "github.com/gogo/protobuf/proto"
  23. "gopkg.in/src-d/go-git.v4"
  24. "gopkg.in/src-d/hercules.v9"
  25. )
  26. // {{.name}} contains the intermediate state which is mutated by Consume(). It should implement
  27. // hercules.LeafPipelineItem.
  28. type {{.name}} struct {
  29. // No special branch merge logic is required
  30. hercules.NoopMerger
  31. // Process each merge commit only once
  32. hercules.OneShotMergeProcessor
  33. }
  34. // {{.name}}Result is returned by Finalize() and represents the analysis result.
  35. type {{.name}}Result struct {
  36. }
  37. // Analysis' name in the graph is usually the same as the type's name, however, does not have to.
  38. func ({{.varname}} *{{.name}}) Name() string {
  39. return "{{.name}}"
  40. }
  41. // LeafPipelineItem-s normally do not act as intermediate nodes and thus we return an empty slice.
  42. func ({{.varname}} *{{.name}}) Provides() []string {
  43. return []string{}
  44. }
  45. // Requires returns the list of dependencies which must be supplied in Consume().
  46. func ({{.varname}} *{{.name}}) Requires() []string {
  47. arr := [...]string{/* insert dependencies here */}
  48. return arr[:]
  49. }
  50. // ListConfigurationOptions tells the engine which parameters can be changed through the command
  51. // line.
  52. func ({{.varname}} *{{.name}}) ListConfigurationOptions() []hercules.ConfigurationOption {
  53. opts := [...]hercules.ConfigurationOption{ /* {
  54. Name: "ParameterName",
  55. Description: "Parameter's description.",
  56. Flag: "my-cmdline-flag",
  57. Type: hercules.BoolConfigurationOption,
  58. Default: false}, */
  59. }
  60. return opts[:]
  61. }
  62. // Flag returns the command line switch which activates the analysis.
  63. func ({{.varname}} *{{.name}}) Flag() string {
  64. return "{{.flag}}"
  65. }
  66. // Description returns the text which explains what the analysis is doing.
  67. func ({{.varname}} *{{.name}}) Description() string {
  68. return "TODO: explain what this analysis is doing."
  69. }
  70. // Configure applies the parameters specified in the command line. Map keys correspond to "Name".
  71. func ({{.varname}} *{{.name}}) Configure(facts map[string]interface{}) error {
  72. return nil
  73. }
  74. // Initialize resets the internal temporary data structures and prepares the object for Consume().
  75. func ({{.varname}} *{{.name}}) Initialize(repository *git.Repository) error {
  76. {{.varname}}.OneShotMergeProcessor.Initialize()
  77. return nil
  78. }
  79. // Consume is called for every commit in the sequence.
  80. func ({{.varname}} *{{.name}}) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  81. if !{{.varname}}.ShouldConsumeCommit(deps) {
  82. return nil, nil
  83. }
  84. return nil, nil
  85. }
  86. // Fork clones the same item several times on branches.
  87. func ({{.varname}} *{{.name}}) Fork(n int) []hercules.PipelineItem {
  88. return hercules.ForkSamePipelineItem({{.varname}}, n)
  89. }
  90. // Finalize produces the result of the analysis. No more Consume() calls are expected afterwards.
  91. func ({{.varname}} *{{.name}}) Finalize() interface{} {
  92. result := {{.name}}Result{}
  93. // insert code here
  94. return result
  95. }
  96. // Serialize converts the result from Finalize() to either Protocol Buffers or YAML.
  97. func ({{.varname}} *{{.name}}) Serialize(result interface{}, binary bool, writer io.Writer) error {
  98. {{.varname}}Result := result.({{.name}}Result)
  99. if binary {
  100. return {{.varname}}.serializeBinary(&{{.varname}}Result, writer)
  101. }
  102. {{.varname}}.serializeText(&{{.varname}}Result, writer)
  103. return nil
  104. }
  105. func ({{.varname}} *{{.name}}) serializeText(result *{{.name}}Result, writer io.Writer) {
  106. // write YAML to writer
  107. }
  108. func ({{.varname}} *{{.name}}) serializeBinary(result *{{.name}}Result, writer io.Writer) error {
  109. message := {{.name}}ResultMessage{
  110. // fill me
  111. }
  112. serialized, err := proto.Marshal(&message)
  113. if err != nil {
  114. return err
  115. }
  116. writer.Write(serialized)
  117. return nil
  118. }
  119. func init() {
  120. hercules.Registry.Register(&{{.name}}{})
  121. }