|
8 | 8 | jobs: |
9 | 9 | count-lines: |
10 | 10 | runs-on: ubuntu-latest |
| 11 | + env: |
| 12 | + # Check cloc docs for supported languages and exact names (e.g., "Vuejs Component", "C#") |
| 13 | + # HIGHLIGHT_LANGS: show these languages individually; everything else goes to "Others" |
| 14 | + HIGHLIGHT_LANGS: "JavaScript,TypeScript,JSX,Vuejs Component,PHP,C#" |
| 15 | + |
| 16 | + # IGNORE_LANGS: drop these languages entirely (not shown and not counted) |
| 17 | + IGNORE_LANGS: "JSON,HTML,CSS,SCSS,Sass,Markdown,SVG,XML,YAML,TOML,CSV,Text,Properties" |
11 | 18 |
|
12 | 19 | steps: |
13 | 20 | - name: Checkout Code |
@@ -48,62 +55,75 @@ jobs: |
48 | 55 | # Run cloc to analyze lines of code, excluding non-source code files |
49 | 56 | echo "Calculating lines of code..." |
50 | 57 | mkdir -p ../output |
51 | | - cloc . \ |
52 | | - --exclude-ext=html,htm,css,json,md,markdown,xml,svg,scss,yaml,toml,csv,txt,yml,sass,lock,log,properties,HTML,HTM,CSS,JSON,MD,MARKDOWN,XML,SVG,SCSS,YAML,TOML,CSV,TXT,YML,SASS,LOCK,LOG,PROPERTIES \ |
53 | | - --json > ../output/cloc-output.json |
| 58 | + # Count LOC for all cloned repos; exclude languages via cloc's own --exclude-lang so totals match SUM |
| 59 | + # NOTE: This does NOT apply .gitignore automatically across multiple repos; for per-repo .gitignore, run cloc per folder |
| 60 | + cloc . --json --report-file=../output/cloc-output.json --exclude-lang="${IGNORE_LANGS}" |
54 | 61 |
|
55 | 62 | # Commit and push the updated cloc-output.json and README.md to the current branch |
56 | 63 | - name: Commit and Push Output |
57 | 64 | env: |
58 | 65 | GH_PAT: ${{ secrets.GH_PAT }} |
59 | | - INCLUDE_LANGS: "C,Javascript,Java,Python" # Mention Languages to be displayed in the README (match CLOC names) |
60 | | - EXCLUDE_LANGS: "XML,Properties,Maven,Gradle" # Mention Languages to be ignored completely in the README |
61 | 66 | run: | |
62 | 67 | git config --global user.name "github-actions[bot]" |
63 | 68 | git config --global user.email "github-actions[bot]@users.noreply.github.com" |
64 | 69 |
|
65 | | - TOTAL_LINES=0 |
| 70 | + # Number formatting: enable thousands separators for all printf in this step |
| 71 | + # LC_ALL/LANG must be set before any printf that uses %'d |
| 72 | +
|
| 73 | + # --- format helper --- |
| 74 | + format_number() { |
| 75 | + printf "%'d\n" "$1" |
| 76 | + } |
| 77 | +
|
| 78 | + # Ensure grouping is enabled for every printf in this step |
| 79 | + export LC_ALL="en_US.UTF-8" |
| 80 | + export LANG="en_US.UTF-8" |
| 81 | +
|
| 82 | + # Grab total from cloc |
| 83 | + TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json) |
| 84 | +
|
66 | 85 | OTHER_LINES=0 |
67 | 86 | FORMATTED_BREAKDOWN="" |
68 | 87 |
|
69 | | - IFS=',' read -r -a INCLUDE <<< "$INCLUDE_LANGS" |
70 | | - IFS=',' read -r -a EXCLUDE <<< "$EXCLUDE_LANGS" |
| 88 | + # Normalize the highlight list for exact, comma-bounded matching (no false partial matches) |
| 89 | + HL=",$(echo "$HIGHLIGHT_LANGS" | tr -d ' ')," |
71 | 90 |
|
72 | | - for entry in $(jq -r 'to_entries | map(select(.key != "header" and .key != "SUM")) | map({lang: .key, lines: .value.code}) | map(select(.lines > 0)) | .[] | @base64' output/cloc-output.json); do |
73 | | - _jq() { echo "$entry" | base64 --decode | jq -r "$1"; } |
74 | | - LANG=$(_jq '.lang') |
75 | | - LINES=$(_jq '.lines') |
| 91 | + while read -r entry; do |
| 92 | + LANG=$(echo "$entry" | jq -r '.lang') |
| 93 | + LINES=$(echo "$entry" | jq -r '.lines') |
76 | 94 |
|
77 | | - # Skip excluded languages |
78 | | - if [[ " ${EXCLUDE[@]} " =~ (^|[[:space:]])$LANG($|[[:space:]]) ]]; then |
79 | | - continue |
80 | | - fi |
81 | | -
|
82 | | - # Include explicitly listed languages |
83 | | - if [[ " ${INCLUDE[@]} " =~ (^|[[:space:]])$LANG($|[[:space:]]) ]]; then |
84 | | - FORMATTED=$(printf "%'d\n" $LINES) |
85 | | - FORMATTED_BREAKDOWN+=$(printf "%-15s --> %10s lines\n" "$LANG" "$FORMATTED")$'\n' |
86 | | - TOTAL_LINES=$((TOTAL_LINES + LINES)) |
| 95 | + if [[ "$HL" == *",$LANG,"* ]]; then |
| 96 | + FORMATTED=$(format_number "$LINES") |
| 97 | + FORMATTED_BREAKDOWN+=$(printf "%-12s --> %s lines\n" "$LANG" "$FORMATTED")$'\n' |
87 | 98 | else |
88 | 99 | OTHER_LINES=$((OTHER_LINES + LINES)) |
89 | 100 | fi |
90 | | - done |
91 | | -
|
| 101 | + done < <( |
| 102 | + jq -c 'to_entries |
| 103 | + | map(select(.key != "header" and .key != "SUM")) |
| 104 | + | map({lang: .key, lines: .value.code}) |
| 105 | + | map(select(.lines > 0)) |
| 106 | + | .[]' output/cloc-output.json |
| 107 | + ) |
| 108 | +
|
| 109 | + # "Others" includes all non-highlighted languages that cloc reported (after --exclude-lang); excluded ones never reach here |
92 | 110 | if [[ $OTHER_LINES -gt 0 ]]; then |
93 | | - FORMATTED_OTHER=$(printf "%'d\n" $OTHER_LINES) |
94 | | - FORMATTED_BREAKDOWN+=$(printf "%-15s --> %10s lines\n" "Others" "$FORMATTED_OTHER")$'\n' |
95 | | - TOTAL_LINES=$((TOTAL_LINES + OTHER_LINES)) |
| 111 | + FORMATTED_OTHER=$(format_number "$OTHER_LINES") |
| 112 | + FORMATTED_BREAKDOWN+=$(printf "%-12s --> %s lines\n" "Others" "$FORMATTED_OTHER")$'\n' |
96 | 113 | fi |
97 | 114 |
|
98 | | - FORMATTED_TOTAL=$(printf "%'d\n" $TOTAL_LINES) |
| 115 | + # Format total ONCE (do not overwrite later) |
| 116 | + FORMATTED_TOTAL=$(format_number "$TOTAL_LINES") |
| 117 | +
|
99 | 118 | CODE_BLOCK="\`\`\` |
100 | 119 | [ LANGUAGES BREAKDOWN ] |
101 | 120 |
|
102 | 121 | $FORMATTED_BREAKDOWN |
103 | 122 | [ TOTAL LINES OF CODE: $FORMATTED_TOTAL ] |
104 | 123 | \`\`\`" |
105 | 124 |
|
106 | | - # Update README.md by replacing the section between predefined comment markers |
| 125 | + # Replace content between markers. Requires these in README.md: |
| 126 | + # <!-- LANGUAGES BREAKDOWN START --> ... <!-- LANGUAGES BREAKDOWN END --> |
107 | 127 | echo "$CODE_BLOCK" > temp_block.txt |
108 | 128 | sed -i '/<!-- LANGUAGES BREAKDOWN START -->/,/<!-- LANGUAGES BREAKDOWN END -->/{ |
109 | 129 | //!d |
|
0 commit comments