Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port OpenRewrite to Main #43884

Merged
merged 17 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sdk/tools/azure-openrewrite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Release History

## 1.0.0-beta.1 (Unreleased)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
102 changes: 102 additions & 0 deletions sdk/tools/azure-openrewrite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Azure Code Migration with OpenRewrite

This repository showcases the integration of OpenRewrite with Maven for code migration purposes.
The migration recipe is defined to transition from `azure-core` to `azure-core-v2` libraries.
jairmyree marked this conversation as resolved.
Show resolved Hide resolved

## Setup

### Prerequisites
The following tools are required to build and execute this project:
- Java (version 8 or higher)
- Maven

### Recipe Configuration

The migration recipe is defined in the `rewrite-java-core` module as detailed below:

```yaml
### Recipe Configuration for OpenRewrite
type: specs.openrewrite.org/v1beta/recipe
name: com.azure.rewrite.java.core.MigrateAzureCoreSamplesToAzureCoreV2
displayName: Migrate azure-core samples to azure-core-v2
description: This recipe migrates the samples in azure-core to azure-core-v2
recipeList:
...
```
You can find the full recipe configuration in the `rewrite.yml` file [here](https://github.com/Azure/azsdk-java-rewrite-recipes/blob/main/rewrite-java-core/src/main/resources/META-INF/rewrite/rewrite.yml).


## Usage
### Maven Plugin Configuration
The OpenRewrite Maven plugin is configured in the `rewrite-java-core` module to run the migration recipe on the sample project
as follows:
```xml
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.7.1</version>
<configuration>
<activeRecipes>
<recipe>com.azure.rewrite.java.core.MigrateAzureCoreSamplesToAzureCoreV2</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>rewrite-java-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
```
The plugin configuration is defined in the `pom.xml` file [here](https://github.com/Azure/azsdk-java-rewrite-recipes/blob/main/rewrite-sample/pom.xml).

## Execution
The `rewrite-sample` module is configured to use the `openrewrite-maven-plugin` to run the OpenRewrite recipe on the sample project.
The `rewrite-sample` module contains the modules `azure-ai-translation-text-v1` and `azure-ai-translation-text-v2`
to demonstrate the migration of code from `azure-core` to `azure-core-v2`.

**Note:** To execute the below commands, ensure that you are within the `rewrite-sample` directory.

### Dry Run
To run the OpenRewrite recipe in dry-run mode, execute the following command:
```shell
mvn rewrite:dryRun
```
If the above command is not recognised, execute the full version of the command:

```shell
mvn org.openrewrite.maven:rewrite-maven-plugin:dryRun
```

This will generate a file `rewrite.patch` in `rewrite-sample/target/rewrite` directory.

### Run (apply changes)
To actually apply the changes to the sample project, execute the following command:
```shell
mvn rewrite:run
```
If the above command is not recognised, execute the full version of the command:

```shell
mvn org.openrewrite.maven:rewrite-maven-plugin:run
```

## Testing
To run the unit tests for the OpenRewrite recipe, execute the following command:
```shell
mvn:test
```


## Openrewrite Reference
- [Rewrite Recipe Starter](https://github.com/moderneinc/rewrite-recipe-starter): Template for building your own recipe JARs
- [Best practices for writing recipes](https://docs.openrewrite.org/recipes/recipes/openrewritebestpractices)
- [Recipe Testing](https://docs.openrewrite.org/authoring-recipes/recipe-testing): Information on testing the recipe with unit tests.
- [Collaboration Proposal](https://github.com/openrewrite/collaboration-proposals/issues/new/choose): collaboration with OpenRewrite
jairmyree marked this conversation as resolved.
Show resolved Hide resolved






137 changes: 137 additions & 0 deletions sdk/tools/azure-openrewrite/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<!-- Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.azure</groupId>
<artifactId>azure-openrewrite</artifactId>
<version>1.0.0</version>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved

<name>azure-openrewrite</name>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recipe-bom</artifactId>
<version>3.0.2</version>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- rewrite-java dependencies only necessary for Java Recipe development -->
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java</artifactId>
<scope>compile</scope>
</dependency>

<!-- You only need the version that corresponds to your current
Java version. It is fine to add all of them, though, as
they can coexist on a classpath. -->
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java-17</artifactId>
<scope>runtime</scope>
</dependency>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved

<!-- rewrite-maven dependency only necessary for Maven Recipe development -->
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.54.1</version> <!-- {x-version-update;com.azure:azure-core;dependency} -->
</dependency>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<!-- Optional dependency on assertJ to provide fluent assertions. -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.2</version>
</dependency>
<!-- Jackson Dataformat Smile -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-smile</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

<dependency>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.azure</groupId>
<artifactId>azure-ai-translation-text</artifactId>
<version>1.0.0-beta.1</version>
<scope>test</scope>
</dependency>
<!-- Alternatively, use this project version -->
<!--
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-translation-text-v1</artifactId>
<version>1.0.0-beta.1</version>
<scope>test</scope>
</dependency>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
-->
</dependencies>

<build>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
<plugins>
<!--
Used to extract PMD report metrics for the recipes
* Run with mvn pmd:pmd
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.15.0</version>
</plugin>
jairmyree marked this conversation as resolved.
Show resolved Hide resolved
</plugins>
</build>
</project>
Loading
Loading