Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

[PT-495] Support android kotlin projects #48

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DetektIntegrationTest {

@Parameterized.Parameters(name = "{0}")
static Iterable<TestProjectRule> rules() {
return [TestProjectRule.forKotlinProject()]
return [TestProjectRule.forKotlinProject(), TestProjectRule.forAndroidKotlinProject()]
}

@Rule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.novoda.test
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is basically a copy of TestAndroidProject. Changes are marked.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is not too important but with the comments I put below, it is possible that these can be 1 single class with different plugin configuration.

Then you can have TestKotlinProject.forAndroid() to configure the android plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that first but failed (see my comment below). Any help is appreciated since I also would like to get rid of this duplication.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just merged it. On a second thought, I don't think it is worth. Because that way, it will be more complicated.


class TestAndroidKotlinProject extends TestProject<TestAndroidKotlinProject> {
private static final Closure<String> TEMPLATE = { TestAndroidKotlinProject project ->
"""
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.20'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been added

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you may have left two whitespaces at the end of the line before when adding it:

image

EDIT: actually, there seem to be a few :) Nothing serious. Just my nitpicky eye 💃

Copy link
Contributor

Choose a reason for hiding this comment

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

👁‍🗨

}
}
plugins {
${formatPlugins(project)}
}
repositories {
google()
jcenter()
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been added

Copy link
Contributor

Choose a reason for hiding this comment

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

to be honest you can also use the plugins and your withPlugin method to add these dynamically from test code.
The plugin id is org.jetbrains.kotlin.android
I think also com.android.library works too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually tried that for the kotlin plugin, but it didn't work. I got this error Extension with name 'android' does not exist.
And afaik this is because of the order. com.android.library needs to be applied first and I couldn't find that one here.


android {
compileSdkVersion 27
buildToolsVersion '27.0.0'

defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName '1.0'
}
sourceSets {
${formatSourceSets(project)}
}
${project.additionalAndroidConfig}
}
${formatExtension(project)}
"""
}

private String additionalAndroidConfig = ''

TestAndroidKotlinProject() {
super(TEMPLATE)
File localProperties = Fixtures.LOCAL_PROPERTIES
if (localProperties.exists()) {
withFile(localProperties, 'local.properties')
}
}

private static String formatSourceSets(TestProject project) {
project.sourceSets
.entrySet()
.collect { Map.Entry<String, List<String>> entry ->
"""$entry.key {
manifest.srcFile '${Fixtures.ANDROID_MANIFEST}'
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe use double quotes just so there's more consistency? you can also just use $x.y here i believe

Copy link
Contributor

Choose a reason for hiding this comment

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

This is printing the entire config. In the generated one we want single quotes if possible.

kotlin {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been changed

Copy link
Contributor

Choose a reason for hiding this comment

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

well, if you add your sources to java folder, you don't need this in test setup.

${entry.value.collect { "srcDir '$it'" }.join('\n\t\t\t\t')}
}
}"""
}
.join('\n\t\t')
}

@Override
List<String> defaultArguments() {
['-x', 'lint'] + super.defaultArguments()
}

TestAndroidKotlinProject withAdditionalAndroidConfig(String additionalAndroidConfig) {
this.additionalAndroidConfig = additionalAndroidConfig
return this
}
}
4 changes: 4 additions & 0 deletions plugin/src/test/groovy/com/novoda/test/TestProjectRule.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ final class TestProjectRule<T extends TestProject> implements TestRule {
new TestProjectRule({ new TestAndroidProject() }, { String name -> "project.android.sourceSets.$name" }, 'Android project')
}

static TestProjectRule<TestAndroidKotlinProject> forAndroidKotlinProject() {
new TestProjectRule({ new TestAndroidKotlinProject() }, { String name -> "project.android.sourceSets.$name" }, 'Android kotlin project')
}

static TestProjectRule<TestKotlinProject> forKotlinProject() {
new TestProjectRule({ new TestKotlinProject() }, { String name -> "project.sourceSets.$name" }, 'Kotlin project')
}
Expand Down