Skip to content

Commit

Permalink
Decoupling plugin from buildScript (#47)
Browse files Browse the repository at this point in the history
* Moved the plugin on its standalone module to be able to publish
* Refactor and clean up
* Gradle wrapper upgrade
* put back warnings as error
* Build sample using in repo plugin
* Extracted Kmp setup in utils.gradle
  • Loading branch information
MarcoSignoretto authored Aug 24, 2021
1 parent 618a6dc commit ac42921
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 177 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ hs_err_pid*
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/*
!.idea/codeStyles/
!.idea/copyright/
**/.idea/*
!**.idea/codeStyles/
!**.idea/copyright/

local.properties

Expand Down
77 changes: 2 additions & 75 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ buildscript {
repositories {
google()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath libs.kotlinx.atomicfu.gradle
Expand All @@ -36,82 +34,11 @@ allprojects {
repositories {
mavenCentral()
google()
maven { url "https://kotlin.bintray.com/kotlinx" }
maven { url "https://plugins.gradle.org/m2/" }
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions.allWarningsAsErrors = true
}
}

void setupMultiplatformLibrary(Project project, Boolean shouldPublish = true) {
project.apply plugin: 'org.jetbrains.kotlin.multiplatform'
if (shouldPublish) {
project.apply from: '../publishing.gradle'
}

project.group = GROUP
project.version = VERSION

project.kotlin {
sourceSets {
commonTest {
dependencies {
implementation libs.kotlin.test
implementation libs.kotlin.test.annotations
}
}
}
}
}

void setupAllTargetsWithDefaultSourceSets(Project project) {
setupIosTargets(project)
setupJvmTarget(project)
setupJsTarget(project)

project.kotlin {
sourceSets {
jvmJsCommonMain.dependsOn commonMain
jvmJsCommonTest.dependsOn commonTest

jvmMain.dependsOn jvmJsCommonMain
jvmTest.dependsOn jvmJsCommonTest

jsMain.dependsOn jvmJsCommonMain
jsTest.dependsOn jvmJsCommonTest
kotlinOptions{
allWarningsAsErrors = true
}
}
}

private void setupIosTargets(Project project) {
project.kotlin {
targets.fromPreset(presets.iosX64, 'iosX64')
targets.fromPreset(presets.iosArm64, 'iosArm64')
}
}

private void setupJvmTarget(Project project) {
project.kotlin {
targets.fromPreset(presets.jvm, 'jvm')

sourceSets {
jvmMain {
dependencies {

}
}

jvmTest {
dependencies {
implementation libs.junit.junit
}
}
}
}
}

private void setupJsTarget(Project project) {
project.apply plugin: JsPlugin
}
27 changes: 0 additions & 27 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,8 @@ repositories {
mavenCentral()
google()
gradlePluginPortal()
maven {
url = uri(project.properties["careem_artifactory_url"] as String? ?: System.getenv("careem_artifactory_url"))
credentials {
username = project.properties["careem_artifactory_username"] as String? ?: System.getenv("careem_artifactory_username")
password = project.properties["careem_artifactory_api_key"] as String? ?: System.getenv("careem_artifactory_api_key")
}
content {
includeGroup("com.careem.mockingbird")
}
mavenContent {
releasesOnly()
}
}
}

dependencies {
implementation(libs.kotlin.gradle)
implementation(libs.kotlin.reflectjvm)
implementation(libs.square.kotlinpoet)
implementation(libs.square.kotlinpoet.metadata)
implementation(libs.square.kotlinpoet.metadata.specs)
implementation(libs.kotlinx.metadatajvm)
implementation(libs.careem.mockingbird) // FIXME avoid this dep but use project dep instead
}

kotlin {
// Add Deps to compilation, so it will become available in main project
sourceSets.getByName("main").kotlin.srcDirs(
"buildSrc/src/main/kotlin"
)
println("ROOT:${rootProject.allprojects}")
}
31 changes: 6 additions & 25 deletions mockingbird-compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ plugins {
`kotlin-dsl`
}

apply(from = "../publishing.gradle")

gradlePlugin {
plugins {
register("mockingbird") {
Expand All @@ -35,38 +37,17 @@ repositories {
gradlePluginPortal()
}

kotlin {
// Add Deps to compilation, so it will become available in main project
sourceSets.getByName("main").kotlin.srcDirs("buildSrc/src/main/kotlin")
println("ROOT:${rootProject.allprojects}")
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}

dependencies {
implementation(libs.kotlin.gradle)
implementation(libs.kotlin.reflectjvm)
implementation(libs.square.kotlinpoet)
implementation(libs.square.kotlinpoet.metadata)
implementation(libs.square.kotlinpoet.metadata.specs)
implementation(libs.kotlinx.metadatajvm)
implementation(project(":mockingbird"))
}

//task pluginVersion {
// def outputDir = file("gen")
//
// inputs.property 'version', version
// outputs.dir outputDir
//
// doLast {
// def versionFile = file("$outputDir/com/squareup/sqldelight/Version.kt")
// versionFile.parentFile.mkdirs()
// versionFile.text = """// Generated file. Do not edit!
//package com.squareup.sqldelight
//
//val VERSION = "${project.version}"
//"""
// }
//}
//
//tasks.getByName('compileKotlin').dependsOn('pluginVersion')
//
//apply from: "$rootDir/gradle/gradle-mvn-push.gradle"

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.careem.mockingbird

import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand Down Expand Up @@ -107,6 +109,8 @@ abstract class MockingbirdPlugin : Plugin<Project> {
}
}


@OptIn(DelicateKotlinPoetApi::class)
private fun generateMockClassFor(project: Project, kmClass: ImmutableKmClass) {
val classToMock = Thread.currentThread().contextClassLoader.loadClass(
kmClass.name.replace(
Expand All @@ -124,6 +128,7 @@ abstract class MockingbirdPlugin : Plugin<Project> {

// TODO fix package name
println("Generating mocks for $simpleName")

val mockClassBuilder = TypeSpec.classBuilder("${simpleName}Mock")
.addType(kmClass.buildMethodObject())
.addType(kmClass.buildArgObject())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.setValue
import kotlin.reflect.KProperty

/*
* Copyright Careem, an Uber Technologies Inc. company
*
Expand All @@ -22,6 +13,16 @@ import kotlin.reflect.KProperty
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.careem.mockingbird

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.setValue
import kotlin.reflect.KProperty

interface MockingbirdPluginExtension {
var generateMocksFor: List<String>
Expand All @@ -39,7 +40,7 @@ internal class MockingbirdPluginExtensionImpl(objects: ObjectFactory) : Mockingb
override var printLogs: Boolean by _printLogs // TODO implement this
}

internal inline operator fun <T> ListProperty<T>.setValue(scope: Any, property: KProperty<*>, value: List<T>) =
internal operator fun <T> ListProperty<T>.setValue(scope: Any, property: KProperty<*>, value: List<T>) =
set(value)

internal inline operator fun <T> ListProperty<T>.getValue(scope: Any, property: KProperty<*>): List<T> = get()
internal operator fun <T> ListProperty<T>.getValue(scope: Any, property: KProperty<*>): List<T> = get()
7 changes: 5 additions & 2 deletions mockingbird/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
setupMultiplatformLibrary(project)
setupAllTargetsWithDefaultSourceSets(project)

apply from: '../utils.gradle'

setupMultiplatformLibrary(project, true)
setupAllTargetsWithDefaultSourceSets(project, true)

kotlin {
explicitApi()
Expand Down
26 changes: 26 additions & 0 deletions samples/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions samples/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ac42921

Please sign in to comment.