Subversion Repositories wimsdev

Rev

Rev 20 | 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. 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;
  26.  
  27.         jsFileIndex = -1;
  28.         jsCCheck = check;
  29.         jsCLevel = (jsCCheck)? 'low':f.cbolevel.value;
  30.         jsCDebug = f.chkdebug.checked;
  31.         jsCNS4 = f.chkns4.checked;
  32.  
  33.         jsCLastUnCompressedSize = 0;
  34.         jsCLastCompressedSize = 0;
  35.  
  36.         // build exclude list
  37.         jsCExclude='';
  38.         if(f.chkcvs.checked) jsCExclude='CVS';
  39.         for(var i=0;i<f.chk.length;i++){
  40.                 if(!f.chk[i].checked){
  41.                         jsCExclude+=((jsCExclude)? ',':'')+f.chk[i].value;
  42.                 }
  43.         }
  44.  
  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.         }
  53.  
  54.         // store javascript files in an array and copy non-javascript files into target
  55.         BuildFileSturcure(source,target,jsCExclude);
  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'){
  74.  
  75.                 // Display status from Crunch() functions
  76.                 f.txtstatus.value='Compiling ['+jsCLastFileName+'] - '+strText;
  77.  
  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');
  85.                         for(var i=0;i<ar.length;i++){
  86.                                 l=strTrim(ar[i]);
  87.                                 if (l && l.length){
  88.                                         ok=false;
  89.                                         ch=l.substr(l.length-1,1);
  90.                                         if(ar[i]!='}' && exclude.indexOf(ch)>=0) ok=true
  91.                                         if(!ok){
  92.                                                 l=l.replace(/</g,'&lt;');
  93.                                                 l=l.replace(/>/g,'&gt;');
  94.                                                 t+=' Line: '+(i+1)+':   '+l+'<br>'
  95.                                                 +' File: '+jsCLastFile+'<br><br>';
  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.                 }
  115.  
  116.                 // Move to next file
  117.                 jsFileIndex+=1;
  118.                 totalFiles=GetTotalFiles();
  119.                 if(jsFileIndex<=totalFiles){
  120.  
  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;
  127.                         a=jsCLastFile.split('\\');
  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
  136.                         else content=content.split("\n"); // PC, UNIX
  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) {
  144.                                 // Remove NS4 conditinal code blocks
  145.                                 content = RemoveCondition('NS4',content);
  146.                         }
  147.                         content=content.replace(/\<\$Link\/\>/g,'\n');
  148.  
  149.                         // Compress the file content
  150.                         Crunch(content,jsCLevel,CompressNextFile);
  151.  
  152.                 }
  153.                 else {
  154.  
  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(".");
  168.  
  169.         // check if file is a part of the debug library
  170.         ignore=(!jsCDebug && f.indexOf('debug')==0)
  171.  
  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
  177.                 if(a[a.length-1]=="js") state=compress;
  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';
  209.  
  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;
  225. };
  226.