Class GradleFileRewriter

java.lang.Object
com.vaadin.copilot.GradleFileRewriter

public class GradleFileRewriter extends Object
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.

  • Constructor Details

    • GradleFileRewriter

      public GradleFileRewriter(Path file) throws IOException
      Creates a rewriter for the given Gradle build file.
      Parameters:
      file - the build.gradle or build.gradle.kts file to be modified
      Throws:
      IOException - if the file cannot be read
  • Method Details

    • getContentWithoutComments

      public String 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:
      true for a build.gradle.kts file, false for a Groovy build.gradle file
    • findDependencyByGroupIdAndArtifactId

      public GradleFileRewriter.Dependency findDependencyByGroupIdAndArtifactId(String groupId, String artifactId)
      Finds dependency under dependencies and dependencyManagement blocks by groupId and artifactId.
      Parameters:
      groupId - group id
      artifactId - artifact id
      Returns:
      dependency if found, null otherwise
    • findDependency

      public Optional<GradleFileRewriter.Dependency> findDependency(String groupId, String artifactId)
      Finds the dependency with the given groupId and artifactId inside the dependencies block.
      Parameters:
      groupId - group id
      artifactId - artifact id
      Returns:
      optional dependency when found
    • getRepositoriesBlock

      public Optional<GradleFileRewriter.Block> getRepositoriesBlock()
      Checks whether repositories block exists.
      Returns:
      block if found, Optional.empty() otherwise
    • getPluginRepositoriesBlock

      public Optional<GradleFileRewriter.Block> getPluginRepositoriesBlock()
      Checks whether buildscript repositories block exists.
      Returns:
      block if found, Optional.empty() otherwise
    • hasRepositoryByIdAndUrl

      public boolean hasRepositoryByIdAndUrl(String id, String url)
      Checks repository exists in repositories block by id and url.
      Parameters:
      id - repository name
      url - repository url
      Returns:
      true if found, false otherwise
    • hasRepositoryById

      public boolean hasRepositoryById(String id)
      Checks repository exists in repositories block by id.
      Parameters:
      id - repository name
      Returns:
      true if found, false otherwise
    • hasRepositoryByUrl

      public boolean hasRepositoryByUrl(String url)
      Checks repository exists in repositories block by url.
      Parameters:
      url - repository url
      Returns:
      true if found, false otherwise
    • addRepository

      public void addRepository(String id, String url)
      Adds repository to build.gradle if it does not exist.
      Parameters:
      id - repository name
      url - repository url
    • hasPluginRepositoryByIdAndUrl

      public boolean hasPluginRepositoryByIdAndUrl(String id, String url)
      Checks plugin repository exists in buildscript repositories block by id and url.
      Parameters:
      id - repository name
      url - repository url
      Returns:
      true if found, false otherwise
    • hasPluginRepositoryById

      public boolean hasPluginRepositoryById(String id)
      Checks plugin repository exists in buildscript repositories block by id.
      Parameters:
      id - repository name
      Returns:
      true if found, false otherwise
    • hasPluginRepositoryByUrl

      public boolean hasPluginRepositoryByUrl(String url)
      Checks plugin repository exists in buildscript repositories block by url.
      Parameters:
      url - repository url
      Returns:
      true if found, false otherwise
    • addPluginRepository

      public void addPluginRepository(String id, String url)
      Adds plugin repository to build.gradle if it does not exist.
      Parameters:
      id - repository name
      url - repository url
    • findPropertyByKey

      public GradleFileRewriter.Property findPropertyByKey(String propertyKey)
      Finds the property by key.
      Parameters:
      propertyKey - property key
      Returns:
      property if found, null otherwise
    • addDependency

      public boolean addDependency(String groupId, String artifactId, String version)
      Adds dependency to build.gradle if needed.
      Parameters:
      groupId - group id
      artifactId - artifact id
      version - dependency version, can be null
      Returns:
      true if dependency was added, false when it already existed
    • addDependency

      public boolean addDependency(String groupId, String artifactId, String version, String configuration)
      Adds dependency to the build file if needed, as configuration 'group:artifact' in a Groovy build file and as configuration("group:artifact") in a Kotlin DSL build file.
      Parameters:
      groupId - group id
      artifactId - artifact id
      version - dependency version, can be null
      configuration - the Gradle configuration to add it to, e.g. testImplementation
      Returns:
      true if dependency was added, false when it already existed
    • updateDependencyVersion

      public void updateDependencyVersion(GradleFileRewriter.Dependency dependency, String newVersion)
      Updates the dependency version for the provided dependency.
      Parameters:
      dependency - dependency to update
      newVersion - new version
    • updateProperty

      public void updateProperty(GradleFileRewriter.Property property, String newValue)
      Updates the given property value.
      Parameters:
      property - property to update
      newValue - new value
    • hasTask

      public boolean hasTask(String taskName)
      Checks whether the build file already declares a task with the given name, either through tasks.register, tasks.named, tasks.create or the task name(...) shorthand, or through a JVM test suite (name(JvmTestSuite) or register('name', JvmTestSuite) inside testing.suites), which registers a task of the same name.
      Parameters:
      taskName - the task name to look for
      Returns:
      true when the task is declared, false otherwise
    • addTopLevelBlock

      public void addTopLevelBlock(String text)
      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

      public void save() throws IOException
      Apply all modifications to a temporary file and then replace the original.
      Throws:
      IOException - when a file operation fails