[2] | 1 | #!/bin/sh |
---|
| 2 | |
---|
| 3 | SED_SCRIPT=".sed_script"; |
---|
| 4 | SED_FILE=".sed_file"; |
---|
| 5 | SOURCE_FILE="New_Component"; |
---|
| 6 | SOURCE_DIR="./$SOURCE_FILE"; |
---|
| 7 | |
---|
| 8 | #-----[ usage ]------------------------------------------------------ |
---|
| 9 | # input : - |
---|
| 10 | # output : - |
---|
| 11 | |
---|
| 12 | function usage () |
---|
| 13 | { |
---|
| 14 | echo ""; |
---|
| 15 | echo "usage : $0 <directory> <name>"; |
---|
| 16 | echo " - <directory> : localisation of component"; |
---|
| 17 | echo " - <name> : name of component"; |
---|
| 18 | echo ""; |
---|
| 19 | echo " Note : This script must be execute in directory's script"; |
---|
| 20 | echo ""; |
---|
| 21 | |
---|
| 22 | exit; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | #-----[ test_usage ]------------------------------------------------- |
---|
| 26 | # input : all parameters |
---|
| 27 | # output : - |
---|
| 28 | |
---|
| 29 | function test_usage () |
---|
| 30 | { |
---|
| 31 | if test ! -d $SOURCE_DIR; then |
---|
| 32 | echo "Source directory \"$SOURCE_DIR\" invalid"; |
---|
| 33 | usage; |
---|
| 34 | fi; |
---|
| 35 | |
---|
| 36 | if test -f $SED_SCRIPT; then |
---|
| 37 | echo "File \"$SED_SCRIPT\" exist"; |
---|
| 38 | usage; |
---|
| 39 | fi; |
---|
| 40 | if test -f $SED_FILE; then |
---|
| 41 | echo "File \"$SED_FILE\" exist"; |
---|
| 42 | usage; |
---|
| 43 | fi; |
---|
| 44 | |
---|
| 45 | if test $# -ne 2; then |
---|
| 46 | usage; |
---|
| 47 | fi; |
---|
| 48 | |
---|
| 49 | # if test -d $1/$2; then |
---|
| 50 | # echo "Component in Directory \"$1/$2\" already exist"; |
---|
| 51 | # usage; |
---|
| 52 | # fi; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | #-----[ rename_file ]------------------------------------------------ |
---|
| 56 | # input : file_old name_old name_new |
---|
| 57 | # output : - |
---|
| 58 | |
---|
| 59 | function rename_file () |
---|
| 60 | { |
---|
| 61 | FILE_OLD=$1; |
---|
| 62 | NAME_OLD=$2; |
---|
| 63 | NAME_NEW=$3; |
---|
| 64 | |
---|
| 65 | FILE_NEW="`dirname $FILE_OLD`/`basename $FILE_OLD |sed s/"$NAME_OLD"/"$NAME_NEW"/`"; |
---|
| 66 | |
---|
| 67 | if test $FILE_NEW != $FILE_OLD; then |
---|
| 68 | mv $FILE_OLD $FILE_NEW; |
---|
| 69 | fi; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | #-----[ rename_directory ]------------------------------------------- |
---|
| 73 | # input : dir name_old name_new |
---|
| 74 | # output : - |
---|
| 75 | |
---|
| 76 | function rename_directory () |
---|
| 77 | { |
---|
| 78 | NAME_OLD=$2; |
---|
| 79 | NAME_NEW=$3; |
---|
| 80 | |
---|
| 81 | # recursion |
---|
| 82 | for ENTRY in $1/*; do |
---|
| 83 | |
---|
| 84 | if test -d $ENTRY; then |
---|
| 85 | rename_directory $ENTRY $2 $3; |
---|
| 86 | else |
---|
| 87 | rename_file $ENTRY $2 $3; |
---|
| 88 | fi; |
---|
| 89 | done; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | #-----[ directory_to ]----------------------------------------------- |
---|
| 93 | # input : directory cmd |
---|
| 94 | # output : - |
---|
| 95 | |
---|
| 96 | function directory_to () |
---|
| 97 | { |
---|
| 98 | res=""; |
---|
| 99 | |
---|
| 100 | for i in `echo "$1" |tr \/ ' '`; do |
---|
| 101 | j=`echo $i |tr [:upper:] [:lower:]`; |
---|
| 102 | case $2 in |
---|
| 103 | "dir_morpheo") |
---|
| 104 | if test -z "$res"; then |
---|
| 105 | res="..\/.."; |
---|
| 106 | else |
---|
| 107 | res="..\/$res"; |
---|
| 108 | fi; |
---|
| 109 | ;; |
---|
| 110 | "component_lower") |
---|
| 111 | res="$j"; |
---|
| 112 | ;; |
---|
| 113 | "component") |
---|
| 114 | res="$i"; |
---|
| 115 | ;; |
---|
| 116 | "directory") |
---|
| 117 | if test -z "$res"; then |
---|
| 118 | res=$i; |
---|
| 119 | else |
---|
| 120 | res="$res\/$i"; |
---|
| 121 | fi; |
---|
| 122 | ;; |
---|
| 123 | "define") |
---|
| 124 | if test -z "$res"; then |
---|
| 125 | res=$j; |
---|
| 126 | else |
---|
| 127 | res="${res}_${j}"; |
---|
| 128 | fi; |
---|
| 129 | ;; |
---|
| 130 | "namespace_begin") |
---|
| 131 | prefixe="namespace "; |
---|
| 132 | postfixe=" {\n"; |
---|
| 133 | if test -z "$res"; then |
---|
| 134 | res="$prefixe$j$postfixe"; |
---|
| 135 | else |
---|
| 136 | res="$res$prefixe$j$postfixe"; |
---|
| 137 | fi; |
---|
| 138 | ;; |
---|
| 139 | "namespace_end") |
---|
| 140 | prefixe="}; \/\/ end namespace "; |
---|
| 141 | postfixe="\n"; |
---|
| 142 | if test -z "$res"; then |
---|
| 143 | res="$prefixe$j$postfixe"; |
---|
| 144 | else |
---|
| 145 | res="$prefixe$j$postfixe$res"; |
---|
| 146 | fi; |
---|
| 147 | ;; |
---|
| 148 | "namespace_use") |
---|
| 149 | if test -z "$res"; then |
---|
| 150 | res="$j"; |
---|
| 151 | else |
---|
| 152 | res="$res::$j"; |
---|
| 153 | fi; |
---|
| 154 | ;; |
---|
| 155 | "namespace_using") |
---|
| 156 | prefixe="using namespace morpheo::behavioural::"; |
---|
| 157 | postfixe=";\n"; |
---|
| 158 | if test -z "$res"; then |
---|
| 159 | res="$prefixe$j$postfixe"; |
---|
| 160 | namespace="$j"; |
---|
| 161 | else |
---|
| 162 | res="$res$prefixe$namespace::$j$postfixe"; |
---|
| 163 | namespace="$namespace::$j"; |
---|
| 164 | fi; |
---|
| 165 | ;; |
---|
| 166 | *) |
---|
| 167 | esac; |
---|
| 168 | done; |
---|
| 169 | |
---|
| 170 | echo $res; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | #-----[ translation_script ]----------------------------------------- |
---|
| 174 | # input : dir component |
---|
| 175 | # output : - |
---|
| 176 | |
---|
| 177 | function translation_script () |
---|
| 178 | { |
---|
| 179 | # All example is the component Generic/RegisterFile |
---|
| 180 | |
---|
| 181 | #@DIR_MORPHEO ../../ |
---|
| 182 | SED=`directory_to $1/$2 "dir_morpheo"`; |
---|
| 183 | echo "s/@DIR_MORPHEO/$SED/g" >> $SED_SCRIPT; |
---|
| 184 | |
---|
| 185 | #@COMPONENT_LOWER registerfile |
---|
| 186 | SED=`directory_to $2 "component_lower"`; |
---|
| 187 | echo "s/@COMPONENT_LOWER/$SED/g" >> $SED_SCRIPT; |
---|
| 188 | |
---|
| 189 | #@COMPONENT RegisterFile |
---|
| 190 | SED=`directory_to $2 "component"`; |
---|
| 191 | echo "s/@COMPONENT/$SED/g" >> $SED_SCRIPT; |
---|
| 192 | |
---|
| 193 | #@DIRECTORY Generic/RegisterFile |
---|
| 194 | SED=`directory_to $1/$2 "directory"`; |
---|
| 195 | echo "s/@DIRECTORY/$SED/g" >> $SED_SCRIPT; |
---|
| 196 | |
---|
| 197 | #@DEFINE generic_registerfile |
---|
| 198 | SED=`directory_to $1/$2 "define"`; |
---|
| 199 | echo "s/@DEFINE/$SED/g" >> $SED_SCRIPT; |
---|
| 200 | |
---|
| 201 | #@NAMESPACE_BEGIN namespace generic { |
---|
| 202 | # namespace registerfile { |
---|
| 203 | SED=`directory_to $1/$2 "namespace_begin"`; |
---|
| 204 | echo "s/@NAMESPACE_BEGIN/$SED/g" >> $SED_SCRIPT; |
---|
| 205 | |
---|
| 206 | #@NAMESPACE_END }; // end namespace registerfile |
---|
| 207 | # }; // end namespace generic |
---|
| 208 | SED=`directory_to $1/$2 "namespace_end"`; |
---|
| 209 | echo "s/@NAMESPACE_END/$SED/g" >> $SED_SCRIPT; |
---|
| 210 | |
---|
| 211 | #@NAMESPACE_USE generic::registerfile |
---|
| 212 | SED=`directory_to $1/$2 "namespace_use"`; |
---|
| 213 | echo "s/@NAMESPACE_USE/$SED/g" >> $SED_SCRIPT; |
---|
| 214 | |
---|
| 215 | #@NAMESPACE_USING using namespace morpheo::behavioural::generic; |
---|
| 216 | SED=`directory_to $1 "namespace_using"`; |
---|
| 217 | echo "s/@NAMESPACE_USING/$SED/g" >> $SED_SCRIPT; |
---|
| 218 | |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | #-----[ translation_file ]------------------------------------------- |
---|
| 224 | # input : file |
---|
| 225 | # output : - |
---|
| 226 | |
---|
| 227 | function translation_file () |
---|
| 228 | { |
---|
| 229 | echo "Translation : $1"; |
---|
| 230 | |
---|
| 231 | cat $1 |sed -f $SED_SCRIPT $1 > $SED_FILE; |
---|
| 232 | mv $SED_FILE $1; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | #-----[ translation_directory_rec ]---------------------------------- |
---|
| 236 | # input : dir |
---|
| 237 | # output : - |
---|
| 238 | |
---|
| 239 | function translation_directory_rec |
---|
| 240 | { |
---|
| 241 | # recursion |
---|
| 242 | for ENTRY in $1/*; do |
---|
| 243 | |
---|
| 244 | if test -d $ENTRY; then |
---|
| 245 | translation_directory_rec $ENTRY; |
---|
| 246 | else |
---|
| 247 | translation_file $ENTRY |
---|
| 248 | fi; |
---|
| 249 | done; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | #-----[ translation_directory ]-------------------------------------- |
---|
| 253 | # input : dir component |
---|
| 254 | # output : - |
---|
| 255 | |
---|
| 256 | function translation_directory () |
---|
| 257 | { |
---|
| 258 | touch $SED_SCRIPT; |
---|
| 259 | |
---|
| 260 | translation_script $1 $2; |
---|
| 261 | translation_directory_rec $1/$2; |
---|
| 262 | |
---|
| 263 | #cat $SED_SCRIPT; |
---|
| 264 | rm $SED_SCRIPT; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | #-----[ main ]------------------------------------------------------- |
---|
| 269 | # input : all parameters |
---|
| 270 | # output : - |
---|
| 271 | |
---|
| 272 | function main () |
---|
| 273 | { |
---|
| 274 | test_usage $*; |
---|
| 275 | |
---|
| 276 | echo "New : $2"; |
---|
| 277 | |
---|
| 278 | # Create directory |
---|
| 279 | mkdir -p $1/$2; |
---|
| 280 | |
---|
| 281 | #Copy |
---|
| 282 | cp -r $SOURCE_DIR/* $1/$2/; |
---|
| 283 | |
---|
| 284 | #translation |
---|
| 285 | rename_directory "$1/$2" $SOURCE_FILE $2; |
---|
| 286 | translation_directory $1 $2; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | #-----[ body ]------------------------------------------------------- |
---|
| 290 | main $*; |
---|