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