Migrate to Gradle (#1159)

* Start gradle root

* Clean up test API ambiguities

These don't compile in gradle anymore and the team[] warns

* Exclude .gradle dirs in git

* Set up moshi module

* Set up moshi-adapters

* Add repositories to subprojects

* Set target/source compatibility

* Set up examples

* Fix location of reflect/test dir

* Set up moshi-kotlin

* Set up code gen

* Opportunistic update code gen deps

* Fix up with code gen

* Set up kotlin tests

* Update snapshots

* Update travis build

* Configure checkstyle

* Cache gradle

* Finish fixing up checkstyle

* Now disable checkstyle until we can fix them all :|

* Update contributing

* Fix tests in codegen

* Remove unnecessary annotation

* Remove maven stuff!

* Suppress warning

* Remove jcenter

* Consolidate dependencies

* Revert "Clean up test API ambiguities"

This reverts commit 3ead69b844b5d7f66134b721e95581f5df1cccd6.

* Fix incap dep

* Opportunistically fix some small kotlinpoet deprecations

* Automatically apply the stdlib to all kotlin projects

* Opportunistic move to opt-in and remove unnecessary annotations

The kotlin maven plugin didn't handle these well in the IDE, gradle does

* Fix Type doc warning

* Fix okio version

* Fix dokka support

* Fix copypasta

* Use new snapshot

* Kotlin 1.4.0
This commit is contained in:
Zac Sweers
2020-08-27 16:50:28 -04:00
committed by GitHub
parent 24c0c49858
commit 4dd4a9d222
48 changed files with 899 additions and 1643 deletions

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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.
*/
import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm")
kotlin("kapt")
id("com.vanniktech.maven.publish")
id("com.github.johnrengelman.shadow") version "5.2.0"
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf(
"-progressive",
"-Xopt-in=com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview"
)
}
}
val shade: Configuration = configurations.maybeCreate("compileShaded")
configurations.getByName("compileOnly").extendsFrom(shade)
dependencies {
implementation(project(":moshi"))
implementation(kotlin("reflect"))
shade(Dependencies.Kotlin.metadata) {
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib")
}
implementation(Dependencies.KotlinPoet.kotlinPoet)
shade(Dependencies.KotlinPoet.metadata) {
exclude(group = "org.jetbrains.kotlin")
}
shade(Dependencies.KotlinPoet.metadataSpecs) {
exclude(group = "org.jetbrains.kotlin")
}
shade(Dependencies.KotlinPoet.elementsClassInspector) {
exclude(group = "org.jetbrains.kotlin")
}
implementation(Dependencies.asm)
implementation(Dependencies.AutoService.annotations)
kapt(Dependencies.AutoService.processor)
implementation(Dependencies.Incap.annotations)
kapt(Dependencies.Incap.processor)
// Copy these again as they're not automatically included since they're shaded
testImplementation(Dependencies.KotlinPoet.metadata)
testImplementation(Dependencies.KotlinPoet.metadataSpecs)
testImplementation(Dependencies.KotlinPoet.elementsClassInspector)
testImplementation(Dependencies.Testing.junit)
testImplementation(Dependencies.Testing.assertj)
testImplementation(Dependencies.Testing.truth)
testImplementation(Dependencies.Testing.compileTesting)
testImplementation(Dependencies.okio2)
}
val relocateShadowJar = tasks.register<ConfigureShadowRelocation>("relocateShadowJar") {
target = tasks.shadowJar.get()
}
val shadowJar = tasks.shadowJar.apply {
configure {
dependsOn(relocateShadowJar)
archiveClassifier.set("")
configurations = listOf(shade)
relocate("com.squareup.kotlinpoet.metadata", "com.squareup.moshi.kotlinpoet.metadata")
relocate("com.squareup.kotlinpoet.classinspector",
"com.squareup.moshi.kotlinpoet.classinspector")
relocate("kotlinx.metadata", "com.squareup.moshi.kotlinx.metadata")
transformers.add(ServiceFileTransformer())
}
}
artifacts {
runtime(shadowJar)
archives(shadowJar)
}
// Shadow plugin doesn't natively support gradle metadata, so we have to tell the maven plugin where
// to get a jar now.
afterEvaluate {
configure<PublishingExtension> {
publications.withType<MavenPublication>().configureEach {
if (name == "pluginMaven") {
// This is to properly wire the shadow jar's gradle metadata and pom information
setArtifacts(artifacts.matching { it.classifier != "" })
// Ugly but artifact() doesn't support TaskProviders
artifact(shadowJar.get())
}
}
}
}

