|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# WARNING: Please, DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Count lines of code of this repository. This is a gocloc command wrapper. It |
| 8 | +# will run gocloc natively if it is installed, otherwise it will run it in a |
| 9 | +# Docker container. |
| 10 | +# |
| 11 | +# Usage: |
| 12 | +# $ [options] ./create-lines-of-code-report.sh |
| 13 | +# |
| 14 | +# Options: |
| 15 | +# BUILD_DATETIME=%Y-%m-%dT%H:%M:%S%z # Build datetime, default is `date -u +'%Y-%m-%dT%H:%M:%S%z'` |
| 16 | +# FORCE_USE_DOCKER=true # If set to true the command is run in a Docker container, default is 'false' |
| 17 | +# VERBOSE=true # Show all the executed commands, default is `false` |
| 18 | + |
| 19 | +# ============================================================================== |
| 20 | + |
| 21 | +function main() { |
| 22 | + |
| 23 | + cd "$(git rev-parse --show-toplevel)" |
| 24 | + |
| 25 | + create-report |
| 26 | + enrich-report |
| 27 | +} |
| 28 | + |
| 29 | +function create-report() { |
| 30 | + |
| 31 | + if command -v gocloc > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then |
| 32 | + run-gocloc-natively |
| 33 | + else |
| 34 | + run-gocloc-in-docker |
| 35 | + fi |
| 36 | + # shellcheck disable=SC2002 |
| 37 | + cat lines-of-code-report.tmp.json \ |
| 38 | + | jq -r '["Language","files","blank","comment","code"],["--------"],(.languages[]|[.name,.files,.blank,.comment,.code]),["-----"],(.total|["TOTAL",.files,.blank,.comment,.code])|@tsv' \ |
| 39 | + | sed 's/Plain Text/Plaintext/g' \ |
| 40 | + | column -t |
| 41 | +} |
| 42 | + |
| 43 | +function run-gocloc-natively() { |
| 44 | + |
| 45 | + gocloc --output-type=json . > lines-of-code-report.tmp.json |
| 46 | +} |
| 47 | + |
| 48 | +function run-gocloc-in-docker() { |
| 49 | + |
| 50 | + # shellcheck disable=SC1091 |
| 51 | + source ./scripts/docker/docker.lib.sh |
| 52 | + |
| 53 | + # shellcheck disable=SC2155 |
| 54 | + local image=$(name=ghcr.io/make-ops-tools/gocloc docker-get-image-version-and-pull) |
| 55 | + docker run --rm --platform linux/amd64 \ |
| 56 | + --volume "$PWD":/workdir \ |
| 57 | + "$image" \ |
| 58 | + --output-type=json \ |
| 59 | + . \ |
| 60 | + > lines-of-code-report.tmp.json |
| 61 | +} |
| 62 | + |
| 63 | +function enrich-report() { |
| 64 | + |
| 65 | + build_datetime=${BUILD_DATETIME:-$(date -u +'%Y-%m-%dT%H:%M:%S%z')} |
| 66 | + git_url=$(git config --get remote.origin.url) |
| 67 | + git_branch=$(git rev-parse --abbrev-ref HEAD) |
| 68 | + git_commit_hash=$(git rev-parse HEAD) |
| 69 | + git_tags=$(echo \""$(git tag | tr '\n' ',' | sed 's/,$//' | sed 's/,/","/g')"\" | sed 's/""//g') |
| 70 | + pipeline_run_id=${GITHUB_RUN_ID:-0} |
| 71 | + pipeline_run_number=${GITHUB_RUN_NUMBER:-0} |
| 72 | + pipeline_run_attempt=${GITHUB_RUN_ATTEMPT:-0} |
| 73 | + |
| 74 | + # shellcheck disable=SC2086 |
| 75 | + jq \ |
| 76 | + '.creationInfo |= . + {"created":"'${build_datetime}'","repository":{"url":"'${git_url}'","branch":"'${git_branch}'","tags":['${git_tags}'],"commitHash":"'${git_commit_hash}'"},"pipeline":{"id":'${pipeline_run_id}',"number":'${pipeline_run_number}',"attempt":'${pipeline_run_attempt}'}}' \ |
| 77 | + lines-of-code-report.tmp.json \ |
| 78 | + > lines-of-code-report.json |
| 79 | + rm -f lines-of-code-report.tmp.json |
| 80 | +} |
| 81 | + |
| 82 | +# ============================================================================== |
| 83 | + |
| 84 | +function is-arg-true() { |
| 85 | + |
| 86 | + if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then |
| 87 | + return 0 |
| 88 | + else |
| 89 | + return 1 |
| 90 | + fi |
| 91 | +} |
| 92 | + |
| 93 | +# ============================================================================== |
| 94 | + |
| 95 | +is-arg-true "${VERBOSE:-false}" && set -x |
| 96 | + |
| 97 | +main "$@" |
| 98 | + |
| 99 | +exit 0 |
0 commit comments