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

li.rudin.mavenjs.plugin.compile.CompilerVisitor Maven / Gradle / Ivy

package li.rudin.mavenjs.plugin.compile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import li.rudin.mavenjs.api.Compiler;
import li.rudin.mavenjs.api.CompilerLoader;
import li.rudin.mavenjs.servlet.StreamUtil;

import org.apache.maven.plugin.logging.Log;

/**
 * http://stackoverflow.com/questions/6214703/copy-entire-directory-contents-to-another-directory
 * @author user
 *
 */
public class CompilerVisitor extends SimpleFileVisitor
{
	private final Log logger;
	
	public CompilerVisitor(Log logger) throws IOException
	{
		this.logger = logger;
	}

	
	@Override
	public FileVisitResult visitFile(final Path sourceFile, final BasicFileAttributes attrs) throws IOException
	{
		for (Compiler compiler: CompilerLoader.compilers)
		{
			if (sourceFile.toString().endsWith(compiler.getSourceExtension()))
			{
				//File can be compiled
				
				Path targetFile = Paths.get(
						sourceFile.toString().substring(
								0,
								sourceFile.toString().length()-compiler.getSourceExtension().length()) + compiler.getTargetExtension());
				
				
				logger.info(sourceFile + " -> " + targetFile);
				
				
				try (
						FileInputStream inputStream = new FileInputStream(sourceFile.toFile());
						FileOutputStream outputStream = new FileOutputStream(targetFile.toFile());
						)
				{
					String txt = StreamUtil.streamToString(inputStream);
					String js = compiler.compile(sourceFile.toString(), txt);
					outputStream.write(js.getBytes());
					
					//TODO: delete file
				}
				catch (Exception e)
				{
					throw new IOException("compile", e);
				}

			}
			
		}
		
		
		return FileVisitResult.CONTINUE;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy