001    package org.apache.maven.scm.plugin;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.io.IOException;
024    
025    import org.apache.maven.plugin.MojoExecutionException;
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFile;
028    import org.apache.maven.scm.command.status.StatusScmResult;
029    import org.apache.maven.scm.repository.ScmRepository;
030    import org.codehaus.plexus.util.StringUtils;
031    
032    /**
033     * Display the modification status of the files in the configured scm url.
034     *
035     * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
036     * @author Olivier Lamy
037     * @version $Id: StatusMojo.java 1054215 2011-01-01 09:45:44Z olamy $
038     * @goal status
039     * @aggregator
040     */
041    public class StatusMojo
042        extends AbstractScmMojo
043    {
044        /** {@inheritDoc} */
045        public void execute()
046            throws MojoExecutionException
047        {
048            super.execute();
049    
050            try
051            {
052                ScmRepository repository = getScmRepository();
053    
054                StatusScmResult result = getScmManager().status( repository, getFileSet() );
055    
056                checkResult( result );
057    
058                File baseDir = getFileSet().getBasedir();
059    
060                // Determine the maximum length of the status column
061                int maxLen = 0;
062    
063                for ( ScmFile file : result.getChangedFiles() )
064                {
065                    maxLen = Math.max( maxLen, file.getStatus().toString().length() );
066                }
067    
068                for ( ScmFile file : result.getChangedFiles() )
069                {
070                    // right align all of the statuses
071                    getLog().info(
072                                   StringUtils.leftPad( file.getStatus().toString(), maxLen ) + " status for "
073                                       + getRelativePath( baseDir, file.getPath() ) );
074                }
075            }
076            catch ( IOException e )
077            {
078                throw new MojoExecutionException( "Cannot run status command : ", e );
079            }
080            catch ( ScmException e )
081            {
082                throw new MojoExecutionException( "Cannot run status command : ", e );
083            }
084        }
085    
086        /**
087         * Formats the filename so that it is a relative directory from the base.
088         *
089         * @param baseDir
090         * @param path
091         * @return The relative path
092         */
093        protected String getRelativePath( File baseDir, String path )
094        {
095            if ( path.equals( baseDir.getAbsolutePath() ) )
096            {
097                return ".";
098            }
099            else if ( path.indexOf( baseDir.getAbsolutePath() ) == 0 )
100            {
101                // the + 1 gets rid of a leading file separator
102                return path.substring( baseDir.getAbsolutePath().length() + 1 );
103            }
104            else
105            {
106                return path;
107            }
108        }
109    }