Skip to content

Instantly share code, notes, and snippets.

@leogdion
Created April 20, 2024 00:46
Show Gist options
  • Save leogdion/96e4d666d06c2b1672bce2b29aaae94c to your computer and use it in GitHub Desktop.
Save leogdion/96e4d666d06c2b1672bce2b29aaae94c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if input file is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <input_file.swift>"
exit 1
fi
input_file="$1"
output_file="${input_file%.swift}_cleaned.swift"
# Check if input file exists
if [ ! -f "$input_file" ]; then
echo "Input file '$input_file' not found!"
exit 1
fi
# Collect unique import statements
imports=$(awk '/^import / {imports[$0]=1} END {for (i in imports) print i}' "$input_file")
# Remove empty lines, lines containing only comments, and import statements
awk '!/^[[:space:]]*(\/\/.*)?$|^import /' "$input_file" > "$output_file.tmp"
# Remove leading and trailing whitespace from each line
sed -i '' -e 's/^[[:space:]]*//;s/[[:space:]]*$//' "$output_file.tmp"
# Append collected import statements at the beginning of the file
echo "$imports" > "$output_file"
cat "$output_file.tmp" >> "$output_file"
# Clean up temporary file
rm "$output_file.tmp"
echo "Swift code cleaned successfully! Output saved to '$output_file'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment