Skip to content

Commit

Permalink
refactor: remove python_coarse_grained_generation
Browse files Browse the repository at this point in the history
Also add the python_generation_mode directive.

Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
  • Loading branch information
f0rmiga committed Oct 27, 2021
1 parent 3df1413 commit 20b6911
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 15 deletions.
4 changes: 2 additions & 2 deletions gazelle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ Python-specific directives are as follows:
| Controls the ignored dependencies from the generated targets. | |
| `# gazelle:python_validate_import_statements`| `true` |
| Controls whether the Python import statements should be validated. Can be "true" or "false" | |
| `# gazelle:python_coarse_grained_generation`| `false` |
| Controls whether a single target should be created in this package, `glob`'ing sub-directories rather than fine-grained target generation. Can be "true" or "false" | |
| `# gazelle:python_generation_mode`| `package` |
| Controls the target generation mode. Can be "package" or "project" | |
| `# gazelle:python_library_naming_convention`| `$package_name$` |
| Controls the `py_library` naming convention. It interpolates $package_name$ with the Bazel package name. E.g. if the Bazel package name is `foo`, setting this to `$package_name$_my_lib` would result in a generated target named `foo_my_lib`. | |
| `# gazelle:python_binary_naming_convention` | `$package_name$_bin` |
Expand Down
15 changes: 10 additions & 5 deletions gazelle/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (py *Configurer) KnownDirectives() []string {
pythonconfig.IgnoreFilesDirective,
pythonconfig.IgnoreDependenciesDirective,
pythonconfig.ValidateImportStatementsDirective,
pythonconfig.CoarseGrainedGeneration,
pythonconfig.GenerationMode,
pythonconfig.LibraryNamingConvention,
pythonconfig.BinaryNamingConvention,
pythonconfig.TestNamingConvention,
Expand Down Expand Up @@ -119,12 +119,17 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
log.Fatal(err)
}
config.SetValidateImportStatements(v)
case pythonconfig.CoarseGrainedGeneration:
v, err := strconv.ParseBool(strings.TrimSpace(d.Value))
if err != nil {
case pythonconfig.GenerationMode:
switch pythonconfig.GenerationModeType(strings.TrimSpace(d.Value)) {
case pythonconfig.GenerationModePackage:
config.SetCoarseGrainedGeneration(false)
case pythonconfig.GenerationModeProject:
config.SetCoarseGrainedGeneration(true)
default:
err := fmt.Errorf("invalid value for directive %q: %s",
pythonconfig.GenerationMode, d.Value)
log.Fatal(err)
}
config.SetCoarseGrainedGeneration(v)
case pythonconfig.LibraryNamingConvention:
config.SetLibraryNamingConvention(strings.TrimSpace(d.Value))
case pythonconfig.BinaryNamingConvention:
Expand Down
22 changes: 18 additions & 4 deletions gazelle/pythonconfig/pythonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ const (
// ValidateImportStatementsDirective represents the directive that controls
// whether the Python import statements should be validated.
ValidateImportStatementsDirective = "python_validate_import_statements"
// CoarseGrainedGeneration represents the directive that controls whether a
// single target should be created englobing sub-directories rather than the
// default fine-grained target generation.
CoarseGrainedGeneration = "python_coarse_grained_generation"
// GenerationMode represents the directive that controls the target generation
// mode. See below for the GenerationModeType constants.
GenerationMode = "python_generation_mode"
// LibraryNamingConvention represents the directive that controls the
// py_library naming convention. It interpolates $package_name$ with the
// Bazel package name. E.g. if the Bazel package name is `foo`, setting this
Expand All @@ -51,6 +50,21 @@ const (
TestNamingConvention = "python_test_naming_convention"
)

// GenerationModeType represents one of the generation modes for the Python
// extension.
type GenerationModeType string

// Generation modes
const (
// GenerationModePackage defines the mode in which targets will be generated
// for each __init__.py, or when an existing BUILD or BUILD.bazel file already
// determines a Bazel package.
GenerationModePackage GenerationModeType = "package"
// GenerationModeProject defines the mode in which a coarse-grained target will
// be generated englobing sub-directories containing Python files.
GenerationModeProject GenerationModeType = "project"
)

const (
packageNameNamingConventionSubstitution = "$package_name$"
)
Expand Down
2 changes: 1 addition & 1 deletion gazelle/testdata/monorepo/coarse_grained/BUILD.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@rules_python//python:defs.bzl", "py_library")

# gazelle:python_extension enabled
# gazelle:python_root
# gazelle:python_coarse_grained_generation true
# gazelle:python_generation_mode project

# gazelle:exclude bar/baz/*_excluded.py

Expand Down
2 changes: 1 addition & 1 deletion gazelle/testdata/monorepo/coarse_grained/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@rules_python//python:defs.bzl", "py_library")

# gazelle:python_extension enabled
# gazelle:python_root
# gazelle:python_coarse_grained_generation true
# gazelle:python_generation_mode project

# gazelle:exclude bar/baz/*_excluded.py

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# gazelle:python_coarse_grained_generation false
# gazelle:python_generation_mode package
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_python//python:defs.bzl", "py_library")

# gazelle:python_coarse_grained_generation false
# gazelle:python_generation_mode package

py_library(
name = "_boundary",
Expand Down

0 comments on commit 20b6911

Please sign in to comment.