Skip to content

Commit

Permalink
reuseable function
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Sep 30, 2024
1 parent c589fd6 commit 67f9643
Showing 1 changed file with 33 additions and 56 deletions.
89 changes: 33 additions & 56 deletions scripts/v4-forge-doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,48 @@ rm -rf submodules/v4-core/forge-docs
forge doc --root submodules/v4-periphery --out forge-docs/v4-periphery
rm -rf submodules/v4-periphery/forge-docs

# Copy v4-core
find forge-docs/v4-core/src -type f -regex '.*\.sol/[^.]*\.[^.]*\.md' | while read file; do
# Extract the directory part (e.g., base, base/hooks, libraries)
dir_structure=$(echo "$file" | sed -r 's|.*/src/([^/]+)/.*\.sol/.*|\1|')
copy_docs() {
local component=$1

# if dir_structure is src, remove it so it appears at the root level
if [ "$dir_structure" == "src" ]; then
dir_structure=""
fi
find "forge-docs/v4-$component/src" -type f -regex '.*\.sol/[^.]*\.[^.]*\.md' | while read file; do
# Extract the directory part (e.g., base, base/hooks, libraries)
dir_structure=$(echo "$file" | sed -r 's|.*/src/([^/]+)/.*\.sol/.*|\1|')

# Extract the base filename without the path and extension (e.g., Multicall_v4, BaseHook, Locker)
base_filename=$(echo "$file" | sed -r 's|.*/([^/]+)\.sol/[^/]*\.(.*)\.md|\1|')
# if dir_structure is src, remove it so it appears at the root level
if [ "$dir_structure" == "src" ]; then
dir_structure=""
fi

# Construct the new file path
new_file="docs/contracts/v4/reference/core/${dir_structure}/${base_filename}.md"
# Extract the base filename without the path and extension (e.g., Multicall_v4, BaseHook, Locker)
base_filename=$(echo "$file" | sed -r 's|.*/([^/]+)\.sol/[^/]*\.(.*)\.md|\1|')

# Create the new directory structure if it doesn't exist
mkdir -p "docs/contracts/v4/reference/core/${dir_structure}"
# Construct the new file path
new_file="docs/contracts/v4/reference/${component}/${dir_structure}/${base_filename}.md"

# Copy the file to the new location
cp "$file" "$new_file"
# Create the new directory structure if it doesn't exist
mkdir -p "docs/contracts/v4/reference/${component}/${dir_structure}"

# Add note:
sed -i '3i | Generated with [forge doc](https://book.getfoundry.sh/reference/forge/forge-doc)' "$new_file"
# Copy the file to the new location
cp "$file" "$new_file"

# Replace relative path links within the file with full paths
# replaces: /src/interfaces/IPoolManager.sol/interface.IPoolManager.md to contracts/v4/reference/core/interfaces/IPoolManager.md
sed -i 's|/src/\([^/]\+\)/\([^/]\+\)\.sol/\([^/]\+\)\.\([^/]\+\)\.md|contracts/v4/reference/core/\1/\2.md|g' "$new_file"
# Add note:
sed -i '3i | Generated with [forge doc](https://book.getfoundry.sh/reference/forge/forge-doc)' "$new_file"

# replaces: /src/ProtocolFees.sol/abstract.ProtocolFees.md to contracts/v4/reference/core/ProtocolFees.md
sed -i -E 's|/src/([^/]+)\.sol/abstract\.[^/]+\.md|contracts/v4/reference/core/\1.md|g' "$new_file"
# Replace relative path links within the file with full paths
sed -i "s|/src/\([^/]\+\)/\([^/]\+\)\.sol/\([^/]\+\)\.\([^/]\+\)\.md|contracts/v4/reference/${component}/\1/\2.md|g" "$new_file"

# replaces: /src/interfaces/external/IERC6909Claims.sol/interface.IERC6909Claims.md to contracts/v4/reference/core/interfaces/IERC6909Claims.md
sed -i -E 's|/src/interfaces/external/([^/]+)\.sol/interface\.[^/]+\.md|contracts/v4/reference/core/interfaces/\1.md|g' "$new_file"
# specially handle core paths
if [ "$component" == "core" ]; then
# replaces: /src/ProtocolFees.sol/abstract.ProtocolFees.md to contracts/v4/reference/core/ProtocolFees.md
sed -i -E 's|/src/([^/]+)\.sol/abstract\.[^/]+\.md|contracts/v4/reference/core/\1.md|g' "$new_file"

echo "Copied: $file -> $new_file"
done
# replaces: /src/interfaces/external/IERC6909Claims.sol/interface.IERC6909Claims.md to contracts/v4/reference/core/interfaces/IERC6909Claims.md
sed -i -E 's|/src/interfaces/external/([^/]+)\.sol/interface\.[^/]+\.md|contracts/v4/reference/core/interfaces/\1.md|g' "$new_file"
fi

# Copy v4-periphery
find forge-docs/v4-periphery/src -type f -regex '.*\.sol/[^.]*\.[^.]*\.md' | while read file; do
# Extract the directory part (e.g., base, base/hooks, libraries)
dir_structure=$(echo "$file" | sed -r 's|.*/src/([^/]+)/.*\.sol/.*|\1|')
echo "Copied: $file -> $new_file"
done
}

# if dir_structure is src, remove it so it appears at the root level
if [ "$dir_structure" == "src" ]; then
dir_structure=""
fi

# Extract the base filename without the path and extension (e.g., Multicall_v4, BaseHook, Locker)
base_filename=$(echo "$file" | sed -r 's|.*/([^/]+)\.sol/[^/]*\.(.*)\.md|\1|')

# Construct the new file path
new_file="docs/contracts/v4/reference/periphery/${dir_structure}/${base_filename}.md"

# Create the new directory structure if it doesn't exist
mkdir -p "docs/contracts/v4/reference/periphery/${dir_structure}"

# Copy the file to the new location
cp "$file" "$new_file"

# Add note:
sed -i '3i | Generated with [forge doc](https://book.getfoundry.sh/reference/forge/forge-doc)' "$new_file"

# Replace relative path links within the file with full paths
sed -i 's|/src/\([^/]\+\)/\([^/]\+\)\.sol/\([^/]\+\)\.\([^/]\+\)\.md|contracts/v4/reference/periphery/\1/\2.md|g' "$new_file"

echo "Copied: $file -> $new_file"
done
copy_docs "periphery"
copy_docs "core"

0 comments on commit 67f9643

Please sign in to comment.