plugin.template 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.v10"
  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. // Logger for consistent output
  34. l hercules.Logger
  35. }
  36. // {{.name}}Result is returned by Finalize() and represents the analysis result.
  37. type {{.name}}Result struct {
  38. }
  39. // Analysis' name in the graph is usually the same as the type's name, however, does not have to.
  40. func ({{.varname}} *{{.name}}) Name() string {
  41. return "{{.name}}"
  42. }
  43. // LeafPipelineItem-s normally do not act as intermediate nodes and thus we return an empty slice.
  44. func ({{.varname}} *{{.name}}) Provides() []string {
  45. return []string{}
  46. }
  47. // Requires returns the list of dependencies which must be supplied in Consume().
  48. func ({{.varname}} *{{.name}}) Requires() []string {
  49. arr := [...]string{/* insert dependencies here */}
  50. return arr[:]
  51. }
  52. // ListConfigurationOptions tells the engine which parameters can be changed through the command
  53. // line.
  54. func ({{.varname}} *{{.name}}) ListConfigurationOptions() []hercules.ConfigurationOption {
  55. opts := [...]hercules.ConfigurationOption{ /* {
  56. Name: "ParameterName",
  57. Description: "Parameter's description.",
  58. Flag: "my-cmdline-flag",
  59. Type: hercules.BoolConfigurationOption,
  60. Default: false}, */
  61. }
  62. return opts[:]
  63. }
  64. // Flag returns the command line switch which activates the analysis.
  65. func ({{.varname}} *{{.name}}) Flag() string {
  66. return "{{.flag}}"
  67. }
  68. // Description returns the text which explains what the analysis is doing.
  69. func ({{.varname}} *{{.name}}) Description() string {
  70. return "TODO: explain what this analysis is doing."
  71. }
  72. // Configure applies the parameters specified in the command line. Map keys correspond to "Name".
  73. func ({{.varname}} *{{.name}}) Configure(facts map[string]interface{}) error {
  74. if l, exists := facts[hercules.ConfigLogger].(hercules.Logger); exists {
  75. {{.varname}}.l = l
  76. }
  77. return nil
  78. }
  79. // Initialize resets the internal temporary data structures and prepares the object for Consume().
  80. func ({{.varname}} *{{.name}}) Initialize(repository *git.Repository) error {
  81. {{.varname}}.l = hercules.NewLogger()
  82. {{.varname}}.OneShotMergeProcessor.Initialize()
  83. return nil
  84. }
  85. // Consume is called for every commit in the sequence.
  86. func ({{.varname}} *{{.name}}) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  87. if !{{.varname}}.ShouldConsumeCommit(deps) {
  88. return nil, nil
  89. }
  90. return nil, nil
  91. }
  92. // Fork clones the same item several times on branches.
  93. func ({{.varname}} *{{.name}}) Fork(n int) []hercules.PipelineItem {
  94. return hercules.ForkSamePipelineItem({{.varname}}, n)
  95. }
  96. // Finalize produces the result of the analysis. No more Consume() calls are expected afterwards.
  97. func ({{.varname}} *{{.name}}) Finalize() interface{} {
  98. result := {{.name}}Result{}
  99. // insert code here
  100. return result
  101. }
  102. // Serialize converts the result from Finalize() to either Protocol Buffers or YAML.
  103. func ({{.varname}} *{{.name}}) Serialize(result interface{}, binary bool, writer io.Writer) error {
  104. {{.varname}}Result := result.({{.name}}Result)
  105. if binary {
  106. return {{.varname}}.serializeBinary(&{{.varname}}Result, writer)
  107. }
  108. {{.varname}}.serializeText(&{{.varname}}Result, writer)
  109. return nil
  110. }
  111. func ({{.varname}} *{{.name}}) serializeText(result *{{.name}}Result, writer io.Writer) {
  112. // write YAML to writer
  113. }
  114. func ({{.varname}} *{{.name}}) serializeBinary(result *{{.name}}Result, writer io.Writer) error {
  115. message := {{.name}}ResultMessage{
  116. // fill me
  117. }
  118. serialized, err := proto.Marshal(&message)
  119. if err != nil {
  120. return err
  121. }
  122. writer.Write(serialized)
  123. return nil
  124. }
  125. func init() {
  126. hercules.Registry.Register(&{{.name}}{})
  127. }