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