Skip to content

Commit

Permalink
generating entities into a different package
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Jul 28, 2021
1 parent 4773308 commit 2f0935f
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/guide/guide.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ configurations {
}

dependencies {
asciidoctorExtensions 'com.bmuschko:asciidoctorj-tabbed-code-extension:0.2'
asciidoctorExtensions 'com.bmuschko:asciidoctorj-tabbed-code-extension:0.3'
}

asciidoctor {
Expand Down
32 changes: 21 additions & 11 deletions docs/guide/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
[.ribbon]
image::ribbon.png[link={project-url}]

--
image::https://api.bintray.com/packages/agorapulse/libs/{project-name]/images/download.svg[link="https://bintray.com/agorapulse/libs/{project-name}/_latestVersion",float="left"]
image::https://github.com/{project-slug}/workflows/Check/badge.svg["Build Status", link="https://github.com/{project-slug}/actions?query=workflow%3ACheck"float="left"]
image::https://coveralls.io/repos/github/{project-slug}/badge.svg?branch=master[link=https://coveralls.io/github/{project-slug}?branch=master",float="left"]
--

image::https://img.shields.io/maven-central/v/com.agorapulse/micronaut-grails.svg?label=Maven%20Central[link="https://search.maven.org/search?q=g:%22com.agorapulse%22%20AND%20a:%22micronaut-grails%22",float="left"]
image::https://github.com/agorapulse/micronaut-grails/workflows/Check/badge.svg["Build Status", link="https://github.com/agorapulse/micronaut-grails/actions?query=workflow%3ACheck"float="left"]
image::https://coveralls.io/repos/github/agorapulse/micronaut-grails/badge.svg?branch=master[link=https://coveralls.io/github/agorapulse/micronaut-grails?branch=master",float="left"]
---

= Micronaut Grails
Expand Down Expand Up @@ -155,26 +152,37 @@ There is an experimental generator for the JPA entities from GORM domain classes

TIP: You can use `MicronautJdbcGenrator` instead of `MicronautJpaGenerator` to generate JDBC repositories instead of generic ones.

[source,groovy,indent=0,options="nowrap"]
.Integration Test Usage
[source,groovy,indent=0,options="nowrap",role="primary"]
.Grails Integration Test Usage
----
include::{root-dir}/examples/micronaut-grails-example/src/test/groovy/micronaut/grails/example/GeneratorSpec.groovy[tags=body]
----

[source,groovy,indent=0,options="nowrap"]
.Micronaut Usage
[source,groovy,indent=0,options="nowrap",role="secondary"]
.Micronaut Test Usage
----
include::{root-dir}/examples/micronaut-grails-domain-library/src/test/groovy/com/agorapulse/micronaut/grails/domain/MicronautGeneratorSpec.groovy[tags=body]
----


[source,groovy,indent=0,options="nowrap"]
[source,groovy,indent=0,options="nowrap",role="secondary"]
.Grails Console Usage
----
ctx.micronautJpaGenerator.generate(new File('/path/to/generated/sources'))
----

[source,groovy,indent=0,options="nowrap",role="secondary"]
.Micronaut Console Usage
----
import com.agorapulse.micronaut.grails.jpa.generator.MicronautJpaGenerator
import org.grails.datastore.gorm.validation.constraints.eval.DefaultConstraintEvaluator
import org.grails.orm.hibernate.HibernateDatastore
new MicronautJpaGenerator(
ctx.getBean(HibernateDatastore),
new DefaultConstraintEvaluator()
).generate(new File('/path/to/generated/sources'))
----

The current generator supports

Expand All @@ -186,6 +194,8 @@ The current generator supports

Please, let us know if there is any feature missing!

The new entities are generated to the packages with suffix which defaults to `.model`.

=== Generated Entities Example

For an `User` entity such as this one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class MicronautGeneratorSpec extends Specification {
noExceptionThrown()

when:
File entityFile = new File(root, 'com/agorapulse/micronaut/grails/domain/Manager.groovy')
File repositoryFile = new File(root, 'com/agorapulse/micronaut/grails/domain/ManagerRepository.groovy')
File entityFile = new File(root, 'com/agorapulse/micronaut/grails/domain/model/Manager.groovy')
File repositoryFile = new File(root, 'com/agorapulse/micronaut/grails/domain/model/ManagerRepository.groovy')
then:
entityFile.exists()
entityFile.text.trim() == fixt.readText('Manager.groovy.txt').trim()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.agorapulse.micronaut.grails.domain
package com.agorapulse.micronaut.grails.domain.model

import groovy.transform.CompileStatic
import javax.persistence.Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.agorapulse.micronaut.grails.domain
package com.agorapulse.micronaut.grails.domain.model

import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.CrudRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package micronaut.grails.example

import grails.compiler.GrailsCompileStatic
import micronaut.grails.example.other.Vehicle

@GrailsCompileStatic
class User {
Expand All @@ -27,6 +28,8 @@ class User {
String password
String email

static hasMany = [vehicles: Vehicle]

static constraints = {
userName blank: false, unique: true
password size: 5..10, blank: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package micronaut.grails.example
package micronaut.grails.example.other

class Vehicle {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class GeneratorSpec extends Specification {
noExceptionThrown()

when:
File entityFile = new File(root, 'micronaut/grails/example/User.groovy')
File repositoryFile = new File(root, 'micronaut/grails/example/UserRepository.groovy')
File entityFile = new File(root, 'micronaut/grails/example/model/User.groovy')
File repositoryFile = new File(root, 'micronaut/grails/example/model/UserRepository.groovy')
then:
entityFile.exists()
entityFile.text.trim() == fixt.readText('User.groovy.txt').trim()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class MicronautJdbcGeneratorSpec extends Specification {
noExceptionThrown()

when:
File entityFile = new File(root, 'micronaut/grails/example/Vehicle.groovy')
File repositoryFile = new File(root, 'micronaut/grails/example/VehicleRepository.groovy')
File entityFile = new File(root, 'micronaut/grails/example/other/model/Vehicle.groovy')
File repositoryFile = new File(root, 'micronaut/grails/example/other/model/VehicleRepository.groovy')
then:
entityFile.exists()
entityFile.text.trim() == fixt.readText('Vehicle.groovy.txt').trim()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package micronaut.grails.example
package micronaut.grails.example.model

import groovy.transform.CompileStatic
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.OneToMany
import javax.persistence.Table
import javax.persistence.UniqueConstraint
import javax.persistence.Version
import javax.validation.constraints.Email
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size
import micronaut.grails.example.other.model.Vehicle

@Entity
@CompileStatic
Expand Down Expand Up @@ -44,4 +46,7 @@ class User {
@Size(max = 255)
String userName

@OneToMany
Set<Vehicle> vehicles

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package micronaut.grails.example
package micronaut.grails.example.model

import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.CrudRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package micronaut.grails.example
package micronaut.grails.example.other.model

import groovy.transform.CompileStatic
import javax.annotation.Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package micronaut.grails.example
package micronaut.grails.example.other.model

import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,18 @@ abstract class MicronautDataGenerator {
}

@SuppressWarnings('NestedForLoop')
int generate(File root) {
int generate(File root, String packageSuffix = '.model') {
Collection<PersistentEntity> entities = datastore.mappingContext.persistentEntities
Collection<Class> entityClasses = entities*.javaClass
entities.each { PersistentEntity entity ->
File packageDirectory = new File(root, (entity.javaClass.package.name).replace('.', File.separator))
File packageDirectory = new File(root, (entity.javaClass.package.name + packageSuffix).replace('.', File.separator))
packageDirectory.mkdirs()

File entityFile = new File(packageDirectory, "${entity.javaClass.simpleName}.groovy")
entityFile.text = generateEntity(entity)
entityFile.text = generateEntity(entity, entityClasses, packageSuffix)

File repositoryFile = new File(packageDirectory, "${entity.javaClass.simpleName}Repository.groovy")
repositoryFile.text = generateRepository(entity)
repositoryFile.text = generateRepository(entity, packageSuffix)
}

List<Class> requiredEnums = []
Expand All @@ -101,8 +102,9 @@ abstract class MicronautDataGenerator {
'LineLength',
'MethodSize',
'UnnecessaryCollectCall',
'CyclomaticComplexity',
])
String generateEntity(PersistentEntity entity) {
String generateEntity(PersistentEntity entity, Collection<Class> persistentEntitiesTypes, String packageSuffix) {
List<UnifiedProperty> unifiedProperties = collectUnifiedProperties(entity)
Set<String> imports = new TreeSet<>(unifiedProperties.collect {
if (it.persistentProperty instanceof OneToMany) {
Expand All @@ -112,7 +114,10 @@ abstract class MicronautDataGenerator {
}.findAll {
it && !it.primitive && it.package && it.package != entity.javaClass.package && it.package.name != 'java.util' && it.package.name != 'java.lang'
}.collect {
it.name
if (it in persistentEntitiesTypes) {
return it.package.name + packageSuffix + '.' + it.simpleName
}
return it.name
})

StringWriter bodyStringWriter = new StringWriter()
Expand Down Expand Up @@ -205,7 +210,7 @@ abstract class MicronautDataGenerator {

StringWriter finalWriter = new StringWriter()
PrintWriter finalPrintWriter = new PrintWriter(finalWriter)
finalPrintWriter.println "package $entity.javaClass.package.name"
finalPrintWriter.println "package $entity.javaClass.package.name$packageSuffix"
finalPrintWriter.println()

imports.each { String imported ->
Expand Down Expand Up @@ -489,7 +494,7 @@ abstract class MicronautDataGenerator {
}

@SuppressWarnings('LineLength')
protected abstract String generateRepository(PersistentEntity entity)
protected abstract String generateRepository(PersistentEntity entity, String packageSuffix)

private static void copyEnum(File root, Class enumType) {
if (!enumType.enum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class MicronautJdbcGenerator extends MicronautDataGenerator {

@Override
@SuppressWarnings('LineLength')
protected String generateRepository(PersistentEntity entity) {
protected String generateRepository(PersistentEntity entity, String packageSuffix) {
return """
package $entity.javaClass.package.name
package $entity.javaClass.package.name$packageSuffix
import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MicronautJpaGenerator extends MicronautDataGenerator {

@Override
@SuppressWarnings('LineLength')
protected String generateRepository(PersistentEntity entity) {
protected String generateRepository(PersistentEntity entity, String packageSuffix) {
String datasourceDefinition = ''

if (entity.mapping.mappedForm.datasources) {
Expand All @@ -45,7 +45,7 @@ class MicronautJpaGenerator extends MicronautDataGenerator {
}

return """
package $entity.javaClass.package.name
package $entity.javaClass.package.name$packageSuffix
import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.CrudRepository
Expand Down

0 comments on commit 2f0935f

Please sign in to comment.