Internal/OpenFlow/mvnParse: tree-fmt

File tree-fmt, 3.7 KB (added by akoshibe, 5 years ago)
Line 
1#!/usr/bin/env bash
2#
3# Different plaforms add things to diffenent places, even bash, so we use env.
4#
5# parse a maven project directory tree so it can be examined for source structure
6# and be pasted into a wiki with a bit of elbowgrease.
7#
8# wiki-indented hierarchy goes into hier_out.
9# descriptions and package names go into pdescr_out and pname_out.
10
11OUT_DIR=/tmp/
12
13usage() {
14cat << EOF
15usage: $0 -i [project] [-c] [options]
16   
17options:
18   -l <level>     : directory levels (defualt 3)
19   -o <file>      : output hierarchy to <file> (default ${OUT_DIR}hier_out)
20   -n <file>      : output project names to <file> (default ${OUT_DIR}pkname_out)
21   -d <file>      : output project descriptions to <file> (default ${OUT_DIR}pkdescr_out)
22   -c             : clear default files in ${OUT_DIR}
23EOF
24}
25
26# argument parsing
27while getopts ":ci:l:o:n:d:" opt; do
28  case $opt in
29    c)
30      cd $OUT_DIR
31      rm hier_out pkname_out pkdescr_out
32      cd -
33      exit
34      ;;
35    i)
36      treedir=$(echo $OPTARG | sed 's:[/]$::')
37      ;;
38    l)
39      level=$OPTARG
40      ;;
41    o)
42      hier_out=$OPTARG
43      ;;
44    n)
45      pname_out=$OPTARG
46      ;;
47    d)
48      pdescr_out=$OPTARG
49      ;;
50    \?)
51      usage
52      exit
53      ;;
54  esac
55done
56
57# sanity check
58if [ -z $treedir ]; then
59  echo "must specify a project directory"
60  echo ""
61  usage
62  exit
63fi
64
65# set defaults if unset
66: ${level:=3}
67: ${hier_out:=${OUT_DIR}hier_out}
68: ${pname_out:=${OUT_DIR}pkname_out}
69: ${pdescr_out:=${OUT_DIR}pkdescr_out}
70
71# collect up to $level of visible directory names
72find $treedir -maxdepth $((${level} - 1)) -type d -not -path '*/\.*' | grep -v src | grep -v target | sort -d > /tmp/h_tmp
73# collect all poms up to that level
74find $treedir -maxdepth ${level} -name pom.xml > /tmp/p_tmp
75# sort and reconstruct path in a greusomely hack way.
76sed 's:pom.xml: pom.xml:g' </tmp/p_tmp > /tmp/p2_tmp
77sort -k 1 /tmp/p2_tmp -o /tmp/p2_tmp
78sed 's: pom.xml:pom.xml:g' </tmp/p2_tmp > /tmp/p_tmp
79
80# spit slash-separated dirpaths into arrays, then replace all
81# but last with wiki notation list bullet
82# Ex: /a/b/c -> [a b c] -> ** c
83while read line; do
84  # split directory into array: ./a/b/c -> a b c
85  arr=($(echo $line | sed 's:[./]: :g'))
86  # replace all but last entry with asterisks
87  st=$(echo ${arr[@]} | sed 's:[a-zA-Z]*-*[a-zA-Z]* :*:g')
88  # tack another asterisk in front, plus a space : **c -> *** c
89  echo \*${st} | sed 's:[a-zA-Z]: &:' >> $hier_out
90  #siz=${#arr[@]}
91  #if [ $siz -le $level ]; then
92  #      echo ${arr[@]} | sed 's:[a-z]* :       :g' | grep -v "src" >> $out
93  #fi
94done < /tmp/h_tmp
95
96# scrape package name and descriptions from pom.xml files, and bullet-point that
97# the correct number of indentations in
98# Ex: ./a/b/pom.xml: a-package-name -> ** a-package-name
99#     "some package that does junk" -> ** some package that does junk
100while read line; do
101  #lines are /a/b/c/pom.xml
102  #fetch second match, if not, the first, and grab stuff between XML tags
103  if [ "$(grep -rn "parent" $line)" ]; then
104    pname=$(grep -hrnm 2 "artifactId" $line | tail -n 1 | sed 's:.*<artifactId>\(.*\)<\/artifactId>:\1:')
105  else
106    pname=$(grep -hrnm 1 "artifactId" $line | sed 's:.*<artifactId>\(.*\)<\/artifactId>:\1:')
107  fi
108  pdescr=$(grep -hrn "description" $line | tr '\n' ' ' | sed 's:.*<description>\(.*\)<\/description>.*:\1:')
109 
110  #make an array and make that number of stars
111  arr=($(echo $line | sed 's:\/: :g'))
112  str=$(echo ${arr[@]} | sed -e 's:[A-Za-z]*-*[A-Za-z]* :*:g' -e 's:\(\**\).*:\1:')
113  echo $line >> /tmp/pname_raw
114  echo "${str}" ${pname} >> $pname_out
115  echo "${str}" ${pdescr} >> $pdescr_out
116done < /tmp/p_tmp
117
118# tack path info to end
119echo "---debug--- " | tee -a $pname_out $pdescr_out
120cat /tmp/pname_raw | tee -a $pname_out $pdescr_out
121
122rm /tmp/pname_raw /tmp/h_tmp /tmp/p_tmp /tmp/p2_tmp