Skip to content

Commit 7985c6a

Browse files
feat: add --exclude-compiler flag to ape compile command (#2494)
Co-authored-by: El De-dog-lo <[email protected]>
1 parent 2b1fbea commit 7985c6a

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

src/ape/cli/options.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,29 @@ def _json_option(name, help, **kwargs):
597597

598598
def config_override_option(**kwargs):
599599
return _json_option("--config-override", help="Config override mappings", **kwargs)
600+
601+
602+
def _excluded_compilers_callback(ctx, param, value):
603+
if not value:
604+
return
605+
606+
return [c.lower() for c in value]
607+
608+
609+
def excluded_compilers_option(**kwargs):
610+
from ape.utils.basemodel import ManagerAccessMixin
611+
612+
registered_compilers_options = [
613+
compiler.name
614+
for compiler in ManagerAccessMixin.compiler_manager.registered_compilers.values()
615+
]
616+
617+
return click.option(
618+
"--exclude-compiler",
619+
"excluded_compilers",
620+
help="Exclude specific compilers from the compilation process",
621+
type=click.Choice(registered_compilers_options, case_sensitive=False),
622+
callback=_excluded_compilers_callback,
623+
multiple=True,
624+
**kwargs,
625+
)

src/ape/managers/compilers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def compile(
9999
contract_filepaths: Union[Path, str, Iterable[Union[Path, str]]],
100100
project: Optional["ProjectManager"] = None,
101101
settings: Optional[dict] = None,
102+
excluded_compilers: Optional[list[str]] = None,
102103
) -> Iterator["ContractType"]:
103104
"""
104105
Invoke :meth:`ape.ape.compiler.CompilerAPI.compile` for each of the given files.
@@ -137,6 +138,8 @@ def compile(
137138

138139
for next_ext, path_set in files_by_ext.items():
139140
compiler = self.registered_compilers[next_ext]
141+
if excluded_compilers and compiler.name.lower() in excluded_compilers:
142+
continue
140143
try:
141144
compiler_settings = settings.get(compiler.name, {})
142145
for contract in compiler.compile(path_set, project=pm, settings=compiler_settings):

src/ape/managers/project.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,17 @@ def _get_needs_compile(self, paths: Iterable[Union[Path, str]]) -> Iterable[Path
449449
else:
450450
yield path
451451

452-
def _compile_contracts(self, paths: Iterable[Union[Path, str]]):
452+
def _compile_contracts(
453+
self,
454+
paths: Iterable[Union[Path, str]],
455+
excluded_compilers: Optional[list[str]] = None,
456+
):
453457
if not (
454458
new_types := {
455459
ct.name: ct
456-
for ct in self.compiler_manager.compile(paths, project=self.project)
460+
for ct in self.compiler_manager.compile(
461+
paths, project=self.project, excluded_compilers=excluded_compilers
462+
)
457463
if ct.name
458464
}
459465
):
@@ -476,7 +482,10 @@ def _compile_all(self, use_cache: bool = True) -> Iterator[ContractContainer]:
476482
yield from self._compile(paths, use_cache=use_cache)
477483

478484
def _compile(
479-
self, paths: Union[Path, str, Iterable[Union[Path, str]]], use_cache: bool = True
485+
self,
486+
paths: Union[Path, str, Iterable[Union[Path, str]]],
487+
use_cache: bool = True,
488+
excluded_compilers: Optional[list[str]] = None,
480489
) -> Iterator[ContractContainer]:
481490
path_ls = list([paths] if isinstance(paths, (Path, str)) else paths)
482491
if not path_ls:
@@ -495,7 +504,7 @@ def _compile(
495504
if needs_compile := list(
496505
self._get_needs_compile(path_ls_final) if use_cache else path_ls_final
497506
):
498-
self._compile_contracts(needs_compile)
507+
self._compile_contracts(needs_compile, excluded_compilers=excluded_compilers)
499508

500509
src_ids = [f"{Path(p).relative_to(self.project.path)}" for p in path_ls_final]
501510
for contract_type in (self.project.manifest.contract_types or {}).values():
@@ -2592,7 +2601,10 @@ def update_manifest(self, **kwargs):
25922601
self.manifest_path.write_text(manifest_text, encoding="utf8")
25932602

25942603
def load_contracts(
2595-
self, *source_ids: Union[str, Path], use_cache: bool = True
2604+
self,
2605+
*source_ids: Union[str, Path],
2606+
use_cache: bool = True,
2607+
excluded_compilers: Optional[list[str]] = None,
25962608
) -> dict[str, ContractContainer]:
25972609
paths: Iterable[Path]
25982610
starting: dict[str, ContractContainer] = {}
@@ -2608,7 +2620,9 @@ def load_contracts(
26082620

26092621
new_types = {
26102622
c.contract_type.name: c
2611-
for c in self.contracts._compile(paths, use_cache=use_cache)
2623+
for c in self.contracts._compile(
2624+
paths, use_cache=use_cache, excluded_compilers=excluded_compilers
2625+
)
26122626
if c.contract_type.name
26132627
}
26142628
return {**starting, **new_types}

src/ape_compile/_cli.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
import click
66

77
from ape.cli.arguments import contract_file_paths_argument
8-
from ape.cli.options import ape_cli_context, config_override_option, project_option
8+
from ape.cli.options import (
9+
ape_cli_context,
10+
config_override_option,
11+
excluded_compilers_option,
12+
project_option,
13+
)
914

1015
if TYPE_CHECKING:
1116
from ethpm_types import ContractType
@@ -42,6 +47,7 @@ def _include_dependencies_callback(ctx, param, value):
4247
help="Also compile dependencies",
4348
callback=_include_dependencies_callback,
4449
)
50+
@excluded_compilers_option()
4551
@config_override_option()
4652
def cli(
4753
cli_ctx,
@@ -50,6 +56,7 @@ def cli(
5056
use_cache: bool,
5157
display_size: bool,
5258
include_dependencies,
59+
excluded_compilers: list[str],
5360
config_override,
5461
):
5562
"""
@@ -68,7 +75,9 @@ def cli(
6875
if file_paths:
6976
contracts = {
7077
k: v.contract_type
71-
for k, v in project.load_contracts(*file_paths, use_cache=use_cache).items()
78+
for k, v in project.load_contracts(
79+
*file_paths, use_cache=use_cache, excluded_compilers=excluded_compilers
80+
).items()
7281
}
7382
cli_ctx.logger.success("'local project' compiled.")
7483
compiled = True

0 commit comments

Comments
 (0)