Subversion Repositories wimsdev

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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