|
| 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