Skip to content

Commit 79cadbe

Browse files
committed
refactor: switch to HIGHLIGHT_LANGS/IGNORE_LANGS for clearer and optimized language handling
1 parent da131ce commit 79cadbe

2 files changed

Lines changed: 63 additions & 37 deletions

File tree

.github/workflows/analyze-code.yml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ jobs:
1212
count-lines:
1313
runs-on: ubuntu-latest
1414
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")
15+
# Check cloc docs for supported languages and exact names (e.g., "Vuejs Component", "C#")
16+
# HIGHLIGHT_LANGS: show these languages individually; everything else goes to "Others"
1717
HIGHLIGHT_LANGS: "JavaScript,TypeScript,JSX,Vuejs Component,PHP,C#"
1818

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"
19+
# IGNORE_LANGS: drop these languages entirely (not shown and not counted)
20+
IGNORE_LANGS: "JSON,HTML,CSS,SCSS,Sass,Markdown,SVG,XML,YAML,TOML,CSV,Text,Properties"
2121

2222
steps:
2323
- name: Checkout Code
@@ -51,12 +51,14 @@ jobs:
5151
DEFAULT_BRANCH=$(curl -H "Authorization: token $GH_PAT" -s "https://api.github.com/repos/$REPO" | jq -r '.default_branch')
5252
5353
# echo "Cloning $REPO (default branch: $DEFAULT_BRANCH)..."
54-
git clone --branch "$DEFAULT_BRANCH" --single-branch "$AUTHENTICATED_REPO" "$(basename $REPO)-$DEFAULT_BRANCH" || echo "Failed to clone a repository"
54+
git clone --quiet --branch "$DEFAULT_BRANCH" --single-branch "$AUTHENTICATED_REPO" "$(basename $REPO)-$DEFAULT_BRANCH" || echo "Failed to clone a repository"
5555
done
5656
5757
# Run cloc to analyze lines of code, excluding non-source code files
5858
echo "Calculating lines of code..."
5959
mkdir -p ../output
60+
# Count LOC for all cloned repos; exclude languages via cloc's own --exclude-lang so totals match SUM
61+
# NOTE: This does NOT apply .gitignore automatically across multiple repos; for per-repo .gitignore, run cloc per folder
6062
cloc . --json --report-file=../output/cloc-output.json --exclude-lang="${IGNORE_LANGS}"
6163
6264
# Commit and push the updated cloc-output.json and README.md to the current branch
@@ -67,6 +69,9 @@ jobs:
6769
git config --global user.name "github-actions[bot]"
6870
git config --global user.email "github-actions[bot]@users.noreply.github.com"
6971
72+
# Number formatting: enable thousands separators for all printf in this step
73+
# LC_ALL/LANG must be set before any printf that uses %'d
74+
7075
# --- format helper ---
7176
format_number() {
7277
printf "%'d\n" "$1"
@@ -82,7 +87,7 @@ jobs:
8287
OTHER_LINES=0
8388
FORMATTED_BREAKDOWN=""
8489
85-
# Iterate over each language and its code lines
90+
# Normalize the highlight list for exact, comma-bounded matching (no false partial matches)
8691
HL=",$(echo "$HIGHLIGHT_LANGS" | tr -d ' '),"
8792
8893
while read -r entry; do
@@ -103,7 +108,7 @@ jobs:
103108
| .[]' output/cloc-output.json
104109
)
105110
106-
# Add Others (formatted)
111+
# "Others" includes all non-highlighted languages that cloc reported (after --exclude-lang); excluded ones never reach here
107112
if [[ $OTHER_LINES -gt 0 ]]; then
108113
FORMATTED_OTHER=$(format_number "$OTHER_LINES")
109114
FORMATTED_BREAKDOWN+=$(printf "%-12s --> %s lines\n" "Others" "$FORMATTED_OTHER")$'\n'
@@ -119,7 +124,8 @@ jobs:
119124
[ TOTAL LINES OF CODE: $FORMATTED_TOTAL ]
120125
\`\`\`"
121126
122-
# Update README.md by replacing the section between predefined comment markers
127+
# Replace content between markers. Requires these in README.md:
128+
# <!-- LANGUAGES BREAKDOWN START --> ... <!-- LANGUAGES BREAKDOWN END -->
123129
echo "$CODE_BLOCK" > temp_block.txt
124130
sed -i '/<!-- LANGUAGES BREAKDOWN START -->/,/<!-- LANGUAGES BREAKDOWN END -->/{
125131
//!d

analyze-code.yml

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ on:
88
jobs:
99
count-lines:
1010
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"
1118

1219
steps:
1320
- name: Checkout Code
@@ -48,62 +55,75 @@ jobs:
4855
# Run cloc to analyze lines of code, excluding non-source code files
4956
echo "Calculating lines of code..."
5057
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}"
5461
5562
# Commit and push the updated cloc-output.json and README.md to the current branch
5663
- name: Commit and Push Output
5764
env:
5865
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
6166
run: |
6267
git config --global user.name "github-actions[bot]"
6368
git config --global user.email "github-actions[bot]@users.noreply.github.com"
6469
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+
6685
OTHER_LINES=0
6786
FORMATTED_BREAKDOWN=""
6887
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 ' '),"
7190
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')
7694
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'
8798
else
8899
OTHER_LINES=$((OTHER_LINES + LINES))
89100
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
92110
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'
96113
fi
97114
98-
FORMATTED_TOTAL=$(printf "%'d\n" $TOTAL_LINES)
115+
# Format total ONCE (do not overwrite later)
116+
FORMATTED_TOTAL=$(format_number "$TOTAL_LINES")
117+
99118
CODE_BLOCK="\`\`\`
100119
[ LANGUAGES BREAKDOWN ]
101120
102121
$FORMATTED_BREAKDOWN
103122
[ TOTAL LINES OF CODE: $FORMATTED_TOTAL ]
104123
\`\`\`"
105124
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 -->
107127
echo "$CODE_BLOCK" > temp_block.txt
108128
sed -i '/<!-- LANGUAGES BREAKDOWN START -->/,/<!-- LANGUAGES BREAKDOWN END -->/{
109129
//!d

0 commit comments

Comments
 (0)