Skip to content
Open
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
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,56 @@ Tax provider capabilities for new tax providers will be validated against a cons
6. After PR is approved and merged
7. Raise PR from release/0.0.9 to dev. Once PR is merged it will auto release the 0.0.9 version of SPI for dev code base
8. After that raise PR from release/0.0.9 to main. Once PR is merged it will auto release the 0.0.9 version of SPI for prod codebase


## Steps to generate a spring boot project for given spec using gradle

Prerequisites:
Use Java11 or higher version with gradle version greater than or equal to 7.
For gradle version less than 7 use Java8.

1. Clone repository in local
```shell
git clone [email protected]:chargebee/cb-provider-spi.git
```

2. Navigate to the repository
```shell
cd cb-provider-spi
```

3. (a) Generate the Gradle project structure with default configurations:
```shell
gradle -b meta-generator.gradle generateGradleProject
```

3. (b) Generate the gradle project with custom configurations use below commands. (Custom params if not provided, it will pick it from generator.config file)
```shell
gradle -b meta-generator.gradle generateGradleProject \
-PspringBootVersion=2.7.1 \
-PopenApiGeneratorVersion=7.0.1 \
-PjavaVersion=11 \
-PgroupId=com.adapter \
-PbasePackage=com.adapter.api \
-PoutputDir=../adapter
```

4. After generating the Gradle project, you can navigate to the output directory and run:
```shell
gradle openApiGenerateFullProject
```

## Generating Spring boot project using shell script

1. Perform the steps 1 and 2 as mentioned in above section.

2. Execute the script file setup_spring_boot.sh using below command:
```shell
chmod +x setup_spring_boot.sh
```
3. Run the script:
```shell
./setup_spring_boot.sh
Copy link
Contributor

@cb-suryak cb-suryak Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup_adapter_spring_boot_project.sh <output_directory> <package_name>

Share with an example
sh setup_adapter_spring_boot_project.sh java8 ~/work/ com.org

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

```


11 changes: 11 additions & 0 deletions generator.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
outputDir: ../adapter
springBootVersion: 2.7.0
springDependencyManagementVersion: 1.0.11.RELEASE
openApiGeneratorVersion: 7.0.1
javaVersion: 11
groupId: com.adapter
basePackage: com.adapter
specs:
- openapi_location_validation.yml
- openapi_tax.yml
- openapi_trn.yml
141 changes: 141 additions & 0 deletions meta-generator.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.yaml:snakeyaml:1.30'
}
}

import org.yaml.snakeyaml.Yaml

def configFile = file('generator.config')
def config = new Yaml().load(configFile.text)

// Define command-line options
def cliOptions = [
'springBootVersion',
'springDependencyManagementVersion',
'openApiGeneratorVersion',
'javaVersion',
'groupId',
'version',
'basePackage',
'outputDir'
]

// Override config with command-line arguments if provided
cliOptions.each { option ->
if (project.hasProperty(option)) {
config[option] = project.property(option)
}
}

def outputDir = file(config.outputDir ?: '../adapter')

task generateGradleProject {
doLast {
// Create the output directory if it doesn't exist
outputDir.mkdirs()

// Create build.gradle
def buildGradleFile = new File(outputDir, 'build.gradle')
buildGradleFile.text = """
plugins {
id 'org.springframework.boot' version '${config.springBootVersion}'
id 'io.spring.dependency-management' version '${config.springDependencyManagementVersion}'
id 'java'
id 'org.openapi.generator' version '${config.openApiGeneratorVersion}'
}

group = '${config.groupId}'
version = '${config.version}'
sourceCompatibility = '${config.javaVersion}'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

def specDir = '${projectDir}/../cb-provider-spi/spec/spi'
def specs = ${config.specs.inspect()}

specs.each { spec ->
def taskName = "generate\${spec.replace('.yml', '').capitalize()}"
tasks.register(taskName, org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = 'spring'
inputSpec = "\$specDir/\$spec"
outputDir = "\$projectDir/src/main/java"
apiPackage = "${config.basePackage}.api.\${spec.replace('.yml', '').replace('_', '')}"
modelPackage = "${config.basePackage}.model.\${spec.replace('.yml', '').replace('_', '')}"
configOptions = [
dateLibrary: 'java8',
interfaceOnly: 'true',
useSpringBoot3: 'true',
groupBy: 'tags'
]
}
}

tasks.named('openApiGenerate').configure {
enabled = false
}

tasks.register('openApiGenerateFullProject') {
dependsOn tasks.withType(org.openapitools.generator.gradle.plugin.tasks.GenerateTask)
}

sourceSets {
main {
java {
srcDir "src/main/java"
}
}
}

compileJava.dependsOn tasks.openApiGenerateFullProject
"""

// Create settings.gradle
def settingsGradleFile = new File(outputDir, 'settings.gradle')
settingsGradleFile.text = """
rootProject.name = '${config.groupId.tokenize('.').last()}'
"""

// Create gradle.properties
def gradlePropertiesFile = new File(outputDir, 'gradle.properties')
gradlePropertiesFile.text = """
org.gradle.parallel=true
org.gradle.caching=true
"""

// Create main application class
def mainClassDir = new File(outputDir, "src/main/java/${config.basePackage.replace('.', '/')}")
mainClassDir.mkdirs()
def mainClassFile = new File(mainClassDir, "Application.java")
mainClassFile.text = """
package ${config.basePackage};

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
"""

// Create src directory structure
new File(outputDir, 'src/main/resources').mkdirs()
new File(outputDir, 'src/test/java').mkdirs()
new File(outputDir, 'src/test/resources').mkdirs()

println "Generated Gradle project structure at ${outputDir.absolutePath}"
}
}
31 changes: 31 additions & 0 deletions setup_spring_boot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Function to log messages
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}

# Function to run a command and log its output
run_command() {
log "Running command: $1"
eval $1
if [ $? -ne 0 ]; then
log "Error: Command failed"
exit 1
fi
log "Command completed successfully"
}

# Step 1: Generate Gradle project
log "Step 1: Generating Gradle project"
run_command "gradle -b meta-generator.gradle generateGradleProject"

# Step 2: Generate OpenAPI code
log "Step 2: Generating OpenAPI code"
run_command "cd ../adapter && gradle openApiGenerateFullProject"

# Step 3: Run Spring Boot application
log "Step 3: Running Spring Boot application"
run_command "cd ../adapter && gradle bootRun"

log "Script completed successfully"