home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / web_dev / files / mwjpp516.exe / %MAINDIR% / Tools / Library / jdkUtilities.script < prev    next >
Encoding:
Text File  |  2001-06-19  |  12.3 KB  |  441 lines

  1. !!Script
  2. // Copyright ⌐ 1997-1998 - Modelworks Software
  3. // @Modified build 216 cm19980914 - see below
  4. // @Modified build 228 cm19981020 - added GetClasspathArgument function
  5. // @Modified build 247 cm19981230 - leave '\' in classpaths
  6. // @Modified build 262 cm19990202 - added support for boot classpaths
  7. // @Modified build 290 cm19990427 - check for duplicate classpaths
  8. // @Modified build 309 cm19990630 - removed empty classpath from the command line 
  9. // @Modified build 314 cm19990723 - fixed problem with no ';' in classpath
  10. // @Modified build 316 cm19990725 - fixed bougs "space in classpath" message
  11. // @Modified build 334 cm19991220 - added archive files in project to classpath
  12.  
  13. // Utility functions for JDK tool scripts
  14.  
  15. // removed function DoCommand warning - cm19980914
  16.  
  17. function GetJDKCompileCommandLine(sourcefile) // cm19981020
  18. {
  19.     // Get the compiler and systemClasses file objects
  20.     var compiler = helpers.lookup("jdk-compiler");
  21.     var systemClasses = helpers.lookup("jdk-classes.zip");
  22.     var appendSystemClasses = getMapFileValue("Preferences", "Append JDK classes.zip file to classpaths", true);
  23.     
  24.     var standardOptions = gOptions.lookup("jdk-compiler", "");
  25.     var useEnvironmentClasspath = gOptions.lookup("useEnvironmentClasspath", false);
  26.     
  27.     if (compiler && compiler.exists)
  28.     {       
  29.         // Quote all paths in case a path contains spaces
  30.         
  31.         // Add the path of the compiler -- this must be first
  32.         var command = "\"" + compiler.path + "\" ";
  33.         
  34.         // Add other options for the compiler
  35.         if (standardOptions.length > 0)
  36.         {
  37.             command += standardOptions + " ";
  38.         }
  39.         
  40.         var jikes = compiler.path.indexOf("jikes") > -1;
  41.         if (jikes)
  42.         {
  43.             command += "+E ";
  44.         }
  45.         
  46.         if (!systemClasses) // Java 2 platform
  47.         {        
  48.             var bootPaths = GetBootClassPaths();
  49.             if (bootPaths.length > 0)
  50.             {
  51.                command += "-bootclasspath \"" + bootPaths + "\" ";
  52.             }
  53.         }
  54.  
  55.         if (useEnvironmentClasspath)
  56.         {
  57.             var tool = newTool();
  58.             tool.setEnvironmentVariable("Classpath", getGlobal("EnvironmentClasspath"));
  59.         }
  60.         else
  61.         {
  62.             // Add the classpaths quoting the classpaths argument
  63.             command +=  "-classpath \"";   // Add the current directory
  64.             var classpath = "";
  65.             if (sourcefile != "")
  66.             {
  67.                 classpath += Java.getClassPath(sourcefile);
  68.             }
  69.             classpath = GetBuildPackageClassPaths(classpath);
  70.             classpath = GetCustomClassPaths(classpath);
  71.             if (appendSystemClasses && systemClasses && systemClasses.exists)
  72.             {
  73.                 if (classpath.length > 0)
  74.                 {
  75.                     classpath += ";" + systemClasses.path;
  76.                 }
  77.                 else
  78.                 {
  79.                     classpath += systemClasses.path;
  80.                 }
  81.             }
  82.             
  83.             if (jikes && !systemClasses)
  84.             {
  85.                 var jdkDirectory = helpers.lookup("jdk-directory", null);
  86.                 if (jdkDirectory)
  87.                 {
  88.                     var rtjar = jdkDirectory.path + "\\jre\\lib\\rt.jar";
  89.                     if (classpath.length > 0)
  90.                     {
  91.                         classpath += ";" + rtjar;
  92.                     }
  93.                     else
  94.                     {
  95.                         classpath += rtjar;
  96.                     }
  97.                 }
  98.             }
  99.             
  100.             command += classpath + "\" ";
  101.         }
  102.         
  103.         return command;
  104.     }
  105.     else
  106.     {
  107.         var result = confirm("You need to run the JDK setup script\n"
  108.         + "before you can use this command.\n" +
  109.         "Do you want to run the setup script now?");
  110.         if (result)
  111.         {
  112.             run("Configuration\\JDK");
  113.         }
  114.     }
  115. }
  116.  
  117. // This function set the global environment CLASSPATH
  118. // Use it as an alternative to the standard "-classpath "
  119. // command line argument
  120. function setCLASSPATH(tool)
  121.     var systemClasses = helpers.lookup("jdk-classes.zip");  
  122.     var appendSystemClasses = getMapFileValue("Preferences", "Append JDK classes.zip file to classpaths", true);
  123.     
  124.     var value = ".";    // Add the current directory
  125.     value = GetPackageClassPaths(value);    
  126.     value = GetCustomClassPaths(value);
  127.     
  128.     if (appendSystemClasses && systemClasses && systemClasses.exists)
  129.     {
  130.         if (value.length > 0)
  131.         {
  132.             value += ";" + systemClasses.path;    
  133.         }
  134.         else
  135.         {
  136.             value += systemClasses.path;    
  137.         }
  138.     }
  139.     
  140.     CheckForSpaceInClasspath(value);
  141.     
  142.     var currentClasspath = 
  143.     tool.setEnvironmentVariable("CLASSPATH", value);
  144.     
  145.     // Return the current CLASSPATH
  146.     return currentClasspath;
  147. }
  148.  
  149. function GetCustomClassPaths(classpaths)
  150. {    
  151.     var entry = gOptions.lookup("standardClasspaths", "");
  152.     if (entry != "")
  153.     {
  154.         if (classpaths.length > 0)
  155.         {
  156.             classpaths += ";" + entry;
  157.         }
  158.         else
  159.         {
  160.             classpaths += entry;
  161.         }
  162.     }
  163.     
  164.     var entry = gOptions.lookup("customClasspaths", "");
  165.     if (entry != "")
  166.     {
  167.         if (classpaths.length > 0)
  168.         {
  169.             classpaths += ";" + entry;
  170.         }
  171.         else
  172.         {
  173.             classpaths += entry;
  174.         }
  175.     }
  176.     
  177.     CheckForSpaceInClasspath(classpaths);
  178.     
  179.     return classpaths;
  180. }
  181.  
  182. function GetBootClassPaths()
  183. {
  184.     var classpaths = "";
  185.     var entry = gOptions.lookup("bootClasspaths", "");
  186.     if (entry != "")
  187.     {
  188.         classpaths = entry;
  189.     }
  190.  
  191.     CheckForSpaceInClasspath(classpaths);
  192.     return classpaths;
  193. }
  194.  
  195. function GetArchiveClassPaths(packagePath) //cm19991220
  196. {
  197.     var classpath = "";
  198.     
  199.     return classpath; // Comment this out if you need to add archive files 
  200.                       // to your classpath
  201.     
  202.     var list = getDirectoryFiles(packagePath, "*.jar;*.zip", true, false);
  203.     if (list)
  204.     {
  205.         var semicolon = "";
  206.         var position = list.getHeadPosition();
  207.         while(position && position.valid)
  208.         {
  209.             classpath += semicolon + list.getNext(position).path;
  210.             semicolon = ";";
  211.         }
  212.     }
  213.     //gOutput.writeLine("found: " + classpath);
  214.     return classpath;
  215. }
  216.  
  217. function GetPackageClassPaths(classpaths)
  218. {
  219.     var project = getCurrentProject();
  220.     if (project)
  221.     {
  222.         var packageList = project.getPackageList();
  223.         if (packageList)
  224.         {
  225.             var position = packageList.getHeadPosition();
  226.             while (position.valid)
  227.             {
  228.                 var packageClassPath = 
  229.                 packageList.getNext(position).getJavaClassPath();
  230.                 if (packageClassPath && classpaths.indexOf(packageClassPath) == -1)
  231.                 {
  232.                     if (classpaths.length > 0)
  233.                     {
  234.                         classpaths += ";" + packageClassPath;
  235.                     }
  236.                     else
  237.                     {
  238.                         classpaths += packageClassPath;
  239.                     }
  240.                 }
  241.                 var archivePaths = GetArchiveClassPaths(packageClassPath); //cm19991220
  242.                 if (archivePaths.length > 0)
  243.                 {
  244.                     classpaths += ";" + archivePaths;
  245.                 }
  246.             }
  247.         }
  248.     }
  249.     CheckForSpaceInClasspath(classpaths);
  250.     
  251.     return classpaths;
  252. }
  253.  
  254. function GetBuildPackageClassPaths(classpaths)
  255. {
  256.     var project = getCurrentProject();
  257.     if (project)
  258.     {
  259.         var packageList = project.getPackageList();
  260.         if (packageList)
  261.         {
  262.             var position = packageList.getHeadPosition();
  263.             while (position.valid)
  264.             {
  265.                 var packageClassPath = packageList.getNext(position).getJavaClassPath();
  266.                 if (packageClassPath && classpaths.indexOf(packageClassPath) == -1)
  267.                 {
  268.                     if (classpaths.length == 0)
  269.                     {
  270.                         classpaths += packageClassPath;
  271.                     }
  272.                     else
  273.                     {
  274.                         classpaths += ";" + packageClassPath;
  275.                     }
  276.                 }
  277.                 var archivePaths = GetArchiveClassPaths(packageClassPath); //cm19991220
  278.                 if (archivePaths.length > 0)
  279.                 {
  280.                     classpaths += ";" + archivePaths;
  281.                 }
  282.             }
  283.             
  284.             // Add SourcePath directories if needed
  285.             position = packageList.getHeadPosition();
  286.             while (position.valid)
  287.             {
  288.                 var pkg = packageList.getNext(position);
  289.                 if (pkg.destinationNotSource)
  290.                 {
  291.                     var packageClassPath = 
  292.                         pkg.getJavaSourceClassPath();
  293.                     if (packageClassPath && classpaths.indexOf(packageClassPath) == -1)
  294.                     {
  295.                         if (classpaths.length == 0)
  296.                         {
  297.                             classpaths += packageClassPath;
  298.                         }
  299.                         else
  300.                         {
  301.                             classpaths += ";" + packageClassPath;
  302.                         }
  303.                     }
  304.                 }
  305.             }
  306.         }
  307.     }
  308.     CheckForSpaceInClasspath(classpaths);
  309.     
  310.     return classpaths;
  311. }
  312.  
  313. function CheckForSpaceInClasspath(classpaths)
  314. {
  315.     var spaceIndex = classpaths.indexOf(" ");
  316.     if (spaceIndex > -1)
  317.     {
  318.         gOutput.writeLine("Warning: Space found in classpath: " + classpaths);
  319.         gOutput.writeLine("Sun's JDK debugger will not work correctly with a space in the classpath.");
  320.     }
  321. }
  322.  
  323. function GetDestinationPath(sourcePath)
  324. {
  325.     var destinationPath = "";
  326.     var project = getCurrentProject();
  327.     if (project)
  328.     {
  329.         var sourcePackage = project.getPackage(sourcePath);
  330.         if (sourcePackage)
  331.         {
  332.             return sourcePackage.getDestinationPath(sourcePath);
  333.         }
  334.     }
  335.     return destinationPath;
  336. }
  337.  
  338. function AppendSeparator(classpath)
  339. {
  340.     if (classpath.length > 0)
  341.     {
  342.         var lastChar = classpath.charAt(classpath.length-1);
  343.         if (lastChar != '"' && lastChar != ';')
  344.         {
  345.             classpath += ';';
  346.         }
  347.     }
  348.     return classpath;
  349. }
  350.  
  351. function GetClasspathArgument(classfilepath, special) // cm19981020
  352. {
  353.     var appendSystemClasses = getMapFileValue("Preferences", "Append JDK classes.zip file to classpaths", true);
  354.     var systemClasses = helpers.lookup("jdk-classes.zip");
  355.  
  356.     var bootclasspath = "";
  357.     
  358.     if (!systemClasses) // Java 2 platform
  359.     {        
  360.         var bootPaths = GetBootClassPaths();
  361.         if (bootPaths.length > 0)
  362.         {
  363.             bootclasspath += "-Xbootclasspath:" + bootPaths + " ";
  364.         }
  365.     }
  366.  
  367.     var classpath = "";
  368.     
  369.     if (classfilepath != "")
  370.     {
  371.         classpath += Java.getClassPath(classfilepath); // Add a local classpath for classfilepath if needed  
  372.     }
  373.  
  374.     classpath = GetPackageClassPaths(classpath);     
  375.     classpath = GetCustomClassPaths(classpath); 
  376.     if (appendSystemClasses && systemClasses && systemClasses.exists)
  377.     {
  378.         classpath = AppendSeparator(classpath);
  379.         classpath += systemClasses.path;
  380.     }
  381.     
  382.     if (special)
  383.     {
  384.         classpath = AppendSeparator(classpath);
  385.         classpath += special;
  386.     }
  387.  
  388.     if (classpath == "")
  389.     {
  390.         return bootclasspath; 
  391.     }
  392.     else
  393.     {
  394.         return bootclasspath + "-classpath \"" + classpath + "\" "; 
  395.     }
  396. }
  397.  
  398. function AddFilesToCompile(commandLine, filePathList)  //cm20000530
  399. {
  400.     var result = "";
  401.     if (gOptions.lookup("useCommandFileWhenCompiling",false))
  402.     {
  403.         result = AddFilePathsToBatchFile(filePathList);
  404.     }
  405.     else
  406.     {
  407.         var position = filePathList.getHeadPosition();
  408.         while(position && position.valid) 
  409.         {
  410.             var path = filePathList.getNext(position);
  411.             result += " \"" + path + "\"";
  412.         }
  413.     }
  414.     return result;
  415. }
  416.  
  417. function AddFilePathsToBatchFile(buildList)  //cm19991223
  418. {
  419.     var path = File.getDataPath() + "\\JDKCommandFile.txt";
  420.     var commandFile = "@" + "\"" + path + "\""; //Bruno Konik20001215
  421.     var editor = newEditor(path, false);
  422.     if (editor)
  423.     {
  424.         editor.removeAll();
  425.         var position = buildList.getHeadPosition();
  426.         while (position && position.valid)
  427.         {
  428.             var path = buildList.getNext(position);
  429.             editor.append(path + "\n");
  430.         }
  431.         
  432.         file = editor.getFile();
  433.         editor.close();
  434.         return commandFile;
  435.     }
  436.     return "";
  437. }
  438.  
  439. !!/Script
  440.