plynch      2002/07/10 03:39:32

  Modified:    src/java/org/apache/maven/app Maven.java
  Log:
  o Attempted to make the Maven header more concise, remove redundancy in the console 
output, and add some ascii art that may or may not facilitate boos and hisses
  
  o vertically aligned the goal description output assuming a console of 80 char 
width, which I believe is standard
  
  o vertically aligned the plugin descriptions, dynamically based on plugin name length
  
  Just some stuff I had lying around. Besides I discovered the BreakIterator class.
  
  Need to templatize the maven version constant, though I hope everyone sees this as 
an improvement.
  
  Revision  Changes    Path
  1.57      +206 -78   jakarta-turbine-maven/src/java/org/apache/maven/app/Maven.java
  
  Index: Maven.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/app/Maven.java,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- Maven.java        9 Jul 2002 23:20:50 -0000       1.56
  +++ Maven.java        10 Jul 2002 10:39:31 -0000      1.57
  @@ -104,6 +104,7 @@
   import java.net.URLClassLoader;
   import java.net.MalformedURLException;
   
  +import java.text.BreakIterator;
   
   import java.util.ArrayList;
   import java.util.Collection;
  @@ -117,14 +118,16 @@
   import java.util.Properties;
   import java.util.StringTokenizer;
   
  -/** 
  +/**
    * Command-line entry-point to maven.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>bob mcwhirter</a>
  - * @task remove all System.out and System.err and move to commons-logging 
  - *      interface
  + * @task remove all System.out and System.err and move to commons-logging
  + *       interface
  + * @task filter the Maven version number into a Java constant that will change
  + *       during compilation
    */
  -public class Maven 
  +public class Maven
   {
       // ------------------------------------------------------------
       //     Constants
  @@ -139,6 +142,9 @@
       /** Initialization jellyscript name. */
       public static final String DRIVER_SCRIPT_NAME = "driver.jelly";
   
  +    /** The current Maven version id */
  +    public static final String APP_VERSION = "1.0-b5-dev";
  +
       // ------------------------------------------------------------
       //     Class members
       // ------------------------------------------------------------
  @@ -146,6 +152,9 @@
       /** Log. */
       private static final Log log = LogFactory.getLog(PluginManager.class);
   
  +    /** Default console width - for formatting output - 78 */
  +    private static final int CONSOLE_WIDTH = 80;
  +
       // ------------------------------------------------------------
       //     Instance members
       // ------------------------------------------------------------
  @@ -443,7 +452,7 @@
        *          reading the project descriptor.
        *  @throws IOException If there is an error while
        *          reading the project descriptor.
  -     *  @throws MalformedURLException If any of the the URLs denoting 
  +     *  @throws MalformedURLException If any of the the URLs denoting
        *          the local or remote repositories is malformed.
        *  @throws SAXException If there is an error while
        *          reading the project descriptor.
  @@ -480,7 +489,7 @@
       {
           this.projectDescriptor = new ProjectDescriptor(getProjectFile());
       }
  -    
  +
       /** Initialize Jelly.
        *
        *  @throws MalformedURLException If the url specifying
  @@ -491,7 +500,7 @@
       {
           this.jellyContext = new MavenJellyContext(getMavenBin().toURL(),
                                                     this.properties);
  -        
  +
           initializeJellyVariables();
       }
   
  @@ -499,7 +508,7 @@
        *
        *  <p>
        *  Adds the following variables to the <code>JellyContext</code>.
  -     *  
  +     *
        *  <ul>
        *    <li>maven.home</li>
        *    <li>maven.bin</li>
  @@ -539,7 +548,7 @@
   
           this.jellyContext.registerTagLibrary("",
                                                jeezTagLib);
  -                                             
  +
           // Register Maven taglibs
           this.jellyContext.registerTagLibrary("jelly:maven",
                                                new MavenTagLibrary());
  @@ -550,7 +559,7 @@
       private void initializeAnt()
       {
           this.antProject = new GrantProject();
  -        
  +
           antProject.setPropsHandler(new GrantPropsHandler(getJellyContext()));
   
           getJellyContext().setAntProject(antProject);
  @@ -581,10 +590,10 @@
           }
   
           project.addBuildListener(listener);
  -        
  +
           PrintStream demuxOut = new PrintStream(new DemuxOutputStream(project,
                                                                        false));
  -        
  +
           PrintStream demuxErr = new PrintStream(new DemuxOutputStream(project,
                                                                        true));
   
  @@ -616,10 +625,10 @@
           {
               return;
           }
  -        
  +
           File driverProperties = new File( mavenBin, "driver.properties" );
           loadProps( driverProperties );
  -        
  +
           JellyUtils.runScript( driver,
                                 getMavenBin().toURL(),
                                 getJellyContext(),
  @@ -639,7 +648,7 @@
           pluginManager.setXMLOutput( getXMLOutput() );
           pluginManager.initializePlugins();
       }
  -    
  +
       /** Run maven.
        *
        *  @throws Exception If an error occurs.
  @@ -650,7 +659,7 @@
   
           context.setVariable("goals",
                               getGoalNames());
  -            
  +
           runGoals(goalNames);
       }
   
  @@ -658,12 +667,17 @@
       /** Display helpful information regarding
        *  all documented goals.
        */
  -    private void displayGoals() 
  +    private void displayGoals()
       {
  +
  +        System.out.println();
  +        System.out.println("Available Plugins/Goals");
  +        System.out.println(format("", "Available [Plugins] / Goals".length(), '`'));
  +
           Collection goals = getWerkzProject().getGoals();
   
           List list = new ArrayList( goals );
  -        
  +
           // sort by name
           Comparator comparator = new Comparator() {
               public int compare(Object o1, Object o2) {
  @@ -673,7 +687,7 @@
               }
           };
           Collections.sort( list, comparator );
  -        
  +
           Iterator goalIter = list.iterator();
           Goal     eachGoal = null;
   
  @@ -682,10 +696,11 @@
           boolean hadDesc = false;
           boolean hasBlanks = false;
   
  -
  -        // sort by plugin
           String lastPluginName = "";
   
  +        int wrapIndent;
  +        String msgPrefix;
  +        boolean isFirst = true;
           while (goalIter.hasNext())
           {
               eachGoal = (Goal) goalIter.next();
  @@ -721,51 +736,149 @@
   
               if(isMain && newPackage)
               {
  -                System.out.println("");
  -                System.out.println("[" + pluginName + "] : " + description);
  +                if(!isFirst)
  +                {
  +                System.out.println();
  +                }
  +                isFirst = false;
  +
  +                msgPrefix = "[" + pluginName + "] : ";
  +                // the indent here is dynamic
  +                wrapIndent = msgPrefix.length();
  +            }
  +             else if(isMain)
  +            {
  +                wrapIndent = 30;
  +                msgPrefix = "o " + format(goalName + " ", wrapIndent, '.') + " ";
  +                // add 3 for spaces and 'o' used to pad the message
  +                wrapIndent+=3;
               }
  -            else if(newPackage)
  -            {
  -                System.out.println("");
  -                System.out.println("[" + pluginName + "]");
  -                String shortName = goalName.substring(pluginName.length() + 1,
  -                                                      goalName.length());
  -                System.out.println("    " + format(shortName + " ", 30, '.')
  -                                   + " " + description);
  -            }
  -            else if(isMain)
  -            {
  -                System.out.println("o   " + format(goalName + " ", 30, '.')
  -                                   + " " + description);
  -            }   
               else
               {
  -                String shortName = goalName.substring(pluginName.length() + 1,
  -                                                      goalName.length());
  -                System.out.println("    " + format(shortName + " ", 30, '.')
  -                                   + " " + description);
  +                if(newPackage)
  +                {
  +                    if(isFirst)
  +                    {
  +                        msgPrefix = "[";
  +                        isFirst = false;
  +                    }
  +                    else
  +                    {
  +                        msgPrefix = "\n[";
  +                    }
  +                    msgPrefix += pluginName + "]\n";
  +                }
  +                else
  +                {
  +                  msgPrefix = "";
  +                }
  +
  +                wrapIndent = 30;
  +                String shortName =
  +                    goalName.substring( pluginName.length()+1,goalName.length() );
  +
  +                msgPrefix = msgPrefix + "  "
  +                            + format(shortName + " ", wrapIndent, '.' )
  +                            + " ";
  +                // add 3 for spaces used to pad the message
  +                wrapIndent+=3;
               }
  +
  +
  +           System.out.println(msgPrefix
  +              + wrapConsoleMessage(description, wrapIndent, CONSOLE_WIDTH ) );
           }
   
  +
           if (hasBlanks)
           {
               System.out.println();
               System.out.println("Non documented goals");
  -            
  +
               goalIter = list.iterator();
               while (goalIter.hasNext())
               {
                   eachGoal = (Goal) goalIter.next();
                   description = eachGoal.getDescription();
  -    
  +
                   if (description == null)
                   {
  -                    System.out.println("    " + eachGoal.getName());
  +                    System.out.println("  " + eachGoal.getName());
                   }
               }
           }
  +
       }
   
  +
  +    /**
  +     * Nicely wraps a message for console output, using a word BreakIterator
  +     * instance to determine wrapping breaks.
  +     *
  +     * @param msg the string message for the console
  +     * @param wrapIndent the number of characters to indent all lines
  +     * after the first one
  +     * @param lineWidth the console width that determines where to wrap
  +     *
  +     */
  +    private String wrapConsoleMessage(String msg, int wrapIndent, int lineWidth)
  +    {
  +        int offset = lineWidth-wrapIndent;
  +        if(msg.length() <= offset)
  +        {
  +            return msg;
  +        }
  +        else
  +        {
  +            BreakIterator bIter = BreakIterator.getWordInstance();
  +            StringBuffer buf = new StringBuffer();
  +            String newLine = "\n";
  +            String pad = " ";
  +            int currentPos = 0;
  +            bIter.setText(msg);
  +
  +            while (offset < bIter.getText().getEndIndex())
  +            {
  +                if (Character.isWhitespace(bIter.getText().first()))
  +                {
  +                  // remove leading whitespace and continue
  +                  msg = msg.substring(1);
  +                  bIter.setText(msg);
  +                  continue;
  +                }
  +
  +                // get the last boundary before the specified offset
  +                currentPos = bIter.preceding(offset);
  +                // append from the start to currentPos
  +                buf.append(msg.substring(0, currentPos));
  +                // start next line
  +                buf.append(newLine);
  +
  +                //pad with spaces to create indent
  +                for (int i = 0; i != wrapIndent && i < lineWidth; i++)
  +                {
  +                  buf.append(pad);
  +                }
  +
  +                // set the text of the break iterator to be the rest
  +                // of the string not already appended
  +                msg = msg.substring(currentPos);
  +
  +                //reset the text for another go
  +                bIter.setText(msg);
  +            }
  +
  +            // remove leading whitespace and continue
  +            while (Character.isWhitespace(msg.charAt(0)))
  +            {
  +                msg = msg.substring(1);
  +            }
  +            buf.append(msg);
  +            return buf.toString();
  +        }
  +    }
  +
  +
       /** Produce a formatted/padded string.
        *
        *  @param orig The string to format.
  @@ -813,7 +926,7 @@
               return;
           }
   
  -        // lets create a child context 
  +        // lets create a child context
           // so that URIs are relative to the projects build file
   
           MavenJellyContext context = new MavenJellyContext( getJellyContext() );
  @@ -823,7 +936,7 @@
           context.setRootURL( projectURL );
           context.setCurrentURL( projectURL );
           context.setInherit( true );
  -        
  +
           JellyUtils.runScript(projectBuildFile,
                                null,
                                context,
  @@ -844,7 +957,7 @@
           if (goalNames.isEmpty())
           {
               String defaultGoalName = getWerkzProject().getDefaultGoalName();
  -            
  +
               if (defaultGoalName != null)
               {
                   goalNames.add(defaultGoalName);
  @@ -862,7 +975,7 @@
           while (goalNameIter.hasNext())
           {
               eachGoalName = (String) goalNameIter.next();
  -            
  +
               eachGoal = getWerkzProject().getGoal(eachGoalName);
   
               if (eachGoal == null)
  @@ -918,7 +1031,7 @@
           {
               setDir(new File(cli.getOptionValue('d')));
           }
  -        
  +
           if ( this.cli.hasOption('p'))
           {
               setProjectFile(new File(cli.getOptionValue('p')));
  @@ -928,7 +1041,7 @@
               setProjectFile(new File(getDir(),
                                       PROJECT_DESCRIPTOR_FILE_NAME));
           }
  -        
  +
           File projectBuildFile = new File(getDir(),
                                            PROJECT_BUILD_FILE_NAME);
   
  @@ -1002,24 +1115,24 @@
   
           propsFile = new File(getDir(),
                                "project.properties");
  -        
  +
           loadProps(propsFile);
  -        
  +
           propsFile = new File(System.getProperty("user.home"),
                                "build.properties");
  -        
  +
           loadProps(propsFile);
  -        
  +
           propsFile = new File(getDir(),
                                "build.properties");
   
           loadProps(propsFile);
   
  -        
  +
           if (getCli().hasOption('D'))
           {
               String[] defStrs = getCli().getOptionValues('D');
  -            
  +
               for (int i = 0; i < defStrs.length; ++i)
               {
                   setCliProperty(defStrs[i]);
  @@ -1043,7 +1156,7 @@
           {
               log.debug("Properties file: " + propsFile);
           }
  -        
  +
           FileInputStream in = null;
   
           try
  @@ -1076,13 +1189,13 @@
           }
       }
   
  -    private void integrateProps(Properties props) 
  +    private void integrateProps(Properties props)
       {
           if (props == null)
           {
               return;
           }
  -        
  +
           JellyContext context = getJellyContext();
   
           Enumeration propNames = props.propertyNames();
  @@ -1114,7 +1227,7 @@
                       {
                           propVal = propText;
                       }
  -                    
  +
                       context.setVariable(eachName,
                                           propVal);
                   }
  @@ -1129,7 +1242,7 @@
       public void verifyProject() throws Exception
       {
           String localRepoProp = getProperty("maven.repo.local");
  -        
  +
           if (localRepoProp == null)
           {
               localRepoProp = getProperty("lib.repo");
  @@ -1151,7 +1264,7 @@
                   System.out.println("   maven.repo.local=${lib.repo}");
                   System.out.println("");
                   System.out.println("----------------------------------------");
  -            } 
  +            }
           }
   
           if (localRepoProp == null)
  @@ -1196,7 +1309,7 @@
           {
               throw new RepoConfigException(localRepo + " is not writable.");
           }
  -            
  +
           ProjectVerifier verifier = new ProjectVerifier();
   
           verifier.setMavenRemoteRepo(remoteRepoProp);
  @@ -1212,6 +1325,28 @@
           verifier.doExecute();
       }
   
  +    /**
  +     * Prints the Maven header to System.out
  +     */
  +    private void printConsoleMavenHeader()
  +    {
  +      System.out.println(" __  __");
  +      System.out.println("|  \\/  |__ Jakarta _ ___");
  +      System.out.println("| |\\/| / _` \\ V / -_) ' \\  ~ intelligent projects ~");
  +      System.out.println("|_|  |_\\__,_|\\_/\\___|_||_|   v. " + APP_VERSION);
  +    }
  +
  +      /**
  +     * Prints the Project header to System.out
  +     */
  +    private void printConsoleProjectHeader(Project mavenProject)
  +    {
  +        System.out.println(mavenProject.getName() + " ("
  +          + mavenProject.getId() + ") "
  +          + mavenProject.getCurrentVersion());
  +        System.out.println(getDir());
  +    }
  +
       /** Objectized version of main().
        *
        *  @param args Command-line arguments.
  @@ -1221,7 +1356,7 @@
           boolean done = false;
   
           Date fullStart = new Date();
  -        
  +
           try
           {
               initializeCore(args);
  @@ -1260,17 +1395,10 @@
   
           Project mavenProject = getProjectDescriptor().getProject();
   
  -        System.out.println("");
  -        System.out.println("            maven: intelligent projects");
  -        System.out.println("");
  -        System.out.println("project-directory: "
  -                           + getDir());
  -        System.out.println("          project: "
  -                           + mavenProject.getName()
  -                           + " (" + mavenProject.getId() + ")");
  -        System.out.println("  current-version: "
  -                           + mavenProject.getCurrentVersion());
  -        System.out.println("");
  +        printConsoleMavenHeader();
  +        System.out.println();
  +        printConsoleProjectHeader(mavenProject);
  +        System.out.println();
   
           boolean failed = false;
   
  @@ -1390,7 +1518,7 @@
           {
               String fileName    = e.getFileName();
               String elementName = e.getElementName();
  -            
  +
               int lineNumber  = e.getLineNumber();
               int column      = e.getColumnNumber();
   
  @@ -1441,7 +1569,7 @@
       // ------------------------------------------------------------
       //     Class methods
       // ------------------------------------------------------------
  -    
  +
       /** Run maven.
        *
        *  @param args The command-line arguments.
  @@ -1449,7 +1577,7 @@
       public static void main(String[] args)
       {
           Maven maven = new Maven();
  -        
  +
           maven.doMain(args);
       }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to