org.jsweet.transpiler.extension.ExtensionManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsweet-transpiler Show documentation
Show all versions of jsweet-transpiler Show documentation
A Java to TypeScript/JavaScript Open Transpiler
The newest version!
/*
* JSweet transpiler - http://www.jsweet.org
* Copyright (C) 2015 CINCHEO SAS
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jsweet.transpiler.extension;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.jsweet.JSweetConfig;
import org.jsweet.transpiler.SourceFile;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* This class deals with the extension directory, which can contain classes to
* tune the transpiler's default behavior.
*
* @author Renaud Pawlak
*/
public class ExtensionManager {
private final static Logger logger = Logger.getLogger(ExtensionManager.class);
private File extensionDir;
/**
* Creates a new extension manager for the given directory.
*/
public ExtensionManager(String extensionDirPath) {
extensionDir = new File(extensionDirPath);
}
/**
* Checks if some Java source files are to be compiled in the extension
* directory, and compile them if necessary.
*/
public void checkAndCompileExtension(File workingDir, String compileClassPath) {
if (!extensionDir.exists()) {
return;
}
logger.info("checking extension (working dir = " + workingDir + ")");
initExtensionClassPath();
SourceFile[] sourceFiles = SourceFile.getSourceFiles(extensionDir);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List optionList = new ArrayList();
optionList.addAll(Arrays.asList("-classpath", compileClassPath, "-parameters", "-g"));
File extensionDescriptorFile = new File(workingDir, "extension.json");
Map extensionDescriptor = new HashMap<>();
try {
extensionDescriptor = new Gson().fromJson(FileUtils.readFileToString(extensionDescriptorFile),
new TypeToken