Package com.vaadin.copilot
Class GradleFileRewriter
java.lang.Object
com.vaadin.copilot.GradleFileRewriter
Rewriter for
build.gradle files that supports dependency, property
and repository updates while trying to preserve the original formatting.
Dependencies can also be looked up in and added to Kotlin DSL
(build.gradle.kts) files, using the Kotlin syntax when the file name
ends with .kts. Other changes always use the Groovy syntax.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordstatic final recordstatic final record -
Constructor Summary
ConstructorsConstructorDescriptionGradleFileRewriter(Path file) Creates a rewriter for the given Gradle build file. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddDependency(String groupId, String artifactId, String version) Adds dependency to build.gradle if needed.booleanaddDependency(String groupId, String artifactId, String version, String configuration) Adds dependency to the build file if needed, asconfiguration 'group:artifact'in a Groovy build file and asconfiguration("group:artifact")in a Kotlin DSL build file.voidaddPluginRepository(String id, String url) Adds plugin repository to build.gradle if it does not exist.voidaddRepository(String id, String url) Adds repository to build.gradle if it does not exist.voidaddTopLevelBlock(String text) Appends a top level block at the end of the build file.findDependency(String groupId, String artifactId) Finds the dependency with the given groupId and artifactId inside the dependencies block.findDependencyByGroupIdAndArtifactId(String groupId, String artifactId) Finds dependency under dependencies and dependencyManagement blocks by groupId and artifactId.findPropertyByKey(String propertyKey) Finds the property by key.Gets the content of the build file as it was read, without its//and/* *\/comments.Checks whether buildscript repositories block exists.Checks whether repositories block exists.booleanChecks plugin repository exists in buildscript repositories block by id.booleanhasPluginRepositoryByIdAndUrl(String id, String url) Checks plugin repository exists in buildscript repositories block by id and url.booleanChecks plugin repository exists in buildscript repositories block by url.booleanChecks repository exists in repositories block by id.booleanhasRepositoryByIdAndUrl(String id, String url) Checks repository exists in repositories block by id and url.booleanhasRepositoryByUrl(String url) Checks repository exists in repositories block by url.booleanChecks whether the build file already declares a task with the given name, either throughtasks.register,tasks.named,tasks.createor thetask name(...)shorthand, or through a JVM test suite (name(JvmTestSuite)orregister('name', JvmTestSuite)insidetesting.suites), which registers a task of the same name.booleanChecks whether the build file uses the Kotlin DSL.voidsave()Apply all modifications to a temporary file and then replace the original.voidupdateDependencyVersion(GradleFileRewriter.Dependency dependency, String newVersion) Updates the dependency version for the provided dependency.voidupdateProperty(GradleFileRewriter.Property property, String newValue) Updates the given property value.
-
Constructor Details
-
GradleFileRewriter
Creates a rewriter for the given Gradle build file.- Parameters:
file- thebuild.gradleorbuild.gradle.ktsfile to be modified- Throws:
IOException- if the file cannot be read
-
-
Method Details
-
getContentWithoutComments
Gets the content of the build file as it was read, without its//and/* *\/comments. Comment markers inside strings are kept.- Returns:
- the build file content without comments
-
isKotlinDsl
public boolean isKotlinDsl()Checks whether the build file uses the Kotlin DSL.- Returns:
truefor abuild.gradle.ktsfile,falsefor a Groovybuild.gradlefile
-
findDependencyByGroupIdAndArtifactId
public GradleFileRewriter.Dependency findDependencyByGroupIdAndArtifactId(String groupId, String artifactId) Finds dependency under dependencies and dependencyManagement blocks by groupId and artifactId.- Parameters:
groupId- group idartifactId- artifact id- Returns:
- dependency if found,
nullotherwise
-
findDependency
Finds the dependency with the given groupId and artifactId inside the dependencies block.- Parameters:
groupId- group idartifactId- artifact id- Returns:
- optional dependency when found
-
getRepositoriesBlock
Checks whether repositories block exists.- Returns:
- block if found,
Optional.empty()otherwise
-
getPluginRepositoriesBlock
Checks whether buildscript repositories block exists.- Returns:
- block if found,
Optional.empty()otherwise
-
hasRepositoryByIdAndUrl
Checks repository exists in repositories block by id and url.- Parameters:
id- repository nameurl- repository url- Returns:
trueif found,falseotherwise
-
hasRepositoryById
Checks repository exists in repositories block by id.- Parameters:
id- repository name- Returns:
trueif found,falseotherwise
-
hasRepositoryByUrl
Checks repository exists in repositories block by url.- Parameters:
url- repository url- Returns:
trueif found,falseotherwise
-
addRepository
Adds repository to build.gradle if it does not exist.- Parameters:
id- repository nameurl- repository url
-
hasPluginRepositoryByIdAndUrl
Checks plugin repository exists in buildscript repositories block by id and url.- Parameters:
id- repository nameurl- repository url- Returns:
trueif found,falseotherwise
-
hasPluginRepositoryById
Checks plugin repository exists in buildscript repositories block by id.- Parameters:
id- repository name- Returns:
trueif found,falseotherwise
-
hasPluginRepositoryByUrl
Checks plugin repository exists in buildscript repositories block by url.- Parameters:
url- repository url- Returns:
trueif found,falseotherwise
-
addPluginRepository
Adds plugin repository to build.gradle if it does not exist.- Parameters:
id- repository nameurl- repository url
-
findPropertyByKey
Finds the property by key.- Parameters:
propertyKey- property key- Returns:
- property if found,
nullotherwise
-
addDependency
Adds dependency to build.gradle if needed.- Parameters:
groupId- group idartifactId- artifact idversion- dependency version, can benull- Returns:
trueif dependency was added,falsewhen it already existed
-
addDependency
public boolean addDependency(String groupId, String artifactId, String version, String configuration) Adds dependency to the build file if needed, asconfiguration 'group:artifact'in a Groovy build file and asconfiguration("group:artifact")in a Kotlin DSL build file.- Parameters:
groupId- group idartifactId- artifact idversion- dependency version, can benullconfiguration- the Gradle configuration to add it to, e.g.testImplementation- Returns:
trueif dependency was added,falsewhen it already existed
-
updateDependencyVersion
Updates the dependency version for the provided dependency.- Parameters:
dependency- dependency to updatenewVersion- new version
-
updateProperty
Updates the given property value.- Parameters:
property- property to updatenewValue- new value
-
hasTask
Checks whether the build file already declares a task with the given name, either throughtasks.register,tasks.named,tasks.createor thetask name(...)shorthand, or through a JVM test suite (name(JvmTestSuite)orregister('name', JvmTestSuite)insidetesting.suites), which registers a task of the same name.- Parameters:
taskName- the task name to look for- Returns:
truewhen the task is declared,falseotherwise
-
addTopLevelBlock
Appends a top level block at the end of the build file.- Parameters:
text- the block to append, already indented relative to column zero
-
save
Apply all modifications to a temporary file and then replace the original.- Throws:
IOException- when a file operation fails
-