vaadin / hilla

Build better business applications, faster. No more juggling REST endpoints or deciphering GraphQL queries. Hilla seamlessly connects Spring Boot and React to accelerate application development.
https://hilla.dev
Apache License 2.0
864 stars 58 forks source link

Create OpenRewrite recipe to help in 2.4 to 24.4 update #2089

Open tarekoraby opened 4 months ago

tarekoraby commented 4 months ago

Describe your motivation

While most changes involved in upgrading a Hilla project from 2.4 to 24.4 are mechanical, they still require reading many doc lines and making multiple find-and-replace operations.

Describe the solution you'd like

As a convenience, create an OpenRewrite recipe that would make all necessary changes.

Describe alternatives you've considered

No response

Additional context

No response

marcushellberg commented 4 months ago

As a starting point, you could feed https://github.com/vaadin/docs/issues/3186 into ChatGPT and ask it to create OpenRewrite recipes. I tried it and it seemed to produce reasonable recipes.

knoobie commented 4 months ago

The upcoming IDEA also includes an integration for OpenRewrite https://blog.jetbrains.com/idea/2024/02/intellij-idea-2024-1-eap-7/#support-for-openrewrite

marcushellberg commented 3 months ago

Here's my current work-in-progress that I created together with my AI buddies. The only problem is that OpenRewrite says no when I try to run it. Need to continue work next week. If anyone with more OpenRewrite experience has time to take a look and can tell me what's wrong, that would be greatly appreciated.

rewrite.yml.txt

bennewi commented 3 months ago

what part isn't working, is it the UpdateMavenPom or the UpdateGradleConfiguration or the UpdateJavaSources or the UpdateFrontendSources or the MoveFrontendFolder?

marcushellberg commented 3 months ago

When I try to run it, I get the following for each of the "sub-recipes" (not sure what the correct term is here)

[ERROR] Recipe validation error in type: Declarative recipeList entries are expected to be strings or mappings
[ERROR] Recipe validation error in type: Declarative recipeList entries are expected to be strings or mappings
[ERROR] Recipe validation error in type: Declarative recipeList entries are expected to be strings or mappings
[ERROR] Recipe validation error in type: Declarative recipeList entries are expected to be strings or mappings
[ERROR] Recipe validation error in type: Declarative recipeList entries are expected to be strings or mappings
[ERROR] Recipe validation error in type: Declarative recipeList entries are expected to be strings or mappings
taefi commented 2 months ago

Never used OpenRewrite before, so maybe some of the things I'm writing here are not entirely correct. Tried gathering some recipes for just updating the POM file by using the ChatGPT4, but ended up getting the exact same errors. Then noticed this issue and tried the provided rewrite.yml.txt and noticed that it contains some AI's hallucinations or recipes that no longer exist, similar to what I got from ChatGPT4.

Examples of things that didn't work for me at all:

As an example of minimum changes needed in POM file to update the some areas, the following worked for me:

type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.HillaReactToVaadin244
displayName: Migrate Hilla-React 2.5 Project to Vaadin 24.4
description: This recipe migrates a Hilla-React 2.5 project to Vaadin 24.4, applying all necessary configuration and source code changes.
recipeList:
  - type: specs.openrewrite.org/v1beta/recipe
    name: com.vaadin.migration.UpdateMavenPom
    displayName: Update Maven POM for Vaadin 24.4
    description: Updates Maven pom.xml files for the migration to Vaadin 24.4, handling property changes, dependency updates, and plugin replacements.
    recipeList:
      - org.openrewrite.maven.AddProperty:
          key: vaadin.version
          value: 24.4.0.alpha21
      - org.openrewrite.maven.RemoveProperty:
          propertyName: hilla.version
      - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
          oldGroupId: dev.hilla
          oldArtifactId: hilla-bom
          newGroupId: com.vaadin
          newArtifactId: vaadin-bom
          newVersion: ${vaadin.version}
          changeManagedDependency: true
      - org.openrewrite.maven.RemoveDependency:
          groupId: dev.hilla
          artifactId: hilla-react
      - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
          oldGroupId: dev.hilla
          oldArtifactId: hilla-spring-boot-starter
          newGroupId: com.vaadin
          newArtifactId: vaadin-spring-boot-starter
      - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
          oldGroupId: dev.hilla
          oldArtifactId: hilla-react-spring-boot-starter
          newGroupId: com.vaadin
          newArtifactId: vaadin-spring-boot-starter

