Skip to content

Commit 083c60b

Browse files
committed
fix: Add INCLUDE_LANGS and EXCLUDE_LANGS vars for flexible language filtering without workflow changes
1 parent 6c4ef72 commit 083c60b

2 files changed

Lines changed: 51 additions & 29 deletions

File tree

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,32 @@ This GitHub Action automatically fetches all your public repositories (excluding
3232
```
3333
Then commit and push the changes.
3434

35-
*Make sure to update the workflow file with your GitHub username wherever required.*
35+
2. **Customize Language Output**
3636

37-
2. **Generate a GitHub Personal Access Token (PAT)**
37+
To customize which languages are shown or excluded, visit lines 59 and 60 of `analyze-code.yml` and edit the `INCLUDE_LANGUAGES` and `EXCLUDE_LANGUAGES` variables as needed.
38+
39+
Languages not listed in `INCLUDE_LANGUAGES` and not excluded by `EXCLUDE_LANGUAGES` will be counted together and displayed as "Other languages" in the breakdown.
40+
41+
4. **Update GitHub Username**
42+
43+
Make sure to update the workflow file with your GitHub username wherever required.
44+
45+
6. **Generate a GitHub Personal Access Token (PAT)**
3846
You need a **Personal Access Token (PAT)** with **`repo`** permissions.
3947
Refer to [GitHub Docs](https://github.com/settings/tokens) on how to generate one.
4048

41-
3. **Add the Token to Repository Secrets**
49+
7. **Add the Token to Repository Secrets**
4250
- Go to **Settings → Secrets and variables → Actions → New repository secret**
4351
- Name the secret **`GH_PAT`**
4452
- Paste the generated token and save.
4553

46-
4. **Update Workflow Permissions**
54+
8. **Update Workflow Permissions**
4755
In the repository where you're running the action, make sure to update workflow permissions:
4856
- Go to **Settings → Actions → General**.
4957
- Under **Workflow permissions**, select **"Read and write permissions"**.
5058
- This allows the workflow to update files like `README.md` automatically.
5159

52-
5. **Trigger the Workflow**
60+
9. **Trigger the Workflow**
5361
- The workflow runs **by default every Sunday at midnight UTC (customizable)**.
5462
- To **run manually**, go to **GitHub Actions → Select Workflow → Run Workflow**.
5563
- To **run on every push**, modify the workflow's `on:` section to:
@@ -60,10 +68,10 @@ This GitHub Action automatically fetches all your public repositories (excluding
6068
- main
6169
```
6270
63-
6. **Wait for Processing**
71+
10. **Wait for Processing**
6472
The time taken depends on the number of repositories and their sizes. Once completed, your `README.md` will be updated with the latest **lines of code breakdown**.
6573

66-
7. **Ensure Placeholders Are Present**
74+
11. **Ensure Placeholders Are Present**
6775
To allow automatic updates, your `README.md` must include the following placeholders:
6876
```
6977
<!-- LANGUAGES BREAKDOWN (STATIC EXAMPLE) START -->

analyze-code.yml

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,40 +48,54 @@ jobs:
4848
# Run cloc to analyze lines of code, excluding non-source code files
4949
echo "Calculating lines of code..."
5050
mkdir -p ../output
51-
cloc . --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 --exclude-lang=XML,Properties,Maven,Gradle --json > ../output/cloc-output.json
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
5254
5355
# Commit and push the updated cloc-output.json and README.md to the current branch
5456
- name: Commit and Push Output
5557
env:
5658
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
5761
run: |
5862
git config --global user.name "github-actions[bot]"
5963
git config --global user.email "github-actions[bot]@users.noreply.github.com"
6064
61-
TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json)
62-
63-
# Function to format numbers with commas (ensuring proper locale settings)
64-
format_number() {
65-
export LC_ALL="en_US.UTF-8"
66-
printf "%'d\n" $1
67-
}
68-
69-
# Build sorted breakdown as CSV: lang,lines
70-
LANG_BREAKDOWN=$(jq -r 'to_entries
71-
| map(select(.key != "header" and .key != "SUM"))
72-
| map({lang: .key, lines: .value.code})
73-
| map(select(.lines > 1))
74-
| sort_by(-.lines)[]
75-
| "\(.lang),\(.lines)"' output/cloc-output.json)
76-
65+
TOTAL_LINES=0
66+
OTHER_LINES=0
7767
FORMATTED_BREAKDOWN=""
78-
while IFS=, read -r LANG LINES; do
79-
FORMATTED=$(format_number $LINES)
80-
FORMATTED_BREAKDOWN+=$(printf "%-15s --> %10s lines\n" "$LANG" "$FORMATTED")$'\n'
81-
done <<< "$LANG_BREAKDOWN"
8268
83-
FORMATTED_TOTAL=$(format_number $TOTAL_LINES)
69+
IFS=',' read -r -a INCLUDE <<< "$INCLUDE_LANGS"
70+
IFS=',' read -r -a EXCLUDE <<< "$EXCLUDE_LANGS"
71+
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')
76+
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))
87+
else
88+
OTHER_LINES=$((OTHER_LINES + LINES))
89+
fi
90+
done
91+
92+
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))
96+
fi
8497
98+
FORMATTED_TOTAL=$(printf "%'d\n" $TOTAL_LINES)
8599
CODE_BLOCK="\`\`\`
86100
[ LANGUAGES BREAKDOWN ]
87101

0 commit comments

Comments
 (0)