View File

@@ -0,0 +1,19 @@
#
# Copyright (C) 2020 Square, Inc.
#
# 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
#
# https://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.
#
POM_NAME=Moshi Kotlin Codegen
POM_ARTIFACT_ID=moshi-kotlin-codegen
POM_PACKAGING=jar

View File

@@ -1,269 +0,0 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi-parent</artifactId>
<version>1.11.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>moshi-kotlin-codegen</artifactId>
<dependencies>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>kotlinpoet</artifactId>
<version>${kotlinpoet.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-metadata-jvm</artifactId>
<version>${kotlinx-metadata.version}</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>kotlinpoet-metadata</artifactId>
<version>${kotlinpoet.version}</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>kotlinpoet-metadata-specs</artifactId>
<version>${kotlinpoet.version}</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>kotlinpoet-classinspector-elements</artifactId>
<version>${kotlinpoet.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>net.ltgt.gradle.incap</groupId>
<artifactId>incap</artifactId>
<version>${incap.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<version>${auto-service.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>${truth.version}</version>
<scope>test</scope>
</dependency>
<!--
The Kotlin compiler usage must be near the end of the list because its .jar file includes an
obsolete version of Guava!
-->
<dependency>
<groupId>com.github.tschuchortdev</groupId>
<artifactId>kotlin-compile-testing</artifactId>
<version>${kotlin-compile-testing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>${okio2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.javadoc.skip>true</maven.javadoc.skip><!-- We use Dokka instead. -->
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>${auto-service.version}</version>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>net.ltgt.gradle.incap</groupId>
<artifactId>incap-processor</artifactId>
<version>${incap.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<!-- So we can declare ourselves as users of KotlinPoetMetadataPreview -->
<arg>-Xopt-in=com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview</arg>
<arg>-Xopt-in=kotlin.OptIn</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<!--
Suppress the surefire classloader which prevents introspecting the classpath.
http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html
-->
<useManifestOnlyJar>false</useManifestOnlyJar>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.squareup:kotlinpoet-metadata</include>
<include>com.squareup:kotlinpoet-metadata-specs</include>
<include>com.squareup:kotlinpoet-classinspector-elements</include>
<include>org.jetbrains.kotlinx:kotlinx-metadata-jvm</include>
</includes>
</artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
<relocations>
<!-- Repackage KotlinPoet-metadata and kotlinx-metadata until their APIs are stable. -->
<relocation>
<pattern>com.squareup.kotlinpoet.metadata</pattern>
<shadedPattern>com.squareup.moshi.kotlinpoet.metadata</shadedPattern>
</relocation>
<relocation>
<pattern>com.squareup.kotlinpoet.classinspector</pattern>
<shadedPattern>com.squareup.moshi.kotlinpoet.classinspector</shadedPattern>
</relocation>
<relocation>
<pattern>kotlinx.metadata</pattern>
<shadedPattern>com.squareup.moshi.kotlinx.metadata</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/assembly/dokka.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<version>${dokka.version}</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>dokka</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,16 +0,0 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>javadoc</id>
<formats>
<format>jar</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>target/dokka/moshi-kotlin-codegen</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>

View File

@@ -19,7 +19,6 @@ import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.classinspector.elements.ElementsClassInspector
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
import com.squareup.moshi.JsonClass
import com.squareup.moshi.kotlin.codegen.api.AdapterGenerator
import com.squareup.moshi.kotlin.codegen.api.PropertyGenerator
@@ -45,7 +44,6 @@ import javax.tools.Diagnostic
* The generated class will match the visibility of the given data class (i.e. if it's internal, the
* adapter will also be internal).
*/
@KotlinPoetMetadataPreview
@AutoService(Processor::class)
@IncrementalAnnotationProcessor(ISOLATING)
class JsonClassCodegenProcessor : AbstractProcessor() {

View File

@@ -45,6 +45,7 @@ import com.squareup.moshi.kotlin.codegen.api.TargetParameter
import com.squareup.moshi.kotlin.codegen.api.TargetProperty
import com.squareup.moshi.kotlin.codegen.api.TargetType
import com.squareup.moshi.kotlin.codegen.api.mapTypes
import com.squareup.moshi.kotlin.codegen.api.rawType
import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
@@ -365,7 +366,7 @@ private fun declaredProperties(
return result
}
private val TargetProperty.isTransient get() = propertySpec.annotations.any { it.className == Transient::class.asClassName() }
private val TargetProperty.isTransient get() = propertySpec.annotations.any { it.typeName == Transient::class.asClassName() }
private val TargetProperty.isSettable get() = propertySpec.mutable || parameter != null
private val TargetProperty.isVisible: Boolean
get() {
@@ -406,19 +407,20 @@ internal fun TargetProperty.generator(
// Merge parameter and property annotations
val qualifiers = parameter?.qualifiers.orEmpty() + propertySpec.annotations.qualifiers(elements)
for (jsonQualifier in qualifiers) {
val qualifierRawType = jsonQualifier.typeName.rawType()
// Check Java types since that covers both Java and Kotlin annotations.
val annotationElement = elements.getTypeElement(jsonQualifier.className.canonicalName)
val annotationElement = elements.getTypeElement(qualifierRawType.canonicalName)
?: continue
annotationElement.getAnnotation(Retention::class.java)?.let {
if (it.value != RetentionPolicy.RUNTIME) {
messager.printMessage(ERROR,
"JsonQualifier @${jsonQualifier.className.simpleName} must have RUNTIME retention")
"JsonQualifier @${qualifierRawType.simpleName} must have RUNTIME retention")
}
}
annotationElement.getAnnotation(Target::class.java)?.let {
if (ElementType.FIELD !in it.value) {
messager.printMessage(ERROR,
"JsonQualifier @${jsonQualifier.className.simpleName} must support FIELD target")
"JsonQualifier @${qualifierRawType.simpleName} must support FIELD target")
}
}
}
@@ -436,13 +438,13 @@ internal fun TargetProperty.generator(
private fun List<AnnotationSpec>?.qualifiers(elements: Elements): Set<AnnotationSpec> {
if (this == null) return setOf()
return filterTo(mutableSetOf()) {
elements.getTypeElement(it.className.toString()).getAnnotation(JSON_QUALIFIER) != null
elements.getTypeElement(it.typeName.toString()).getAnnotation(JSON_QUALIFIER) != null
}
}
private fun List<AnnotationSpec>?.jsonName(): String? {
if (this == null) return null
return find { it.className == JSON }?.let { annotation ->
return find { it.typeName == JSON }?.let { annotation ->
val mirror = requireNotNull(annotation.tag<AnnotationMirror>()) {
"Could not get the annotation mirror from the annotation spec"
}

View File

@@ -15,7 +15,6 @@
*/
package com.squareup.moshi.kotlin.codegen
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.tschuchort.compiletesting.KotlinCompilation
@@ -36,7 +35,6 @@ import kotlin.reflect.full.createType
import kotlin.reflect.full.declaredMemberProperties
/** Execute kotlinc to confirm that either files are generated or errors are printed. */
@OptIn(KotlinPoetMetadataPreview::class)
class JsonClassCodegenProcessorTest {
@Rule @JvmField var temporaryFolder: TemporaryFolder = TemporaryFolder()