I'll continue working on this to adopt other needed changes such as renaming the maven plugin, etc. Not sure, if it is a good idea for maintenance purposes to include everything in the same file or not. I will try separate files for separate scopes: Updating the POM, updating Java files, Updating TS and frontend related files, ...

marcushellberg commented 2 months ago

Here's how far I got when I tried. It requires you to move the frontend folder manually as there's no recipe for moving files.

type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.HillaToVaadin244
displayName: Migrate Hilla Project to Vaadin 24.4
description: This recipe migrates a Hilla project to Vaadin 24.4, applying all necessary configuration and source code changes.
recipeList:
  - com.vaadin.migration.UpdateDependencies
  - com.vaadin.migration.UpdateJavaSources
  - com.vaadin.migration.UpdateFrontendSources
---
type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.UpdateDependencies
displayName: Update Dependencies for Vaadin 24.4
description: Updates Maven or Gradle files for the migration to Vaadin 24.4, handling property changes, dependency updates, and plugin replacements.
recipeList:
  - org.openrewrite.maven.RenamePropertyKey:
        oldKey: hilla.version
        newKey: vaadin.version
  - org.openrewrite.maven.ChangePropertyValue:
        key: vaadin.version
        newValue: 24.4.0
  - org.openrewrite.java.dependencies.ChangeDependency:
      oldGroupId: "dev.hilla"
      oldArtifactId: "hilla-bom"
      newGroupId: "com.vaadin"
      newArtifactId: "vaadin-bom"
      newVersion: "${vaadin.version}"
  - org.openrewrite.java.dependencies.ChangeDependency:
      oldGroupId: "dev.hilla"
      oldArtifactId: "hilla"
      newGroupId: "com.vaadin"
      newArtifactId: "vaadin"
  - org.openrewrite.java.dependencies.ChangeDependency:
      oldGroupId: "dev.hilla"
      oldArtifactId: "hilla-spring-boot-starter"
      newGroupId: "com.vaadin"
      newArtifactId: "vaadin-spring-boot-starter"
  - org.openrewrite.java.dependencies.ChangeDependency:
      oldGroupId: "dev.hilla"
      oldArtifactId: "hilla-react-spring-boot-starter"
      newGroupId: "com.vaadin"
      newArtifactId: "vaadin-spring-boot-starter"
  - org.openrewrite.maven.ChangePluginGroupIdAndArtifactId:
      oldGroupId: "dev.hilla"
      oldArtifactId: "hilla-maven-plugin"
      newGroupId: "com.vaadin"
      newArtifact: "vaadin-maven-plugin"
  - org.openrewrite.java.dependencies.RemoveDependency:
      groupId: "dev.hilla"
      artifactId: "hilla-react"

---
type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.UpdateJavaSources
displayName: Update Java Sources for Vaadin 24.4
description: Updates Java source files, replacing Hilla namespaces with Vaadin namespaces.
recipeList:
  - org.openrewrite.java.ChangePackage:
      oldPackageName: dev.hilla
      newPackageName: com.vaadin.hilla
---
type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.UpdateFrontendSources
displayName: Update Frontend Sources for Vaadin 24.4
description: Updates TypeScript sources, replacing Hilla imports with their new Vaadin equivalents and adjusting paths as part of the migration to Vaadin 24.4.
recipeList:
  - org.openrewrite.text.FindAndReplace:
      filePattern: "**/*.{ts,tsx}"
      find: "@hilla/frontend"
      replace: "@vaadin/hilla-frontend"
  - org.openrewrite.text.FindAndReplace:
      filePattern: "**/*.{ts,tsx}"
      find: "@hilla/form"
      replace: "@vaadin/hilla-lit-form"
  - org.openrewrite.text.FindAndReplace:
      filePattern: "**/*.{ts,tsx}"
      find: "@hilla/react-form"
      replace: "@vaadin/hilla-react-form"
  - org.openrewrite.text.FindAndReplace:
      filePattern: "**/*.{ts,tsx}"
      find: "Frontend/generated/dev/hilla"
      replace: "Frontend/generated/com/vaadin/hilla"
  - org.openrewrite.text.FindAndReplace:
      filePattern: "**/*.{ts,tsx}"
      find: "@hilla/(.*)"
      replace: "@vaadin/hilla-$1"
      regex: true
