Skip to content

Commit 489f979

Browse files
committed
chore: address PR feedback
Signed-off-by: behnazh-w <[email protected]>
1 parent bfbaff7 commit 489f979

File tree

10 files changed

+132
-96
lines changed

10 files changed

+132
-96
lines changed

src/macaron/build_spec_generator/build_command_patcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _patch_commands(
7373
try:
7474
new_cli_command = effective_cli_parser.apply_patch(
7575
cli_command=cli_command,
76-
options_patch=patch,
76+
patch_options=patch,
7777
)
7878
except PatchBuildCommandError as error:
7979
logger.error(

src/macaron/build_spec_generator/cli_command_parser/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,16 @@ def to_cmds(self) -> list[str]:
114114
"""Return the CLI Command as a list of strings."""
115115

116116

117+
# T is a generic type variable restricted to subclasses of CLICommand.
118+
# It ensures that only derived types of CLICommand can be used with
119+
# generic classes or functions parameterized by T.
117120
T = TypeVar("T", bound="CLICommand")
121+
122+
# Y_contra is a contravariant type variable intended for CLI argument
123+
# patch values. Using contravariance allows generic classes or functions
124+
# to accept supertypes of the specified type parameter, making it easier
125+
# to support broader value types when implementing patching for different
126+
# build tools.
118127
Y_contra = TypeVar("Y_contra", contravariant=True)
119128

120129

@@ -160,6 +169,6 @@ def is_build_tool(self, executable_path: str) -> bool:
160169
def apply_patch(
161170
self,
162171
cli_command: T,
163-
options_patch: Mapping[str, Y_contra | None],
172+
patch_options: Mapping[str, Y_contra | None],
164173
) -> T:
165174
"""Return a new CLICommand object with its option patched, while persisting the executable path."""

src/macaron/build_spec_generator/cli_command_parser/gradle_cli_command.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,4 @@ class GradleCLICommand:
382382

383383
def to_cmds(self) -> list[str]:
384384
"""Return the CLI Command as a list of strings."""
385-
result = []
386-
result.append(self.executable)
387-
result.extend(self.options.to_option_cmds())
388-
return result
385+
return [self.executable] + self.options.to_option_cmds()

src/macaron/build_spec_generator/cli_command_parser/gradle_cli_parser.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[bool]:
5252

5353
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
5454
"""Add a new argument to argparser.ArgumentParser representing this option."""
55-
kwargs: dict[str, Any] = {}
56-
57-
kwargs["action"] = "store_true"
55+
kwargs: dict[str, Any] = {"action": "store_true"}
5856
if self.dest:
5957
kwargs["dest"] = self.dest
6058

@@ -75,7 +73,7 @@ def get_patch_type_str(self) -> str:
7573

7674

7775
@dataclass
78-
class GradleOptionalNegateableFlag(OptionDef[bool]):
76+
class GradleOptionalNegatableFlag(OptionDef[bool]):
7977
"""This option represents an optional negatable flag in Gradle CLI command.
8078
8179
For example: --build-cache/--no-build-cache
@@ -144,7 +142,7 @@ def get_patch_type_str(self) -> str:
144142

145143

146144
@dataclass
147-
class GradlePropeties(OptionDef[dict[str, str | None]]):
145+
class GradleProperties(OptionDef[dict[str, str | None]]):
148146
"""This option represents an option used to define property values of a Gradle CLI command.
149147
150148
This option can be defined multiple times and the values are appended into a list of string in argparse.
@@ -175,7 +173,7 @@ def get_patch_type_str(self) -> str:
175173

176174
@dataclass
177175
class GradleTask(OptionDef[list[str]]):
178-
"""This option represents the positional task option in Maven CLI command.
176+
"""This option represents the positional task option in Gradle CLI command.
179177
180178
argparse.Namespace stores this as a list of string. This is stored internally as a list of string.
181179
"""
@@ -314,25 +312,25 @@ def get_patch_type_str(self) -> str:
314312
short_names=None,
315313
long_name="--write-locks",
316314
),
317-
GradleOptionalNegateableFlag(
315+
GradleOptionalNegatableFlag(
318316
long_name="--build-cache",
319317
),
320-
GradleOptionalNegateableFlag(
318+
GradleOptionalNegatableFlag(
321319
long_name="--configuration-cache",
322320
),
323-
GradleOptionalNegateableFlag(
321+
GradleOptionalNegatableFlag(
324322
long_name="--configure-on-demand",
325323
),
326-
GradleOptionalNegateableFlag(
324+
GradleOptionalNegatableFlag(
327325
long_name="--daemon",
328326
),
329-
GradleOptionalNegateableFlag(
327+
GradleOptionalNegatableFlag(
330328
long_name="--parallel",
331329
),
332-
GradleOptionalNegateableFlag(
330+
GradleOptionalNegatableFlag(
333331
long_name="--scan",
334332
),
335-
GradleOptionalNegateableFlag(
333+
GradleOptionalNegatableFlag(
336334
long_name="--watch-fs",
337335
),
338336
# This has been validated by setting up a minimal gradle project. Gradle version 8.14.2
@@ -430,11 +428,11 @@ def get_patch_type_str(self) -> str:
430428
short_name=None,
431429
long_name="--warning-mode",
432430
),
433-
GradlePropeties(
431+
GradleProperties(
434432
short_name="-D",
435433
long_name="--system-prop",
436434
),
437-
GradlePropeties(
435+
GradleProperties(
438436
short_name="-P",
439437
long_name="--project-prop",
440438
),
@@ -447,7 +445,7 @@ def get_patch_type_str(self) -> str:
447445
class GradleCLICommandParser:
448446
"""A Gradle CLI Command Parser."""
449447

450-
ACCEPTABLE_EXECUTABLE = ["gradle", "gradlew"]
448+
ACCEPTABLE_EXECUTABLE = {"gradle", "gradlew"}
451449

452450
def __init__(self) -> None:
453451
"""Initialize the instance."""
@@ -561,8 +559,35 @@ def _patch_properties_mapping(
561559
option_long_name: str,
562560
patch_value: GradleOptionPatchValueType,
563561
) -> dict[str, str]:
562+
"""
563+
Apply a patch value to an existing properties dictionary for a specified Gradle option.
564+
565+
This function locates the metadata definition for the given option by its long name,
566+
ensures it is a properties-type option, validates the patch value type, and then
567+
applies the patch using `patch_mapping`. Throws a `PatchBuildCommandError` if the
568+
option is not valid or the patch value's type is incorrect.
569+
570+
Parameters
571+
----------
572+
original_props: dict[str, str]
573+
The original mapping of property names to values.
574+
option_long_name: str
575+
The long name of the Gradle option to patch.
576+
patch_value: GradleOptionPatchValueType
577+
The patch to apply to the properties mapping.
578+
579+
Returns
580+
-------
581+
dict[str, str]
582+
The updated properties mapping after applying the patch.
583+
584+
Raises
585+
------
586+
PatchBuildCommandError
587+
If the option is not a valid properties-type option or the patch value does not have a valid type.
588+
"""
564589
prop_opt_def = self.option_defs.get(option_long_name)
565-
if not prop_opt_def or not isinstance(prop_opt_def, GradlePropeties):
590+
if not prop_opt_def or not isinstance(prop_opt_def, GradleProperties):
566591
raise PatchBuildCommandError(f"{option_long_name} from the patch is not a property type option.")
567592

568593
if not prop_opt_def.is_valid_patch_option(patch_value):
@@ -578,11 +603,11 @@ def _patch_properties_mapping(
578603
def apply_patch(
579604
self,
580605
cli_command: GradleCLICommand,
581-
options_patch: Mapping[str, GradleOptionPatchValueType | None],
606+
patch_options: Mapping[str, GradleOptionPatchValueType | None],
582607
) -> GradleCLICommand:
583608
"""Patch the options of a Gradle CLI command, while persisting the executable path.
584609
585-
`options_patch` is a mapping with:
610+
`patch_options` is a mapping with:
586611
587612
- **Key**: the long name of a Gradle CLI option as string. For example: ``--continue``, ``--build-cache``.
588613
For patching tasks, use the key ``tasks``.
@@ -630,7 +655,7 @@ def apply_patch(
630655
executable=cli_command.executable,
631656
options=self.apply_option_patch(
632657
cli_command.options,
633-
patch=options_patch,
658+
patch=patch_options,
634659
),
635660
)
636661

src/macaron/build_spec_generator/cli_command_parser/maven_cli_command.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,4 @@ class MavenCLICommand:
318318

319319
def to_cmds(self) -> list[str]:
320320
"""Return the CLI Command as a list of strings."""
321-
result = []
322-
result.append(self.executable)
323-
result.extend(self.options.to_option_cmds())
324-
return result
321+
return [self.executable] + self.options.to_option_cmds()

src/macaron/build_spec_generator/cli_command_parser/maven_cli_parser.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def get_patch_type_str(self) -> str:
123123

124124

125125
@dataclass
126-
class MavenSystemPropeties(OptionDef[dict[str, str | None]]):
126+
class MavenSystemProperties(OptionDef[dict[str, str | None]]):
127127
"""This option represents the -D/--define option of a Maven CLI command.
128128
129129
This option can be defined multiple times and the values are appended into a list of string in argparse.
@@ -289,7 +289,7 @@ def get_patch_type_str(self) -> str:
289289
short_name="-b",
290290
long_name="--builder",
291291
),
292-
MavenSystemPropeties(
292+
MavenSystemProperties(
293293
short_name="-D",
294294
long_name="--define",
295295
),
@@ -347,7 +347,7 @@ def get_patch_type_str(self) -> str:
347347
class MavenCLICommandParser:
348348
"""A Maven CLI Command Parser."""
349349

350-
ACCEPTABLE_EXECUTABLE = ["mvn", "mvnw"]
350+
ACCEPTABLE_EXECUTABLE = {"mvn", "mvnw"}
351351

352352
def __init__(self) -> None:
353353
"""Initialize the instance."""
@@ -446,16 +446,6 @@ def parse(self, cmd_list: list[str]) -> "MavenCLICommand":
446446
except SystemExit as sys_exit_err:
447447
raise CommandLineParseError(f"Failed to parse the Maven CLI Options {' '.join(options)}.") from sys_exit_err
448448

449-
# Handle cases where goal or plugin phase is not provided.
450-
if not parsed_opts.goals:
451-
# Allow cases such as:
452-
# mvn --help
453-
# mvn --version
454-
# Note that we don't allow mvn -V or mvn --show-version as this command will
455-
# fail for mvn.
456-
if not parsed_opts.help_ and not parsed_opts.version:
457-
raise CommandLineParseError(f"No goal detected for {' '.join(options)}.")
458-
459449
maven_cli_options = MavenCLIOptions.from_parsed_arg(parsed_opts)
460450

461451
return MavenCLICommand(
@@ -469,8 +459,36 @@ def _patch_properties_mapping(
469459
option_long_name: str,
470460
patch_value: MavenOptionPatchValueType,
471461
) -> dict[str, str]:
462+
"""
463+
Apply a patch to the Maven system properties mapping for a specific option.
464+
465+
Retrieves the system property option definition for the specified long name,
466+
validates its type and the patch value, and applies the patch update to the
467+
original properties dictionary. Raises an error if the option or patch type
468+
is invalid for Maven `--define` options.
469+
470+
Parameters
471+
----------
472+
original_props : dict[str, str]
473+
The original dictionary of Maven system property names and their values.
474+
option_long_name : str
475+
The long name of the Maven option to patch (usually '--define').
476+
patch_value : MavenOptionPatchValueType
477+
The value to patch into the original properties dictionary.
478+
479+
Returns
480+
-------
481+
dict[str, str]
482+
The updated mapping with the patch applied.
483+
484+
Raises
485+
------
486+
PatchBuildCommandError
487+
If the option is not a Maven system property option or if the patch value
488+
has an invalid type.
489+
"""
472490
define_opt_def = self.option_defs.get(option_long_name)
473-
if not define_opt_def or not isinstance(define_opt_def, MavenSystemPropeties):
491+
if not define_opt_def or not isinstance(define_opt_def, MavenSystemProperties):
474492
raise PatchBuildCommandError(f"{option_long_name} from the patch is not a --define option.")
475493

476494
if not define_opt_def.is_valid_patch_option(patch_value):
@@ -484,11 +502,11 @@ def _patch_properties_mapping(
484502
def apply_patch(
485503
self,
486504
cli_command: MavenCLICommand,
487-
options_patch: Mapping[str, MavenOptionPatchValueType | None],
505+
patch_options: Mapping[str, MavenOptionPatchValueType | None],
488506
) -> MavenCLICommand:
489507
"""Patch the options of a Gradle CLI command, while persisting the executable path.
490508
491-
`options_patch` is a mapping with:
509+
`patch_options` is a mapping with:
492510
493511
- **Key**: the long name of a Maven CLI option as a string. For example: ``--define``, ``--settings``.
494512
For patching goals or plugin phases, use the key `goals` with the value being a list of strings.
@@ -529,7 +547,7 @@ def apply_patch(
529547
executable=cli_command.executable,
530548
options=self.apply_option_patch(
531549
cli_command.options,
532-
patch=options_patch,
550+
patch=patch_options,
533551
),
534552
)
535553

src/macaron/build_spec_generator/jdk_finder.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ def find_jdk_version_from_remote_maven_repo_standalone(
166166
The version part of the GAV coordinate.
167167
asset_name: str
168168
The name of artifact to download and extract the jdk version.
169-
ext: JavaArtifactExt
170-
The extension of the main artifact file.
171169
remote_maven_repo_url: str
172170
The URL to the remote maven layout repository.
173171

0 commit comments

Comments
 (0)