refactored out the KDocLoader stuff from the KotlinCompiler; also introduced CompilerPluginContext to wrap up the Project, BindingContext and List<JetFiles> in case a plugin were to need any of those things (or we added extra stuff later like the environment)

This commit is contained in:
James Strachan
2012-03-29 16:35:58 +01:00
parent ed04839901
commit beedd36456
14 changed files with 108 additions and 199 deletions
@@ -32,9 +32,6 @@ public class CompilerArguments {
@Argument(value = "output", description = "output directory")
public String outputDir;
@Argument(value = "docOutput", description = "KDoc output directory")
public String docOutputDir;
@Argument(value = "jar", description = "jar file name")
public String jar;
@@ -77,14 +74,6 @@ public class CompilerArguments {
this.classpath = classpath;
}
public String getDocOutputDir() {
return docOutputDir;
}
public void setDocOutputDir(String docOutputDir) {
this.docOutputDir = docOutputDir;
}
public boolean isHelp() {
return help;
}
@@ -1,100 +0,0 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 org.jetbrains.jet.cli;
import com.google.common.collect.Lists;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.Disposable;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.CompilerPlugin;
import org.jetbrains.jet.compiler.JetCoreEnvironment;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.resolve.DescriptorRenderer;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.List;
/**
* A simple facade to auto-detect the KDoc processor if its available on the classpath
*/
public class KDocLoader {
private final String outputDir;
public KDocLoader(String outputDir) {
this.outputDir = outputDir;
}
public CompilerPlugin createCompilerPlugin() {
// lets see if we can see the KDoc class
String name = "org.jetbrains.kotlin.doc.KDoc";
Class<?> aClass = null;
try {
aClass = loadClass(name);
} catch (ClassNotFoundException e) {
System.out.println("Could not find class: " + name);
return null;
}
if (aClass != null) {
try {
File dir = new File(outputDir);
Constructor<?> constructor = aClass.getConstructor(File.class);
if (constructor != null) {
return (CompilerPlugin) constructor.newInstance(dir);
}
} catch (Exception e) {
System.out.println("Failed to create Processor: " + e);
}
}
return null;
}
public static Class<?> loadClass(String name) throws ClassNotFoundException {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name);
} catch (ClassNotFoundException e1) {
return KDocLoader.class.getClassLoader().loadClass(name);
}
}
}
/**
* Installs the KDoc compiler plugin if it can be created
*/
public static boolean install(String docOutputDir, JetCoreEnvironment environment) {
KDocLoader loader = new KDocLoader(docOutputDir);
CompilerPlugin processor = loader.createCompilerPlugin();
if (processor != null) {
environment.getCompilerPlugins().add(processor);
return true;
} else {
return false;
}
}
}
@@ -166,13 +166,13 @@ public class KotlinCompiler {
environment.setStubs(arguments.stubs);
if (arguments.docOutputDir != null) {
KDocLoader.install(arguments.docOutputDir, environment.getEnvironment());
KDocLoader.install(arguments.docOutputDir, environment.getMyEnvironment());
}
// install any compiler plugins
List<CompilerPlugin> plugins = arguments.getCompilerPlugins();
if (plugins != null) {
environment.getEnvironment().getCompilerPlugins().addAll(plugins);
environment.getMyEnvironment().getCompilerPlugins().addAll(plugins);
}
if (arguments.stdlib != null) {
@@ -1,11 +1,9 @@
/**
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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
* 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
*
@@ -15,17 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.compiler;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.List;
/**
* A simple interface for compiler plugins to run after the compiler has finished such as for things like
* generating documentation or code generation etc
*/
public interface CompilerPlugin {
void processFiles(BindingContext context, List<JetFile> sources);
void processFiles(CompilerPluginContext context);
}
@@ -0,0 +1,51 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.compiler;
import com.intellij.openapi.project.Project;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.List;
/**
* Represents the context of available state in which a {@link CompilerPlugin} runs such as
* the {@link Project}, the {@link BindingContext} and the underlying {@link JetFile} files.
*/
public class CompilerPluginContext {
private final Project project;
private final BindingContext context;
private final List<JetFile> files;
public CompilerPluginContext(Project project, BindingContext context, List<JetFile> files) {
this.project = project;
this.context = context;
this.files = files;
}
public BindingContext getContext() {
return context;
}
public List<JetFile> getFiles() {
return files;
}
public Project getProject() {
return project;
}
}