Skip to content

Commit

Permalink
Let precompiled script plugins with CRLF line separators find its acc…
Browse files Browse the repository at this point in the history
…essors

By normalising the line separators in the script text before computing its hash.

Fixes #12248
  • Loading branch information
bamboo committed Feb 18, 2020
1 parent 2ef64cb commit 3f04f65
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.lang.StringEscapeUtils;
import org.gradle.internal.SystemProperties;

import javax.annotation.Nullable;
import java.io.File;
import java.util.Arrays;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -61,8 +62,16 @@ public static String getPlatformLineSeparator() {
/**
* Converts all line separators in the specified string to the specified line separator.
*/
public static String convertLineSeparators(String str, String sep) {
return str == null ? null : str.replaceAll("\r\n|\r|\n", sep);
@Nullable
public static String convertLineSeparators(@Nullable String str, String sep) {
return str == null ? null : replaceLineSeparatorsOf(str, sep);
}

/**
* Converts all line separators in the specified non-null string to the specified line separator.
*/
public static String replaceLineSeparatorsOf(CharSequence string, String bySeparator) {
return Pattern.compile("\r\n|\r|\n").matcher(string).replaceAll(bySeparator);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ import org.gradle.kotlin.dsl.fixtures.pluginDescriptorEntryFor
import org.gradle.kotlin.dsl.support.zipTo

import org.gradle.test.fixtures.file.LeaksFileHandles
import org.gradle.util.TextUtil.replaceLineSeparatorsOf

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat

import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
Expand All @@ -59,6 +60,27 @@ import java.io.File
@LeaksFileHandles("Kotlin Compiler Daemon working directory")
class PrecompiledScriptPluginAccessorsTest : AbstractPrecompiledScriptPluginTest() {

@Test
fun `can use type-safe accessors for applied plugins with CRLF line separators`() {

withKotlinDslPlugin()

withPrecompiledKotlinScript("my-java-library.gradle.kts",
replaceLineSeparatorsOf(
"""
plugins { java }
java { }
tasks.compileJava { }
""",
"\r\n"
)
)

compileKotlin()
}

@Test
@ToBeFixedForInstantExecution
fun `fails the build with help message for plugin spec with version`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.initialization.Settings
import org.gradle.api.invocation.Gradle
import org.gradle.internal.hash.Hashing
import org.gradle.kotlin.dsl.precompile.PrecompiledScriptDependenciesResolver

import org.gradle.kotlin.dsl.support.KotlinScriptType
import org.gradle.kotlin.dsl.support.KotlinScriptTypeMatch
Expand Down Expand Up @@ -101,7 +101,7 @@ data class PrecompiledScriptPlugin(internal val scriptFile: File) {
}

val hashString by lazy {
Hashing.hashString(scriptText).toString()
PrecompiledScriptDependenciesResolver.hashOf(scriptText)
}

val scriptText: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.gradle.kotlin.dsl.precompile
import org.gradle.internal.hash.Hashing

import org.gradle.kotlin.dsl.resolver.KotlinBuildScriptDependencies
import org.gradle.util.TextUtil.replaceLineSeparatorsOf

import java.util.concurrent.Future

Expand All @@ -33,7 +34,8 @@ class PrecompiledScriptDependenciesResolver : ScriptDependenciesResolver {

companion object {

fun hashOf(charSequence: CharSequence) = Hashing.hashString(charSequence).toString()
fun hashOf(charSequence: CharSequence) =
Hashing.hashString(replaceLineSeparatorsOf(charSequence, "\n")).toString()

fun implicitImportsForScript(scriptText: CharSequence, environment: Environment?) =
implicitImportsFrom(environment) + precompiledScriptPluginImportsFrom(environment, scriptText)
Expand Down

0 comments on commit 3f04f65

Please sign in to comment.