Skip to content

Commit

Permalink
Merge pull request #32 from originx/master
Browse files Browse the repository at this point in the history
Add support for ADT 3.0 and ktlin tasks for multidimension projects
  • Loading branch information
Tapchicoma authored Nov 14, 2017
2 parents ee6e816 + d26f377 commit 3b557e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- ?
## Changed
- Update kotlin to 1.1.60 version
- Bumped android tools versions to 3.0 and now support multidimension projects >0.10.x (#29)
### Fixed
- ?

Expand Down
3 changes: 2 additions & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ version = "2.3.1-SNAPSHOT"

repositories {
jcenter()
google()
}

dependencies {
compileOnly(gradleApi())
compileOnly(kotlin("gradle-plugin", "1.1.60"))
compileOnly("com.android.tools.build:gradle:2.3.3")
compileOnly("com.android.tools.build:gradle:3.0.0")
compile("net.swiftzer.semver:semver:1.0.0")

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jlleitschuh.gradle.ktlint

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.FeaturePlugin
import com.android.build.gradle.InstantAppPlugin
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.TestPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
Expand All @@ -11,6 +15,7 @@ import org.gradle.api.internal.HasConvention
import org.gradle.api.plugins.Convention
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.StopExecutionException
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import kotlin.reflect.KClass
import net.swiftzer.semver.SemVer
Expand Down Expand Up @@ -63,22 +68,30 @@ open class KtlintPlugin : Plugin<Project> {
}
}

private fun addKtLintTasksToAndroidKotlinPlugin(target: Project,
extension: KtlintExtension) {
private fun addKtLintTasksToAndroidKotlinPlugin(target: Project, extension: KtlintExtension) {
target.pluginManager.withPlugin("kotlin-android") {
target.afterEvaluate {
val ktLintConfig = createConfiguration(target, extension)

target.extensions.findByType(BaseExtension::class.java)?.sourceSets?.forEach {
val kotlinSourceDir = it.java.sourceFiles
val runArgs = it.java.srcDirs.map { "${it.path}/**/*.kt" }.toMutableSet()
val variantManager = when {
target.plugins.hasPlugin(AppPlugin::class.java) -> target.plugins.findPlugin(AppPlugin::class.java)?.variantManager
target.plugins.hasPlugin(LibraryPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager
target.plugins.hasPlugin(InstantAppPlugin::class.java) -> target.plugins.findPlugin(InstantAppPlugin::class.java)?.variantManager
target.plugins.hasPlugin(FeaturePlugin::class.java) -> target.plugins.findPlugin(FeaturePlugin::class.java)?.variantManager
target.plugins.hasPlugin(TestPlugin::class.java) -> target.plugins.findPlugin(TestPlugin::class.java)?.variantManager
else -> throw StopExecutionException("Must be applied with 'android' or 'android-library' plugin.")
}

variantManager?.variantScopes?.forEach {
val kotlinSourceDir = target.files(it.variantData.javaSources)
val runArgs = it.variantData.javaSources.map { "${it.dir.path}/**/*.kt" }.toMutableSet()
addAdditionalRunArgs(extension, runArgs)

val checkTask = createCheckTask(target, extension, it.name, ktLintConfig, kotlinSourceDir, runArgs)
val checkTask = createCheckTask(target, extension, it.fullVariantName, ktLintConfig, kotlinSourceDir, runArgs)
addKtlintCheckTaskToProjectMetaCheckTask(target, checkTask)
setCheckTaskDependsOnKtlintCheckTask(target, checkTask)

val ktlintSourceSetFormatTask = createFormatTask(target, it.name, ktLintConfig, kotlinSourceDir, runArgs)
val ktlintSourceSetFormatTask = createFormatTask(target, it.fullVariantName, ktLintConfig, kotlinSourceDir, runArgs)
addKtlintFormatTaskToProjectMetaFormatTask(target, ktlintSourceSetFormatTask)
}
}
Expand Down

0 comments on commit 3b557e9

Please sign in to comment.