modify wrong changelog date
[platform/framework/web/web-ui-fw.git] / tools / inline-protos.sh
1 #!/bin/bash
2
3 WIDGET_FNAME=$1
4 WIDGET_BASE_DIR="`dirname ${WIDGET_FNAME}`"
5 VERBOSE=$(test "x$2x" != "xx" && echo "1" || echo "0")
6
7 rm_tmpfile() # No args
8 {
9     if test $VERBOSE -eq 1; then
10         echo "Removing ${TMP_FNAME}" > /dev/stderr
11     fi
12     rm -f "${TMP_FNAME}"
13 }
14
15 trap rm_tmpfile TERM INT
16
17 process_fname() # $1 = file name, n_pass
18 {
19   FNAME="$1"
20   N_PASS="$2"
21   cat ${FNAME} | \
22     sed -rn '1h;1!H;${;g;s!((\$|jQuery)([\n \t]|/\*.*\*/)*\.([\n \t]|/\*.*\*/)*widget([\n \t]|/\*.*\*/)*\(([\n \t]|/\*.*\*/)*")([^"]*)("[^{]*\{)!\1'$'\1''\7'$'\1''\8!g;p;}' | \
23     sed -rn '1h;1!H;${;g;s!(/\*|\*/|//|[{}])!'$'\1''\1'$'\1''!g;p;}' | \
24     gawk \
25       -v 'nPass='"${N_PASS}" \
26       -v 'inComment=0' \
27       -v 'inWidgetBody=0' \
28       -v 'inHtmlProto=0' \
29       -v 'widgetName=' \
30       -v 'braceCount=0' \
31       -v 'sourceToken=0' \
32       -v 'sourceValue=' \
33       -v 'seenSource=0' \
34       -v 'needAnotherPass=0' \
35       -v 'needToAddComma=0' \
36       '
37       function dumpProto(protoName) {
38         protoFile = "'"${WIDGET_BASE_DIR}"'/proto-html/" protoName ".prototype.html";
39         if (system("test -r " protoFile)) {
40           print "\033[7minline-protos.sh: Warning: " protoFile " not found\033[0m" > "/dev/stderr";
41           printf("\"%s\"", protoName);
42         }
43         else {
44           insideTag = 0;
45           printf("\n [ \"<div>");
46           while (1 == (getline inputLine < protoFile)) {
47             for (Nix1 = 1 ; Nix1 <= length(inputLine) ; Nix1++) {
48               theChar = substr(inputLine, Nix1, 1);
49               if ("<" == theChar)
50                 insideTag = 1;
51               else
52               if (">" == theChar)
53                 insideTag = 0;
54               if (insideTag && "\"" == theChar)
55                 theChar = "'"'"'";
56               printf("%s", theChar);
57             }
58             printf("\" ,\n  \"");
59           }
60           printf("</div>\" ].join(\"\")");
61           close(protoFile);
62         }
63       }
64
65       function establishInliningVariables(widgetName, token) {
66         if (1 == inWidgetBody) {
67           if (1 == braceCount) {
68             if (match(token, /[ \t]([^: \t]*):/, arToken))
69               inHtmlProto = ((arToken[1] == "_htmlProto") ? 1 : 0);
70             printf("%s", token);
71           }
72           else {
73             if (inHtmlProto == 1) {
74               if (nPass == 1 && seenSource == 0) {
75                 printf("\nsource:\n");
76                 dumpProto(widgetName);
77                 seenSource = 1;
78                 needToAddComma = 1;
79               }
80               if (braceCount == 2) {
81                 sourceTokenHasMatched = 0;
82                 if (match(token, /[ \t]([a-zA-Z0-9_]*)[ \t]*:/, arToken)) {
83                   sourceToken = ((arToken[1] == "source") ? 1 : 0);
84                   sourceTokenHasMatched = 1;
85                   if (needToAddComma == 1) {
86                     printf(",");
87                     needToAddComma = 0;
88                   }
89                 }
90
91                 if (sourceToken == 0) {
92                   if (sourceValue != "") {
93                     if (match(sourceValue, /^[\n\t ]*("([^"]*)")/, sourceWidgetAr)) {
94                       sourceWidgetName = sourceWidgetAr[2];
95                       printf("%s", substr(sourceValue, 1, sourceWidgetAr[1, "start"] - 1));
96                       dumpProto(sourceWidgetName);
97                       printf("%s", substr(sourceValue, sourceWidgetAr[1, "start"] + sourceWidgetAr[1, "length"]));
98                     }
99                     else
100                       printf("%s", sourceValue);
101                     sourceValue = "";
102                   }
103                   printf("%s", token);
104                 }
105                 else {
106                   if (sourceTokenHasMatched) {
107                     nArSource = split(token, arSource, ":");
108                     printf("%s:", arSource[1]);
109
110                     for (idxArSource = 2 ; idxArSource <= nArSource ; idxArSource++) {
111                       seenSource = 1;
112                       sourceValue = sourceValue arSource[idxArSource];
113                       if (idxArSource < nArSource)
114                         sourceValue = sourceValue ":";
115                     }
116                   }
117                   else {
118                     seenSource = 1;
119                     sourceValue = sourceValue token;
120                   }
121                 }
122               }
123               else
124                 printf("%s", token);
125             }
126             else
127               printf("%s", token);
128           }
129         }
130         else
131           printf("%s", token);
132       }
133       {
134         n = split($0, ar, /\001/);
135         if (n > 1) {
136           for (Nix = 1 ; Nix <= n ; Nix++) {
137             if (ar[Nix] == "/*") {
138               inComment = 1;
139               printf("%s", ar[Nix]);
140             }
141             else
142             if (ar[Nix] == "*/") {
143               inComment = 0;
144               printf("%s", ar[Nix]);
145             }
146             else
147             if (ar[Nix] == "//") {
148               for (; Nix <= n ; Nix++)
149                 printf("%s", ar[Nix]);
150             }
151             else
152             if (1 == inComment)
153               printf("%s", ar[Nix]);
154             else
155             if (Nix % 2) {
156               establishInliningVariables(widgetName, ar[Nix]);
157             }
158             else {
159               /* ar[Nix] is an interesting token not inside comments */
160               if ("{" == ar[Nix] || "}" == ar[Nix]) {
161                 if (widgetName != "" && inWidgetBody == 0) {
162                   if ("{" == ar[Nix])
163                     inWidgetBody = 1;
164                   else
165                     exit(1);
166                 }
167                 if (inWidgetBody == 1) {
168                   if ("{" == ar[Nix])
169                     braceCount++;
170                   else
171                   if ("}" == ar[Nix]) {
172                     if (inHtmlProto == 1 && braceCount == 2) {
173                       if (seenSource == 0)
174                         needAnotherPass = 1;
175                       seenSource = 0;
176                     }
177                     braceCount--;
178                   }
179                 }
180               }
181               else {
182                 split(ar[Nix], arWidget, ".");
183                 widgetName = arWidget[2];
184               }
185               if (widgetName == "")
186                 printf("%s", ar[Nix]);
187               else
188                 printf("%s", ar[Nix]);
189             }
190           }
191           print("");
192         }
193         else
194           if (inComment)
195             print;
196           else
197             establishInliningVariables(widgetName, $0 "\n");
198       }
199       END { exit(needAnotherPass); }
200     '
201 }
202
203 if test "x${WIDGET_BASE_DIR}x" = "xx"; then
204   echo "Usage: $(basename $0) <widget_file_path>"
205   exit 1
206 fi
207
208 FNAME=${WIDGET_FNAME}
209 TMP_FNAME=`mktemp`
210 N_PASS=0
211 while ! process_fname $FNAME $N_PASS > $TMP_FNAME; do 
212         if test $VERBOSE -eq 1; then
213                 echo "Going for another pass with ${TMP_FNAME}" > /dev/stderr
214         fi
215         N_PASS=`expr "$N_PASS" + 1`
216 done
217 cat $TMP_FNAME
218 rm_tmpfile
219
220