Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ADT 3.0 and ktlin tasks for multidimension projects #32

Merged
merged 6 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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 All @@ -19,7 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Define a different output file for each sourceSet
### Fixed
- Fixed plugin doesn't apply custom reporter for ktlint versions >0.10.x (#28)
- Fixed plugin doesn't apply custom reporter for ktlint versions >0.10.x (#28)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary whitespace change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, damn, will update

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this commit d26f377


## [2.2.1] - 2017-10-06
### 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,28 +68,37 @@ 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.")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all of these types guaranteed to be on the classpath when either the android or the android-library plugin is applied?

If only the android plugin is applied will you get a ClassDefNotFoundException for the AppPlugin class?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, everything is ok. I've tested on my local test project.

}

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)
}
}
}
}


private fun createConfiguration(target: Project, extension: KtlintExtension) =
target.configurations.maybeCreate("ktlint").apply {
target.dependencies.add(this.name,
Expand Down