PMD Comments Removal
#66
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Source DRYness | |
| on: [push, pull_request, workflow_dispatch] | |
| jobs: | |
| pmd: | |
| name: PMD | |
| runs-on: "ubuntu-latest" | |
| env: | |
| pr_everything: 0 | |
| steps: | |
| - name: Clone - PR | |
| uses: actions/checkout@v4 | |
| - name: Set up Java | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: '17' | |
| - name: Run CPD for Fortran | |
| continue-on-error: true | |
| run: | | |
| # Get latest PMD version from GitHub API | |
| PMD_VERSION=$(curl -s https://api.github.com/repos/pmd/pmd/releases/latest | grep '"tag_name":' | cut -d'"' -f4 | sed 's/pmd_releases\///') | |
| echo "Using PMD version: $PMD_VERSION" | |
| curl -sSL -o pmd.zip \ | |
| "https://github.com/pmd/pmd/releases/download/pmd_releases/${PMD_VERSION}/pmd-dist-${PMD_VERSION}-bin.zip" | |
| unzip -q pmd.zip | |
| PMD_HOME="pmd-bin-${PMD_VERSION}" | |
| SOURCE_DIR="${1:-src}" | |
| total_files=$(find "$SOURCE_DIR" -type f \( -name "*.f" -o -name "*.f90" -o -name "*.for" -o -name "*.fpp" -o -name "*.F" -o -name "*.F90" \) | wc -l) | |
| processed=0 | |
| find "$SOURCE_DIR" -type f \( -name "*.f" -o -name "*.f90" -o -name "*.for" -o -name "*.fpp" -o -name "*.F" -o -name "*.F90" \) -print0 | | |
| while IFS= read -r -d $'\0' file; do | |
| processed=$((processed + 1)) | |
| echo -e "[$processed/$total_files] Processing ${file}..." | |
| # Create a temporary file with same permissions as original | |
| TMP_FILE=$(mktemp) | |
| if [ $? -ne 0 ]; then | |
| echo -e "Failed to create temporary file for $file, skipping" | |
| continue | |
| fi | |
| # Copy permissions from original file | |
| chmod --reference="$file" "$TMP_FILE" | |
| # More comprehensive sed command to handle different Fortran comment styles: | |
| # 1. Replace lines that are entirely comments with an empty line: | |
| # - Lines starting with '!' (free form comments) | |
| # - Lines starting with 'c', 'C', '*', 'd', 'D' in column 1 (fixed form comments) | |
| # 2. Remove end-of-line comments (anything after '!' that isn't in a string) | |
| # 3. Preserve strings containing '!' characters | |
| sed -E ' | |
| # Label for loop to handle multi-line continuations | |
| :a | |
| # If line ends with '&' (continuation character), append next line | |
| /&$/ { | |
| N | |
| # Replace '&' followed by newline, optional spaces, and another '&' with a single space | |
| s/&[[:space:]]*\n[[:space:]]*&/ / | |
| ba # Go back to label 'a' to process more continuations if present | |
| } | |
| # After handling all continuations, remove any remaining '&' characters | |
| s/&//g | |
| # Remove lines that are entirely comments: | |
| # - Lines starting with '!' (free form) | |
| /^\s*!/s/.*// | |
| # - Lines starting with 'c', 'C', '*', 'd', 'D' (fixed form, column 1) | |
| /^[cC*dD]/s/.*// | |
| # - Lines starting with space/tab and then 'c', 'C', '*', 'd', 'D' (fixed form with leading space) | |
| /^[ \t]*[cC*dD]/s/.*// | |
| # Remove end-of-line comments, preserving '!' inside strings. | |
| # This pattern looks for '!' not preceded by an odd number of quotes. | |
| # It captures everything before the '!' as group 1 and replaces the whole line with group 1. | |
| s/([^"'\''\\]*("[^"]*")?('\''[^'\'']*'\''?)?[^"'\''\\]*)[!].*$/\1/ | |
| ' "$file" > "$TMP_FILE" | |
| # Check if file was actually modified before replacing | |
| if cmp -s "$file" "$TMP_FILE"; then | |
| echo -e "No changes needed for $file" | |
| rm "$TMP_FILE" | |
| else | |
| # Overwrite the original file with the processed content | |
| mv "$TMP_FILE" "$file" | |
| echo -e "Successfully processed $file" | |
| fi | |
| done | |
| "${PMD_HOME}/bin/pmd" cpd \ | |
| --dir src \ | |
| --language fortran \ | |
| --minimum-tokens=20 \ | |
| --no-fail-on-violation \ | |
| --no-fail-on-error |