Add the support suppressWarnings, verbose and version in build tools.

This commit is contained in:
Zalim Bashorov
2014-10-29 14:44:34 +03:00
parent 70db76b219
commit 9e11b40fe1
40 changed files with 510 additions and 29 deletions
@@ -29,6 +29,8 @@ import org.apache.tools.ant.types.Commandline
import com.sampullara.cli.Args
import java.io.IOException
import org.jetbrains.jet.config
import org.jetbrains.jet.cli.common.messages.PrintingMessageCollector
import org.jetbrains.jet.cli.common.messages.MessageRenderer
/**
* {@code file.getCanonicalPath()} convenience wrapper.
@@ -55,6 +57,9 @@ public abstract class KotlinCompilerBaseTask<T : CommonCompilerArguments> : Task
public var src: Path? = null
public var output: File? = null
public var nowarn: Boolean = false
public var verbose: Boolean = false
public var printVersion: Boolean = false
public val additionalArguments: MutableList<Commandline.Argument> = arrayListOf()
@@ -87,6 +92,10 @@ public abstract class KotlinCompilerBaseTask<T : CommonCompilerArguments> : Task
output ?: throw BuildException("\"output\" should be specified")
arguments.suppressWarnings = nowarn
arguments.verbose = verbose
arguments.version = printVersion
val args = additionalArguments.flatMap { it.getParts()!!.toList() }
try {
Args.parse(arguments, args.copyToArray())
@@ -105,7 +114,8 @@ public abstract class KotlinCompilerBaseTask<T : CommonCompilerArguments> : Task
log("Compiling ${arguments.freeArgs} => [${outputPath}]");
val exitCode = compiler.exec(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR, config.Services.EMPTY, arguments)
val collector = PrintingMessageCollector(System.err, MessageRenderer.PLAIN, arguments.verbose)
val exitCode = compiler.exec(collector, config.Services.EMPTY, arguments)
if (exitCode != ExitCode.OK) {
throw BuildException("Compilation finished with exit code $exitCode")
@@ -116,14 +116,8 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
errStream.print(messageRenderer.renderPreamble());
printVersionIfNeeded(errStream, arguments, messageRenderer);
MessageCollector collector = new PrintingMessageCollector(errStream, messageRenderer, arguments.verbose);
if (arguments.suppressWarnings) {
collector = new FilteringMessageCollector(collector, Predicates.equalTo(CompilerMessageSeverity.WARNING));
}
try {
return exec(collector, services, arguments);
}
@@ -134,6 +128,12 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
@NotNull
public ExitCode exec(@NotNull MessageCollector messageCollector, @NotNull Services services, @NotNull A arguments) {
printVersionIfNeeded(messageCollector, arguments);
if (arguments.suppressWarnings) {
messageCollector = new FilteringMessageCollector(messageCollector, Predicates.equalTo(CompilerMessageSeverity.WARNING));
}
GroupingMessageCollector groupingCollector = new GroupingMessageCollector(messageCollector);
try {
Disposable rootDisposable = Disposer.newDisposable();
@@ -164,17 +164,12 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
@NotNull Disposable rootDisposable
);
protected void printVersionIfNeeded(
@NotNull PrintStream errStream,
@NotNull A arguments,
@NotNull MessageRenderer messageRenderer
) {
if (arguments.version) {
String versionMessage = messageRenderer.render(CompilerMessageSeverity.INFO,
"Kotlin Compiler version " + KotlinVersion.VERSION,
CompilerMessageLocation.NO_LOCATION);
errStream.println(versionMessage);
}
protected void printVersionIfNeeded(@NotNull MessageCollector messageCollector, @NotNull A arguments) {
if (!arguments.version) return;
messageCollector.report(CompilerMessageSeverity.INFO,
"Kotlin Compiler version " + KotlinVersion.VERSION,
CompilerMessageLocation.NO_LOCATION);
}
/**
@@ -11,6 +11,6 @@
<orderEntry type="module" module-name="compiler-tests" />
<orderEntry type="module" module-name="js.tests" scope="TEST" />
<orderEntry type="module" module-name="util" />
<orderEntry type="module" module-name="cli-common" scope="TEST" />
</component>
</module>
</module>
@@ -102,6 +102,21 @@ public class AntTaskJsTest extends AntTaskBaseTest {
doJsAntTest();
}
@Test
public void suppressWarnings() throws Exception {
doJsAntTest();
}
@Test
public void verbose() throws Exception {
doJsAntTest();
}
@Test
public void version() throws Exception {
doJsAntTest();
}
@Test
public void noSrcParam() throws Exception {
@@ -76,6 +76,21 @@ public class AntTaskJvmTest extends AntTaskBaseTest {
doJvmAntTest();
}
@Test
public void suppressWarnings() throws Exception {
doJvmAntTest();
}
@Test
public void verbose() throws Exception {
doJvmAntTest();
}
@Test
public void version() throws Exception {
doJvmAntTest();
}
@Test
public void javacCompiler() throws Exception {
doJvmAntTest("-cp", getClassPathForAnt(),
@@ -31,6 +31,7 @@ import kotlin.KotlinPackage;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.common.KotlinVersion;
import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.jet.test.Tmpdir;
import org.jetbrains.jet.utils.PathUtil;
@@ -95,6 +96,8 @@ public abstract class KotlinIntegrationTestBase {
content = normalizePath(content, getTestDataDir(), "[TestData]");
content = normalizePath(content, tmpdir.getTmpDir(), "[Temp]");
content = normalizePath(content, getCompilerLib(), "[CompilerLib]");
content = normalizePath(content, getKotlinProjectHome(), "[KotlinProjectHome]");
content = content.replaceAll(KotlinVersion.VERSION, "[KotlinVersion]");
content = StringUtil.convertLineSeparators(content);
return content;
}
@@ -0,0 +1,10 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[kotlin2js] Compiling [[TestData]/root1] => [[Temp]/out.js]
BUILD SUCCESSFUL
Total time: [time]
Return code: 0
@@ -0,0 +1,7 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlin2js src="${test.data}/root1" output="${temp}/out.js" nowarn="true"/>
</target>
</project>
@@ -0,0 +1,14 @@
package foo
import kotlin.Any
import kotlin.Any
fun foo(p: Int??) {
}
trait T {
abstract fun foo()
}
fun box(): String = "OK"
@@ -0,0 +1,15 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[kotlin2js] Compiling [[TestData]/root1] => [[Temp]/out.js]
[kotlin2js] LOGGING: Compiling source files: [TestData]/root1/foo.kt
[kotlin2js] OUTPUT: Output:
[kotlin2js] [Temp]/out.js
[kotlin2js] Sources:
[kotlin2js] [TestData]/root1/foo.kt
BUILD SUCCESSFUL
Total time: [time]
Return code: 0
@@ -0,0 +1,7 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlin2js src="${test.data}/root1" output="${temp}/out.js" verbose="true"/>
</target>
</project>
@@ -0,0 +1,3 @@
package foo
fun box(): String = "OK"
@@ -0,0 +1,11 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[kotlin2js] Compiling [[TestData]/root1] => [[Temp]/out.js]
[kotlin2js] INFO: Kotlin Compiler version [KotlinVersion]
BUILD SUCCESSFUL
Total time: [time]
Return code: 0
@@ -0,0 +1,7 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlin2js src="${test.data}/root1" output="${temp}/out.js" printVersion="true"/>
</target>
</project>
@@ -0,0 +1,3 @@
package foo
fun box(): String = "OK"
@@ -0,0 +1,10 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[kotlinc] Compiling [[TestData]/hello.kt] => [[Temp]/hello.jar]
BUILD SUCCESSFUL
Total time: [time]
Return code: 0
@@ -0,0 +1,7 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlinc src="${test.data}/hello.kt" output="${temp}/hello.jar" nowarn="true" />
</target>
</project>
@@ -0,0 +1,13 @@
package hello
fun foo(p: Int??) {
}
trait T {
abstract fun foo()
}
fun main(args : Array<String>) {
println("Hi!")
}
@@ -0,0 +1,4 @@
OUT:
Hi!
Return code: 0
@@ -0,0 +1,12 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[kotlinc] Compiling [[TestData]/hello.kt] => [[Temp]/hello.jar]
[kotlinc] LOGGING: Using Kotlin home directory [KotlinProjectHome]/dist/kotlinc
[kotlinc] LOGGING: Configuring the compilation environment
BUILD SUCCESSFUL
Total time: [time]
Return code: 0
@@ -0,0 +1,7 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlinc src="${test.data}/hello.kt" output="${temp}/hello.jar" verbose="true" />
</target>
</project>
@@ -0,0 +1,5 @@
package hello
fun main(args : Array<String>) {
println("Hi!")
}
@@ -0,0 +1,4 @@
OUT:
Hi!
Return code: 0
@@ -0,0 +1,11 @@
OUT:
Buildfile: [TestData]/build.xml
build:
[kotlinc] Compiling [[TestData]/hello.kt] => [[Temp]/hello.jar]
[kotlinc] INFO: Kotlin Compiler version [KotlinVersion]
BUILD SUCCESSFUL
Total time: [time]
Return code: 0
@@ -0,0 +1,7 @@
<project name="Ant Task Test" default="build">
<taskdef resource="org/jetbrains/jet/buildtools/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
<target name="build">
<kotlinc src="${test.data}/hello.kt" output="${temp}/hello.jar" printVersion="true" />
</target>
</project>
@@ -0,0 +1,5 @@
package hello
fun main(args : Array<String>) {
println("Yo!")
}
@@ -0,0 +1,4 @@
OUT:
Yo!
Return code: 0
@@ -1,13 +1,11 @@
package org.jetbrains.kotlin.gradle.tasks
import org.gradle.api.tasks.compile.AbstractCompile
import org.jetbrains.kotlin.gradle.plugin.KSpec
import java.io.File
import org.gradle.api.GradleException
import org.jetbrains.jet.cli.common.ExitCode
import org.gradle.api.tasks.SourceTask
import org.jetbrains.kotlin.doc.KDocArguments
import java.util.HashMap
import java.util.HashSet
import org.jetbrains.kotlin.doc.KDocCompiler
import org.gradle.api.tasks.TaskAction
@@ -22,11 +20,8 @@ import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.apache.commons.lang.StringUtils
import org.gradle.api.initialization.dsl.ScriptHandler
import org.apache.commons.io.FileUtils
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.doc.KDocConfig
import java.util.concurrent.Callable
import org.gradle.api.Project
import org.jetbrains.jet.config.Services
@@ -101,6 +96,10 @@ public open class KotlinCompile(): AbstractCompile() {
return
}
args.suppressWarnings = kotlinOptions.suppressWarnings
args.version = kotlinOptions.version
args.verbose = logger.isDebugEnabled()
args.freeArgs = sources.map { it.getAbsolutePath() }
if (StringUtils.isEmpty(kotlinOptions.classpath)) {
@@ -1,8 +1,8 @@
package org.jetbrains.kotlin.gradle
import org.junit.Test
import org.junit.Ignore
import org.jetbrains.kotlin.gradle.BaseGradleIT.Project
import org.gradle.api.logging.LogLevel
class BasicKotlinGradleIT : BaseGradleIT() {
@@ -21,6 +21,38 @@ class BasicKotlinGradleIT : BaseGradleIT() {
}
}
Test fun testSuppressWarningsAndVersionInVerboseMode() {
val project = Project("suppressWarningsAndVersion", "1.6")
project.build("build", "-Pkotlin.gradle.plugin.version=0.1-SNAPSHOT") {
assertSuccessful()
assertContains(":compileKotlin", "i: Kotlin Compiler version", "v: Using Kotlin home directory")
assertNotContains("w:")
}
project.build("build", "-Pkotlin.gradle.plugin.version=0.1-SNAPSHOT") {
assertSuccessful()
assertContains(":compileKotlin UP-TO-DATE")
assertNotContains("w:")
}
}
Test fun testSuppressWarningsAndVersionInNonVerboseMode() {
val project = Project("suppressWarningsAndVersion", "1.6", minLogLevel = LogLevel.INFO)
project.build("build", "-Pkotlin.gradle.plugin.version=0.1-SNAPSHOT") {
assertSuccessful()
assertContains(":compileKotlin", "i: Kotlin Compiler version")
assertNotContains("w:", "v:")
}
project.build("build", "-Pkotlin.gradle.plugin.version=0.1-SNAPSHOT") {
assertSuccessful()
assertContains(":compileKotlin UP-TO-DATE")
assertNotContains("w:", "v:")
}
}
Test fun testKotlinCustomDirectory() {
Project("customSrcDir", "1.6").build("build", "-Pkotlin.gradle.plugin.version=0.1-SNAPSHOT") {
assertSuccessful()
@@ -0,0 +1,40 @@
buildscript {
repositories {
mavenCentral()
maven {
url 'file://' + pathToKotlinPlugin
}
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin-core:0.1-SNAPSHOT'
}
}
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin
apply plugin: KotlinPlugin
sourceSets {
main {
kotlin {
srcDir 'src'
}
}
}
repositories {
maven {
url 'file://' + pathToKotlinPlugin
}
mavenCentral()
}
compileKotlin {
kotlinOptions.suppressWarnings = true
kotlinOptions.version = true
}
task wrapper(type: Wrapper) {
gradleVersion="1.4"
}
@@ -0,0 +1,14 @@
package foo
import kotlin.Any
import kotlin.Any
fun foo(p: Int??) {
}
trait T {
abstract fun foo()
}
fun box(): String = "OK"
@@ -35,6 +35,12 @@
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>gradle-api</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
@@ -7,8 +7,10 @@ import java.util.Scanner
import org.junit.Before
import org.junit.After
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import kotlin.test.assertEquals
import kotlin.test.fail
import org.gradle.api.logging.LogLevel
open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
@@ -23,7 +25,7 @@ open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
deleteRecursively(workingDir)
}
class Project(val projectName: String, val wrapperVersion: String = "1.4")
class Project(val projectName: String, val wrapperVersion: String = "1.4", val minLogLevel: LogLevel = LogLevel.DEBUG)
class CompiledProject(val project: Project, val output: String, val resultCode: Int)
@@ -50,14 +52,21 @@ open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
return this
}
fun CompiledProject.assertNotContains(vararg expected: String): CompiledProject {
for (str in expected) {
assertFalse(output.contains(str), "Should not contain '$str', actual output: $output")
}
return this
}
fun CompiledProject.assertReportExists(pathToReport: String = ""): CompiledProject {
assertTrue(File(File(workingDir, project.projectName), pathToReport).exists(), "The report [$pathToReport] does not exist.")
return this
}
private fun createCommand(params: Array<String>): List<String> {
private fun Project.createCommand(params: Array<String>): List<String> {
val pathToKotlinPlugin = "-PpathToKotlinPlugin=" + File("local-repo").getAbsolutePath()
val tailParameters = params + listOf(pathToKotlinPlugin, "--no-daemon", "--debug")
val tailParameters = params + listOf(pathToKotlinPlugin, "--no-daemon", "--${minLogLevel.name().toLowerCase()}")
return if (isWindows())
listOf("cmd", "/C", "gradlew.bat") + tailParameters
@@ -0,0 +1,45 @@
<?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>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>test-js-suppressWarningsAndVersion</artifactId>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-js-library</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>js</goal>
</goals>
</execution>
</executions>
<configuration>
<nowarn>true</nowarn>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,20 @@
package org.jetbrains
import kotlin.Any
import kotlin.Any
fun foo(p: Int??) {
}
trait T {
abstract fun foo()
}
fun main(args : Array<String>) {
println(getGreeting())
}
fun getGreeting() : String {
return "Hello, World!"
}
@@ -0,0 +1,6 @@
import java.io.*;
File file = new File(basedir, "target/js/test-js-suppressWarningsAndVersion.js");
if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("Could not find generated JS : " + file);
}
@@ -0,0 +1,71 @@
<?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>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>test-helloworld</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-runtime</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
</plugin>
</plugins>
</pluginManagement>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<nowarn>true</nowarn>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,20 @@
package org.jetbrains
import kotlin.Any
import kotlin.Any
fun foo(p: Int??) {
}
trait T {
abstract fun foo()
}
fun main(args : Array<String>) {
System.out?.println(getGreeting())
}
fun getGreeting() : String {
return "Hello, World!"
}
@@ -0,0 +1,6 @@
import java.io.*;
File file = new File(basedir, "target/test-helloworld-0.1-SNAPSHOT.jar");
if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("Could not find generated JAR: " + file);
}
@@ -64,6 +64,13 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
return defaultSourceDirs;
}
/**
* Suppress all warnings.
*
* @parameter default-value="false"
*/
public boolean nowarn;
// TODO not sure why this doesn't work :(
// * @parameter default-value="$(project.basedir}/src/main/resources"
@@ -238,6 +245,8 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
throw new MojoExecutionException("No source roots to compile");
}
arguments.suppressWarnings = nowarn;
arguments.freeArgs.addAll(sources);
LOG.info("Compiling Kotlin sources from " + sources );