/**
 * Copyright 2014 Ben Navetta
 *
 * Licensed 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.bennavetta.vetinari.build;

import com.bennavetta.vetinari.Site;
import com.bennavetta.vetinari.VetinariContext;
import com.bennavetta.vetinari.VetinariException;
import com.bennavetta.vetinari.parse.SiteLoader;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Build a site by applying {@link BuildPhase}s in order.
 */
@Singleton
@Slf4j
public class SiteBuilder
{
	private final SiteLoader siteLoader;
	private final VetinariContext context;

	private final Set<BuildPhase> phases;

	@Inject
	public SiteBuilder(SiteLoader siteLoader, VetinariContext context, Set<BuildPhase> phases)
	{
		this.siteLoader = siteLoader;
		this.context = context;
		this.phases = phases;
	}

	/**
	 * Build the site. The site loaded from a {@link SiteLoader} is passed through the ordered sequence
	 * of build phases, each receiving the result of the previous.
	 * @throws Exception if any phase throws an exception, or there is an error loading the site
	 */
	public void build() throws VetinariException
	{
		Site site = siteLoader.load(context);

		List<BuildPhase> runList = context.getSiteConfig().getStringList("phases").stream()
			.map(name -> Iterables.find(phases, phase -> name.equals(phase.getName())))
			.collect(Collectors.toList());

		for(BuildPhase phase : runList)
		{
			log.info("Running phase {}", phase.getName());
			site = phase.process(site);
		}
		// File output should be a phase, so we don't have to handle it here.
	}
}

@@ -629,16 +629,162 @@
 build;%0A%0A
+import java.util.List;%0Aimport java.util.Set;%0Aimport java.util.stream.Collectors;%0A%0Aimport javax.inject.Inject;%0A%0Aimport lombok.extern.slf4j.Slf4j;%0A%0A
 import c
@@ -1025,240 +1025,24 @@
 gle.
-common.collect.Lists;%0Aimport com.google.inject.Singleton;%0A%0Aimport lombok.extern.slf4j.Slf4j;%0A%0Aimport javax.inject.Inject;%0A%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport java.util.stream.Collectors
+inject.Singleton
 ;%0A%0A/
@@ -1817,16 +1817,19 @@
 ontext);
+%0A%09%09
 %0A%0A%09%09List

reorganize imports