Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --exclude-compiler flag to ape compile command #2494

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
23 changes: 23 additions & 0 deletions src/ape/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,26 @@ def _json_option(name, help, **kwargs):

def config_override_option(**kwargs):
return _json_option("--config-override", help="Config override mappings", **kwargs)


def _excluded_compilers_callback(ctx, param, value):
if not value:
return

return [c.lower() for c in value]


def excluded_compilers_option(**kwargs):
from ape.utils.basemodel import ManagerAccessMixin

registered_compilers_options = list(ManagerAccessMixin.compiler_manager.registered_compilers)

return click.option(
"--exclude-compiler",
"excluded_compilers",
help="Exclude specific compilers from the compilation process",
type=click.Choice(registered_compilers_options, case_sensitive=False),
callback=_excluded_compilers_callback,
multiple=True,
**kwargs,
)
3 changes: 3 additions & 0 deletions src/ape/managers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def compile(
contract_filepaths: Union[Path, str, Iterable[Union[Path, str]]],
project: Optional["ProjectManager"] = None,
settings: Optional[dict] = None,
excluded_compilers: Optional[list[str]] = None,
) -> Iterator["ContractType"]:
"""
Invoke :meth:`ape.ape.compiler.CompilerAPI.compile` for each of the given files.
Expand Down Expand Up @@ -137,6 +138,8 @@ def compile(

for next_ext, path_set in files_by_ext.items():
compiler = self.registered_compilers[next_ext]
if excluded_compilers and compiler.name.lower() in excluded_compilers:
continue
try:
compiler_settings = settings.get(compiler.name, {})
for contract in compiler.compile(path_set, project=pm, settings=compiler_settings):
Expand Down
26 changes: 20 additions & 6 deletions src/ape/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,17 @@ def _get_needs_compile(self, paths: Iterable[Union[Path, str]]) -> Iterable[Path
else:
yield path

def _compile_contracts(self, paths: Iterable[Union[Path, str]]):
def _compile_contracts(
self,
paths: Iterable[Union[Path, str]],
excluded_compilers: Optional[list[str]] = None,
):
if not (
new_types := {
ct.name: ct
for ct in self.compiler_manager.compile(paths, project=self.project)
for ct in self.compiler_manager.compile(
paths, project=self.project, excluded_compilers=excluded_compilers
)
if ct.name
}
):
Expand All @@ -476,7 +482,10 @@ def _compile_all(self, use_cache: bool = True) -> Iterator[ContractContainer]:
yield from self._compile(paths, use_cache=use_cache)

def _compile(
self, paths: Union[Path, str, Iterable[Union[Path, str]]], use_cache: bool = True
self,
paths: Union[Path, str, Iterable[Union[Path, str]]],
use_cache: bool = True,
excluded_compilers: Optional[list[str]] = None,
) -> Iterator[ContractContainer]:
path_ls = list([paths] if isinstance(paths, (Path, str)) else paths)
if not path_ls:
Expand All @@ -495,7 +504,7 @@ def _compile(
if needs_compile := list(
self._get_needs_compile(path_ls_final) if use_cache else path_ls_final
):
self._compile_contracts(needs_compile)
self._compile_contracts(needs_compile, excluded_compilers=excluded_compilers)

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

def load_contracts(
self, *source_ids: Union[str, Path], use_cache: bool = True
self,
*source_ids: Union[str, Path],
use_cache: bool = True,
excluded_compilers: Optional[list[str]] = None,
) -> dict[str, ContractContainer]:
paths: Iterable[Path]
starting: dict[str, ContractContainer] = {}
Expand All @@ -2608,7 +2620,9 @@ def load_contracts(

new_types = {
c.contract_type.name: c
for c in self.contracts._compile(paths, use_cache=use_cache)
for c in self.contracts._compile(
paths, use_cache=use_cache, excluded_compilers=excluded_compilers
)
if c.contract_type.name
}
return {**starting, **new_types}
Expand Down
13 changes: 11 additions & 2 deletions src/ape_compile/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import click

from ape.cli.arguments import contract_file_paths_argument
from ape.cli.options import ape_cli_context, config_override_option, project_option
from ape.cli.options import (
ape_cli_context,
config_override_option,
excluded_compilers_option,
project_option,
)

if TYPE_CHECKING:
from ethpm_types import ContractType
Expand Down Expand Up @@ -42,6 +47,7 @@ def _include_dependencies_callback(ctx, param, value):
help="Also compile dependencies",
callback=_include_dependencies_callback,
)
@excluded_compilers_option()
@config_override_option()
def cli(
cli_ctx,
Expand All @@ -50,6 +56,7 @@ def cli(
use_cache: bool,
display_size: bool,
include_dependencies,
excluded_compilers: list[str],
config_override,
):
"""
Expand All @@ -68,7 +75,9 @@ def cli(
if file_paths:
contracts = {
k: v.contract_type
for k, v in project.load_contracts(*file_paths, use_cache=use_cache).items()
for k, v in project.load_contracts(
*file_paths, use_cache=use_cache, excluded_compilers=excluded_compilers
).items()
}
cli_ctx.logger.success("'local project' compiled.")
compiled = True
Expand Down
Loading