From 6974e6cdb2c0832e599417c10dd9d63bdf9dacff Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sat, 25 Mar 2017 13:09:20 -0700 Subject: [PATCH] Backward compatibility. --- .../com/beust/kobalt/app/ParsedBuildFile.kt | 23 ++++++++++++++++--- .../com/beust/kobalt/internal/ProfileTest.kt | 14 ++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt b/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt index 05f805ef..de6fa8fc 100644 --- a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt +++ b/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt @@ -11,6 +11,7 @@ import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.misc.BlockExtractor import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.kobaltLog +import com.beust.kobalt.misc.warn import com.beust.kobalt.plugin.kotlin.kotlinCompilePrivate import java.io.File import java.net.URL @@ -51,11 +52,27 @@ class ParsedBuildFile(val buildFile: BuildFile, val context: KobaltContext, val fun correctProfileLine(line: String): String { (context.profiles as List).forEach { profile -> val re = Regex(".*va[rl][ \\t]+([a-zA-Z0-9_]+)[ \\t]*.*profile\\(\\).*") + val oldRe = Regex(".*va[rl][ \\t]+([a-zA-Z0-9_]+)[ \\t]*=[ \\t]*[tf][ra][ul][es].*") val matcher = re.matchEntire(line) - if (matcher != null && matcher.groups.size > 0) { + val oldMatcher = oldRe.matchEntire(line) + + fun profileMatch(matcher: MatchResult?) : Pair { + val variable = if (matcher != null) matcher.groups[1]?.value else null + return Pair(profile == variable, variable) + } + + if ((matcher != null && matcher.groups.size > 0) || (oldMatcher != null && oldMatcher.groups.size> 0)) { containsProfiles = true - val variable = matcher.groups[1]?.value - if (profile == variable) { + val match = profileMatch(matcher) + val oldMatch = profileMatch(oldMatcher) + if (match.first || oldMatch.first) { + val variable = if (match.first) match.second else oldMatch.second + + if (oldMatch.first) { + warn("Old profile syntax detected for \"$line\"," + + " please update to \"val $variable by profile()\"") + } + with("val $variable = true") { kobaltLog(2, "Activating profile $profile in build file") activeProfiles.add(profile) diff --git a/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt b/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt index 19e4f052..2a77d3ee 100644 --- a/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt +++ b/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt @@ -16,7 +16,7 @@ import java.util.* @Guice(modules = arrayOf(TestModule::class)) class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactory) : BaseTest(compilerFactory) { - private fun runTestWithProfile(enabled: Boolean) : Project { + private fun runTestWithProfile(enabled: Boolean, oldSyntax: Boolean) : Project { val projectVal = "p" + Math.abs(Random().nextInt()) val projectDirectory = createTemporaryProjectDirectory() @@ -24,7 +24,9 @@ class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor return """ import com.beust.kobalt.* import com.beust.kobalt.api.* - val profile by profile() + val profile""" + + (if (oldSyntax) " = false\n" else " by profile()\n") + + """ val $projectVal = project { name = if (profile) "profileOn" else "profileOff" directory = "$projectDirectory" @@ -46,7 +48,13 @@ class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor @Test(dataProvider = "dp") fun profilesShouldWork(enabled: Boolean, expected: String) { Kobalt.init(TestModule()) - assertThat(runTestWithProfile(enabled).name).isEqualTo(expected) + assertThat(runTestWithProfile(enabled, oldSyntax = false).name).isEqualTo(expected) + } + + @Test(dataProvider = "dp") + fun profilesShouldWorkOldSyntax(enabled: Boolean, expected: String) { + Kobalt.init(TestModule()) + assertThat(runTestWithProfile(enabled, oldSyntax = true).name).isEqualTo(expected) } }