Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,32 @@ This GitHub Action automatically fetches all your public repositories (excluding
```
Then commit and push the changes.

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

2. **Generate a GitHub Personal Access Token (PAT)**
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.

Languages not listed in `INCLUDE_LANGUAGES` and not excluded by `EXCLUDE_LANGUAGES` will be counted together and displayed as "Other languages" in the breakdown.

4. **Update GitHub Username**

Make sure to update the workflow file with your GitHub username wherever required.

6. **Generate a GitHub Personal Access Token (PAT)**
You need a **Personal Access Token (PAT)** with **`repo`** permissions.
Refer to [GitHub Docs](https://github.com/settings/tokens) on how to generate one.

3. **Add the Token to Repository Secrets**
7. **Add the Token to Repository Secrets**
- Go to **Settings → Secrets and variables → Actions → New repository secret**
- Name the secret **`GH_PAT`**
- Paste the generated token and save.

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

5. **Trigger the Workflow**
9. **Trigger the Workflow**
- The workflow runs **by default every Sunday at midnight UTC (customizable)**.
- To **run manually**, go to **GitHub Actions → Select Workflow → Run Workflow**.
- To **run on every push**, modify the workflow's `on:` section to:
Expand All @@ -60,10 +68,10 @@ This GitHub Action automatically fetches all your public repositories (excluding
- main
```

6. **Wait for Processing**
10. **Wait for Processing**
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**.

7. **Ensure Placeholders Are Present**
11. **Ensure Placeholders Are Present**
To allow automatic updates, your `README.md` must include the following placeholders:
```
<!-- LANGUAGES BREAKDOWN (STATIC EXAMPLE) START -->
Expand Down
72 changes: 39 additions & 33 deletions analyze-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,58 @@ jobs:
# Run cloc to analyze lines of code, excluding non-source code files
echo "Calculating lines of code..."
mkdir -p ../output
cloc . --exclude-ext=json,html,css,svg,md,py,ps1,scss --json > ../output/cloc-output.json
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 \
--json > ../output/cloc-output.json

# Commit and push the updated cloc-output.json and README.md to the current branch
- name: Commit and Push Output
env:
GH_PAT: ${{ secrets.GH_PAT }}
INCLUDE_LANGS: "C,Javascript,Java,Python" # Mention Languages to be displayed in the README (match CLOC names)
EXCLUDE_LANGS: "XML,Properties,Maven,Gradle" # Mention Languages to be ignored completely in the README
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"

# Format and update README
TOTAL_LINES=$(jq '.SUM.code // 0' output/cloc-output.json)
JS_LINES=$(jq '.JavaScript.code // 0' output/cloc-output.json)
TS_LINES=$(jq '.TypeScript.code // 0' output/cloc-output.json)
JSX_LINES=$(jq '.JSX.code // 0' output/cloc-output.json)
CSHARP_LINES=$(jq '."C#".code // 0' output/cloc-output.json)
VUE_LINES=$(jq '."Vuejs Component".code // 0' output/cloc-output.json)
PHP_LINES=$(jq '.PHP.code // 0' output/cloc-output.json)
OTHER_LINES=$((TOTAL_LINES - JS_LINES - TS_LINES - JSX_LINES - PHP_LINES - CSHARP_LINES - VUE_LINES))

# Function to format numbers with commas (ensuring proper locale settings)
format_number() {
export LC_ALL="en_US.UTF-8"
printf "%'d\n" $1
}

FORMATTED_TOTAL=$(format_number $TOTAL_LINES)
FORMATTED_JS=$(format_number $JS_LINES)
FORMATTED_TS=$(format_number $TS_LINES)
FORMATTED_JSX=$(format_number $JSX_LINES)
FORMATTED_CSHARP=$(format_number $CSHARP_LINES)
FORMATTED_VUE=$(format_number $VUE_LINES)
FORMATTED_PHP=$(format_number $PHP_LINES)
FORMATTED_OTHER=$(format_number $OTHER_LINES)
TOTAL_LINES=0
OTHER_LINES=0
FORMATTED_BREAKDOWN=""

IFS=',' read -r -a INCLUDE <<< "$INCLUDE_LANGS"
IFS=',' read -r -a EXCLUDE <<< "$EXCLUDE_LANGS"

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
_jq() { echo "$entry" | base64 --decode | jq -r "$1"; }
LANG=$(_jq '.lang')
LINES=$(_jq '.lines')

# Skip excluded languages
if [[ " ${EXCLUDE[@]} " =~ (^|[[:space:]])$LANG($|[[:space:]]) ]]; then
continue
fi

# Include explicitly listed languages
if [[ " ${INCLUDE[@]} " =~ (^|[[:space:]])$LANG($|[[:space:]]) ]]; then
FORMATTED=$(printf "%'d\n" $LINES)
FORMATTED_BREAKDOWN+=$(printf "%-15s --> %10s lines\n" "$LANG" "$FORMATTED")$'\n'
TOTAL_LINES=$((TOTAL_LINES + LINES))
else
OTHER_LINES=$((OTHER_LINES + LINES))
fi
done

if [[ $OTHER_LINES -gt 0 ]]; then
FORMATTED_OTHER=$(printf "%'d\n" $OTHER_LINES)
FORMATTED_BREAKDOWN+=$(printf "%-15s --> %10s lines\n" "Others" "$FORMATTED_OTHER")$'\n'
TOTAL_LINES=$((TOTAL_LINES + OTHER_LINES))
fi

FORMATTED_TOTAL=$(printf "%'d\n" $TOTAL_LINES)
CODE_BLOCK="\`\`\`
[ LANGUAGES BREAKDOWN ]

JavaScript --> $FORMATTED_JS lines
TypeScript --> $FORMATTED_TS lines
JSX --> $FORMATTED_JSX lines
Vue.js --> $FORMATTED_VUE lines
PHP --> $FORMATTED_PHP lines
C# --> $FORMATTED_CSHARP lines
Other --> $FORMATTED_OTHER lines

$FORMATTED_BREAKDOWN
[ TOTAL LINES OF CODE: $FORMATTED_TOTAL ]
\`\`\`"

Expand Down