streamlet-examples/__relpipe_in_filesystem_script_cloc
branchv_0
changeset 37 3dbe113637ef
parent 34 0b9e4af08cc8
equal deleted inserted replaced
36:8b97ab3cd9cc 37:3dbe113637ef
       
     1 #!/bin/bash
       
     2 
       
     3 # Relational pipes
       
     4 # Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     5 #
       
     6 # This program is free software: you can redistribute it and/or modify
       
     7 # it under the terms of the GNU General Public License as published by
       
     8 # the Free Software Foundation, version 3 of the License.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13 # GNU General Public License for more details.
       
    14 #
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17 
       
    18 
       
    19 # This streamlet counts lines of code of given files. It calls the tool cloc.
       
    20 #
       
    21 # With no options, these attributes are provided: language, code, comment, blank
       
    22 # Specific attributes can be selected using options – e.g. --option 'attribute' 'code'
       
    23 # or --option "attribute" "total" (sum of code, comment and blank lines, hidden by default).
       
    24 #
       
    25 # Optional prefix can be added to attribute names: --option 'prefix' 'my_prefix_'
       
    26 
       
    27 
       
    28 . "$(dirname $0)/streamlet-common.sh"
       
    29 
       
    30 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
       
    31 	clocFields=()
       
    32 
       
    33 	for (( i=0; i<${#optionNames[@]}; i++)); do
       
    34 		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
       
    35 			if [[ "${optionValues[$i]}" =~ ^(language|blank|comment|code)$ ]]; then
       
    36 				clocFields+=("${optionValues[$i]}");
       
    37 			else
       
    38 				echo "Unsupported attribute: ${optionValues[$i]}" >&2
       
    39 			fi
       
    40 		elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then
       
    41 			clocPrefix="${optionValues[$i]}";
       
    42 		else
       
    43 			echo "Unsupported option: ${optionNames[$i]}" >&2
       
    44 		fi
       
    45 	done
       
    46 
       
    47 	if [[ -z "$clocFields" ]]; then
       
    48 		clocFields=( "language" "code" "comment" "blank" ); # + "total"
       
    49 	fi
       
    50 
       
    51 	for (( i=0; i<${#clocFields[@]}; i++)); do
       
    52 		if [[ "x${clocFields[$i]}" == "xlanguage" ]]; then local type="string"; else local type="integer"; fi
       
    53 		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$clocPrefix${clocFields[$i]}}"    "$type"
       
    54 	done
       
    55 
       
    56 	send WAITING_FOR_INPUT_ATTRIBUTES
       
    57 }
       
    58 
       
    59 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
       
    60 	local language files blank comment code total;
       
    61 	
       
    62 	[[ -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"; }' );
       
    63 
       
    64 	for (( i=0; i<${#clocFields[@]}; i++)); do
       
    65 		value="${!clocFields[$i]}";
       
    66 
       
    67 		if [[ "x$files" == "x1" ]]; then                                isNull="false";
       
    68 		elif [[ "x${clocFields[$i]}" == "xlanguage" ]]; then value="";  isNull="true";
       
    69 		else                                                 value="0"; isNull="true"; fi
       
    70 
       
    71 		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
       
    72 	done
       
    73 	
       
    74 	send WAITING_FOR_INPUT_ATTRIBUTES;
       
    75 }
       
    76 
       
    77 initialize
       
    78 processMessages