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: uml diagrams & diagram generate script #266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mikelord007
Copy link

@mikelord007 mikelord007 commented Jul 28, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new script to automate the generation of UML diagrams from Solidity contracts, improving visualization and documentation.
    • Added a new command in the build process for generating diagrams, enhancing project tooling.
  • Dependencies

    • Integrated a new dependency (sol2uml) to support UML diagram generation, enriching the functionality related to Solidity contracts.

Copy link
Contributor

coderabbitai bot commented Jul 28, 2024

Walkthrough

The recent updates introduce a new automation script for generating UML diagrams from Solidity contracts, enhancing project documentation. A corresponding dependency on sol2uml is added to facilitate this process. This integration streamlines the development workflow by allowing developers to visualize contract structures efficiently, all while maintaining the existing functionality of the project.

Changes

File(s) Change Summary
package.json Added "generate:diagrams" script and new dependency "sol2uml": "^2.5.20"
scripts/generate_uml_diagrams.sh Introduced script to automate UML diagram generation and Solidity contract flattening

Poem

🐇 In the meadow of code, the rabbits play,
With diagrams hopping, brightening the day.
A script for the contracts, so clever and neat,
Visualizing the flows, a wonderful treat!
With sol2uml in tow, we dance and we cheer,
For clearer designs, our vision is clear! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2b8508e and 2bbeadc.

