addModule.sh 2.78 KB
Newer Older
1
2
#!/bin/bash

Simon Spannagel's avatar
Simon Spannagel committed
3
4
5
6
7
echo -e "\nPreparing code basis for a new module:\n"

# Ask for module name:
read -p "Name of the module? " MODNAME

8
9
10
11
12
13
14
15
16
17
18
# Ask for module type:
echo -e "Type of the module?\n"
type=0
select yn in "global" "detector" "dut"; do
    case $yn in
        global ) type=1; break;;
        detector ) type=2; break;;
        dut ) type=3; break;;
    esac
done

Simon Spannagel's avatar
Simon Spannagel committed
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
echo "Creating directory and files..."

echo
# Try to find the modules directory:
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P )"
DIRECTORIES[0]="${BASEDIR}/../../src/modules"
DIRECTORIES[1]="../src/modules"
DIRECTORIES[2]="src/modules"

MODDIR=""
for DIR in "${DIRECTORIES[@]}"; do
    if [ -d "$DIR" ]; then
	MODDIR="$DIR"
	break
    fi
done

# Create directory
mkdir "$MODDIR/$MODNAME"

# Copy over CMake file and sources from Dummy:
sed -e "s/Dummy/$MODNAME/g" $MODDIR/Dummy/CMakeLists.txt > $MODDIR/$MODNAME/CMakeLists.txt

# Copy over the README, setting current git username/email as author
# If this fails, use system username and hostname
MYNAME=$(git config user.name)
MYMAIL=$(git config user.email)
if [ -z "$MYNAME" ]; then
    MYNAME=$(whoami)
fi
if [ -z "$MYMAIL" ]; then
    MYMAIL=$(hostname)
51
fi
Simon Spannagel's avatar
Simon Spannagel committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
sed -e "s/Dummy/$MODNAME/g" \
    -e "s/\*NAME\*/$MYNAME/g" \
    -e "s/\*EMAIL\*/$MYMAIL/g" \
    -e "s/Functional/Immature/g" \
    "$MODDIR/Dummy/README.md" > "$MODDIR/$MODNAME/README.md"

# Copy over source code skeleton:
sed -e "s/Dummy/$MODNAME/g" "$MODDIR/Dummy/Dummy.h" > "$MODDIR/$MODNAME/${MODNAME}.h"
sed -e "s/Dummy/$MODNAME/g" "$MODDIR/Dummy/Dummy.cpp" > "$MODDIR/$MODNAME/${MODNAME}.cpp"

# Options for sed vary slightly between mac and linux
opt=-i
platform=`uname`
if [ "$platform" == "Darwin" ]; then opt="-i \"\""; fi

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Change to detector or dut module type if necessary:
if [ "$type" != 1 ]; then

  # Prepare sed commands to change to per detector module
  # Change module type in CMakeLists
  if [ "$type" == 3 ]; then
    command="sed $opt 's/_GLOBAL_/_DUT_/g' $MODDIR/$MODNAME/CMakeLists.txt"
  else
    command="sed $opt 's/_GLOBAL_/_DETECTOR_/g' $MODDIR/$MODNAME/CMakeLists.txt"
  fi
  eval $command

  # Change header file
  command="sed ${opt} \
      -e 's/param detectors.*/param detector Pointer to the detector for this module instance/g' \
      -e 's/std::vector<Detector\*> detectors/Detector\* detector/g' \
      $MODDIR/$MODNAME/${MODNAME}.h"
  eval $command
  # Change implementation file
  command="sed ${opt} \
      -e 's/std::vector<Detector\*> detectors/Detector\* detector/g' \
      -e 's/move(detectors)/move\(detector\)/g' \
      $MODDIR/$MODNAME/${MODNAME}.cpp"
  eval $command
fi

Simon Spannagel's avatar
Simon Spannagel committed
93
94
95
96
97
98
99
# Print a summary of the module created:
FINALPATH=`realpath $MODDIR/$MODNAME`
echo "Name:   $MODNAME"
echo "Author: $MYNAME ($MYMAIL)"
echo "Path:   $FINALPATH"
echo
echo "Re-run CMake in order to build your new module."