001package org.apache.maven.scm.provider.perforce.command.edit; 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 022import org.apache.maven.scm.ScmException; 023import org.apache.maven.scm.ScmFileSet; 024import org.apache.maven.scm.ScmResult; 025import org.apache.maven.scm.command.edit.AbstractEditCommand; 026import org.apache.maven.scm.command.edit.EditScmResult; 027import org.apache.maven.scm.provider.ScmProviderRepository; 028import org.apache.maven.scm.provider.perforce.PerforceScmProvider; 029import org.apache.maven.scm.provider.perforce.command.PerforceCommand; 030import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository; 031import org.codehaus.plexus.util.cli.CommandLineException; 032import org.codehaus.plexus.util.cli.CommandLineUtils; 033import org.codehaus.plexus.util.cli.Commandline; 034 035import java.io.File; 036import java.io.IOException; 037 038/** 039 * @author Mike Perham 040 * 041 */ 042public class PerforceEditCommand 043 extends AbstractEditCommand 044 implements PerforceCommand 045{ 046 /** 047 * {@inheritDoc} 048 */ 049 @Override 050 protected ScmResult executeEditCommand( ScmProviderRepository repo, ScmFileSet files ) 051 throws ScmException 052 { 053 Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files ); 054 PerforceEditConsumer consumer = new PerforceEditConsumer(); 055 try 056 { 057 if ( getLogger().isDebugEnabled() ) 058 { 059 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) ); 060 } 061 062 CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); 063 int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err ); 064 065 if ( exitCode != 0 ) 066 { 067 String cmdLine = CommandLineUtils.toString( cl.getCommandline() ); 068 069 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() ); 070 msg.append( '\n' ); 071 msg.append( "Command line was:" + cmdLine ); 072 073 throw new CommandLineException( msg.toString() ); 074 } 075 } 076 catch ( CommandLineException e ) 077 { 078 if ( getLogger().isErrorEnabled() ) 079 { 080 getLogger().error( "CommandLineException " + e.getMessage(), e ); 081 } 082 } 083 084 if ( consumer.isSuccess() ) 085 { 086 return new EditScmResult( cl.toString(), consumer.getEdits() ); 087 } 088 089 return new EditScmResult( cl.toString(), "Unable to edit file(s)", consumer.getErrorMessage(), false ); 090 } 091 092 public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory, 093 ScmFileSet files ) 094 throws ScmException 095 { 096 Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory ); 097 098 command.createArg().setValue( "edit" ); 099 100 try 101 { 102 String candir = workingDirectory.getCanonicalPath(); 103 104 for ( File f : files.getFileList() ) 105 { 106 File file = null; 107 if ( f.isAbsolute() ) 108 { 109 file = new File( f.getPath() ); 110 } 111 else 112 { 113 file = new File( workingDirectory, f.getPath() ); 114 } 115 // I want to use relative paths to add files to make testing 116 // simpler. 117 // Otherwise the absolute path will be different on everyone's 118 // machine 119 // and testing will be a little more painful. 120 String canfile = file.getCanonicalPath(); 121 if ( canfile.startsWith( candir ) ) 122 { 123 canfile = canfile.substring( candir.length() + 1 ); 124 } 125 command.createArg().setValue( canfile ); 126 } 127 } 128 catch ( IOException e ) 129 { 130 throw new ScmException( e.getMessage(), e ); 131 } 132 return command; 133 } 134}