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.
  • Constructor Details

    • GradleFileRewriter

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

    • 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
    • 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
    • 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