Skip to content

Commit acf1cb4

Browse files
committed
chore(githooks): add pre-commit script to sync solc version
This commit adds a new pre-commit script called "pre-commit-solc-sync" to the githooks directory. The script is responsible for extracting the solc version from the "foundry.toml" file and updating the ".vscode/settings.json" file with the extracted version. If the "solidity.compileUsingRemoteVersion" key already exists in the settings file, the script updates the version. Otherwise, it modifies the last key, adds the new key, and closes the JSON object. Finally, the updated settings file is added to the commit. The purpose of this change is to automate the synchronization of the solc version used for Solidity compilation in the project everywhere else its also need to be configured configured. See juanfranblanco/vscode-solidity#463
1 parent 2e46fee commit acf1cb4

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

githooks/pre-commit-solc-sync

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
# Initialize a flag to track if we are in the [profile.default] section
4+
in_profile_default=false
5+
6+
# Loop through each line of foundry.toml
7+
while IFS= read -r line
8+
do
9+
# Check if we are entering the [profile.default] section
10+
if [[ "$line" =~ ^\[profile\.default\] ]]; then
11+
in_profile_default=true
12+
fi
13+
14+
# Check if we find the solc version in the [profile.default] section
15+
if $in_profile_default && [[ "$line" =~ solc\ *=\ *\"[0-9]+\.[0-9]+\.[0-9]+\" ]]; then
16+
# Extract the version number and trim spaces
17+
SOLC_VERSION=$(echo "$line" | sed -E 's/solc *= *"([0-9]+\.[0-9]+\.[0-9]+)"/\1/' | xargs)
18+
break
19+
fi
20+
21+
# Stop checking after we leave the [profile.default] section
22+
if [[ "$line" =~ ^\[.*\] && ! "$line" =~ ^\[profile\.default\] ]]; then
23+
in_profile_default=false
24+
fi
25+
done < foundry.toml
26+
27+
# Check if the version was extracted successfully
28+
if [ -z "$SOLC_VERSION" ]; then
29+
echo "Error: Could not find solc version in foundry.toml"
30+
exit 1
31+
fi
32+
33+
echo "Extracted solc version: $SOLC_VERSION"
34+
35+
# Update the .vscode/settings.json file
36+
SETTINGS_FILE=".vscode/settings.json"
37+
38+
# Update the .vscode/settings.json file
39+
SETTINGS_FILE=".vscode/settings.json"
40+
41+
# Create the file if it doesn't exist
42+
if [ ! -f "$SETTINGS_FILE" ]; then
43+
echo '{}' > "$SETTINGS_FILE"
44+
fi
45+
46+
# Check if the "solidity.compileUsingRemoteVersion" key exists
47+
if grep -q '"solidity.compileUsingRemoteVersion"' "$SETTINGS_FILE"; then
48+
# If the key exists, update the version
49+
sed -i -E "s/\"solidity.compileUsingRemoteVersion\": *\"[^\"]*\"/\"solidity.compileUsingRemoteVersion\": \"$SOLC_VERSION\"/" "$SETTINGS_FILE"
50+
else
51+
# Key does not exist, so we modify the last key, add the new one, and close the bracket
52+
LAST_KEY=$(tail -n 2 "$SETTINGS_FILE" | head -n 1 | sed 's/,$//') # Remove trailing comma if exists
53+
sed -i '$d' "$SETTINGS_FILE" # Remove the last closing brace
54+
sed -i '$d' "$SETTINGS_FILE" # Remove the line that will be rewriten
55+
# Write the last key back with a comma, add the new key, and close the JSON object
56+
echo "$LAST_KEY," >> "$SETTINGS_FILE"
57+
echo " \"solidity.compileUsingRemoteVersion\": \"$SOLC_VERSION\"" >> "$SETTINGS_FILE"
58+
echo "}" >> "$SETTINGS_FILE"
59+
fi
60+
61+
# Add the updated settings.json to the commit
62+
git add "$SETTINGS_FILE"
63+
64+
echo "Updated .vscode/settings.json with solc version $SOLC_VERSION"
65+
66+
exit 0

0 commit comments

Comments
 (0)