001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.local.command.checkout; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025 026import org.apache.maven.scm.ScmException; 027import org.apache.maven.scm.ScmFile; 028import org.apache.maven.scm.ScmFileSet; 029import org.apache.maven.scm.ScmFileStatus; 030import org.apache.maven.scm.ScmVersion; 031import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand; 032import org.apache.maven.scm.command.checkout.CheckOutScmResult; 033import org.apache.maven.scm.provider.ScmProviderRepository; 034import org.apache.maven.scm.provider.local.command.LocalCommand; 035import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils; 036import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository; 037import org.codehaus.plexus.util.FileUtils; 038 039/** 040 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> 041 */ 042public class LocalCheckOutCommand extends AbstractCheckOutCommand implements LocalCommand { 043 /** 044 * {@inheritDoc} 045 */ 046 protected CheckOutScmResult executeCheckOutCommand( 047 ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow) 048 throws ScmException { 049 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo; 050 051 if (version != null) { 052 throw new ScmException("The local scm doesn't support tags."); 053 } 054 055 File root = new File(repository.getRoot()); 056 057 String module = repository.getModule(); 058 059 File source = new File(root, module); 060 061 File baseDestination = fileSet.getBasedir(); 062 063 if (!root.exists()) { 064 throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ")."); 065 } 066 067 if (!source.exists()) { 068 throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ")."); 069 } 070 071 List<ScmFile> checkedOutFiles; 072 073 try { 074 if (baseDestination.exists()) { 075 FileUtils.deleteDirectory(baseDestination); 076 } 077 078 if (!baseDestination.mkdirs()) { 079 throw new ScmException( 080 "Could not create destination directory '" + baseDestination.getAbsolutePath() + "'."); 081 } 082 083 if (logger.isInfoEnabled()) { 084 logger.info("Checking out '" + source.getAbsolutePath() + "' to '" + baseDestination.getAbsolutePath() 085 + "'."); 086 } 087 088 List<File> fileList; 089 090 if (fileSet.getFileList().isEmpty()) { 091 fileList = FileUtils.getFiles(source.getAbsoluteFile(), "**", null); 092 } else { 093 fileList = fileSet.getFileList(); 094 } 095 096 checkedOutFiles = checkOut(source, baseDestination, fileList, repository.getModule()); 097 098 // write metadata file 099 LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils(); 100 metadataUtils.writeMetadata(baseDestination, metadataUtils.buildMetadata(source)); 101 } catch (IOException ex) { 102 throw new ScmException("Error while checking out the files.", ex); 103 } 104 105 return new LocalCheckOutScmResult(null, checkedOutFiles); 106 } 107 108 private List<ScmFile> checkOut(File source, File baseDestination, List<File> files, String module) 109 throws ScmException, IOException { 110 String sourcePath = source.getAbsolutePath(); 111 112 List<ScmFile> checkedOutFiles = new ArrayList<>(); 113 114 for (File file : files) { 115 String dest = file.getAbsolutePath(); 116 117 dest = dest.substring(sourcePath.length() + 1); 118 119 File destination = new File(baseDestination, dest); 120 121 destination = destination.getParentFile(); 122 123 if (!destination.exists() && !destination.mkdirs()) { 124 throw new ScmException( 125 "Could not create destination directory '" + destination.getAbsolutePath() + "'."); 126 } 127 128 FileUtils.copyFileToDirectory(file, destination); 129 130 String fileName = "/" + module + "/" + dest; 131 132 checkedOutFiles.add(new ScmFile(fileName, ScmFileStatus.CHECKED_OUT)); 133 } 134 135 return checkedOutFiles; 136 } 137}