11name : Update Lines of Code in Readme
22
33on :
4+ push :
5+ branches :
6+ - main
47 schedule :
58 - cron : " 0 0 * * 0" # Runs weekly on Sunday at midnight (UTC)
69 workflow_dispatch : # Allows manual trigger
710
811jobs :
912 count-lines :
1013 runs-on : ubuntu-latest
14+ env :
15+ # Check official cloc documentation for supported languages and their exact names
16+ # Languages to show individually in README (others will be grouped under "Others")
17+ HIGHLIGHT_LANGS : " C,JavaScript,Java,Python"
18+
19+ # Languages to ignore completely (not shown and not counted in totals)
20+ IGNORE_LANGS : " JSON,HTML,CSS,SCSS,Sass,Markdown,SVG,YAML,TOML,CSV,Text,Properties"
1121
1222 steps :
1323 - name : Checkout Code
4757 # Run cloc to analyze lines of code, excluding non-source code files
4858 echo "Calculating lines of code..."
4959 mkdir -p ../output
50- cloc . --exclude-ext= json,html,css,svg,md,py,ps1,scss --json > ../output/cloc-output.json
60+ cloc . --json --report-file= ../output/cloc-output.json --exclude-lang="${IGNORE_LANGS}"
5161
5262 # Commit and push the updated cloc-output.json and README.md to the current branch
5363 - name : Commit and Push Output
@@ -57,42 +67,55 @@ jobs:
5767 git config --global user.name "github-actions[bot]"
5868 git config --global user.email "github-actions[bot]@users.noreply.github.com"
5969
60- # Format and update README
61- TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json)
62- JS_LINES=$(jq '.JavaScript.code // 0' output/cloc-output.json)
63- TS_LINES=$(jq '.TypeScript.code // 0' output/cloc-output.json)
64- JSX_LINES=$(jq '.JSX.code // 0' output/cloc-output.json)
65- CSHARP_LINES=$(jq '."C#".code // 0' output/cloc-output.json)
66- VUE_LINES=$(jq '."Vuejs Component".code // 0' output/cloc-output.json)
67- PHP_LINES=$(jq '.PHP.code // 0' output/cloc-output.json)
68- OTHER_LINES=$((TOTAL_LINES - JS_LINES - TS_LINES - JSX_LINES - PHP_LINES - CSHARP_LINES - VUE_LINES))
69-
70- # Function to format numbers with commas (ensuring proper locale settings)
70+ # --- format helper ---
7171 format_number() {
72- export LC_ALL="en_US.UTF-8"
73- printf "%'d\n" $1
72+ printf "%'d\n" "$1"
7473 }
7574
76- FORMATTED_TOTAL=$(format_number $TOTAL_LINES)
77- FORMATTED_JS=$(format_number $JS_LINES)
78- FORMATTED_TS=$(format_number $TS_LINES)
79- FORMATTED_JSX=$(format_number $JSX_LINES)
80- FORMATTED_CSHARP=$(format_number $CSHARP_LINES)
81- FORMATTED_VUE=$(format_number $VUE_LINES)
82- FORMATTED_PHP=$(format_number $PHP_LINES)
83- FORMATTED_OTHER=$(format_number $OTHER_LINES)
75+ # Ensure grouping is enabled for every printf in this step
76+ export LC_ALL="en_US.UTF-8"
77+ export LANG="en_US.UTF-8"
78+
79+ # Grab total from cloc
80+ TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json)
81+
82+ OTHER_LINES=0
83+ FORMATTED_BREAKDOWN=""
84+
85+ # Iterate over each language and its code lines
86+ HL=",$(echo "$HIGHLIGHT_LANGS" | tr -d ' '),"
87+
88+ while read -r entry; do
89+ LANG=$(echo "$entry" | jq -r '.lang')
90+ LINES=$(echo "$entry" | jq -r '.lines')
91+
92+ if [[ "$HL" == *",$LANG,"* ]]; then
93+ FORMATTED=$(format_number "$LINES")
94+ FORMATTED_BREAKDOWN+=$(printf "%-15s --> %10s lines\n" "$LANG" "$FORMATTED")$'\n'
95+ else
96+ OTHER_LINES=$((OTHER_LINES + LINES))
97+ fi
98+ done < <(
99+ jq -c 'to_entries
100+ | map(select(.key != "header" and .key != "SUM"))
101+ | map({lang: .key, lines: .value.code})
102+ | map(select(.lines > 0))
103+ | .[]' output/cloc-output.json
104+ )
105+
106+ # Add Others (formatted)
107+ if [[ $OTHER_LINES -gt 0 ]]; then
108+ FORMATTED_OTHER=$(format_number "$OTHER_LINES")
109+ FORMATTED_BREAKDOWN+=$(printf "%-15s --> %10s lines\n" "Others" "$FORMATTED_OTHER")$'\n'
110+ fi
111+
112+ # Format total ONCE (do not overwrite later)
113+ FORMATTED_TOTAL=$(format_number "$TOTAL_LINES")
84114
85115 CODE_BLOCK="\`\`\`
86116 [ LANGUAGES BREAKDOWN ]
87117
88- JavaScript --> $FORMATTED_JS lines
89- TypeScript --> $FORMATTED_TS lines
90- JSX --> $FORMATTED_JSX lines
91- Vue.js --> $FORMATTED_VUE lines
92- PHP --> $FORMATTED_PHP lines
93- C# --> $FORMATTED_CSHARP lines
94- Other --> $FORMATTED_OTHER lines
95-
118+ $FORMATTED_BREAKDOWN
96119 [ TOTAL LINES OF CODE: $FORMATTED_TOTAL ]
97120 \`\`\`"
98121
0 commit comments