openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.15k stars 320 forks source link

Newbie question: does openwrite necessarily need to be applied as project plugin? #446

Closed kkom closed 3 years ago

kkom commented 3 years ago

Hey!

I have a newbie question – does openwrite necessary need to be applied as a plugin to a Maven or Gradle project?

The number of applications that we maintain is growing and we'd like to be able to apply some refactors across all of them – without having to add openrewrite and, more importantly, the specific recipes to each individual Gradle project. Similar to how tools like fastmod, jscodeshift or comby can just work on any source code provided to them.

Are there any fundamental reasons why that wouldn't be possible with openrewrite? I realise that it analyses the AST, which may be making it difficult. On the other hand, jscodeshift works on AST as well (but JavaScript is an interpreted language...). Again, please forgive my ignorance as I'm new to Java. :)

To be concrete, we're looking at integrating openrewrite with a framework like batch changes - which works based on specs that define a series of container-based refactoring steps: https://docs.sourcegraph.com/batch_changes/how-tos/creating_a_batch_change#writing-a-batch-spec

For example:

steps:
  - run: fastmod --accept-all https://gogle.com https://google.com
    container: kkom/fastmod:0.4.1

Would appreciate any thoughts on whether what we're after is even close to being feasible!

Cheers, Konrad

CC: @igor-sakhankov @gastonfournier

jkschneider commented 3 years ago

Hi @kkom!

does openwrite necessary need to be applied as a plugin to a Maven or Gradle project?

The simple answer is no. The build tool plugins are one of several different possible workflows to execute recipes on a repository or set of repositories.

There's a team at Moderne working on applying Rewrite recipes in batch across large swaths of code outside of the build: https://youtu.be/s8uR-EmxMUU.

The basic requirement is to have some process to produce the AST (which can be done in batch itself), store them off somewhere in a serialized form, deserialize them and execute the recipe.

More detail

On the other hand, jscodeshift works on AST as well

Rewrite's AST operates on a type-attributed AST to be able to make accurate changes. This requires the project's classpath of binary dependencies to produce. Whatever mechanism is used for production of ASTs needs to be aware of the classpath, and build tools are of course designed to produce this.

without having to add openrewrite and, more importantly, the specific recipes to each individual Gradle project.

This is tough with Gradle unless your organization has a central plugin that it distributes to all team. With Maven, we typically see rewrite distributed as part of a parent POM that is shared amongst teams.

Happy to chat more!

kkom commented 3 years ago

Thank you so much for your quick and extensive response @jkschneider ! We're going to give it a try from a Docker container then and report back / ask any questions if some interesting problem comes up :)

jkschneider commented 3 years ago

Sounds good! You can always catch us on Rewrite slack too if you'd like: https://join.slack.com/t/rewriteoss/shared_invite/zt-nj42n3ea-b~62rIHzb3Vo0E1APKCXEA