hilla icon indicating copy to clipboard operation
hilla copied to clipboard

Create OpenRewrite recipe to help in 2.4 to 24.4 update

Open tarekoraby opened this issue 1 year ago • 9 comments

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

tarekoraby avatar Feb 21 '24 07:02 tarekoraby

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.

marcushellberg avatar Feb 21 '24 17:02 marcushellberg

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

knoobie avatar Feb 29 '24 21:02 knoobie

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

marcushellberg avatar Mar 15 '24 23:03 marcushellberg

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

bennewi avatar Mar 16 '24 06:03 bennewi

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

marcushellberg avatar Mar 16 '24 22:03 marcushellberg

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:

  • having a hierarchical yaml file that can have a top level recipe and a recipeList that contains children recipes.
  • There is no such recipe as org.openrewrite.maven.ChangePropertyKey. It should be replaced by an add + remove operation (this is unfortunate, but since it is in the properties, reviewing this change should not be a big deal for the developers). Maybe there is some regex matching recipe to achieve the same thing without add+removal.

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, ...

taefi avatar Apr 10 '24 14:04 taefi

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

marcushellberg avatar Apr 10 '24 14:04 marcushellberg

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

cromoteca avatar Apr 25 '24 19:04 cromoteca

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

taefi avatar May 02 '24 10:05 taefi