Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Jul 2, 2024
1 parent a7b4148 commit 7048c3e
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 19 deletions.
42 changes: 42 additions & 0 deletions samples/android-multi-module/dinosaurs/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("com.squareup.wire")
}

buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath(libs.pluginz.android)
classpath("com.squareup.wire:wire-gradle-plugin")
}
}

android {
namespace = "com.squareup.wire.android.app.multi.dinosaurs"
}

dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.constraintLayout)
implementation(libs.androidMaterial)
testImplementation(libs.junit)
}

wire {
sourcePath {
srcDir("src/main/proto")
}

sourcePath {
srcProject(":samples:android-multi-module:location")
include("squareup/location/continent.proto")
}

kotlin {
out = "src/main/kotlin"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto2";

package squareup.dinosaurs;

option java_package = "com.squareup.dinosaurs";

import "squareup/geology/period.proto";
import "squareup/location/continent.proto";

message Dinosaur {
/** Common name of this dinosaur, like "Stegosaurus". */
optional string name = 1;

/** URLs with images of this dinosaur. */
repeated string picture_urls = 2;

optional double length_meters = 3;
optional double mass_kilograms = 4;
optional squareup.geology.Period period = 5;
optional squareup.location.Continent continent = 6;
}
34 changes: 34 additions & 0 deletions samples/android-multi-module/geology/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("com.squareup.wire")
}

buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath(libs.pluginz.android)
classpath("com.squareup.wire:wire-gradle-plugin")
}
}

android {
namespace = "com.squareup.wire.android.app.multi.geology"
}

dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.constraintLayout)
implementation(libs.androidMaterial)
testImplementation(libs.junit)
}

wire {
protoLibrary = true

kotlin {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto2";

package squareup.geology;

option java_package = "com.squareup.geology";

enum Period {
/** 145.5 million years ago — 66.0 million years ago. */
CRETACEOUS = 1;

/** 201.3 million years ago — 145.0 million years ago. */
JURASSIC = 2;

/** 252.17 million years ago — 201.3 million years ago. */
TRIASSIC = 3;
}
55 changes: 55 additions & 0 deletions samples/android-multi-module/location/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("com.squareup.wire")
id("maven-publish")
}

buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath(libs.pluginz.android)
classpath("com.squareup.wire:wire-gradle-plugin")
}
}

android {
namespace = "com.squareup.wire.android.app.multi.location"

publishing {
singleVariant("debug") {
withSourcesJar()
}
}
}

publishing {
publications {
create("debug", MavenPublication::class.java) {
groupId = "com.my-company"
artifactId = "my-library"
version = "1.0"

afterEvaluate {
from(components.getByName("debug"))
}
}
}
}

dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.constraintLayout)
implementation(libs.androidMaterial)
testImplementation(libs.junit)
}

wire {
protoLibrary = true

kotlin {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2024 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.
*/
package london

fun main() {
println("hello")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto2";

package squareup.location;

option java_package = "com.squareup.location";

enum Continent {
AFRICA = 0;
AMERICA = 1;
ANTARCTICA = 2;
ASIA = 3;
AUSTRALIA = 4;
EUROPE = 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto2";

package squareup.location;

option java_package = "com.squareup.location";

enum Planet {
MERCURY = 0;
VENUS = 1;
EARTH = 2;
MARS = 3;
JUPITER = 4;
SATURN = 5;
URANUS = 6;
NEPTUNE = 7;
}
13 changes: 8 additions & 5 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ include(":wire-bom")
include(":wire-compiler")
include(":wire-golden-files")
include(":wire-gradle-plugin")
include(":wire-gradle-plugin-playground")
// include(":wire-gradle-plugin-playground")
include(":wire-grpc-client")
include(":wire-grpc-mockwebserver")
include(":wire-grpc-tests")
Expand Down Expand Up @@ -77,9 +77,12 @@ if (startParameter.projectProperties.get("swift") != "false") {
// include(":samples:android-lib-java-sample")
// include(":samples:android-lib-kotlin-sample")
// include(":samples:js")
include(":samples:multi-platform-multi-module:dinosaurs-native")
include(":samples:multi-platform-multi-module:geology-native")
include(":samples:multi-platform-multi-module:location-js")
// include(":samples:multi-platform-multi-module:dinosaurs-native")
// include(":samples:multi-platform-multi-module:geology-native")
// include(":samples:multi-platform-multi-module:location-js")
include(":samples:android-multi-module:dinosaurs")
include(":samples:android-multi-module:geology")
include(":samples:android-multi-module:location")
// include(":samples:native")
// include(":samples:wire-codegen-sample")
// include(":samples:wire-grpc-sample:client")
Expand All @@ -88,6 +91,6 @@ include(":samples:multi-platform-multi-module:location-js")
// include(":samples:wire-grpc-sample:server-plain")
// include(":wire-benchmarks")
// include(":wire-golden-files")
include(":wire-gradle-plugin-playground")
// include(":wire-gradle-plugin-playground")
// include(":wire-grpc-tests")
// include(":wire-protoc-compatibility-tests")
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
*/
package com.squareup.wire.gradle

import com.android.build.api.attributes.BuildTypeAttr
import com.android.build.gradle.internal.attributes.VariantAttr
import com.squareup.wire.schema.EventListener
import java.io.File
import java.io.Serializable
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.internal.catalog.DelegatingProjectDependency
import org.gradle.api.internal.file.FileOrUriNotationConverter
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderConvertible
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType

open class WireExtension(
private val project: Project,
Expand Down Expand Up @@ -285,6 +289,12 @@ open class WireExtension(
.apply {
isCanBeConsumed = false
isTransitive = false
attributes { attributesContainer ->
attributesContainer.attribute(
BuildTypeAttr.ATTRIBUTE,
project.objects.named(BuildTypeAttr::class.java, "debug")
)
}
}
internal val sourceDirectoriesAndLocalJars = mutableListOf<File>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.squareup.wire.gradle

import java.lang.reflect.Array as JavaArray
import com.android.build.api.attributes.BuildTypeAttr
import com.squareup.wire.VERSION
import com.squareup.wire.gradle.internal.libraryProtoOutputPath
import com.squareup.wire.gradle.internal.targetDefaultOutputPath
Expand All @@ -26,7 +28,6 @@ import com.squareup.wire.schema.ProtoTarget
import com.squareup.wire.schema.Target
import com.squareup.wire.schema.newEventListenerFactory
import java.io.File
import java.lang.reflect.Array as JavaArray
import java.util.concurrent.atomic.AtomicBoolean
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -37,6 +38,7 @@ import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.compile.JavaCompile
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
Expand Down Expand Up @@ -68,7 +70,11 @@ class WirePlugin : Plugin<Project> {
// TODO(Benoit) If another project, on which this one depends, exposes multiple variants,
// Wire won't be able to pick one. We force the resolution to JVM. On the other hand, this
// breaks inter-module dependencies for non-jvm modules. We need to fix it.
attributesContainer.attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm)
attributesContainer.attribute(KotlinPlatformType.attribute, KotlinPlatformType.androidJvm)
attributesContainer.attribute(
BuildTypeAttr.ATTRIBUTE,
project.objects.named(BuildTypeAttr::class.java, "debug"),
)
}
}

Expand Down Expand Up @@ -120,7 +126,8 @@ class WirePlugin : Plugin<Project> {

val protoSourceConfiguration = project.configurations.getByName("protoSource")
val protoPathConfiguration = project.configurations.getByName("protoPath")
val projectDependenciesJvmConfiguration = project.configurations.getByName("protoProjectDependenciesJvm")
val projectDependenciesJvmConfiguration =
project.configurations.getByName("protoProjectDependenciesJvm")

val outputs = extension.outputs
check(outputs.isNotEmpty()) {
Expand Down Expand Up @@ -258,7 +265,8 @@ class WirePlugin : Plugin<Project> {
task.projectDirProperty.set(project.layout.projectDirectory)
task.buildDirProperty.set(project.layout.buildDirectory)

val factories = extension.eventListenerFactories + extension.eventListenerFactoryClasses().map(::newEventListenerFactory)
val factories = extension.eventListenerFactories + extension.eventListenerFactoryClasses()
.map(::newEventListenerFactory)
task.eventListenerFactories.set(factories)
}

Expand All @@ -271,17 +279,27 @@ class WirePlugin : Plugin<Project> {

val protoOutputDirectory = task.map { it.protoLibraryOutput }
if (extension.protoLibrary) {
val sourceSets = project.extensions.getByType(SourceSetContainer::class.java)
// Note that there are no source sets for some platforms such as native.
// TODO(Benoit) Probably should be checking for other names than `main`. As well, source
// sets might be created 'afterEvaluate'. Does that mean we should do this work in
// `afterEvaluate` as well? See: https://kotlinlang.org/docs/multiplatform-dsl-reference.html#source-sets
if (sourceSets.findByName("main") != null) {
sourceSets.getByName("main") { main: SourceSet ->
main.resources.srcDir(protoOutputDirectory)
val sourceContainerNames = project.extensions.extensionsSchema.map { it.name }
sourceContainerNames.forEach { sourceContainerName ->
val extension = project.extensions.getByName(sourceContainerName)
when (extension) {
is SourceSetContainer -> {
extension.findByName("main")?.let { main: SourceSet ->
main.resources.srcDir(protoOutputDirectory)
}
}

is KotlinProjectExtension -> {
extension.sourceSets.findByName("main")?.resources?.srcDir(protoOutputDirectory)
}

else -> {
project.logger.warn(
"${project.displayName} doesn't have a 'main' source sets. The .proto files will not automatically be added to the artifact.",
)
}
}
} else {
project.logger.warn("${project.displayName} doesn't have a 'main' source sets. The .proto files will not automatically be added to the artifact.")
(project.extensions.getByName("kotlin") as? KotlinProjectExtension)?.sourceSets
}
}

Expand Down

0 comments on commit 7048c3e

Please sign in to comment.