generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate client factories for each client
- Loading branch information
1 parent
9374e7c
commit 1d17a35
Showing
17 changed files
with
338 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
buildSrc/src/main/java/io/micronaut/oraclecloud/gradle/GenerateClientFactories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package io.micronaut.oraclecloud.gradle; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.file.ConfigurableFileCollection; | ||
import org.gradle.api.file.Directory; | ||
import org.gradle.api.file.DirectoryProperty; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.provider.Provider; | ||
import org.gradle.api.tasks.CacheableTask; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.InputFiles; | ||
import org.gradle.api.tasks.Internal; | ||
import org.gradle.api.tasks.OutputDirectory; | ||
import org.gradle.api.tasks.PathSensitive; | ||
import org.gradle.api.tasks.PathSensitivity; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.stream.Collectors; | ||
|
||
@CacheableTask | ||
public abstract class GenerateClientFactories extends DefaultTask { | ||
|
||
private static final String TEMPLATE = """ | ||
package {{package}}; | ||
import io.micronaut.oraclecloud.clients.SdkClients; | ||
import io.micronaut.core.annotation.Internal; | ||
@Internal | ||
final class SdkProcessorDummy { | ||
@SdkClients(value = SdkClients.Kind.ASYNC, clientClasses = {{clientClassNames}}) | ||
final static class SdkAsyncProcessorDummy { | ||
} | ||
@SdkClients(value = SdkClients.Kind.REACTOR, clientClasses = {{clientClassNames}}) | ||
final static class SdkReactorProcessorDummy { | ||
} | ||
@SdkClients(value = SdkClients.Kind.RXJAVA2, clientClasses = {{clientClassNames}}) | ||
final static class SdkRxProcessorDummy { | ||
} | ||
} | ||
"""; | ||
|
||
@PathSensitive(PathSensitivity.NONE) | ||
@InputFiles | ||
public abstract ConfigurableFileCollection getSources(); | ||
|
||
@Input | ||
public abstract Property<String> getPackageName(); | ||
|
||
@Internal | ||
public abstract DirectoryProperty getBaseOutputDirectory(); | ||
|
||
@OutputDirectory | ||
public Provider<Directory> getOutputDirectory() { | ||
return getBaseOutputDirectory(); | ||
} | ||
|
||
@TaskAction | ||
public void generateClientFactories() { | ||
String clientNames = getSources().getAsFileTree() | ||
.matching((filterable) -> | ||
filterable.include("com/oracle/bmc/*/*Client.java") | ||
) | ||
.getFiles() | ||
.stream() | ||
.map(f -> '"' + getPackage(f) + "." + getClientName(f) + '"') | ||
.collect(Collectors.joining(", ")); | ||
|
||
String pack = getPackageName().get(); | ||
|
||
if (!clientNames.isEmpty()) { | ||
String a = "3"; | ||
File outputFile = getOutputDirectory().get() | ||
.file(pack.replace('.', File.separatorChar) + "/SdkProcessorDummy.java") | ||
.getAsFile(); | ||
|
||
String content = TEMPLATE.replace("{{package}}", pack) | ||
.replace("{{clientClassNames}}", "{" + clientNames + "}"); | ||
writeToFile(outputFile, content); | ||
} | ||
} | ||
|
||
private void writeToFile(File file, String content) { | ||
file.getParentFile().mkdirs(); | ||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { | ||
writer.write(content); | ||
} catch (IOException e) { | ||
getLogger().error("Failed to write to file", e); | ||
} | ||
} | ||
|
||
private String getClientName(File file) { | ||
String name = file.getName(); | ||
if (name.endsWith(".java")) { | ||
name = name.substring(0, name.length() - ".java".length()); | ||
} | ||
return name; | ||
} | ||
|
||
private String getPackage(File file) { | ||
String path = file.getParentFile().getAbsolutePath(); | ||
return path.substring(path.indexOf("com/oracle/bmc")).replace(File.separatorChar, '.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...nt-netty/src/test/groovy/io/micronaut/oraclecloud/factory/ObjectStorageFactorySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.micronaut.oraclecloud.factory; | ||
|
||
import com.oracle.bmc.objectstorage.ObjectStorageAsyncClient | ||
import com.oracle.bmc.objectstorage.ObjectStorageClient | ||
import io.micronaut.context.event.BeanCreatedEvent | ||
import io.micronaut.context.event.BeanCreatedEventListener | ||
import io.micronaut.core.annotation.NonNull | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import jakarta.inject.Singleton | ||
import spock.lang.Specification | ||
|
||
|
||
@MicronautTest | ||
class ObjectStorageFactorySpec extends Specification { | ||
|
||
public static final String ENDPOINT = "http://my_endpoint" | ||
public static final String ASYNC_ENDPOINT = "http://my_endpoint_async" | ||
|
||
@Inject | ||
ObjectStorageClient client | ||
|
||
@Inject | ||
ObjectStorageAsyncClient asyncClient | ||
|
||
void "test database client configuration"() { | ||
when: | ||
client | ||
|
||
then: | ||
client != null | ||
client.endpoint == ENDPOINT | ||
} | ||
|
||
void "test database async client configuration"() { | ||
when: | ||
asyncClient | ||
|
||
then: | ||
asyncClient != null | ||
asyncClient.endpoint == ASYNC_ENDPOINT | ||
} | ||
|
||
@Singleton | ||
static class DatabaseClientBuilderListener | ||
implements BeanCreatedEventListener<ObjectStorageClient.Builder> { | ||
@Override | ||
ObjectStorageClient.Builder onCreated( | ||
@NonNull BeanCreatedEvent<ObjectStorageClient.Builder> event | ||
) { | ||
var builder = event.bean | ||
builder.endpoint(ENDPOINT) | ||
return builder | ||
} | ||
} | ||
|
||
@Singleton | ||
static class DatabaseAsyncClientBuilderListener | ||
implements BeanCreatedEventListener<ObjectStorageAsyncClient.Builder> { | ||
@Override | ||
ObjectStorageAsyncClient.Builder onCreated( | ||
@NonNull BeanCreatedEvent<ObjectStorageAsyncClient.Builder> event | ||
) { | ||
var builder = event.bean | ||
builder.endpoint(ASYNC_ENDPOINT) | ||
return builder | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
plugins { | ||
id 'io.micronaut.build.internal.oraclecloud-module' | ||
id("io.micronaut.build.internal.oraclecloud-bom") | ||
} | ||
|
||
spotlessJavaMiscCheck.enabled = false | ||
compileJava.options.fork = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.