# HG changeset patch # User František Kučera # Date 1578788482 -3600 # Node ID 3dbe113637efdada9ccabc5eb06108f245a566d2 # Parent 8b97ab3cd9cc68b3a6982f872da2789386878552 streamlet: cloc – count lines of code diff -r 8b97ab3cd9cc -r 3dbe113637ef streamlet-examples/__relpipe_in_filesystem_script_cloc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/streamlet-examples/__relpipe_in_filesystem_script_cloc Sun Jan 12 01:21:22 2020 +0100 @@ -0,0 +1,78 @@ +#!/bin/bash + +# Relational pipes +# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +# This streamlet counts lines of code of given files. It calls the tool cloc. +# +# With no options, these attributes are provided: language, code, comment, blank +# Specific attributes can be selected using options – e.g. --option 'attribute' 'code' +# or --option "attribute" "total" (sum of code, comment and blank lines, hidden by default). +# +# Optional prefix can be added to attribute names: --option 'prefix' 'my_prefix_' + + +. "$(dirname $0)/streamlet-common.sh" + +processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() { + clocFields=() + + for (( i=0; i<${#optionNames[@]}; i++)); do + if [[ "x${optionNames[$i]}" == "xattribute" ]]; then + if [[ "${optionValues[$i]}" =~ ^(language|blank|comment|code)$ ]]; then + clocFields+=("${optionValues[$i]}"); + else + echo "Unsupported attribute: ${optionValues[$i]}" >&2 + fi + elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then + clocPrefix="${optionValues[$i]}"; + else + echo "Unsupported option: ${optionNames[$i]}" >&2 + fi + done + + if [[ -z "$clocFields" ]]; then + clocFields=( "language" "code" "comment" "blank" ); # + "total" + fi + + for (( i=0; i<${#clocFields[@]}; i++)); do + if [[ "x${clocFields[$i]}" == "xlanguage" ]]; then local type="string"; else local type="integer"; fi + send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$clocPrefix${clocFields[$i]}}" "$type" + done + + send WAITING_FOR_INPUT_ATTRIBUTES +} + +processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() { + local language files blank comment code total; + + [[ -d "$currentFile" ]] || read_nullbyte language files blank comment code total < <( cloc "$currentFile" | perl -ne 'if (/(.*?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) { print "$1\0$2\0$3\0$4\0$5\0"; print $3 + $4 + $5; print "\0"; }' ); + + for (( i=0; i<${#clocFields[@]}; i++)); do + value="${!clocFields[$i]}"; + + if [[ "x$files" == "x1" ]]; then isNull="false"; + elif [[ "x${clocFields[$i]}" == "xlanguage" ]]; then value=""; isNull="true"; + else value="0"; isNull="true"; fi + + send OUTPUT_ATTRIBUTE "$value" "$isNull"; + done + + send WAITING_FOR_INPUT_ATTRIBUTES; +} + +initialize +processMessages diff -r 8b97ab3cd9cc -r 3dbe113637ef streamlet-examples/streamlet-common.sh