CLI: added "-printArgs" arguments to commandline compiler.
This commit is contained in:
+7
@@ -33,6 +33,8 @@ public abstract class CommonCompilerArguments extends CompilerArguments {
|
||||
public boolean help;
|
||||
@Argument(value = "suppress", description = "Suppress compiler messages by severity (warnings)")
|
||||
public String suppress;
|
||||
@Argument(value = "printArgs", description = "Print commandline arguments")
|
||||
public boolean printArgs;
|
||||
|
||||
@Override
|
||||
public boolean isHelp() {
|
||||
@@ -58,6 +60,11 @@ public abstract class CommonCompilerArguments extends CompilerArguments {
|
||||
return verbose;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrintArgs() {
|
||||
return printArgs;
|
||||
}
|
||||
|
||||
public void setTags(boolean tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
+1
@@ -27,6 +27,7 @@ public abstract class CompilerArguments {
|
||||
public abstract boolean isTags();
|
||||
public abstract boolean isVersion();
|
||||
public abstract boolean isVerbose();
|
||||
public abstract boolean isPrintArgs();
|
||||
|
||||
public abstract String getSrc();
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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 com.sampullara.cli;
|
||||
|
||||
import com.intellij.util.containers.ComparatorUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class ArgumentUtils {
|
||||
|
||||
private ArgumentUtils() {}
|
||||
|
||||
@NotNull
|
||||
public static <T> String convertArgumentsToString(T arguments, T defaultArguments) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
convertArgumentsToString(arguments, defaultArguments, arguments.getClass(), result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static <T> void convertArgumentsToString(T arguments, T defaultArguments, Class clazz, StringBuilder result) {
|
||||
Class superClazz = clazz.getSuperclass();
|
||||
if (superClazz != null) {
|
||||
convertArgumentsToString(arguments, defaultArguments, superClazz, result);
|
||||
}
|
||||
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
Argument argument = field.getAnnotation(Argument.class);
|
||||
if (argument == null) continue;
|
||||
|
||||
Object value;
|
||||
Object defaultValue;
|
||||
try {
|
||||
value = field.get(arguments);
|
||||
defaultValue = field.get(defaultArguments);
|
||||
}
|
||||
catch (IllegalAccessException ignored) {
|
||||
// skip this field
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ComparatorUtil.equalsNullable(value, defaultValue)) continue;
|
||||
|
||||
String name = Args.getAlias(argument);
|
||||
if (name == null) {
|
||||
name = Args.getName(argument, field);
|
||||
}
|
||||
|
||||
result.append("-").append(name).append(" ");
|
||||
|
||||
Class<?> fieldType = field.getType();
|
||||
if (fieldType != boolean.class && fieldType != Boolean.class) {
|
||||
result.append(value).append(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,9 @@ import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.sampullara.cli.Args;
|
||||
import com.sampullara.cli.ArgumentUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.cli.common.arguments.CompilerArguments;
|
||||
import org.jetbrains.jet.cli.common.messages.*;
|
||||
@@ -118,6 +120,7 @@ public abstract class CLICompiler<A extends CompilerArguments> {
|
||||
MessageRenderer messageRenderer = getMessageRenderer(arguments);
|
||||
errStream.print(messageRenderer.renderPreamble());
|
||||
|
||||
printArgumentsIfNeeded(errStream, arguments, messageRenderer);
|
||||
printVersionIfNeeded(errStream, arguments, messageRenderer);
|
||||
|
||||
MessageCollector collector = new PrintingMessageCollector(errStream, messageRenderer, arguments.isVerbose());
|
||||
@@ -167,9 +170,11 @@ public abstract class CLICompiler<A extends CompilerArguments> {
|
||||
return arguments.isTags() ? MessageRenderer.TAGS : MessageRenderer.PLAIN;
|
||||
}
|
||||
|
||||
protected void printVersionIfNeeded(@NotNull PrintStream errStream,
|
||||
protected void printVersionIfNeeded(
|
||||
@NotNull PrintStream errStream,
|
||||
@NotNull A arguments,
|
||||
@NotNull MessageRenderer messageRenderer) {
|
||||
@NotNull MessageRenderer messageRenderer
|
||||
) {
|
||||
if (arguments.isVersion()) {
|
||||
String versionMessage = messageRenderer.render(CompilerMessageSeverity.INFO,
|
||||
"Kotlin Compiler version " + KotlinVersion.VERSION,
|
||||
@@ -178,6 +183,22 @@ public abstract class CLICompiler<A extends CompilerArguments> {
|
||||
}
|
||||
}
|
||||
|
||||
private void printArgumentsIfNeeded(
|
||||
@NotNull PrintStream errStream,
|
||||
@NotNull A arguments,
|
||||
@NotNull MessageRenderer messageRenderer
|
||||
) {
|
||||
if (arguments.isPrintArgs()) {
|
||||
String freeArgs = StringUtil.join(arguments.freeArgs, "");
|
||||
String argumentsAsString = ArgumentUtils.convertArgumentsToString(arguments, createArguments());
|
||||
String printArgsMessage = messageRenderer.render(CompilerMessageSeverity.INFO,
|
||||
"Invoking compiler " + getClass().getName() +
|
||||
" with arguments " + argumentsAsString + freeArgs,
|
||||
CompilerMessageLocation.NO_LOCATION);
|
||||
errStream.println(printArgsMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful main for derived command line tools
|
||||
*/
|
||||
@@ -198,12 +219,6 @@ public abstract class CLICompiler<A extends CompilerArguments> {
|
||||
if (rc != OK) {
|
||||
System.err.println("exec() finished with " + rc + " return code");
|
||||
}
|
||||
if (Boolean.parseBoolean(System.getProperty("kotlin.print.cmd.args"))) {
|
||||
System.out.println("Command line arguments:");
|
||||
for (String arg : args) {
|
||||
System.out.println(arg);
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
catch (CompileEnvironmentException e) {
|
||||
|
||||
@@ -18,4 +18,5 @@ Usage: org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments
|
||||
-version [flag] Display compiler version
|
||||
-help (-h) [flag] Show help
|
||||
-suppress [String] Suppress compiler messages by severity (warnings)
|
||||
-printArgs [flag] Print commandline arguments
|
||||
OK
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
INFO: Invoking compiler org.jetbrains.jet.cli.jvm.K2JVMCompiler with arguments -printArgs -script compiler/testData/cli/hello.ktscript
|
||||
hello
|
||||
Command line arguments:
|
||||
-script
|
||||
compiler/testData/cli/hello.ktscript
|
||||
OK
|
||||
|
||||
@@ -19,4 +19,5 @@ Usage: org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments
|
||||
-version [flag] Display compiler version
|
||||
-help (-h) [flag] Show help
|
||||
-suppress [String] Suppress compiler messages by severity (warnings)
|
||||
-printArgs [flag] Print commandline arguments
|
||||
INTERNAL_ERROR
|
||||
|
||||
@@ -144,13 +144,7 @@ public class CliTest {
|
||||
|
||||
@Test
|
||||
public void printArguments() {
|
||||
try {
|
||||
System.setProperty("kotlin.print.cmd.args", "true");
|
||||
executeCompilerCompareOutputJVM(new String[] {"-script", "compiler/testData/cli/hello.ktscript"});
|
||||
}
|
||||
finally {
|
||||
System.clearProperty("kotlin.print.cmd.args");
|
||||
}
|
||||
executeCompilerCompareOutputJVM(new String[] {"-printArgs", "-script", "compiler/testData/cli/hello.ktscript"});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user