Skip to content

Commit 429d0a8

Browse files
authored
Merge pull request #2 from sathwikhbhat/enhancement/dynamic-languages-workflow
Improve workflow to include all user languages in descending order
2 parents ef5687a + 083c60b commit 429d0a8

2 files changed

Lines changed: 54 additions & 40 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: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,58 @@ 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=json,html,css,svg,md,py,ps1,scss --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-
# Format and update README
62-
TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json)
63-
JS_LINES=$(jq '.JavaScript.code // 0' output/cloc-output.json)
64-
TS_LINES=$(jq '.TypeScript.code // 0' output/cloc-output.json)
65-
JSX_LINES=$(jq '.JSX.code // 0' output/cloc-output.json)
66-
CSHARP_LINES=$(jq '."C#".code // 0' output/cloc-output.json)
67-
VUE_LINES=$(jq '."Vuejs Component".code // 0' output/cloc-output.json)
68-
PHP_LINES=$(jq '.PHP.code // 0' output/cloc-output.json)
69-
OTHER_LINES=$((TOTAL_LINES - JS_LINES - TS_LINES - JSX_LINES - PHP_LINES - CSHARP_LINES - VUE_LINES))
70-
71-
# Function to format numbers with commas (ensuring proper locale settings)
72-
format_number() {
73-
export LC_ALL="en_US.UTF-8"
74-
printf "%'d\n" $1
75-
}
76-
77-
FORMATTED_TOTAL=$(format_number $TOTAL_LINES)
78-
FORMATTED_JS=$(format_number $JS_LINES)
79-
FORMATTED_TS=$(format_number $TS_LINES)
80-
FORMATTED_JSX=$(format_number $JSX_LINES)
81-
FORMATTED_CSHARP=$(format_number $CSHARP_LINES)
82-
FORMATTED_VUE=$(format_number $VUE_LINES)
83-
FORMATTED_PHP=$(format_number $PHP_LINES)
84-
FORMATTED_OTHER=$(format_number $OTHER_LINES)
65+
TOTAL_LINES=0
66+
OTHER_LINES=0
67+
FORMATTED_BREAKDOWN=""
68+
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
8597
98+
FORMATTED_TOTAL=$(printf "%'d\n" $TOTAL_LINES)
8699
CODE_BLOCK="\`\`\`
87100
[ LANGUAGES BREAKDOWN ]
88101
89-
JavaScript --> $FORMATTED_JS lines
90-
TypeScript --> $FORMATTED_TS lines
91-
JSX --> $FORMATTED_JSX lines
92-
Vue.js --> $FORMATTED_VUE lines
93-
PHP --> $FORMATTED_PHP lines
94-
C# --> $FORMATTED_CSHARP lines
95-
Other --> $FORMATTED_OTHER lines
96-
102+
$FORMATTED_BREAKDOWN
97103
[ TOTAL LINES OF CODE: $FORMATTED_TOTAL ]
98104
\`\`\`"
99105

0 commit comments

Comments
 (0)