cromoteca commented 2 months ago

I modified the recipe above and tested it on basic projects. The current form is:

type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.HillaToVaadin244
displayName: Migrate Hilla Project to Vaadin 24.4
description: This recipe migrates a Hilla project to Vaadin 24.4, applying all necessary configuration and source code changes.
recipeList:
  - org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2
  - com.vaadin.migration.UpdateDependencies
  - com.vaadin.migration.UpdateJavaSources
  - com.vaadin.migration.UpdateFrontendSources
---
type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.UpdateDependencies
displayName: Update Dependencies for Vaadin 24.4
description: Updates Maven or Gradle files for the migration to Vaadin 24.4, handling property changes, dependency updates, and plugin replacements.
recipeList:
  - org.openrewrite.maven.RenamePropertyKey:
        oldKey: hilla.version
        newKey: vaadin.version
  - org.openrewrite.maven.ChangePropertyValue:
        key: vaadin.version
        newValue: 24.4.1
  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
      oldGroupId: 'dev.hilla'
      oldArtifactId: 'hilla-bom'
      newGroupId: 'com.vaadin'
      newArtifactId: 'vaadin-bom'
      newVersion: '${vaadin.version}'
  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
      oldGroupId: 'dev.hilla'
      oldArtifactId: 'hilla'
      newGroupId: 'com.vaadin'
      newArtifactId: 'vaadin'
  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
      oldGroupId: 'dev.hilla'
      oldArtifactId: 'hilla-spring-boot-starter'
      newGroupId: 'com.vaadin'
      newArtifactId: 'vaadin-spring-boot-starter'
  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
      oldGroupId: 'dev.hilla'
      oldArtifactId: 'hilla-react-spring-boot-starter'
      newGroupId: 'com.vaadin'
      newArtifactId: 'vaadin-spring-boot-starter'
  - org.openrewrite.maven.ChangePluginGroupIdAndArtifactId:
      oldGroupId: 'dev.hilla'
      oldArtifactId: 'hilla-maven-plugin'
      newGroupId: 'com.vaadin'
      newArtifact: 'vaadin-maven-plugin'
  - org.openrewrite.maven.RemoveDependency:
      groupId: 'dev.hilla'
      artifactId: 'hilla-react'

---
type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.UpdateJavaSources
displayName: Update Java Sources for Vaadin 24.4
description: Updates Java source files, replacing Hilla namespaces with Vaadin namespaces.
recipeList:
  - org.openrewrite.java.ChangePackage:
      oldPackageName: dev.hilla
      newPackageName: com.vaadin.hilla
---
type: specs.openrewrite.org/v1beta/recipe
name: com.vaadin.migration.UpdateFrontendSources
displayName: Update Frontend Sources for Vaadin 24.4
description: Updates TypeScript sources, replacing Hilla imports with their new Vaadin equivalents and adjusting paths as part of the migration to Vaadin 24.4.
recipeList:
  - org.openrewrite.text.FindAndReplace:
      find: '@hilla/form'
      replace: '@vaadin/hilla-lit-form'
      filePattern: '**/*.{ts,tsx}'
  - org.openrewrite.text.FindAndReplace:
      find: '@hilla/react-components'
      replace: '@vaadin/react-components'
      filePattern: '**/*.{ts,tsx}'
  - org.openrewrite.text.FindAndReplace:
      find: 'Frontend/generated/dev/hilla'
      replace: 'Frontend/generated/com/vaadin/hilla'
      filePattern: '**/*.{ts,tsx}'
  - org.openrewrite.text.FindAndReplace:
      find: '@hilla/'
      replace: '@vaadin/hilla-'
      filePattern: '**/*.{ts,tsx}'

To run it, paste it into a rewrite.yml file in the root of your project, then run this command:

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
    -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:LATEST \
    -Drewrite.activeRecipes=com.vaadin.migration.HillaToVaadin244
taefi commented 2 months ago

@cromoteca Were there any efforts to publish this officially on the https://docs.openrewrite.org/recipes/ ?