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

com.marvelution.maven.components.migration.manager.phases.prepare.MoveSourcesPhase Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.marvelution.maven.components.migration.manager.phases.prepare;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.plexus.util.IOUtil;

import com.marvelution.maven.components.migration.manager.MigrationResult;
import com.marvelution.maven.components.migration.manager.configuration.MigrationDescriptor;
import com.marvelution.maven.components.migration.manager.environment.MigrationEnvironment;
import com.marvelution.maven.components.migration.manager.exception.MigrationExecutionException;
import com.marvelution.maven.components.migration.manager.exception.MigrationFailureException;
import com.marvelution.maven.components.migration.manager.phases.AbstractMoveSourcesPhase;
import com.marvelution.utils.StringUtils;
import com.marvelution.utils.io.DirectoryUtils;

/**
 * {@link MigrationPhase} to move all the sources to the Maven directory structure
 * 
 * @author Mark Rekveld
 */
public class MoveSourcesPhase extends AbstractMoveSourcesPhase {

	/**
	 * {@inheritDoc}
	 */
	public MigrationResult simulate(final MigrationDescriptor descriptor, final MigrationEnvironment environment)
					throws MigrationExecutionException, MigrationFailureException {
		final MigrationResult result = new MigrationResult();
		if (!descriptor.isSkipDirectoryMigration()) {
			final Map mappedSources = mapSourceAndDestinationDirectories(descriptor);
			for (final Iterator iter = mappedSources.entrySet().iterator(); iter.hasNext();) {
				final Entry mapping = (Entry) iter.next();
				final File source = new File(descriptor.getWorkingDirectory(), mapping.getKey().toString());
				final String[] files = DirectoryUtils.scanDirectoryForFiles(source, new String[] {"**/**"});
				getLogger().info(
					"Fun run would move " + files.length + " files from " + mapping.getKey().toString() + " to "
						+ mapping.getValue().toString());
			}
		} else {
			getLogger().info("Skipping directory migration");
		}
		processSiteResources(descriptor);
		result.setResultCode(MigrationResult.SUCCESS);
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	protected Map mapSourceAndDestinationDirectories(final MigrationDescriptor descriptor) {
		final Map directoryMap = new HashMap();
		if (StringUtils.isNotEmpty(descriptor.getSourceDirectory())
			&& !DEFAULT_SOURCE_DIRECTORY.equals(descriptor.getSourceDirectory())) {
			directoryMap.put(descriptor.getSourceDirectory(), DEFAULT_SOURCE_DIRECTORY);
		}
		if (StringUtils.isNotEmpty(descriptor.getResourcesDirectory())
			&& !DEFAULT_RESOURCE_DIRECTORY.equals(descriptor.getResourcesDirectory())) {
			directoryMap.put(descriptor.getResourcesDirectory(), DEFAULT_RESOURCE_DIRECTORY);
		}
		if (StringUtils.isNotEmpty(descriptor.getTestSourceDirectory())
			&& !DEFAULT_TEST_SOURCE_DIRECTORY.equals(descriptor.getTestSourceDirectory())) {
			directoryMap.put(descriptor.getTestSourceDirectory(), DEFAULT_TEST_SOURCE_DIRECTORY);
		}
		if (StringUtils.isNotEmpty(descriptor.getTestResourcesDirectory())
			&& !DEFAULT_TEST_RESOURCE_DIRECTORY.equals(descriptor.getTestResourcesDirectory())) {
			directoryMap.put(descriptor.getTestResourcesDirectory(), DEFAULT_TEST_RESOURCE_DIRECTORY);
		}
		if (StringUtils.isNotEmpty(descriptor.getWebappDirectory())
			&& !DEFAULT_WEBAPP_DIRECTORY.equals(descriptor.getWebappDirectory())) {
			directoryMap.put(descriptor.getWebappDirectory(), DEFAULT_WEBAPP_DIRECTORY);
		} else if (StringUtils.isNotEmpty(descriptor.getEarAppDirectory())
			&& !DEFAULT_EARAPP_DIRECTORY.equals(descriptor.getEarAppDirectory())) {
			directoryMap.put(descriptor.getEarAppDirectory(), DEFAULT_EARAPP_DIRECTORY);
		}
		return directoryMap;
	}

	/**
	 * {@inheritDoc}
	 */
	protected void processSiteResources(final MigrationDescriptor descriptor) throws MigrationExecutionException {
		getLogger().info("Copying site resources for default Maven documentation site");
		final File basedir = new File(descriptor.getWorkingDirectory(), DEFAULT_SITE_DIRECTORY);
		for (int i = 0; i < SITE_RESOURCES.length; i++) {
			final String resource = SITE_RESOURCES[i];
			final File file = new File(basedir, resource.replaceAll("#/#", File.separator));
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			final InputStream inputStream = getResourceStream("site-resources/" + resource, null);
			OutputStream outputStream = null;
			try {
				outputStream = new FileOutputStream(file);
				IOUtil.copy(inputStream, outputStream);
			} catch (Exception e) {
				throw new MigrationExecutionException("Failed to copy site resource", e);
			} finally {
				IOUtil.close(inputStream);
				IOUtil.close(outputStream);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy