All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.perforce.maven.mojo.shelve.P4ShelveMojo Maven / Gradle / Ivy

The newest version!
package com.perforce.maven.mojo.shelve;

/**
 * Copyright (c) 2010 Perforce Software. All rights reserved.
 */

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;

import com.perforce.maven.mojo.P4Mojo;
import com.perforce.maven.scm.provider.p4.command.P4ClientExecutor;
import com.perforce.maven.scm.provider.p4.command.P4Executor;
import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository;
import com.perforce.p4java.core.ChangelistStatus;
import com.perforce.p4java.core.IChangelist;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.generic.core.Changelist;
import com.perforce.p4java.impl.mapbased.server.Server;
import com.perforce.p4java.option.client.ReopenFilesOptions;
import com.perforce.p4java.option.client.ShelveFilesOptions;

/**
 * Perforce shelve command.
 *
 * @goal shelve
 */
public class P4ShelveMojo extends P4Mojo {

    /** The Constant command. */
    public static final String command = "Perforce shelve command";

    @Override
    public ScmResult executeP4Command() throws MojoExecutionException {
        try {
            ScmRepository repository = getScmRepository();
            P4ScmProviderRepository providerRepository = (P4ScmProviderRepository) repository
                    .getProviderRepository();
            ScmProvider scmProvider = scmManager
                    .getProviderByRepository(repository);
            ScmFileSet fileSet = new ScmFileSet(scmDirectory, includes,
                    excludes);
            ScmResult scmResult = executeShelveCommand(providerRepository,
                    fileSet);
            return scmResult;
        } catch (ScmException e) {
            throw new MojoExecutionException("Couldn't shelve files.", e);
        } catch (IOException e) {
            throw new MojoExecutionException(
                    "Problem occurred while shelving files.", e);
        }
    }

    protected ScmResult executeShelveCommand(ScmProviderRepository repo,
            ScmFileSet files) throws ScmException {
        P4ShelveResult p4Result = new P4ShelveResult();
        P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
        P4ClientExecutor executor = new P4ClientExecutor(p4Repo, files,
                getLogger());
        boolean shouldRun = true;
        List fileSpecs = null;
        List filePaths = new ArrayList();
        List fileList = files.getFileList();
        if (fileList != null) {
            for (File f : fileList) {
                if (!f.getName().contentEquals(".")) {
                    filePaths.add(P4Executor.encodeWildcards(f
                            .getAbsolutePath()));
                }
            }
            if (!filePaths.isEmpty()) {
                fileSpecs = FileSpecBuilder.makeFileSpecList(filePaths
                        .toArray(new String[filePaths.size()]));
            }
        }
        if (fileSpecs == null || fileSpecs.isEmpty()) {
            if (!P4Executor.isEmpty(includes) || !P4Executor.isEmpty(excludes)) {
                shouldRun = false;
            }
        }
        if (shouldRun) {
            int changelistId = parseChangelist(System
                    .getProperty(P4Executor.P4_CHANGELIST_PROPERTY));
            if (changelistId == IChangelist.UNKNOWN) {
                changelistId = IChangelist.DEFAULT;
            }
            List changelistFileSpecs = null;
            try {
                IChangelist changelist = executor.getServer().getChangelist(
                        changelistId);
                changelistFileSpecs = changelist.getFiles(true);
                if (changelistFileSpecs != null
                        && !changelistFileSpecs.isEmpty()) {
                    if (changelistId == IChangelist.DEFAULT) {
                        String changelistDesc = System.getProperty(
                                P4Executor.P4_CHANGELIST_DESCRIPTION_PROPERTY,
                                "Open Files");
                        Changelist newChangelistImpl = new Changelist(
                                IChangelist.UNKNOWN, executor.getClient()
                                        .getName(), executor.getP4User(),
                                ChangelistStatus.NEW, new Date(),
                                changelistDesc, false,
                                (Server) executor.getServer());
                        IChangelist retChangelist = executor.getClient()
                                .createChangelist(newChangelistImpl);
                        ReopenFilesOptions reopenFilesOptions = new ReopenFilesOptions();
                        reopenFilesOptions.setChangelistId(retChangelist
                                .getId());
                        List retFileSpecs = executor.getClient()
                                .reopenFiles(changelistFileSpecs,
                                        reopenFilesOptions);
                        // Refresh the changelist
                        if (retChangelist.canRefresh()) {
                            retChangelist.refresh();
                        }
                        changelistId = retChangelist.getId();
                        System.setProperty(P4Executor.P4_CHANGELIST_PROPERTY,
                                String.valueOf(changelistId));
                    }
                    ShelveFilesOptions shelveFilesOptions = new ShelveFilesOptions();
                    List retFileSpecs = executor.getClient()
                            .shelveFiles(fileSpecs, changelistId,
                                    shelveFilesOptions);
                    p4Result.process(retFileSpecs);
                }
            } catch (P4JavaException e) {
                if (getLog().isErrorEnabled()) {
                    getLog().error(e);
                }
                throw new ScmException(e.getLocalizedMessage(), e);
            }
        }
        if (getLog().isInfoEnabled()) {
            getLog().info(p4Result.getCommandOutput());
        }
        return new ScmResult(
                command,
                p4Result.isSuccess() ? "Shelve successful" : "Unable to shelve",
                p4Result.getCommandOutput(), p4Result.isSuccess());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy