From ebd9664313c0a1984fc9a06d0e247e4832212957 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Tue, 5 Sep 2023 15:49:19 +0200 Subject: *.sh -> src --- Makefile | 6 +++--- extract-elm.sh | 44 ----------------------------------------- extract-nix.sh | 44 ----------------------------------------- extract.sh | 58 ------------------------------------------------------ src/extract-elm.sh | 44 +++++++++++++++++++++++++++++++++++++++++ src/extract-nix.sh | 44 +++++++++++++++++++++++++++++++++++++++++ src/extract.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 149 insertions(+), 149 deletions(-) delete mode 100755 extract-elm.sh delete mode 100755 extract-nix.sh delete mode 100755 extract.sh create mode 100755 src/extract-elm.sh create mode 100755 src/extract-nix.sh create mode 100755 src/extract.sh diff --git a/Makefile b/Makefile index a38b3c4..ef3ecb6 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ test: test-elm test-nix test-elm: tests/elm.elm - ./extract-elm.sh tests/elm.elm | \ + src/extract-elm.sh tests/elm.elm | \ jq .text -r | \ cmp tests/expect -test-nix: tests/nix.nix - ./extract-nix.sh tests/nix.nix | \ +test-nix: tests/nix.nix + src/extract-nix.sh tests/nix.nix | \ jq .text -r | \ cmp tests/expect diff --git a/extract-elm.sh b/extract-elm.sh deleted file mode 100755 index 375d8a8..0000000 --- a/extract-elm.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash - -set -efu - -input_file=${1-/dev/stdin} - -tree-grepper \ - --query elm '([(line_comment) (block_comment)]+)' \ - --format json $input_file | - jq 'sort_by(.file)' | - jq '.[]' --indent 0 | - while read -r line; do - file=$(echo "$line" | jq -r .file) - file_type=$(echo "$line" | jq -r .file_type) - items=$(echo "$line" | - jq '.matches[] | { file: $file, file_type: $file_type, match: . }' --arg file "$file" --arg file_type "$file_type" | - jq '. | select(.match.text | test("TODO .+"))' - ) - if test -z "$items"; then - continue - fi - - echo "$items" | jq --slurp '.[]' --indent 0 | - while read -r item; do - start_row=$(echo "$item" | jq '.match.start.row') - end_row=$(echo "$item" | jq '.match.end.row') - - last_commit=$(git --no-pager blame -L "$start_row,$start_row" "$file" -p | - head -n 1 | - cut -d ' ' -f 1 - ) - - text=$(echo "$item" | - jq .match.text -r | - sed 's/^-- *TODO *//' | - sed 's/^{-|\? *TODO *//' | - sed 's/ *-}$//') - - echo "$item" | jq '. + {"text": $text, "last_commit": $last_commit}' \ - --arg text "$text" \ - --arg last_commit "$last_commit" \ - -c - done - done diff --git a/extract-nix.sh b/extract-nix.sh deleted file mode 100755 index e092db2..0000000 --- a/extract-nix.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash - -set -efu - -input_file=${1-/dev/stdin} - -tree-grepper \ - --query nix '((comment)+)' \ - --format json $input_file | - jq 'sort_by(.file)' | - jq '.[]' --indent 0 | - while read -r line; do - file=$(echo "$line" | jq -r .file) - file_type=$(echo "$line" | jq -r .file_type) - items=$(echo "$line" | - jq '.matches[] | { file: $file, file_type: $file_type, match: . }' --arg file "$file" --arg file_type "$file_type" | - jq '. | select(.match.text | test("TODO .+"))' - ) - if test -z "$items"; then - continue - fi - - echo "$items" | jq --slurp '.[]' --indent 0 | - while read -r item; do - start_row=$(echo "$item" | jq '.match.start.row') - end_row=$(echo "$item" | jq '.match.end.row') - - last_commit=$(git --no-pager blame -L "$start_row,$start_row" "$file" -p | - head -n 1 | - cut -d ' ' -f 1 - ) - - text=$(echo "$item" | - jq .match.text -r | - sed 's/^# *TODO *//' | - sed 's/^\/\* *TODO *//' | - sed 's/ *\*\/$//') - - echo "$item" | jq '. + {"text": $text, "last_commit": $last_commit}' \ - --arg text "$text" \ - --arg last_commit "$last_commit" \ - -c - done - done diff --git a/extract.sh b/extract.sh deleted file mode 100755 index ba611d3..0000000 --- a/extract.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env bash - -set -efu - -input_dir=$1 - -find "$input_dir" -type f | - while read -r input_file; do - ext= - case $input_file in - *.elm) ext="elm" ;; - *.nix) ext="nix" ;; - *) ;; - esac - - if test -z $ext; then - echo "warning, file $input_file ignored" >&2 - continue - fi - - ./extract-$ext.sh "$input_file" - done | - while read -r item; do - start_row=$(echo "$item" | jq '.match.start.row') - end_row=$(echo "$item" | jq '.match.end.row') - - last_commit=$(echo "$item" | jq '.last_commit' -r) - text=$(echo "$item" | jq '.text' -r) - file=$(echo "$item" | jq '.file' -r) - - first_commit=$(git --no-pager log --reverse -S"$text" --format=%H | \ - head -n 1) - - created_at=$(git show $first_commit --no-patch --format=%ad) - heading=$(echo "$text" | sed '/^$/Q' | sed 's/.*TODO //') - body=$(echo "$text" | tail -n +$(($(echo "$heading" | wc -l) + 2)) | \ - awk -F '[^ ]' ' - NR == 1 {n = length($1)} - {sub("^ {1,"n"}", ""); print}' - ) - - GREEN='\033[0;32m' - BOLD=$(tput bold) - NORMAL=$(tput sgr0) - NC='\033[0m' - echo -e "$GREEN--- $created_at --- $file$NC" - echo - echo "$BOLD$heading$NORMAL" | fold -s - if test -n "$body"; then - echo - echo "$body" | fold -s - echo - fi - echo - cat "$file" | nl -w 4 -s "| " -p -d '' -b a| tail -n +$(($start_row - 2)) | head -n $(($end_row - $start_row + 7)) - echo - echo - done diff --git a/src/extract-elm.sh b/src/extract-elm.sh new file mode 100755 index 0000000..375d8a8 --- /dev/null +++ b/src/extract-elm.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +set -efu + +input_file=${1-/dev/stdin} + +tree-grepper \ + --query elm '([(line_comment) (block_comment)]+)' \ + --format json $input_file | + jq 'sort_by(.file)' | + jq '.[]' --indent 0 | + while read -r line; do + file=$(echo "$line" | jq -r .file) + file_type=$(echo "$line" | jq -r .file_type) + items=$(echo "$line" | + jq '.matches[] | { file: $file, file_type: $file_type, match: . }' --arg file "$file" --arg file_type "$file_type" | + jq '. | select(.match.text | test("TODO .+"))' + ) + if test -z "$items"; then + continue + fi + + echo "$items" | jq --slurp '.[]' --indent 0 | + while read -r item; do + start_row=$(echo "$item" | jq '.match.start.row') + end_row=$(echo "$item" | jq '.match.end.row') + + last_commit=$(git --no-pager blame -L "$start_row,$start_row" "$file" -p | + head -n 1 | + cut -d ' ' -f 1 + ) + + text=$(echo "$item" | + jq .match.text -r | + sed 's/^-- *TODO *//' | + sed 's/^{-|\? *TODO *//' | + sed 's/ *-}$//') + + echo "$item" | jq '. + {"text": $text, "last_commit": $last_commit}' \ + --arg text "$text" \ + --arg last_commit "$last_commit" \ + -c + done + done diff --git a/src/extract-nix.sh b/src/extract-nix.sh new file mode 100755 index 0000000..e092db2 --- /dev/null +++ b/src/extract-nix.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +set -efu + +input_file=${1-/dev/stdin} + +tree-grepper \ + --query nix '((comment)+)' \ + --format json $input_file | + jq 'sort_by(.file)' | + jq '.[]' --indent 0 | + while read -r line; do + file=$(echo "$line" | jq -r .file) + file_type=$(echo "$line" | jq -r .file_type) + items=$(echo "$line" | + jq '.matches[] | { file: $file, file_type: $file_type, match: . }' --arg file "$file" --arg file_type "$file_type" | + jq '. | select(.match.text | test("TODO .+"))' + ) + if test -z "$items"; then + continue + fi + + echo "$items" | jq --slurp '.[]' --indent 0 | + while read -r item; do + start_row=$(echo "$item" | jq '.match.start.row') + end_row=$(echo "$item" | jq '.match.end.row') + + last_commit=$(git --no-pager blame -L "$start_row,$start_row" "$file" -p | + head -n 1 | + cut -d ' ' -f 1 + ) + + text=$(echo "$item" | + jq .match.text -r | + sed 's/^# *TODO *//' | + sed 's/^\/\* *TODO *//' | + sed 's/ *\*\/$//') + + echo "$item" | jq '. + {"text": $text, "last_commit": $last_commit}' \ + --arg text "$text" \ + --arg last_commit "$last_commit" \ + -c + done + done diff --git a/src/extract.sh b/src/extract.sh new file mode 100755 index 0000000..cc7dfc6 --- /dev/null +++ b/src/extract.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +set -efu + +input_dir=$1 + +find "$input_dir" -type f | + while read -r input_file; do + ext= + case $input_file in + *.elm) ext="elm" ;; + *.nix) ext="nix" ;; + *) ;; + esac + + if test -z $ext; then + echo "warning, file $input_file ignored" >&2 + continue + fi + + "$(dirname "$0")"/extract-$ext.sh "$input_file" + done | + while read -r item; do + start_row=$(echo "$item" | jq '.match.start.row') + end_row=$(echo "$item" | jq '.match.end.row') + + last_commit=$(echo "$item" | jq '.last_commit' -r) + text=$(echo "$item" | jq '.text' -r) + file=$(echo "$item" | jq '.file' -r) + + first_commit=$(git --no-pager log --reverse -S"$text" --format=%H | \ + head -n 1) + + created_at=$(git show $first_commit --no-patch --format=%ad) + heading=$(echo "$text" | sed '/^$/Q' | sed 's/.*TODO //') + body=$(echo "$text" | tail -n +$(($(echo "$heading" | wc -l) + 2)) | \ + awk -F '[^ ]' ' + NR == 1 {n = length($1)} + {sub("^ {1,"n"}", ""); print}' + ) + + GREEN='\033[0;32m' + BOLD=$(tput bold) + NORMAL=$(tput sgr0) + NC='\033[0m' + echo -e "$GREEN--- $created_at --- $file$NC" + echo + echo "$BOLD$heading$NORMAL" | fold -s + if test -n "$body"; then + echo + echo "$body" | fold -s + echo + fi + echo + cat "$file" | nl -w 4 -s "| " -p -d '' -b a| tail -n +$(($start_row - 2)) | head -n $(($end_row - $start_row + 7)) + echo + echo + done -- cgit v1.2.3