Skip to content

Add count-lines action #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2024
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
30 changes: 30 additions & 0 deletions .github/workflows/self-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Developer workflow

on:
pull_request:
push:
branches:
- main
workflow_call:
inputs:
repo:
type: string
required: false

env:
COUNT_LINES_REPO: ${{ inputs.repo || 'kir-dev/cmsch' }}

jobs:
count-lines:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
repository: ${{ env.COUNT_LINES_REPO }}
- uses: kir-dev/automations/actions/count-lines@main
id: counter
with:
target_directory: .
- name: Generate summary
run: |
echo 'There are ${{ steps.counter.outputs.line_count }} lines in this project' >> $GITHUB_STEP_SUMMARY
25 changes: 25 additions & 0 deletions actions/count-lines/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: "Count Lines"
description: "Counts the number of lines in a specified directory and outputs the result."
inputs:
target_directory:
description: "The directory to count lines in"
required: true
ignore_rule:
description: "Rule to apply for ignoring files: 'git' for .gitignore, 'hidden' for hidden directories, or 'none' for no exclusions."
required: true
default: "git"
command_path:
description: "Path to the script that will be executed"
required: true
default: $GITHUB_ACTION_PATH/../../scripts/count-lines.sh
outputs:
line_count:
description: "The number of lines counted in the directory"
value: ${{ steps.run-script.outputs.line_count }}
runs:
using: composite
steps:
- name: Run script
id: run-script
shell: bash
run: ${{ inputs.command_path }} ${{ inputs.target_directory }} ${{ inputs.ignore_rule }}
50 changes: 50 additions & 0 deletions scripts/count-lines.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
set -euo pipefail
set -x

TARGET_DIR=$1
IGNORE_RULE=$2

# Enable this script to be executed locally
: ${GITHUB_OUTPUT:=/dev/stdout}
: ${GITHUB_STATE:=/dev/stdout}
: ${GITHUB_STEP_SUMMARY:=/dev/stdout}
: ${GITHUB_ENV:=/tmp/.github-env}

if [[ -n "$GITHUB_ENV" && -f "$GITHUB_ENV" ]]; then
source "$GITHUB_ENV"
fi

if [ ! -d "$TARGET_DIR" ]; then
echo "Directory '$TARGET_DIR' not found" >&2
exit 1
fi

pushd "$TARGET_DIR"

tree >&2

function count_lines {
echo $(xargs wc --lines | tail -n 1 | awk '{print $1}')
}

case "$IGNORE_RULE" in
git)
line_count=$(git ls-files | count_lines)
;;
hidden)
line_count=$(find . -type f -not -path '*/.*/*' | count_lines)
;;
none)
line_count=$(find . -type f | count_lines)
;;
*)
echo "Invalid IGNORE_RULE value. Use 'git', 'hidden', or 'none'" >&2
exit 1
;;
esac

echo $GITHUB_OUTPUT >&2
echo "line_count=$line_count" >> $GITHUB_OUTPUT

popd
Loading