Skip to content

Commit

Permalink
Update fix for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jayadamsmorgan committed Mar 23, 2024
1 parent 91e16f2 commit 975b883
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions generate_resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ STDLIB_PATH="pkl/stdlib"
OUTPUT_FILE="Sources/pkl-lsp/Resources.swift"

# Start the Resources struct and hashMap
echo "// This file is auto-generated. Do not edit directly.\n" > "$OUTPUT_FILE"
echo "// This file is auto-generated. Do not edit directly." > "$OUTPUT_FILE"
echo "public enum Resources {" >> "$OUTPUT_FILE"
echo " public static let stdlib: [String: String] = [" >> "$OUTPUT_FILE"

# Process each .pkl file
find "$STDLIB_PATH" -name "*.pkl" | while IFS= read -r file; do
while IFS= read -r file; do
filename=$(basename -- "$file")
echo "Processing $filename..."
echo " \"${filename}\": \"\"\"" >> "$OUTPUT_FILE"
Expand All @@ -21,22 +21,14 @@ find "$STDLIB_PATH" -name "*.pkl" | while IFS= read -r file; do
echo "$escapedLine" >> "$OUTPUT_FILE"
done < "$file"
echo "\"\"\"," >> "$OUTPUT_FILE"
done
done < <(find "$STDLIB_PATH" -name "*.pkl")

# Finish the hashMap and Resources struct
# Use `head` and `tail` to remove the last comma from the hashMap
echo " ]" >> "$OUTPUT_FILE"
echo "}" >> "$OUTPUT_FILE"
# Use sed to remove the last comma. This approach is compatible across both GNU and BSD sed.
# -i '' -e: For BSD sed compatibility, specifying an empty extension for in-place editing without backup.
# $!N; $!ba; : For GNU sed, to accumulate the whole file into pattern space.
# s/,\n \]/\n ]/: To remove the last comma before the closing square bracket.
sed -i '' -e '$!N; $!ba; s/,\n \]/\n ]/' "$OUTPUT_FILE"

# Correctly format the Swift dictionary to remove the last comma
# Temporary file for intermediate output
TMP_FILE="${OUTPUT_FILE}.tmp"
echo "]}" >> "$OUTPUT_FILE"

# Leave the last two lines (closing brackets) untouched, remove the last comma from the rest
head -n -2 "$OUTPUT_FILE" | sed '$ s/,$//' > "$TMP_FILE"
tail -n 2 "$OUTPUT_FILE" >> "$TMP_FILE"

# Replace original file with corrected one
mv "$TMP_FILE" "$OUTPUT_FILE"
echo "Done!"

0 comments on commit 975b883

Please sign in to comment.