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&oslash;l</a>
041 *
042 */
043public class LocalCheckOutCommand extends AbstractCheckOutCommand implements LocalCommand {
044    /** {@inheritDoc} */
045    protected CheckOutScmResult executeCheckOutCommand(
046            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
047            throws ScmException {
048        LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
049
050        if (version != null) {
051            throw new ScmException("The local scm doesn't support tags.");
052        }
053
054        File root = new File(repository.getRoot());
055
056        String module = repository.getModule();
057
058        File source = new File(root, module);
059
060        File baseDestination = fileSet.getBasedir();
061
062        if (!root.exists()) {
063            throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
064        }
065
066        if (!source.exists()) {
067            throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
068        }
069
070        List<ScmFile> checkedOutFiles;
071
072        try {
073            if (baseDestination.exists()) {
074                FileUtils.deleteDirectory(baseDestination);
075            }
076
077            if (!baseDestination.mkdirs()) {
078                throw new ScmException(
079                        "Could not create destination directory '" + baseDestination.getAbsolutePath() + "'.");
080            }
081
082            if (logger.isInfoEnabled()) {
083                logger.info("Checking out '" + source.getAbsolutePath() + "' to '" + baseDestination.getAbsolutePath()
084                        + "'.");
085            }
086
087            List<File> fileList;
088
089            if (fileSet.getFileList().isEmpty()) {
090                fileList = FileUtils.getFiles(source.getAbsoluteFile(), "**", null);
091            } else {
092                fileList = fileSet.getFileList();
093            }
094
095            checkedOutFiles = checkOut(source, baseDestination, fileList, repository.getModule());
096
097            // write metadata file
098            LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils();
099            metadataUtils.writeMetadata(baseDestination, metadataUtils.buildMetadata(source));
100        } catch (IOException ex) {
101            throw new ScmException("Error while checking out the files.", ex);
102        }
103
104        return new LocalCheckOutScmResult(null, checkedOutFiles);
105    }
106
107    private List<ScmFile> checkOut(File source, File baseDestination, List<File> files, String module)
108            throws ScmException, IOException {
109        String sourcePath = source.getAbsolutePath();
110
111        List<ScmFile> checkedOutFiles = new ArrayList<>();
112
113        for (File file : files) {
114            String dest = file.getAbsolutePath();
115
116            dest = dest.substring(sourcePath.length() + 1);
117
118            File destination = new File(baseDestination, dest);
119
120            destination = destination.getParentFile();
121
122            if (!destination.exists() && !destination.mkdirs()) {
123                throw new ScmException(
124                        "Could not create destination directory '" + destination.getAbsolutePath() + "'.");
125            }
126
127            FileUtils.copyFileToDirectory(file, destination);
128
129            File parent = file.getParentFile();
130
131            String fileName = "/" + module + "/" + dest;
132
133            checkedOutFiles.add(new ScmFile(fileName, ScmFileStatus.CHECKED_OUT));
134        }
135
136        return checkedOutFiles;
137    }
138}