This commit is contained in:
konsoletyper 2014-09-21 19:24:48 +04:00
parent 50bba7b9e0
commit eccac6a9e2
9 changed files with 212 additions and 36 deletions

View File

@ -7,7 +7,9 @@ Bundle-Vendor: Alexey Andreev <konsoletyper@gmail.com>
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.teavm.eclipse;bundle-version="[0.2,0.3)",
org.eclipse.m2e.core;bundle-version="[1.3,2)",
org.eclipse.core.runtime;bundle-version="[3.8.0,4.0)",
org.eclipse.core.runtime;bundle-version="[3.8,4.0)",
org.eclipse.m2e.maven.runtime;bundle-version="[1.3,2)",
org.eclipse.core.resources;bundle-version="[3.6,4)",
org.eclipse.ui;bundle-version="[3.6,4.0)"
org.eclipse.core.variables;bundle-version="[3.2,4)",
org.eclipse.ui;bundle-version="[3.6,4.0)",
org.eclipse.jdt.core;bundle-version="[3.8,4.0.0)"

View File

@ -1,14 +1,22 @@
package org.teavm.eclipse.m2e;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator;
@ -16,12 +24,14 @@ import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest;
import org.teavm.eclipse.TeaVMEclipsePlugin;
import org.teavm.eclipse.TeaVMProfile;
import org.teavm.eclipse.TeaVMProjectSettings;
import org.teavm.eclipse.TeaVMRuntimeMode;
/**
*
* @author Alexey Andreev
*/
public class TeaVMProjectConfigurator extends AbstractProjectConfigurator {
private static final String TOOL_ID = "teavm-eclipse-m2e-plugin.tool";
private static final String TEAVM_ARTIFACT_ID = "teavm-maven-plugin";
private static final String TEAVM_GROUP_ID = "org.teavm";
private static final String TEAVM_MAIN_GOAL = "build-javascript";
@ -29,6 +39,7 @@ public class TeaVMProjectConfigurator extends AbstractProjectConfigurator {
private Set<String> usedExecutionIds = new HashSet<>();
private IMaven maven;
private MavenProject mavenProject;
private IProject project;
@Override
public void configure(ProjectConfigurationRequest configurationRequest, IProgressMonitor monitor)
@ -38,7 +49,7 @@ public class TeaVMProjectConfigurator extends AbstractProjectConfigurator {
List<MojoExecution> executions = configurationRequest.getMavenProjectFacade().getMojoExecutions(
TEAVM_GROUP_ID, TEAVM_ARTIFACT_ID, monitor, TEAVM_MAIN_GOAL);
TeaVMEclipsePlugin teaVMPlugin = TeaVMEclipsePlugin.getDefault();
IProject project = configurationRequest.getProject();
project = configurationRequest.getProject();
boolean hasNature = project.hasNature(TeaVMEclipsePlugin.NATURE_ID);
int sz = executions.size();
if (!hasNature) {
@ -48,21 +59,29 @@ public class TeaVMProjectConfigurator extends AbstractProjectConfigurator {
TeaVMProjectSettings settings = teaVMPlugin.getSettings(project);
settings.load();
try {
Set<String> coveredProfiles = new HashSet<>();
for (MojoExecution execution : executions) {
if (monitor.isCanceled()) {
return;
}
String profileId = getIdForProfile(execution);
coveredProfiles.add(profileId);
TeaVMProfile profile = settings.getProfile(profileId);
if (profile == null) {
profile = settings.createProfile();
profile.setName(profileId);
}
profile.setExternalToolId(TOOL_ID);
configureProfile(execution, profile, new SubProgressMonitor(monitor, 1000));
if (monitor.isCanceled()) {
return;
}
}
for (TeaVMProfile profile : settings.getProfiles()) {
if (!coveredProfiles.contains(profile.getName()) && profile.getExternalToolId().equals(TOOL_ID)) {
settings.deleteProfile(profile);
}
}
if (!hasNature) {
teaVMPlugin.addNature(new SubProgressMonitor(monitor, 1000), project);
}
@ -74,19 +93,117 @@ public class TeaVMProjectConfigurator extends AbstractProjectConfigurator {
private void configureProfile(MojoExecution execution, TeaVMProfile profile, IProgressMonitor monitor)
throws CoreException {
monitor.beginTask("Configuring profile " + profile.getName(), 30);
monitor.beginTask("Configuring profile " + profile.getName(), 110);
String buildDir = getProjectBuildDirectory();
String mainClass = maven.getMojoParameterValue(mavenProject, execution, "mainClass", String.class,
new SubProgressMonitor(monitor, 10));
profile.setMainClass(mainClass);
String targetDir = maven.getMojoParameterValue(mavenProject, execution, "targetDirectory", String.class,
new SubProgressMonitor(monitor, 10));
profile.setTargetDirectory(targetDir);
profile.setTargetDirectory(targetDir != null ? absolutePathToWorkspacePath(targetDir) :
buildDir + "/javascript");
String targetFileName = maven.getMojoParameterValue(mavenProject, execution, "targetFileName", String.class,
new SubProgressMonitor(monitor, 10));
profile.setTargetFileName(targetFileName != null ? targetFileName : "classes.js");
Boolean minifying = maven.getMojoParameterValue(mavenProject, execution, "minifying", Boolean.class,
new SubProgressMonitor(monitor, 10));
profile.setMinifying(minifying != null ? minifying : true);
String runtime = maven.getMojoParameterValue(mavenProject, execution, "runtime", String.class,
new SubProgressMonitor(monitor, 10));
profile.setRuntimeMode(runtime != null ? getRuntimeMode(runtime) : TeaVMRuntimeMode.SEPARATE);
Properties properties = maven.getMojoParameterValue(mavenProject, execution, "properties", Properties.class,
new SubProgressMonitor(monitor, 10));
profile.setProperties(properties != null ? properties : new Properties());
Boolean debug = maven.getMojoParameterValue(mavenProject, execution, "debugInformationGenerated",
Boolean.class, new SubProgressMonitor(monitor, 10));
profile.setDebugInformationGenerated(debug != null ? debug : false);
Boolean sourceMaps = maven.getMojoParameterValue(mavenProject, execution, "sourceMapsGenerated",
Boolean.class, new SubProgressMonitor(monitor, 10));
profile.setSourceMapsGenerated(sourceMaps != null ? sourceMaps : false);
Boolean incremental = maven.getMojoParameterValue(mavenProject, execution, "incremental", Boolean.class,
new SubProgressMonitor(monitor, 10));
profile.setIncremental(incremental != null ? incremental : false);
String cacheDir = maven.getMojoParameterValue(mavenProject, execution, "cacheDirectory", String.class,
new SubProgressMonitor(monitor, 10));
profile.setCacheDirectory(cacheDir != null ? absolutePathToWorkspacePath(cacheDir) :
buildDir + "/teavm-cache");
String[] transformers = maven.getMojoParameterValue(mavenProject, execution, "transformers", String[].class,
new SubProgressMonitor(monitor, 10));
profile.setTransformers(transformers != null ? transformers : new String[0]);
profile.setClassAliases(readClassAliases(execution));
monitor.done();
}
private Map<String, String> readClassAliases(MojoExecution execution) {
Map<String, String> aliases = new HashMap<>();
Xpp3Dom aliasesElem = execution.getConfiguration().getChild("classAliases");
if (aliasesElem != null) {
for (Xpp3Dom item : aliasesElem.getChildren()) {
String className = item.getChild("className").getValue();
String alias = item.getChild("alias").getValue();
aliases.put(className, alias);
}
}
return aliases;
}
private String getProjectBuildDirectory() throws CoreException {
IStringVariableManager varManager = VariablesPlugin.getDefault().getStringVariableManager();
if (!project.hasNature(JavaCore.NATURE_ID)) {
return varManager.generateVariableExpression("workspace_loc", "/" + project.getName());
}
IJavaProject javaProject = JavaCore.create(project);
String path = javaProject.getOutputLocation().toString();
return varManager.generateVariableExpression("workspace_loc", path);
}
private String absolutePathToWorkspacePath(String path) {
try {
IStringVariableManager varManager = VariablesPlugin.getDefault().getStringVariableManager();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IContainer[] containers = root.findContainersForLocationURI(new URI("file://" + path));
if (containers.length == 0) {
return null;
}
IContainer container = containers[0];
String suffix = "";
while (!(container instanceof IProject)) {
suffix = "/" + container.getName() + suffix;
container = container.getParent();
}
path = container.getFullPath().toString();
return varManager.generateVariableExpression("workspace_loc", path) + suffix;
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
private TeaVMRuntimeMode getRuntimeMode(String name) {
switch (name) {
case "SEPARATE":
return TeaVMRuntimeMode.SEPARATE;
case "MERGED":
return TeaVMRuntimeMode.MERGE;
case "NONE":
return TeaVMRuntimeMode.NONE;
default:
return TeaVMRuntimeMode.NONE;
}
}
private String getIdForProfile(MojoExecution pluginExecution) {
String executionId = pluginExecution.getExecutionId();
if (executionId != null && usedExecutionIds.add(executionId)) {

View File

@ -6,22 +6,22 @@ Bundle-Version: 0.2.0.qualifer
Bundle-Vendor: Alexey Andreev <konsoletyper@gmail.com>
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-Activator: org.teavm.eclipse.TeaVMEclipsePlugin
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.8.0,4.0)",
org.eclipse.debug.core;bundle-version="[3.7.0,4.0)",
org.eclipse.debug.ui;bundle-version="[3.8.0,4.0)",
org.eclipse.swt;bundle-version="[3.8.1,4.0)",
org.eclipse.ui;bundle-version="[3.7.1,4.0)",
org.eclipse.jdt.debug;bundle-version="[3.7.101,4.0.0)",
org.eclipse.jdt.debug.ui;bundle-version="[3.6.100,4.0.0)",
org.eclipse.jdt.core;bundle-version="[3.8.3,4.0.0)",
org.eclipse.jdt.launching;bundle-version="[3.6.1,4.0.0)",
org.eclipse.ui.editors;bundle-version="[3.8.0,4.0.0)",
org.eclipse.ui.ide;bundle-version="[3.8.2,4.0.0)",
org.eclipse.jdt.ui;bundle-version="[3.8.2,4.0.0)",
org.eclipse.core.filesystem;bundle-version="[1.3.200,2)",
org.eclipse.core.variables;bundle-version="[3.2.600,4)",
org.eclipse.core.databinding.observable;bundle-version="[1.4.1,2)",
org.eclipse.jface.databinding;bundle-version="[1.6.0,2)"
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.8,4.0)",
org.eclipse.debug.core;bundle-version="[3.7,4.0)",
org.eclipse.debug.ui;bundle-version="[3.8,4.0)",
org.eclipse.swt;bundle-version="[3.8,4.0)",
org.eclipse.ui;bundle-version="[3.7,4.0)",
org.eclipse.jdt.debug;bundle-version="[3.7,4.0.0)",
org.eclipse.jdt.debug.ui;bundle-version="[3.6,4.0.0)",
org.eclipse.jdt.core;bundle-version="[3.8,4.0.0)",
org.eclipse.jdt.launching;bundle-version="[3.6,4.0.0)",
org.eclipse.ui.editors;bundle-version="[3.8,4.0.0)",
org.eclipse.ui.ide;bundle-version="[3.8,4.0.0)",
org.eclipse.jdt.ui;bundle-version="[3.8,4.0.0)",
org.eclipse.core.filesystem;bundle-version="[1.3,2)",
org.eclipse.core.variables;bundle-version="[3.2,4)",
org.eclipse.core.databinding.observable;bundle-version="[1.4,2)",
org.eclipse.jface.databinding;bundle-version="[1.6,2)"
Bundle-ClassPath: .,
lib/asm-5.0.1.jar,
lib/asm-commons-5.0.1.jar,

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2014 Alexey Andreev.
@ -13,7 +14,6 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

View File

@ -43,6 +43,7 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
public static final String PROPERTIES = "properties";
public static final String CLASSES = "classes";
public static final String TRANSFORMERS = "transformers";
public static final String EXTERNAL_TOOL_ID = "externalTool";
private static final String NEW_PROFILE_NAME = "New profile";
private List<ProfileImpl> profiles = new ArrayList<>();
@ -71,7 +72,7 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
if (profileMap.get(profile.getName()) != profile) {
return;
}
profileMap.remove(profile);
profileMap.remove(profile.getName());
profiles.remove(profile);
}
@ -109,7 +110,8 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
}
for (String key : globalPreferences.childrenNames()) {
if (!profileMap.containsKey(key)) {
globalPreferences.node(key).removeNode();
Preferences node = globalPreferences.node(key);
node.removeNode();
}
}
globalPreferences.flush();
@ -160,7 +162,8 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
private boolean debugInformationGenerated;
private Properties properties = new Properties();
private String[] transformers = new String[0];
private Map<String, String> classeAliases = new HashMap<>();
private Map<String, String> classAliases = new HashMap<>();
private String externalToolId = "";
@Override
public String getName() {
@ -293,12 +296,12 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
@Override
public Map<String, String> getClassAliases() {
return new HashMap<>(classeAliases);
return new HashMap<>(classAliases);
}
@Override
public void setClassAliases(Map<String, String> classAliases) {
this.classeAliases = new HashMap<>(classAliases);
this.classAliases = new HashMap<>(classAliases);
}
@Override
@ -311,6 +314,16 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
this.transformers = transformers.clone();
}
@Override
public String getExternalToolId() {
return externalToolId;
}
@Override
public void setExternalToolId(String toolId) {
this.externalToolId = toolId;
}
public void load() throws BackingStoreException {
preferences.sync();
enabled = preferences.getBoolean(ENABLED, true);
@ -335,8 +348,9 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
Preferences classesPrefs = preferences.node(CLASSES);
classesPrefs.sync();
for (String key : classesPrefs.keys()) {
classeAliases.put(key, classesPrefs.get(key, "_"));
classAliases.put(key, classesPrefs.get(key, "_"));
}
externalToolId = preferences.get(EXTERNAL_TOOL_ID, "");
}
public void save() throws BackingStoreException {
@ -365,10 +379,11 @@ public class PreferencesBasedTeaVMProjectSettings implements TeaVMProjectSetting
transformersPrefs.flush();
Preferences classesPrefs = preferences.node(CLASSES);
classesPrefs.clear();
for (String key : classeAliases.keySet()) {
classesPrefs.put(key, classeAliases.get(key));
for (String key : classAliases.keySet()) {
classesPrefs.put(key, classAliases.get(key));
}
classesPrefs.flush();
preferences.put(EXTERNAL_TOOL_ID, externalToolId);
preferences.flush();
}
}

View File

@ -78,4 +78,8 @@ public interface TeaVMProfile {
Map<String, String> getClassAliases();
void setClassAliases(Map<String, String> classAliases);
String getExternalToolId();
void setExternalToolId(String toolId);
}

View File

@ -99,7 +99,7 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder {
tool.setDebugInformationGenerated(profile.isDebugInformationGenerated());
tool.setSourceMapsFileGenerated(profile.isSourceMapsGenerated());
String targetDir = profile.getTargetDirectory();
tool.setTargetDirectory(new File(varManager.performStringSubstitution(targetDir)));
tool.setTargetDirectory(new File(varManager.performStringSubstitution(targetDir, false)));
tool.setTargetFileName(profile.getTargetFileName());
tool.setMinifying(profile.isMinifying());
tool.setRuntime(mapRuntime(profile.getRuntimeMode()));
@ -107,7 +107,8 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder {
tool.getProperties().putAll(profile.getProperties());
tool.setIncremental(profile.isIncremental());
String cacheDir = profile.getCacheDirectory();
tool.setCacheDirectory(!cacheDir.isEmpty() ? new File(varManager.performStringSubstitution(cacheDir)) : null);
tool.setCacheDirectory(!cacheDir.isEmpty() ?
new File(varManager.performStringSubstitution(cacheDir, false)) : null);
for (ClassHolderTransformer transformer : instantiateTransformers(profile, classLoader)) {
tool.getTransformers().add(transformer);
}

View File

@ -53,6 +53,7 @@ import org.teavm.eclipse.TeaVMRuntimeMode;
public class TeaVMProfileDialog extends Dialog {
private static List<TeaVMRuntimeMode> runtimeModes = Arrays.asList(TeaVMRuntimeMode.SEPARATE,
TeaVMRuntimeMode.MERGE, TeaVMRuntimeMode.NONE);
private TabFolder tabFolder;
private Text nameField;
private Text mainClassField;
private Button mainClassChooseButton;
@ -107,7 +108,7 @@ public class TeaVMProfileDialog extends Dialog {
area.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ScrolledComposite scrollContainer = new ScrolledComposite(area, SWT.V_SCROLL | SWT.H_SCROLL);
scrollContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TabFolder tabFolder = new TabFolder(scrollContainer, SWT.TOP);
tabFolder = new TabFolder(scrollContainer, SWT.TOP);
scrollContainer.setContent(tabFolder);
scrollContainer.setExpandHorizontal(true);
scrollContainer.setExpandVertical(true);
@ -644,6 +645,18 @@ public class TeaVMProfileDialog extends Dialog {
cacheDirectoryWorkspaceButton.setEnabled(incrementalButton.getSelection());
}
private static void setEnabledRecursive(Composite composite, boolean enabled) {
Control[] children = composite.getChildren();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof Composite) {
setEnabledRecursive((Composite) children[i], enabled);
} else {
children[i].setEnabled(enabled);
}
}
composite.setEnabled(enabled);
}
private void load() {
nameField.setText(profile.getName());
mainClassField.setText(profile.getMainClass() != null ? profile.getMainClass() : "");
@ -671,6 +684,11 @@ public class TeaVMProfileDialog extends Dialog {
row.alias = entry.getValue();
classAliases.add(row);
}
for (Control control : tabFolder.getTabList()) {
if (control instanceof Composite) {
setEnabledRecursive((Composite)control, profile.getExternalToolId().isEmpty());
}
}
}
private boolean save() {

View File

@ -99,6 +99,11 @@ public class TeaVMProjectPropertyPage extends PropertyPage implements IWorkbench
TableColumn fileColumn = new TableColumn(profilesTable, SWT.LEFT);
fileColumn.setText("Target file");
fileColumn.setWidth(150);
profilesTable.addSelectionListener(new SelectionAdapter() {
@Override public void widgetSelected(SelectionEvent e) {
updateTableSelection();
}
});
addProfileButton = new Button(container, SWT.PUSH);
addProfileButton.setText("Add...");
@ -130,6 +135,16 @@ public class TeaVMProjectPropertyPage extends PropertyPage implements IWorkbench
return container;
}
private void updateTableSelection() {
if (profilesTable.getSelectionCount() != 1) {
removeProfileButton.setEnabled(false);
return;
}
TableItem item = profilesTable.getSelection()[0];
TeaVMProfile profile = (TeaVMProfile)item.getData();
removeProfileButton.setEnabled(profile.getExternalToolId().isEmpty());
}
private void loadProfiles() {
try {
settings.load();
@ -197,6 +212,10 @@ public class TeaVMProjectPropertyPage extends PropertyPage implements IWorkbench
return;
}
TableItem item = profilesTable.getSelection()[0];
TeaVMProfile profile = (TeaVMProfile)item.getData();
if (!profile.getExternalToolId().isEmpty()) {
return;
}
boolean confirmed = MessageDialog.openConfirm(getShell(), "Deletion confirmation",
"Are you sure to delete profile " + item.getText(0) + "?");
if (!confirmed) {