aboutsummaryrefslogtreecommitdiff
path: root/exampleData/ruleSets/buildCombinedRules.sh
blob: 9e74c2f8259a342a7e1c041dc4a7ed37e7b13eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
#
# This script builds a collection of FiveUI rulesets (contained in various .json files)
# into a combined rule sets file: combinedRules.json.
#
# Author: Benjamin Jones <bjones@galois.com>
# Copyright 2013 Galois, Inc.
#
# For the script to work, the input .json files must have rules separated by
# comment lines starting with //---. Also, the last rule in the file must be
# followed by //,. See colorRules.json for an example.
#
# Usage: buildCombinedRules <rule1.json> [<rule2.json> ...]
#

FILES=$*
OUTFILE="combinedRules.json"
TMPFILE=`mktemp -t $0`
HEADER="combinedHeader" # file containing header
FOOTER="combinedFooter" # file containing footer

function cutstart {
  echo `cat $1 | grep -n '//---' | awk -F ':' {'print $1'} | head -1`
}
function cutend {
  echo `cat $1 | grep -n '//---' | awk -F ':' {'print $1'} | tail -1`
}

### Script ###

cat $HEADER > $OUTFILE
for file in $FILES
do
  if [ $file == $OUTFILE ]
  then
    echo "skipping: $OUTFILE"
  else
    CUTSTART=`cutstart $file`
    CUTEND=`cutend $file`
    echo "combining: $file"
    echo "// --- included from file: $file --- //" >> $OUTFILE
    # cut rules out, change //, -> , and remove single-line comments
    cat $file | head -$(($CUTEND-1)) | tail +$CUTSTART \
        | sed 's/\/\/,/,/' \
        | sed 's/\/\/.*$//' \
        >> $OUTFILE
  fi
done
LEN=`wc -l $OUTFILE | awk '{print $1}'`
cat $OUTFILE | head -$((LEN-1)) > $TMPFILE
echo "}" >> $TMPFILE
mv $TMPFILE $OUTFILE
cat $FOOTER >> $OUTFILE