-
Notifications
You must be signed in to change notification settings - Fork 48
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
Deploying by a contract name [1/2] — refactor feature dependencies #565
Merged
kasperski95
merged 21 commits into
master
from
feature/support-contract-names-in-cheatcodes
Aug 4, 2022
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
37fd44a
add ContractCompiler
kasperski95 087734d
relocate and rename project contract compiler
kasperski95 9fef501
relocate project compiler
kasperski95 1d9bdf6
extract project cairo path builder from project compiler
kasperski95 96edf8a
extract contract writer
kasperski95 26b6758
fix tests
kasperski95 4e9519f
remove project contract compiler
kasperski95 5412b3a
refine exceptions
kasperski95 b975c53
fix types
kasperski95 24bdec5
fix types
kasperski95 f66ec51
Merge branch 'master' of https://github.com/software-mansion/protosta…
kasperski95 ed884be
refine name
kasperski95 04c3f3c
refine naming
kasperski95 3d350c3
refine
kasperski95 ce81cd9
refine
kasperski95 b227dcb
rename assert to check
kasperski95 3c7a629
add `_save_as_json` method
kasperski95 8445df6
move `ProjectCompilerConfig` outside `ProjectCompiler`
kasperski95 575316d
change how configs are supplied
kasperski95 b2a9122
fix test
kasperski95 33ad368
refine naming
kasperski95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .project_compiler import ( | ||
CompilationException, | ||
ProjectCairoPathBuilder, | ||
ProjectCompiler, | ||
ProjectCompilerConfig, | ||
SourceFileNotFoundException, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from starkware.starknet.services.api.contract_class import ContractClass | ||
|
||
|
||
class CompiledContractWriter: | ||
def __init__(self, contract: ContractClass, contract_name: str) -> None: | ||
self._contract = contract | ||
self._contract_name = contract_name | ||
|
||
def save(self, output_dir: Path) -> None: | ||
self.save_compiled_contract(output_dir) | ||
self.save_compiled_contract_abi(output_dir) | ||
|
||
def save_compiled_contract(self, output_dir: Path) -> None: | ||
self._create_output_dir(output_dir) | ||
serialized_contract = self._contract.Schema().dump(self._contract) | ||
file_path = output_dir / f"{self._contract_name}.json" | ||
self._save_as_json(data=serialized_contract, path=file_path) | ||
|
||
def save_compiled_contract_abi(self, output_dir: Path) -> None: | ||
if not self._contract.abi: | ||
return | ||
self._create_output_dir(output_dir) | ||
file_path = output_dir / f"{self._contract_name}_abi.json" | ||
self._save_as_json(data=self._contract.abi, path=file_path) | ||
|
||
@staticmethod | ||
def _create_output_dir(output_dir: Path): | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
@staticmethod | ||
def _save_as_json(data: Any, path: Path): | ||
assert path.suffix == ".json" | ||
with open( | ||
path, | ||
mode="w", | ||
encoding="utf-8", | ||
) as output_file: | ||
json.dump(data, output_file, indent=4, sort_keys=True) | ||
output_file.write("\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import List, Optional | ||
|
||
from protostar.protostar_toml.protostar_project_section import ProtostarProjectSection | ||
|
||
|
||
class ProjectCairoPathBuilder: | ||
def __init__( | ||
self, | ||
project_root_path: Path, | ||
project_section_loader: ProtostarProjectSection.Loader, | ||
) -> None: | ||
self._project_root_path = project_root_path | ||
self._project_section_loader = project_section_loader | ||
|
||
def build_project_cairo_path_list( | ||
self, relative_cairo_path_list: List[Path] | ||
) -> List[Path]: | ||
"""NOTE: This function uses `list` suffix to mitigate confusion and | ||
remain consistency with StarkNet's `cairo_path`.""" | ||
|
||
return [ | ||
*relative_cairo_path_list, | ||
self._project_root_path, | ||
*self._build_libs_cairo_path_list(), | ||
] | ||
|
||
def _build_libs_cairo_path_list(self) -> List[Path]: | ||
libs_path = self._get_libs_path() | ||
if libs_path is None: | ||
return [] | ||
return [libs_path, *self._build_packages_cairo_path_list()] | ||
|
||
def _build_packages_cairo_path_list(self) -> List[Path]: | ||
libs_path = self._get_libs_path() | ||
if libs_path is None: | ||
return [] | ||
(root, dirs, _) = next(os.walk(str(libs_path.resolve()))) | ||
return [Path(root, directory).resolve() for directory in dirs] | ||
|
||
def _get_libs_path(self) -> Optional[Path]: | ||
project_section = self._project_section_loader.load() | ||
return project_section.get_libs_path(self._project_root_path) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be a plain function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, it could. Something feels off when mixing paradigms.
It's easier to find a file that contains a class rather than a list of functions. Naming files in the same way as the main exported function is confusing because a function and a filename use the same casing, and that makes importing/patching confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can still define single public module function, and name the module the same way. That's what I have seen in the wild.
To me, using classes too much in Python/JS is mixing paradigms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it when I used the functional paradigm. Auto import functionality confused the function with the module, if they were named in the same way. Also, I couldn't patch the function, if it was imported from the
__init__.py
(barrel file) from the same reason.Functional programming is more common in Python, but it doesn't mean that this is the proper tool for the job.
https://khalilstemmler.com/articles/software-design-architecture/full-stack-software-design/