Files ignored due to path filters (14)
  • contract-class-diagrams/evm/ERC20Custody.sol.png is excluded by !**/*.png
  • contract-class-diagrams/evm/Zeta.eth.sol.png is excluded by !**/*.png
  • contract-class-diagrams/evm/Zeta.non-eth.sol.png is excluded by !**/*.png
  • contract-class-diagrams/evm/ZetaConnector.base.sol.png is excluded by !**/*.png
  • contract-class-diagrams/evm/ZetaConnector.eth.sol.png is excluded by !**/*.png
  • contract-class-diagrams/evm/ZetaConnector.non-eth.sol.png is excluded by !**/*.png
  • contract-class-diagrams/zevm/SystemContract.sol.png is excluded by !**/*.png
  • contract-class-diagrams/zevm/Uniswap.sol.png is excluded by !**/*.png
  • contract-class-diagrams/zevm/UniswapPeriphery.sol.png is excluded by !**/*.png
  • contract-class-diagrams/zevm/WZETA.sol.png is excluded by !**/*.png
  • contract-class-diagrams/zevm/ZRC20.sol.png is excluded by !**/*.png
  • contract-class-diagrams/zevm/ZRC20New.sol.png is excluded by !**/*.png
  • contract-class-diagrams/zevm/ZetaConnectorZEVM.sol.png is excluded by !**/*.png
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
Files selected for processing (2)
  • package.json (2 hunks)
  • scripts/generate_uml_diagrams.sh (1 hunks)
Additional context used
Path-based instructions (1)
scripts/generate_uml_diagrams.sh (1)

Pattern scripts/**: Review the Hardhat scripts for best practices.

Additional comments not posted (4)
scripts/generate_uml_diagrams.sh (2)

1-4: LGTM!

The shebang line and initial comments are appropriate and provide context for the script.


47-48: LGTM!

The script ends appropriately after processing all files in all folders.

package.json (2)

87-87: LGTM!

The new script command generate:diagrams appropriately executes the shell script for generating UML diagrams.


102-102: LGTM!

The new dependency sol2uml is appropriately added to facilitate UML diagram generation.

Comment on lines +5 to +12
folders=("/evm" "/zevm")

# Output directory for the flattened files
output_dir="./contracts"

# Directory for the class diagrams
diagram_folder="./contract-class-diagrams"

Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure the diagram folder exists.

Before processing files, ensure that the diagram folder exists to avoid potential errors.

+ # Ensure the diagram folder exists
+ mkdir -p "$diagram_folder"

folders=("/evm" "/zevm")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
folders=("/evm" "/zevm")
# Output directory for the flattened files
output_dir="./contracts"
# Directory for the class diagrams
diagram_folder="./contract-class-diagrams"
# Ensure the diagram folder exists
mkdir -p "$diagram_folder"
folders=("/evm" "/zevm")
# Output directory for the flattened files
output_dir="./contracts"
# Directory for the class diagrams
diagram_folder="./contract-class-diagrams"

Comment on lines +42 to +46
# Run the sol2uml command
npx sol2uml class "$output_file" -f png -o "$diagram_folder/$folder/$base_name.png"

# Remove the flattened file
rm "$output_file"
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve error handling for sol2uml command.

If the sol2uml command fails, consider exiting the script or retrying the command.

npx sol2uml class "$output_file" -f png -o "$diagram_folder/$folder/$base_name.png"

# Check if the command was successful
if [ $? -eq 0 ]; then
    echo "Successfully generated UML diagram for $file"
else
    echo "Error generating UML diagram for $file"
+   exit 1
fi

# Remove the flattened file
rm "$output_file"
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Run the sol2uml command
npx sol2uml class "$output_file" -f png -o "$diagram_folder/$folder/$base_name.png"
# Remove the flattened file
rm "$output_file"
# Run the sol2uml command
npx sol2uml class "$output_file" -f png -o "$diagram_folder/$folder/$base_name.png"
# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Successfully generated UML diagram for $file"
else
echo "Error generating UML diagram for $file"
exit 1
fi
# Remove the flattened file
rm "$output_file"

Comment on lines +24 to +40

# Get the base name of the file (without path)
base_name=$(basename "$file")
echo "Processing file: $file"

# Construct the output file path
output_file="$output_dir/Flattened_$base_name"

# Run the flatten command
npx hardhat flatten "$file" > "$output_file"

# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Successfully flattened $file to $output_file"
else
echo "Error flattening $file"
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve error handling for flatten command.

If the flatten command fails, consider exiting the script or retrying the command.

npx hardhat flatten "$file" > "$output_file"

# Check if the command was successful
if [ $? -eq 0 ]; then
    echo "Successfully flattened $file to $output_file"
else
    echo "Error flattening $file"
+   exit 1
fi
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Get the base name of the file (without path)
base_name=$(basename "$file")
echo "Processing file: $file"
# Construct the output file path
output_file="$output_dir/Flattened_$base_name"
# Run the flatten command
npx hardhat flatten "$file" > "$output_file"
# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Successfully flattened $file to $output_file"
else
echo "Error flattening $file"
fi
# Get the base name of the file (without path)
base_name=$(basename "$file")
echo "Processing file: $file"
# Construct the output file path
output_file="$output_dir/Flattened_$base_name"
# Run the flatten command
npx hardhat flatten "$file" > "$output_file"
# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Successfully flattened $file to $output_file"
else
echo "Error flattening $file"
exit 1
fi

Comment on lines +13 to +23
# Loop through all .sol files in the contracts_dir
for folder in "${folders[@]}"; do
contracts_dir="$baseFolder$folder"
echo "Processing folder: $contracts_dir"

for file in "$contracts_dir"/*.sol; do
# Check if any .sol files exist in the directory
if [[ ! -e "$file" ]]; then
echo "No .sol files found in $contracts_dir"
continue
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

Handle cases where no .sol files are found more gracefully.

Instead of continuing silently, consider exiting the script or skipping to the next folder if no .sol files are found.

if [[ ! -e "$file" ]]; then
    echo "No .sol files found in $contracts_dir"
+   break
    continue
fi
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Loop through all .sol files in the contracts_dir
for folder in "${folders[@]}"; do
contracts_dir="$baseFolder$folder"
echo "Processing folder: $contracts_dir"
for file in "$contracts_dir"/*.sol; do
# Check if any .sol files exist in the directory
if [[ ! -e "$file" ]]; then
echo "No .sol files found in $contracts_dir"
continue
fi
# Loop through all .sol files in the contracts_dir
for folder in "${folders[@]}"; do
contracts_dir="$baseFolder$folder"
echo "Processing folder: $contracts_dir"
for file in "$contracts_dir"/*.sol; do
# Check if any .sol files exist in the directory
if [[ ! -e "$file" ]]; then
echo "No .sol files found in $contracts_dir"
break
continue
fi

Copy link
Member

@lumtis lumtis left a comment

Choose a reason for hiding this comment

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

Hey @mikelord007, thank you very much working on this.

To give some updates on the development. We decided to separate the codebase into v1 and v2: #268

We're mostly interested in v2 for the diagrams but we also decided to switch from Hardhat to Foundry for it. Do you have a similar solution for diagram generation using Foundry suite?

@mikelord007
Copy link
Author

Hey @lumtis

I don't think there is a tool in foundry that can do the same.
For the current solution ( sol2uml ) we don't need hardhat, just nodejs + soul2uml will do.
( in the current process, we're using hardhat to flatten the contracts so sol2uml doesn't fail while generating the diagrams, but flattening can be done with foundry too )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants