001package org.apache.maven.scm.provider.starteam.command.update; 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.ScmVersion; 025import org.apache.maven.scm.command.changelog.ChangeLogCommand; 026import org.apache.maven.scm.command.update.AbstractUpdateCommand; 027import org.apache.maven.scm.command.update.UpdateScmResult; 028import org.apache.maven.scm.provider.ScmProviderRepository; 029import org.apache.maven.scm.provider.starteam.command.StarteamCommand; 030import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils; 031import org.apache.maven.scm.provider.starteam.command.changelog.StarteamChangeLogCommand; 032import org.apache.maven.scm.provider.starteam.command.checkout.StarteamCheckOutConsumer; 033import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository; 034import org.codehaus.plexus.util.StringUtils; 035import org.codehaus.plexus.util.cli.CommandLineUtils; 036import org.codehaus.plexus.util.cli.Commandline; 037import org.codehaus.plexus.util.cli.DefaultConsumer; 038import org.codehaus.plexus.util.cli.StreamConsumer; 039 040import java.io.File; 041import java.util.ArrayList; 042import java.util.List; 043 044/** 045 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a> 046 * 047 */ 048public class StarteamUpdateCommand 049 extends AbstractUpdateCommand 050 implements StarteamCommand 051{ 052 // ---------------------------------------------------------------------- 053 // AbstractUpdateCommand Implementation 054 // ---------------------------------------------------------------------- 055 056 /** {@inheritDoc} */ 057 protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version ) 058 throws ScmException 059 { 060 if ( getLogger().isInfoEnabled() ) 061 { 062 getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() ); 063 } 064 065 StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo; 066 067 StarteamCheckOutConsumer consumer = new StarteamCheckOutConsumer( getLogger(), fileSet.getBasedir() ); 068 069 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 070 071 List<File> updateFiles = fileSet.getFileList(); 072 073 if ( updateFiles.size() == 0 ) 074 { 075 //update everything 076 Commandline cl = createCommandLine( repository, fileSet, version ); 077 078 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() ); 079 080 if ( exitCode != 0 ) 081 { 082 return new UpdateScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false ); 083 } 084 else 085 { 086 //hiden feature to allow Continuous Integration machine to 087 // delete local files. It affectively remove all build ouput as well 088 String doDeleteLocal = System.getProperty( "maven.scm.starteam.deleteLocal" ); 089 090 if ( "true".equalsIgnoreCase( doDeleteLocal ) ) 091 { 092 this.deleteLocal( repository, fileSet, version ); 093 } 094 } 095 } 096 else 097 { 098 //update only interested files already on the local disk 099 for ( int i = 0; i < updateFiles.size(); ++i ) 100 { 101 File updateFile = (File) updateFiles.get( i ); 102 ScmFileSet scmFileSet = new ScmFileSet( fileSet.getBasedir(), updateFile ); 103 Commandline cl = createCommandLine( repository, scmFileSet, version ); 104 105 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() ); 106 107 if ( exitCode != 0 ) 108 { 109 return new UpdateScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), 110 false ); 111 } 112 } 113 } 114 115 return new UpdateScmResult( null, consumer.getCheckedOutFiles() ); 116 117 } 118 119 // ---------------------------------------------------------------------- 120 // 121 // ---------------------------------------------------------------------- 122 123 public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet fileSet, 124 ScmVersion version ) 125 { 126 List<String> args = new ArrayList<String>(); 127 128 args.add( "-merge" ); 129 args.add( "-neverprompt" ); 130 131 if ( version != null && StringUtils.isNotEmpty( version.getName() ) ) 132 { 133 args.add( "-vl" ); 134 args.add( version.getName() ); 135 } 136 137 StarteamCommandLineUtils.addEOLOption( args ); 138 139 return StarteamCommandLineUtils.createStarteamCommandLine( "co", args, fileSet, repo ); 140 } 141 142 /** {@inheritDoc} */ 143 protected ChangeLogCommand getChangeLogCommand() 144 { 145 StarteamChangeLogCommand command = new StarteamChangeLogCommand(); 146 147 command.setLogger( getLogger() ); 148 149 return command; 150 } 151 152 private void deleteLocal( StarteamScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version ) 153 throws ScmException 154 { 155 if ( fileSet.getFileList().size() != 0 ) 156 { 157 return; 158 } 159 160 Commandline cl = createDeleteLocalCommand( repo, fileSet, version ); 161 162 StreamConsumer consumer = new DefaultConsumer(); 163 164 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 165 166 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() ); 167 168 if ( exitCode != 0 ) 169 { 170 throw new ScmException( "Error executing delete-local: " + stderr.toString() ); 171 } 172 } 173 174 public static Commandline createDeleteLocalCommand( StarteamScmProviderRepository repo, ScmFileSet dir, 175 ScmVersion version ) 176 { 177 List<String> args = new ArrayList<String>(); 178 179 if ( version != null && StringUtils.isNotEmpty( version.getName() ) ) 180 { 181 args.add( "-cfgl" ); 182 args.add( version.getName() ); 183 } 184 185 args.add( "-filter" ); 186 args.add( "N" ); 187 188 return StarteamCommandLineUtils.createStarteamCommandLine( "delete-local", args, dir, repo ); 189 } 190 191}