-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from NixOS/fix-version-bump
Fix :bumpVersion with Gradle Configuration Cache
- Loading branch information
Showing
3 changed files
with
73 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.ProjectLayout | ||
import org.gradle.api.model.ObjectFactory | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.UntrackedTask | ||
import org.gradle.kotlin.dsl.property | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Replaces the value of [propertyName] in the given [propertiesFile]. | ||
* The old value given by [oldValue] is replaced by the value given by [newValue]. | ||
*/ | ||
@UntrackedTask(because = "Changes project files in-place") | ||
abstract class ChangePropertyTask @Inject constructor( | ||
objects: ObjectFactory, | ||
layout: ProjectLayout, | ||
) : DefaultTask() { | ||
|
||
@get:Input | ||
val oldValue = objects.property<String>() | ||
|
||
@get:Input | ||
val newValue = objects.property<String>() | ||
|
||
@get:Input | ||
val propertyName = objects.property<String>() | ||
|
||
@get:OutputFile | ||
val propertiesFile = objects.fileProperty() | ||
.convention(layout.projectDirectory.file("gradle.properties")) | ||
|
||
@TaskAction | ||
fun run() { | ||
val escapedName = Regex.escape(propertyName.get()) | ||
val escapedOldValue = Regex.escape(oldValue.get()) | ||
val escapedReplacement = Regex.escapeReplacement(newValue.get()) | ||
|
||
val file = propertiesFile.get().asFile | ||
file.writeText( | ||
file.readText().replace( | ||
Regex( | ||
"^(\\s*$escapedName\\s*=\\s*)$escapedOldValue(\\s*)$", | ||
RegexOption.MULTILINE, | ||
), | ||
"$1${escapedReplacement}$2", | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import org.gradle.api.GradleException | ||
import java.util.regex.Pattern | ||
|
||
object PluginVersions { | ||
/** | ||
* Increments the last integer within the given string. | ||
*/ | ||
@JvmStatic | ||
fun increment(previous: String): String { | ||
val matcher = Pattern.compile("(.*\\D)(\\d+)(\\D*)").matcher(previous) | ||
if (matcher.matches()) { | ||
val incrementedNumber = Integer.parseInt(matcher.group(2)) + 1 | ||
return matcher.group(1) + incrementedNumber + matcher.group(3) | ||
} else { | ||
throw GradleException("Unsupported version: $previous") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters