lang.go 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright 2018 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package language
  13. import (
  14. "github.com/bazelbuild/bazel-gazelle/internal/config"
  15. "github.com/bazelbuild/bazel-gazelle/internal/resolve"
  16. "github.com/bazelbuild/bazel-gazelle/internal/rule"
  17. )
  18. // Language describes an extension for Gazelle that provides support for
  19. // a set of Bazel rules.
  20. //
  21. // Languages are used primarily by the fix and update commands. The order
  22. // in which languages are used matters, since languages may depend on
  23. // one another. For example, go depends on proto, since go_proto_libraries
  24. // are generated from metadata stored in proto_libraries.
  25. //
  26. // A single instance of Language is created for each fix / update run. Some
  27. // state may be stored in this instance, but stateless behavior is encouraged,
  28. // especially since some operations may be concurrent in the future.
  29. //
  30. // Tasks languages are used for
  31. //
  32. // * Configuration (embedded interface config.Configurer). Languages may
  33. // define command line flags and alter the configuration in a directory
  34. // based on directives in build files.
  35. //
  36. // * Fixing deprecated usage of rules in build files.
  37. //
  38. // * Generating rules from source files in a directory.
  39. //
  40. // * Resolving library imports (embedded interface resolve.Resolver). For
  41. // example, import strings like "github.com/foo/bar" in Go can be resolved
  42. // into Bazel labels like "@com_github_foo_bar//:go_default_library".
  43. //
  44. // Tasks languages support
  45. //
  46. // * Generating load statements: languages list files and symbols that may
  47. // be loaded.
  48. //
  49. // * Merging generated rules into existing rules: languages provide metadata
  50. // that helps with rule matching, merging, and deletion.
  51. type Language interface {
  52. config.Configurer
  53. resolve.Resolver
  54. // Kinds returns a map of maps rule names (kinds) and information on how to
  55. // match and merge attributes that may be found in rules of those kinds. All
  56. // kinds of rules generated for this language may be found here.
  57. Kinds() map[string]rule.KindInfo
  58. // Loads returns .bzl files and symbols they define. Every rule generated by
  59. // GenerateRules, now or in the past, should be loadable from one of these
  60. // files.
  61. Loads() []rule.LoadInfo
  62. // GenerateRules extracts build metadata from source files in a directory.
  63. // GenerateRules is called in each directory where an update is requested
  64. // in depth-first post-order.
  65. //
  66. // c is the configuration for the current directory.
  67. // dir is the absolute path to the directory to scan.
  68. // rel is the relative path to the directory from the repository root.
  69. // f is the build file. It may be nil. It should not be modified.
  70. // subdirs is a list of subdirectory names.
  71. // regularFiles is a list of normal files in the directory.
  72. // genFiles is a list of generated files, found in outputs of rules.
  73. // otherEmpty and otherGen are lists of empty and generated rules created
  74. // by other languages processed before this language.
  75. //
  76. // empty is a list of empty rules that may be deleted after merge.
  77. // gen is a list of generated rules that may be updated or added.
  78. //
  79. // Any non-fatal errors this function encounters should be logged using
  80. // log.Print.
  81. GenerateRules(c *config.Config, dir, rel string, f *rule.File, subdirs, regularFiles, genFiles []string, otherEmpty, otherGen []*rule.Rule) (empty, gen []*rule.Rule)
  82. // Fix repairs deprecated usage of language-specific rules in f. This is
  83. // called before the file is indexed. Unless c.ShouldFix is true, fixes
  84. // that delete or rename rules should not be performed.
  85. Fix(c *config.Config, f *rule.File)
  86. }