Skip to content

Commit

Permalink
Update project template for CUBA 7.2 #190
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-shustanov committed Oct 5, 2020
1 parent fc6824f commit 2698fda
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ class ProjectInitCommand(override val kodein: Kodein = cubaKodein) : GeneratorCo
}

confirmation("kotlinSupport", "Support kotlin?") {
askIf {answers ->
PlatformVersion((answers[PLATFORM_VERSION] ?: answers[PREDEFINED_PLATFORM_VERSION]) as String) >= PlatformVersion.v7_2
askIf { answers ->
PlatformVersion((answers[PLATFORM_VERSION]
?: answers[PREDEFINED_PLATFORM_VERSION]) as String) >= PlatformVersion.v7_2
}
}

Expand Down Expand Up @@ -199,9 +200,17 @@ class ProjectInitCommand(override val kodein: Kodein = cubaKodein) : GeneratorCo

Files.createDirectories(cwd)

val kotlinSupport = model.kotlinSupport

val templateTips = TemplateProcessor(resources.getTemplate("project"), bindings, PlatformVersion(model.platformVersion)) {
listOf("modules", "build.gradle", "settings.gradle", "\${gitignore}").forEach {
transform(it)
listOf("modules", "build.gradle", "settings.gradle", "\${gitignore}").forEach { it ->
transform(it) { path ->
return@transform if (kotlinSupport) {
!path.fileName.toString().endsWith(".java")
} else {
!path.fileName.toString().endsWith(".kt")
}
}
}

listOf("gradle", "gradlew", "gradlew.bat").forEach {
Expand All @@ -215,13 +224,6 @@ class ProjectInitCommand(override val kodein: Kodein = cubaKodein) : GeneratorCo
}

templateTips?.let { writer.println(it.format(cwd.toAbsolutePath().toString())) }

val dpTipsMessageName = when (model.database.database) {
databases[5] -> "oracleDbTips"
else -> return
}

writer.println(messages[dpTipsMessageName])
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.haulmont.cuba.cli.cubaplugin.project

import com.haulmont.cuba.cli.commands.CommandExecutionException
import com.haulmont.cuba.cli.commands.CommonParameters
import com.haulmont.cuba.cli.cubaplugin.model.PlatformVersion
import com.haulmont.cuba.cli.localMessages
import com.haulmont.cuba.cli.prompting.Answers

Expand All @@ -36,6 +37,7 @@ class ProjectInitModel(answers: Answers) {
val database: DatabaseModel = DatabaseModel(answers)
val kotlinSupport: Boolean by answers.withDefault { false }
val kotlinVersion: String by answers.withDefault { "1.3.41" }
val testContainerPrefix: String = namespace.capitalize()
}

class DatabaseModel(answers: Answers) {
Expand All @@ -62,6 +64,9 @@ class DatabaseModel(answers: Answers) {
} else ""

init {
val platformVersion = PlatformVersion(answers["platformVersion"] as? String
?: answers["predefinedPlatformVersion"] as String)

when (database) {
databases[0] -> {
schema = "jdbc:hsqldb:hsql:"
Expand All @@ -74,15 +79,21 @@ class DatabaseModel(answers: Answers) {
databases[1] -> {
schema = "jdbc:postgresql:"
driver = "org.postgresql.Driver"
driverDependency = "'org.postgresql:postgresql:9.4.1212'"
driverDependency = if (platformVersion >= PlatformVersion.v7_2)
"'org.postgresql:postgresql:42.2.9'"
else
"'org.postgresql:postgresql:9.4.1212'"
driverDependencyName = "postgres"
username = "cuba"
password = "cuba"
}
databases[2] -> {
databases[2], databases[4] -> {
schema = "jdbc:sqlserver:"
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
driverDependency = "'com.microsoft.sqlserver:mssql-jdbc:6.4.0.jre8'"
driverDependency = if (platformVersion < PlatformVersion.v7_1)
"'com.microsoft.sqlserver:mssql-jdbc:7.0.0.jre8'"
else
"'com.microsoft.sqlserver:mssql-jdbc:7.2.2.jre8'"
driverDependencyName = "mssql"
username = "sa"
password = "saPass1"
Expand All @@ -95,18 +106,10 @@ class DatabaseModel(answers: Answers) {
username = "sa"
password = "saPass1"
}
databases[4] -> {
schema = "jdbc:sqlserver:"
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
driverDependency = "'com.microsoft.sqlserver:mssql-jdbc:6.4.0.jre8'"
driverDependencyName = "mssql"
username = "sa"
password = "saPass1"
}
databases[5] -> {
schema = "jdbc:oracle:thin:@"
driver = "oracle.jdbc.OracleDriver"
driverDependency = "files(\"\$cuba.tomcat.dir/lib/ojdbc6.jar\")"
driverDependency = "com.oracle.database.jdbc:ojdbc6:11.2.0.4"
driverDependencyName = "oracle"
username = (answers["projectName"] as String).replace('-', '_')
password = "cuba"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TemplateProcessor(templateBasePath: Path, private val bindings: Map<String
}
}

private fun process(from: Path, to: Path, withTransform: Boolean) {
private fun process(from: Path, to: Path, withTransform: Boolean, filter: PathFilter) {
val targetAbsolutePath = to.toAbsolutePath()

val baseTemplatePath = templatePath.toAbsolutePath().toString()
Expand All @@ -76,6 +76,7 @@ class TemplateProcessor(templateBasePath: Path, private val bindings: Map<String
from.walk()
.filter { !isTemplateMetadata(it) }
.filter { Files.isRegularFile(it) }
.filter(filter)
.forEach { inputPath ->
val outputFile = inputPath.toAbsolutePath().toString()
.replace(baseTemplatePath, targetDirectoryPath)
Expand Down Expand Up @@ -151,20 +152,20 @@ class TemplateProcessor(templateBasePath: Path, private val bindings: Map<String
return kClass.memberProperties.first { it.name == name }.getter.call(obj)!!
}

fun copy(subPath: Path, to: Path = projectRoot) {
process(templatePath.resolve(subPath), to, false)
fun copy(subPath: Path, to: Path = projectRoot, filter: PathFilter = {true}) {
process(templatePath.resolve(subPath), to, false, filter)
}

fun copy(subPath: String, to: Path = projectRoot) {
process(templatePath.resolve(subPath), to, false)
fun copy(subPath: String, to: Path = projectRoot, filter: PathFilter = {true}) {
process(templatePath.resolve(subPath), to, false, filter)
}

fun transform(subPath: Path, to: Path = projectRoot) {
process(templatePath.resolve(subPath), to, true)
fun transform(subPath: Path, to: Path = projectRoot, filter: PathFilter = {true}) {
process(templatePath.resolve(subPath), to, true, filter)
}

fun transform(subPath: String, to: Path = projectRoot) {
process(templatePath.resolve(subPath), to, true)
fun transform(subPath: String, to: Path = projectRoot, filter: PathFilter = {true}) {
process(templatePath.resolve(subPath), to, true, filter)
}

fun transform(subPath: String, to: OutputStream) {
Expand Down Expand Up @@ -228,4 +229,6 @@ class TemplateProcessor(templateBasePath: Path, private val bindings: Map<String
.readText()
}
}
}
}

typealias PathFilter = (Path) -> Boolean
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,5 @@
# limitations under the License.
#

oracleDbTips=\
@|white You have selected Oracle Database.\n\n\
Oracle does not allow to redistribute its JDBC driver, so do the following:\n\
> Download ojdbc6.jar from www.oracle.com.\n\
> Run `gradle setupTomcat deploy`, then copy ojdbc6.jar into {your_project}/deploy/tomcat/lib directory.|@

databases=HSQLDB,PostgreSQL,Microsoft SQL Server,Microsoft SQL Server 2005,Microsoft SQL Server 2012+,Oracle Database,MySQL
databaseAliases=hsql,postgresql,msql,msql2005,msql2012,oracle,mysql
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def webModule = project(":${modulePrefix}-web")
def servletApi = 'javax.servlet:javax.servlet-api:3.1.0'


apply(plugin: 'idea')
apply(plugin: 'cuba')
#if ( $project.kotlinSupport )
apply(plugin: 'org.jetbrains.kotlin.jvm')
Expand All @@ -50,9 +49,6 @@ cuba {
version = '0.1'
isSnapshot = true
}
tomcat {
dir = "$project.rootDir/deploy/tomcat"
}
}

dependencies {
Expand All @@ -65,7 +61,6 @@ def ${project.database.driverDependencyName} = ${project.database.driverDependen
configure([globalModule, coreModule, webModule]) {
apply(plugin: 'java')
apply(plugin: 'maven')
apply(plugin: 'idea')
apply(plugin: 'cuba')
#if ( $project.kotlinSupport )
apply(plugin: 'org.jetbrains.kotlin.jvm')
Expand All @@ -86,6 +81,9 @@ configure([globalModule, coreModule, webModule]) {
artifacts {
archives sourceJar
}
test {
useJUnitPlatform()
}
}

configure(globalModule) {
Expand Down Expand Up @@ -126,14 +124,7 @@ configure(coreModule) {
}

task cleanConf(description: 'Cleans up conf directory') {
doLast {
def dir = new File(cuba.tomcat.dir, "/conf/${modulePrefix}-core")
if (dir.isDirectory()) {
ant.delete(includeemptydirs: true) {
fileset(dir: dir, includes: '**/*', excludes: 'local.app.properties')
}
}
}
delete "$cuba.appHome/${modulePrefix}-core/conf"
}

task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
Expand All @@ -142,25 +133,9 @@ configure(coreModule) {
}

task createDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
#if (${project.database.connectionParams} != "")
connectionParams = '${project.database.connectionParams}'
#end
dbms = '${project.database.driverDependencyName}'
host = 'localhost'
dbName = '${project.namespace}'
dbUser = '${project.database.username}'
dbPassword = '${project.database.password}'
}

task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
#if (${project.database.connectionParams} != "")
connectionParams = '${project.database.connectionParams}'
#end
dbms = '${project.database.driverDependencyName}'
host = 'localhost'
dbName = '${project.namespace}'
dbUser = '${project.database.username}'
dbPassword = '${project.database.password}'
}
#if (${project.database.driverDependencyName} == "hsql")

Expand All @@ -184,7 +159,6 @@ configure(webModule) {
dependencies {
compileOnly(servletApi)
compile(globalModule)

}

task webArchive(type: Zip) {
Expand All @@ -200,7 +174,7 @@ configure(webModule) {
task deployConf(type: Copy) {
from file('src')
include "${project.rootPackageDirectory}/**"
into "$cuba.tomcat.dir/conf/${modulePrefix}"
into "$cuba.appHome/${modulePrefix}/conf"
}

task clearMessagesCache(type: CubaClearMessagesCache) {
Expand All @@ -209,14 +183,7 @@ configure(webModule) {
deployConf.dependsOn clearMessagesCache

task cleanConf(description: 'Cleans up conf directory') {
doLast {
def dir = new File(cuba.tomcat.dir, "/conf/${modulePrefix}")
if (dir.isDirectory()) {
ant.delete(includeemptydirs: true) {
fileset(dir: dir, includes: '**/*', excludes: 'local.app.properties')
}
}
}
delete "$cuba.appHome/${modulePrefix}/conf"
}

task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ cuba.anonymousSessionId = b55843a7-3425-91cd-b721-7f9de29f104e

cuba.webContextName = app-core
cuba.availableLocales = English|en
cuba.localeSelectVisible = false
cuba.localeSelectVisible = false

cuba.dataSourceProvider = application
cuba.dataSource.username = ${project.database.username}
cuba.dataSource.password = ${project.database.password}
cuba.dataSource.dbName = ${project.namespace}
cuba.dataSource.host = localhost
#if( ! ${project.database.connectionParams} )
cuba.dataSource.connectionParams = ${project.database.connectionParams}
#end
#if( ${project.database.database} == "Microsoft SQL Server 2005" )
cuba.dataSource.connectionTestQuery=select 1
#end
cuba.dataSource.driverClassName = ${project.database.driver}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ${project.rootPackage};

import com.haulmont.cuba.testsupport.TestContainer;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.ArrayList;
import java.util.Arrays;

public class ${project.testContainerPrefix}TestContainer extends TestContainer {

public ${project.testContainerPrefix}TestContainer() {
super();
//noinspection ArraysAsListWithZeroOrOneArgument
appComponents = new ArrayList<>(Arrays.asList(
// list add-ons here: "com.haulmont.reports", "com.haulmont.addon.bproc", etc.<%
"com.haulmont.cuba"
));
appPropertiesFiles = Arrays.asList(
// List the files defined in your web.xml
// in appPropertiesConfig context parameter of the core module
"${project.rootPackageDirectory}/app.properties",
// Add this file which is located in CUBA and defines some properties
// specifically for test environment. You can replace it with your own
// or add another one in the end.
"${project.rootPackageDirectory}/test-app.properties");
autoConfigureDataSource();
}

public static class Common extends ${project.testContainerPrefix}TestContainer {

public static final ${project.testContainerPrefix}TestContainer.Common INSTANCE = new ${project.testContainerPrefix}TestContainer.Common();

private static volatile boolean initialized;

private Common() {
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
if (!initialized) {
super.beforeAll(extensionContext);
initialized = true;
}
setupContext();
}

@SuppressWarnings("RedundantThrows")
@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
cleanupContext();
// never stops - do not call super
}
}
}
Loading

0 comments on commit 2698fda

Please sign in to comment.