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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
exclude_compiler: Optional[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 exclude_compiler and compiler.name.lower() == exclude_compiler.lower():
continue
try:
compiler_settings = settings.get(compiler.name, {})
for contract in compiler.compile(path_set, project=pm, settings=compiler_settings):
Expand Down
24 changes: 18 additions & 6 deletions src/ape/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,15 @@ 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]], exclude_compiler: Optional[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, exclude_compiler=exclude_compiler
)
if ct.name
}
):
Expand All @@ -476,7 +480,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,
exclude_compiler: Optional[str] = None,
) -> Iterator[ContractContainer]:
path_ls = list([paths] if isinstance(paths, (Path, str)) else paths)
if not path_ls:
Expand All @@ -495,7 +502,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, exclude_compiler=exclude_compiler)

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 @@ -2526,7 +2533,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,
exclude_compiler: Optional[str] = None,
) -> dict[str, ContractContainer]:
paths: Iterable[Path]
starting: dict[str, ContractContainer] = {}
Expand All @@ -2542,7 +2552,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, exclude_compiler=exclude_compiler
)
if c.contract_type.name
}
return {**starting, **new_types}
Expand Down
10 changes: 9 additions & 1 deletion src/ape_compile/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def _include_dependencies_callback(ctx, param, value):
help="Also compile dependencies",
callback=_include_dependencies_callback,
)
@click.option(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may also be useful in the ape pm compile command. We could potentially add this to the ape.cli.options module so it can be re-used.

"--exclude-compiler",
type=click.Choice(["solidity", "vyper"], case_sensitive=False),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a click.Choice. There are more compilers besides solidity and vyper. With Ape's plugin system, there isn't a fixed number of compilers.

A normal str type is just find.

help="Exclude a specific compiler (solidity or vyper)",
)
@config_override_option()
def cli(
cli_ctx,
Expand All @@ -50,6 +55,7 @@ def cli(
use_cache: bool,
display_size: bool,
include_dependencies,
exclude_compiler: str,
config_override,
):
"""
Expand All @@ -68,7 +74,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, exclude_compiler=exclude_compiler
).items()
}
cli_ctx.logger.success("'local project' compiled.")
compiled = True
Expand Down
Loading