1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Backward compatibility.

This commit is contained in:
Cedric Beust 2017-03-25 13:09:20 -07:00
parent 86c166ff76
commit 6974e6cdb2
2 changed files with 31 additions and 6 deletions

View file

@ -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<String>).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<Boolean, String?> {
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)

View file

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