Skip to content

Commit

Permalink
change how configs are supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperski95 committed Aug 3, 2022
1 parent 8445df6 commit 575316d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
8 changes: 4 additions & 4 deletions protostar/commands/build/build_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ async def run(self, args):
log_color_provider.colorize("GRAY", "Building projects' contracts")
):
try:
self._project_compiler.set_config(
ProjectCompilerConfig(
self._project_compiler.compile_project(
output_dir=args.output,
config=ProjectCompilerConfig(
hint_validation_disabled=args.disable_hint_validation,
relative_cairo_path=args.cairo_path,
)
),
)
self._project_compiler.compile_project(output_dir=args.output)
except BaseException as exc:
self._logger.error("Build failed")
raise exc
Expand Down
53 changes: 31 additions & 22 deletions protostar/compiler/project_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,57 +37,64 @@ def __init__(
project_root_path: Path,
project_cairo_path_builder: ProjectCairoPathBuilder,
contracts_section_loader: ProtostarContractsSection.Loader,
config: Optional[ProjectCompilerConfig] = None,
default_config: Optional[ProjectCompilerConfig] = None,
):
self._project_root_path = project_root_path
self._project_cairo_path_builder = project_cairo_path_builder
self._contracts_section_loader = contracts_section_loader
self._config = config or ProjectCompilerConfig(relative_cairo_path=[])

def set_config(self, config: ProjectCompilerConfig) -> None:
self._config = config
self._default_config = default_config or ProjectCompilerConfig(
relative_cairo_path=[]
)

def compile_project(
self,
output_dir: Path,
self, output_dir: Path, config: Optional[ProjectCompilerConfig] = None
) -> None:
contracts_section = self._contracts_section_loader.load()
for contract_name in contracts_section.get_contract_names():
contract = self.compile_contract_from_contract_name(contract_name)
contract = self.compile_contract_from_contract_name(contract_name, config)
CompiledContractWriter(contract, contract_name).save(
output_dir=self._get_compilation_output_dir(output_dir)
)

def compile_contract_from_contract_identifier(
self, contract_identifier: ContractIdentifier
self,
contract_identifier: ContractIdentifier,
config: Optional[ProjectCompilerConfig] = None,
) -> ContractClass:
if isinstance(contract_identifier, Path):
return self.compile_contract_from_contract_source_paths(
[contract_identifier]
[contract_identifier], config
)
return self.compile_contract_from_contract_name(contract_identifier)
return self.compile_contract_from_contract_name(contract_identifier, config)

def compile_contract_from_contract_name(self, contract_name: str) -> ContractClass:
def compile_contract_from_contract_name(
self, contract_name: str, config: Optional[ProjectCompilerConfig] = None
) -> ContractClass:
try:
contract_paths = self._map_contract_name_to_contract_source_paths(
contract_name
)
return self.compile_contract_from_contract_source_paths(contract_paths)
return self.compile_contract_from_contract_source_paths(
contract_paths, config
)
except (StarkException, VmException, PreprocessorError) as err:
raise CompilationException(contract_name, err) from err

def compile_contract_from_contract_source_paths(
self,
contract_paths: List[Path],
self, contract_paths: List[Path], config: Optional[ProjectCompilerConfig] = None
) -> ContractClass:
current_config = config or self._default_config

return StarknetCompiler(
config=CompilerConfig(
include_paths=self._build_str_cairo_path_list(),
disable_hint_validation=self._config.hint_validation_disabled,
include_paths=self._build_str_cairo_path_list(
current_config.relative_cairo_path
),
disable_hint_validation=current_config.hint_validation_disabled,
),
pass_manager_factory=StarknetPassManagerFactory,
).compile_contract(
*contract_paths, add_debug_info=self._config.debugging_info_attached
*contract_paths, add_debug_info=current_config.debugging_info_attached
)

def _map_contract_name_to_contract_source_paths(
Expand All @@ -100,19 +107,21 @@ def _map_contract_name_to_contract_source_paths(
source_paths = [
self._project_root_path / path for path in relative_source_paths
]
map(self._assert_source_file_exists, source_paths)
map(self._check_source_file_exists, source_paths)
return source_paths

@staticmethod
def _assert_source_file_exists(source_path: Path) -> None:
def _check_source_file_exists(source_path: Path) -> None:
if not source_path.exists():
raise SourceFileNotFoundException(source_path)

def _build_str_cairo_path_list(self) -> List[str]:
def _build_str_cairo_path_list(
self, user_relative_cairo_path: List[Path]
) -> List[str]:
return [
str(path)
for path in self._project_cairo_path_builder.build_project_cairo_path_list(
self._config.relative_cairo_path
user_relative_cairo_path
)
]

Expand Down
14 changes: 11 additions & 3 deletions tests/integration/project_compiler/project_compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import pytest
from pytest_mock import MockerFixture

from protostar.compiler import CompilationException, ProjectCompiler
from protostar.compiler import (
CompilationException,
ProjectCompiler,
ProjectCompilerConfig,
)
from protostar.compiler.project_cairo_path_builder import ProjectCairoPathBuilder
from protostar.protostar_toml.protostar_contracts_section import (
ProtostarContractsSection,
Expand Down Expand Up @@ -84,7 +88,9 @@ def test_handling_cairo_errors(tmp_path: Path, datadir: Path, create_loader):
}
)
),
config=ProjectCompiler.Config(relative_cairo_path=[project_root_path]),
default_config=ProjectCompilerConfig(
relative_cairo_path=[project_root_path]
),
).compile_project(output_dir=tmp_path)


Expand All @@ -107,5 +113,7 @@ def test_handling_not_existing_main_files(tmp_path: Path, datadir: Path, create_
}
)
),
config=ProjectCompiler.Config(relative_cairo_path=[project_root_path]),
default_config=ProjectCompilerConfig(
relative_cairo_path=[project_root_path]
),
).compile_project(output_dir=tmp_path)

0 comments on commit 575316d

Please sign in to comment.