Rev 20 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20 | reyssat | 1 | /* |
2 | DynAPI Compiler Beta 1 |
||
3 | Written 2002 by Raymond Irving |
||
4 | |||
5 | The DynAPI Distribution is distributed under the terms of the GNU LGPL license. |
||
6 | */ |
||
7 | |||
8 | var jsCLevel='high'; |
||
9 | var jsCExclude=''; |
||
10 | var jsCLastFile = ''; |
||
11 | var jsCLastFileName = ''; |
||
12 | var jsCDebug=false; //@IF:DEBUG directive |
||
13 | var jsCNS4=true; //@IF:NS4 directive |
||
14 | var jsFileIndex = -1; |
||
15 | |||
16 | var jsCLastUnCompressedSize = 0; |
||
17 | var jsCLastCompressedSize = 0; |
||
18 | |||
19 | // Compiles or Compresses the select dynapi source folder |
||
20 | // When check==true compressor will check dynapi files in source folder for missing semi-colons |
||
21 | function compress(check) { |
||
22 | |||
23 | var f=document.frmcompress; |
||
24 | var source=f.txtsource.value; |
||
25 | var target=f.txttarget.value; |
||
11521 | obado | 26 | |
20 | reyssat | 27 | jsFileIndex = -1; |
28 | jsCCheck = check; |
||
29 | jsCLevel = (jsCCheck)? 'low':f.cbolevel.value; |
||
11521 | obado | 30 | jsCDebug = f.chkdebug.checked; |
31 | jsCNS4 = f.chkns4.checked; |
||
20 | reyssat | 32 | |
33 | jsCLastUnCompressedSize = 0; |
||
34 | jsCLastCompressedSize = 0; |
||
35 | |||
36 | // build exclude list |
||
37 | jsCExclude=''; |
||
11521 | obado | 38 | if(f.chkcvs.checked) jsCExclude='CVS'; |
20 | reyssat | 39 | for(var i=0;i<f.chk.length;i++){ |
11521 | obado | 40 | if(!f.chk[i].checked){ |
20 | reyssat | 41 | jsCExclude+=((jsCExclude)? ',':'')+f.chk[i].value; |
42 | } |
||
43 | } |
||
11521 | obado | 44 | |
20 | reyssat | 45 | // check for valid source and target path |
46 | if(!CheckFolder(source)) { |
||
47 | alert('Invalid Source Path "'+source+'"'); |
||
48 | return; |
||
49 | }else if(!CheckFolder(target)) { |
||
50 | alert('Invalid Destination Path "'+target+'"'); |
||
51 | return; |
||
52 | } |
||
11521 | obado | 53 | |
20 | reyssat | 54 | // store javascript files in an array and copy non-javascript files into target |
11521 | obado | 55 | BuildFileSturcure(source,target,jsCExclude); |
20 | reyssat | 56 | |
57 | document.images['bar'].style.width='0%'; |
||
58 | document.images['bar'].style.visibility='visible'; |
||
59 | f.txtstatus.value='Compiling....'; |
||
60 | f.txtsizestatus.value=''; |
||
61 | document.images['bar'].style.width="1%"; |
||
62 | CompressNextFile(); |
||
63 | showScreen(jsCCheck); |
||
64 | }; |
||
65 | |||
66 | // CompressNextFile: Compresses one file at a time using the crunch() function (compressor.js) |
||
67 | function CompressNextFile(state,strText){ |
||
68 | |||
69 | var f=document.frmcompress; |
||
70 | var i,a,p,srcFile,tarFile,content; |
||
71 | var totalFiles; |
||
72 | |||
73 | if(state=='status'){ |
||
11521 | obado | 74 | |
20 | reyssat | 75 | // Display status from Crunch() functions |
11521 | obado | 76 | f.txtstatus.value='Compiling ['+jsCLastFileName+'] - '+strText; |
77 | |||
20 | reyssat | 78 | } |
79 | else{ |
||
80 | if (jsCCheck && state=="complete"){ |
||
81 | // Check file for semi-colon errors |
||
82 | // a much better error checking system is needed here |
||
83 | var t='',l,exclude='{},;'; |
||
84 | var ar = strText.split('\n'); |
||
11521 | obado | 85 | for(var i=0;i<ar.length;i++){ |
20 | reyssat | 86 | l=strTrim(ar[i]); |
87 | if (l && l.length){ |
||
88 | ok=false; |
||
89 | ch=l.substr(l.length-1,1); |
||
11521 | obado | 90 | if(ar[i]!='}' && exclude.indexOf(ch)>=0) ok=true |
20 | reyssat | 91 | if(!ok){ |
92 | l=l.replace(/</g,'<'); |
||
93 | l=l.replace(/>/g,'>'); |
||
94 | t+=' Line: '+(i+1)+': '+l+'<br>' |
||
11521 | obado | 95 | +' File: '+jsCLastFile+'<br><br>'; |
20 | reyssat | 96 | } |
97 | } |
||
98 | } |
||
99 | if(t){ |
||
100 | var dv=document.all['dvscreen']; |
||
101 | dv.innerHTML=dv.innerHTML+t+"<hr>"; |
||
102 | } |
||
103 | } |
||
104 | if(state=="complete"){ |
||
105 | // Save compressed data |
||
106 | if(jsCLevel!='none' && (jsCLastFileName+'').toLowerCase()=='dynapi.js') { |
||
107 | strText='// The DynAPI Distribution is distributed under the terms of the GNU LGPL license.\n' |
||
108 | +strText; |
||
109 | } |
||
110 | jsCLastCompressedSize+=strText.length; |
||
111 | SaveFile(jsCLastFile,strText); |
||
112 | f.txtsizestatus.value=' Last size in bytes: Before = '+jsCLastUnCompressedSize+' / After = '+jsCLastCompressedSize+' / ' |
||
113 | +' Diff = '+(jsCLastUnCompressedSize-jsCLastCompressedSize); |
||
114 | } |
||
11521 | obado | 115 | |
20 | reyssat | 116 | // Move to next file |
117 | jsFileIndex+=1; |
||
118 | totalFiles=GetTotalFiles(); |
||
11521 | obado | 119 | if(jsFileIndex<=totalFiles){ |
120 | |||
20 | reyssat | 121 | // get target and source file |
122 | srcFile=GetSRCFile(jsFileIndex); |
||
123 | tarFile=jsCLastFile=GetTARFile(jsFileIndex); |
||
124 | |||
125 | // update status |
||
126 | p=((jsFileIndex+1)/(totalFiles+1))*100; |
||
11521 | obado | 127 | a=jsCLastFile.split('\\'); |
20 | reyssat | 128 | jsCLastFileName=a[a.length-1]; |
129 | f.txtstatus.value='Compiling ['+jsCLastFileName+']....'; |
||
130 | document.images['bar'].style.width=p+"%"; |
||
131 | |||
132 | // get file content |
||
133 | content=OpenFile(srcFile)+''; |
||
134 | jsCLastUnCompressedSize+=content.length; |
||
135 | if(content.indexOf('\n')<0) content=content.split("\r"); // MAC |
||
11521 | obado | 136 | else content=content.split("\n"); // PC, UNIX |
20 | reyssat | 137 | content=content.join('<$Link/>'); |
138 | if(!jsCDebug){ |
||
139 | // Remove Debug conditional code blocks |
||
140 | content = RemoveCondition('DEBUG',content); |
||
141 | content=content.replace(/dynapi\.debug\.print\(/g,'dPrint\('); |
||
142 | } |
||
143 | if(!jsCNS4) { |
||
11521 | obado | 144 | // Remove NS4 conditinal code blocks |
20 | reyssat | 145 | content = RemoveCondition('NS4',content); |
146 | } |
||
147 | content=content.replace(/\<\$Link\/\>/g,'\n'); |
||
148 | |||
11521 | obado | 149 | // Compress the file content |
150 | Crunch(content,jsCLevel,CompressNextFile); |
||
151 | |||
20 | reyssat | 152 | } |
153 | else { |
||
11521 | obado | 154 | |
20 | reyssat | 155 | // finished compressing js files |
156 | f.txtstatus.value="Ready"; |
||
157 | document.images['bar'].style.visibility='hidden'; |
||
158 | } |
||
159 | } |
||
160 | }; |
||
161 | |||
162 | // jsCheckFileState: Used by fsobject to check if the file should be copied or compressed |
||
163 | function jsCheckFileState(f,pth){ |
||
164 | var state=0,ignore=false; |
||
165 | var a,copy=2, compress=1; |
||
166 | |||
167 | f=f+''; a=f.split("."); |
||
11521 | obado | 168 | |
20 | reyssat | 169 | // check if file is a part of the debug library |
170 | ignore=(!jsCDebug && f.indexOf('debug')==0) |
||
11521 | obado | 171 | |
20 | reyssat | 172 | // check if file is a part of the NS4 library |
173 | ignore=(ignore || (!jsCNS4 && f.indexOf('ns4')==0)) |
||
174 | |||
175 | if(!ignore){ |
||
176 | // if .js file the compress otherwise copy the file |
||
11521 | obado | 177 | if(a[a.length-1]=="js") state=compress; |
20 | reyssat | 178 | else state=copy; |
179 | } |
||
180 | |||
181 | return state; |
||
182 | }; |
||
183 | |||
184 | // Remove conditional statements |
||
185 | function RemoveCondition(tag,content) { |
||
186 | var lines = content.split('//]:'+tag); |
||
187 | var t = ['']; |
||
188 | |||
189 | // loop through conditional staments |
||
190 | for (i=0;i<lines.length;i++) { |
||
191 | if(lines[i].indexOf('//@IF:'+tag+'[')<0) t[t.length] = lines[i]; |
||
192 | else{ |
||
193 | if(tag=='NS4') t[t.length] = lines[i].replace(/\/\/\@IF:NS4\[(.*)$/g,''); |
||
194 | else if(tag=='DEBUG') t[t.length] = lines[i].replace(/\/\/\@IF:DEBUG\[(.*)$/g,''); |
||
195 | } |
||
196 | } |
||
197 | return t.join(''); |
||
198 | } |
||
199 | |||
200 | |||
201 | // Misc functions |
||
202 | |||
203 | function showScreen(b){ |
||
204 | var dv=document.all['dvscreen']; |
||
205 | dv.innerHTML=''; |
||
206 | dv=document.all['dvscreenrow']; |
||
207 | if(b) dv.style.display='block'; |
||
208 | else dv.style.display='none'; |
||
11521 | obado | 209 | |
20 | reyssat | 210 | } |
211 | |||
212 | function showHideAbout() { |
||
213 | var dv=document.all['dvabout']; |
||
214 | if(dv.style.display=='block') dv.style.display='none'; |
||
215 | else dv.style.display='block'; |
||
216 | } |
||
217 | |||
218 | function strTrim(s,dir){ |
||
219 | if(!s) return; |
||
220 | else s+=''; // make sure s is a string |
||
221 | dir=(dir)? dir:'<>'; |
||
222 | if(dir=='<'||dir=='<>') s=s.replace(/^(\s+)/g,''); |
||
223 | if(dir=='>'||dir=='<>') s=s.replace(/(\s+)$/g,''); |
||
224 | return s; |
||
11521 | obado | 225 | }; |