Skip to content

Commit 21695c7

Browse files
authored
Merge pull request #151 from Cognifide/instances-dsl
Instances DSL
2 parents 82c01d7 + 0cc7bf1 commit 21695c7

11 files changed

Lines changed: 269 additions & 142 deletions

File tree

README.md

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ buildscript {
115115
}
116116
117117
dependencies {
118-
classpath 'com.cognifide.gradle:aem-plugin:3.1.0'
118+
classpath 'com.cognifide.gradle:aem-plugin:3.1.1'
119119
}
120120
}
121121
@@ -501,7 +501,7 @@ allprojects { subproject ->
501501
plugins.withId 'com.cognifide.aem.base', {
502502
aem {
503503
config {
504-
localInstance("http://localhost:6502")
504+
localInstance "http://localhost:6502"
505505
contentPath = subproject.file("src/main/aem")
506506
}
507507
}
@@ -539,29 +539,62 @@ In AEM configuration section, there is possibility to use `localInstance` or `re
539539
```groovy
540540
aem {
541541
config {
542-
localAuthorInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-author
543-
localPublishInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-publish
542+
localAuthorInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-author
543+
localPublishInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-publish
544544
545-
localInstance("http://localhost:4502") // local-author
546-
localInstance("http://localhost:4502", "admin", "admin", "author", 14502) // local-author
545+
localInstance "http://localhost:4502" // local-author
546+
localInstance "http://localhost:4502", { // local-author
547+
user = "admin"
548+
password = "admin"
549+
typeName = "author"
550+
debugPort = 14502
551+
}
547552
548-
localInstance("http://localhost:4503") // local-publish
549-
localInstance("http://localhost:4503", "admin", "admin", "publish", 14502) // local-publish
553+
localInstance "http://localhost:4503" // local-publish
554+
localInstance "http://localhost:4503", { // local-publish
555+
user = "admin"
556+
password = "admin"
557+
typeName = "publish"
558+
debugPort = 14502
559+
}
550560
551-
remoteAuthorInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-author
552-
remotePublishInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-publish
561+
remoteAuthorInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-author
562+
remotePublishInstance() // property: aem.instance.author.httpUrl or default 'http://localhost:4502' ; local-publish
553563
554-
remoteInstance("http://192.168.10.1:4502", "user1", "password2", "integration") // integration-author
555-
remoteInstance("http://192.168.10.2:4503", "user2", "password2", "integration") // integration-publish
564+
remoteInstance "http://192.168.10.1:4502", { // integration-author1
565+
user = "user1"
566+
password = "password2"
567+
environment = "integration"
568+
typeName = "author1"
569+
}
570+
remoteInstance "http://192.168.10.1:8080", { // integration-author2
571+
user = "user1"
572+
password = "password2"
573+
environment = "integration"
574+
typeName = "author2
575+
}
576+
remoteInstance "http://192.168.10.2:4503", { // integration-publish1
577+
user = "user2"
578+
password = "password2"
579+
environment = "integration"
580+
typeName = "publish1"
581+
}
582+
remoteInstance "http://192.168.10.2:8080", { // integration-publish2
583+
user = "user2"
584+
password = "password2"
585+
environment = "integration"
586+
typeName = "publish2"
587+
}
556588
}
557589
}
558590
```
559591

560-
Rules:
592+
**Rules:**
561593

562-
* Instance name is a combination of `${environment}-${type}` e.g *local-author*, *integration-publish* etc.
594+
* Instance name is a combination of *${environment}-${typeName}* e.g *local-author*, *integration-publish* etc.
595+
* Instance type name must start with prefix *author* or *publish*. Sample valid names: *author*, *author1*, *author2*, *author-master* and *publish*, *publish1* *publish2* etc.
563596
* Only instances being defined as *local* are being considered in command `aemSetup`, `aemCreate`, `aemUp` etc (that comes from `com.cognifide.aem.instance` plugin).
564-
* All instances being defined as *local* or *remote* are being considered in commands CRX package deployment related like `aemDeploy`, `aemUpload`, `aemInstall` etc.
597+
* All instances being defined as *local* or *remote* are being considered in commands CRX package deployment related like `aemSatisfy`, `aemDeploy`, `aemUpload`, `aemInstall` etc.
565598

566599
### Understand why there are one or two plugins to be applied in build script
567600

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group 'com.cognifide.gradle'
9-
version '3.1.0'
9+
version '3.1.1'
1010
description = 'Gradle AEM Plugin'
1111
defaultTasks = ['clean', 'build', 'publishToMavenLocal']
1212

src/main/kotlin/com/cognifide/gradle/aem/api/AemConfig.kt

Lines changed: 132 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import com.cognifide.gradle.aem.internal.PropertyParser
77
import com.cognifide.gradle.aem.pkg.ComposeTask
88
import com.cognifide.gradle.aem.pkg.PackagePlugin
99
import com.fasterxml.jackson.annotation.JsonIgnore
10+
import groovy.lang.Closure
1011
import org.gradle.api.DefaultTask
1112
import org.gradle.api.Project
1213
import org.gradle.api.tasks.Input
1314
import org.gradle.api.tasks.Internal
1415
import org.gradle.language.base.plugins.LifecycleBasePlugin
16+
import org.gradle.util.ConfigureUtil
1517
import java.io.File
1618
import java.io.Serializable
1719
import java.util.*
@@ -421,63 +423,82 @@ class AemConfig(
421423
instance(LocalInstance.create(httpUrl))
422424
}
423425

424-
fun localInstance(httpUrl: String, type: String) {
425-
instance(LocalInstance.create(httpUrl, type))
426+
fun localInstance(httpUrl: String, configurer: LocalInstance.() -> Unit) {
427+
instance(LocalInstance.create(httpUrl, configurer))
426428
}
427429

428-
fun localInstance(httpUrl: String, user: String, password: String) {
429-
instance(LocalInstance.create(httpUrl, user, password))
430+
fun localInstance(httpUrl: String, configurer: Closure<*>) {
431+
localInstance(httpUrl, { ConfigureUtil.configure(configurer, this) })
430432
}
431433

432-
fun localInstance(httpUrl: String, user: String, password: String, type: String) {
433-
instance(LocalInstance.create(httpUrl, user, password, type))
434+
fun localAuthorInstance() {
435+
localAuthorInstance({})
434436
}
435437

436-
fun localInstance(httpUrl: String, user: String, password: String, type: String, debugPort: Int) {
437-
instance(LocalInstance(httpUrl, user, password, type, debugPort))
438+
fun localAuthorInstance(configurer: LocalInstance.() -> Unit) {
439+
localInstance(propParser.string(Instance.AUTHOR_URL_PROP, Instance.URL_AUTHOR_DEFAULT), configurer)
438440
}
439441

440-
fun localAuthorInstance() {
441-
val httpUrl = propParser.string(Instance.AUTHOR_URL_PROP, Instance.URL_AUTHOR_DEFAULT)
442-
instance(LocalInstance.create(httpUrl))
442+
fun localAuthorInstance(configurer: Closure<*>) {
443+
localAuthorInstance({ ConfigureUtil.configure(configurer, this) })
443444
}
444445

445446
fun localPublishInstance() {
446-
val httpUrl = propParser.string(Instance.PUBLISH_URL_PROP, Instance.URL_PUBLISH_DEFAULT)
447-
instance(LocalInstance.create(httpUrl))
447+
localPublishInstance({})
448448
}
449449

450-
fun remoteInstance(httpUrl: String) {
451-
instance(RemoteInstance.create(httpUrl, deployEnvironment))
450+
fun localPublishInstance(configurer: LocalInstance.() -> Unit) {
451+
localInstance(propParser.string(Instance.PUBLISH_URL_PROP, Instance.URL_PUBLISH_DEFAULT), configurer)
452452
}
453453

454-
fun remoteInstance(httpUrl: String, environment: String) {
455-
instance(RemoteInstance.create(httpUrl, environment))
454+
fun localPublishInstance(configurer: Closure<*>) {
455+
localPublishInstance({ ConfigureUtil.configure(configurer, this) })
456456
}
457457

458-
fun remoteInstance(httpUrl: String, user: String, password: String) {
459-
instance(RemoteInstance.create(httpUrl, user, password, deployEnvironment))
458+
fun remoteInstance(httpUrl: String) {
459+
instance(RemoteInstance.create(httpUrl))
460460
}
461461

462-
fun remoteInstance(httpUrl: String, user: String, password: String, environment: String) {
463-
instance(RemoteInstance.create(httpUrl, user, password, environment))
462+
fun remoteInstance(httpUrl: String, configurer: RemoteInstance.() -> Unit) {
463+
instance(RemoteInstance.create(httpUrl, {
464+
this.environment = deployEnvironment
465+
this.apply(configurer)
466+
}))
464467
}
465468

466-
fun remoteInstance(httpUrl: String, user: String, password: String, type: String, environment: String) {
467-
instance(RemoteInstance(httpUrl, user, password, type, environment))
469+
fun remoteInstance(httpUrl: String, configurer: Closure<*>) {
470+
remoteInstance(httpUrl, { ConfigureUtil.configure(configurer, this) })
468471
}
469472

470473
fun remoteAuthorInstance() {
471-
val httpUrl = propParser.string(Instance.AUTHOR_URL_PROP, Instance.URL_AUTHOR_DEFAULT)
472-
instance(RemoteInstance.create(httpUrl, deployEnvironment))
474+
remoteAuthorInstance({})
475+
}
476+
477+
fun remoteAuthorInstance(configurer: RemoteInstance.() -> Unit) {
478+
remoteInstance(propParser.string(Instance.AUTHOR_URL_PROP, Instance.URL_AUTHOR_DEFAULT), configurer)
479+
}
480+
481+
fun remoteAuthorInstance(configurer: Closure<*>) {
482+
remoteAuthorInstance({ ConfigureUtil.configure(configurer, this) })
473483
}
474484

475485
fun remotePublishInstance() {
476-
val httpUrl = propParser.string(Instance.PUBLISH_URL_PROP, Instance.URL_PUBLISH_DEFAULT)
477-
instance(RemoteInstance.create(httpUrl, deployEnvironment))
486+
remotePublishInstance({})
487+
}
488+
489+
fun remotePublishInstance(configurer: RemoteInstance.() -> Unit) {
490+
remoteInstance(propParser.string(Instance.PUBLISH_URL_PROP, Instance.URL_PUBLISH_DEFAULT), configurer)
491+
}
492+
493+
fun remotePublishInstance(configurer: Closure<*>) {
494+
remotePublishInstance({ ConfigureUtil.configure(configurer, this) })
478495
}
479496

480497
private fun instance(instance: Instance) {
498+
if (instances.containsKey(instance.name)) {
499+
throw AemException("Instance named '${instance.name}' is already defined. Enumerate instance types (for instance 'author1', 'author2') or distinguish environments.")
500+
}
501+
481502
instances[instance.name] = instance
482503
}
483504

@@ -552,6 +573,90 @@ class AemConfig(
552573
@Internal
553574
fun isUniqueProjectName() = project == project.rootProject || project.name == project.rootProject.name
554575

576+
/**
577+
* @Deprecated Will be removed in 4.0.0
578+
*/
579+
fun localInstance(httpUrl: String, typeName: String) {
580+
localInstance(httpUrl, {
581+
this.typeName = typeName
582+
})
583+
}
584+
585+
/**
586+
* @Deprecated Will be removed in 4.0.0
587+
*/
588+
fun localInstance(httpUrl: String, user: String, password: String) {
589+
localInstance(httpUrl, {
590+
this.user = user
591+
this.password = password
592+
})
593+
}
594+
595+
/**
596+
* @Deprecated Will be removed in 4.0.0
597+
*/
598+
fun localInstance(httpUrl: String, user: String, password: String, typeName: String) {
599+
localInstance(httpUrl, {
600+
this.user = user
601+
this.password = password
602+
this.typeName = typeName
603+
})
604+
}
605+
606+
/**
607+
* @Deprecated Will be removed in 4.0.0
608+
*/
609+
fun localInstance(httpUrl: String, user: String, password: String, type: String, debugPort: Int) {
610+
localInstance(httpUrl, {
611+
this.user = user
612+
this.password = password
613+
this.typeName = type
614+
this.debugPort = debugPort
615+
})
616+
}
617+
618+
/**
619+
* @Deprecated Will be removed in 4.0.0
620+
*/
621+
fun remoteInstance(httpUrl: String, environment: String) {
622+
remoteInstance(httpUrl, {
623+
this.environment = environment
624+
})
625+
}
626+
627+
/**
628+
* @Deprecated Will be removed in 4.0.0
629+
*/
630+
fun remoteInstance(httpUrl: String, user: String, password: String) {
631+
remoteInstance(httpUrl, {
632+
this.user = user
633+
this.password = password
634+
})
635+
}
636+
637+
/**
638+
* @Deprecated Will be removed in 4.0.0
639+
*/
640+
fun remoteInstance(httpUrl: String, user: String, password: String, environment: String) {
641+
remoteInstance(httpUrl, {
642+
this.user = user
643+
this.password = password
644+
this.environment = environment
645+
})
646+
}
647+
648+
/**
649+
* @Deprecated Will be removed in 4.0.0
650+
*/
651+
fun remoteInstance(httpUrl: String, user: String, password: String, typeName: String, environment: String) {
652+
remoteInstance(httpUrl, {
653+
this.user = user
654+
this.password = password
655+
this.typeName = typeName
656+
this.environment = environment
657+
})
658+
}
659+
555660
companion object {
556661

557662
/**

src/main/kotlin/com/cognifide/gradle/aem/instance/Instance.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,22 @@ interface Instance : Serializable {
4343
4 -> {
4444
val (httpUrl, type, user, password) = parts
4545

46-
RemoteInstance(httpUrl, user, password, type, ENVIRONMENT_CMD)
46+
RemoteInstance.create(httpUrl, {
47+
this.user = user
48+
this.password = password
49+
this.typeName = type
50+
})
4751
}
4852
3 -> {
4953
val (httpUrl, user, password) = parts
50-
val type = InstanceType.nameByUrl(httpUrl)
5154

52-
RemoteInstance(httpUrl, user, password, type, ENVIRONMENT_CMD)
55+
RemoteInstance.create(httpUrl, {
56+
this.user = user
57+
this.password = password
58+
})
5359
}
5460
else -> {
55-
RemoteInstance.create(urlRaw, ENVIRONMENT_CMD)
61+
RemoteInstance.create(urlRaw)
5662
}
5763
}
5864
}
@@ -64,8 +70,8 @@ interface Instance : Serializable {
6470
val publishUrl = project.properties.getOrElse(PUBLISH_URL_PROP, { URL_PUBLISH_DEFAULT }) as String
6571

6672
return listOf(
67-
RemoteInstance.create(authorUrl, config.deployEnvironment),
68-
RemoteInstance.create(publishUrl, config.deployEnvironment)
73+
RemoteInstance.create(authorUrl, { environment = config.deployEnvironment }),
74+
RemoteInstance.create(publishUrl, { environment = config.deployEnvironment })
6975
)
7076
}
7177

src/main/kotlin/com/cognifide/gradle/aem/instance/InstanceType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum class InstanceType {
1212
private val AUTHOR_RULES = listOf("*02")
1313

1414
fun byName(type: String): InstanceType {
15-
return values().find { it.name.startsWith(type, true) }
15+
return values().find { type.startsWith(it.name, ignoreCase = true) }
1616
?: throw AemException("Invalid instance type: $type")
1717
}
1818

src/main/kotlin/com/cognifide/gradle/aem/instance/InstanceUrl.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ class InstanceUrl(raw: String) {
1717
val httpUrl: String
1818
get() = "${config.protocol}://${config.host}:${config.port}"
1919

20-
val type: String
20+
val typeName: String
2121
get() = InstanceType.nameByUrl(httpUrl)
2222

23+
val type: InstanceType
24+
get() = InstanceType.byUrl(httpUrl)
25+
26+
val debugPort: Int
27+
get() = "1${Instance.portOfUrl(httpUrl)}".toInt()
28+
2329
private fun userPart(index: Int): String? {
2430
return config.userInfo?.split(":")
2531
?.takeIf { it.size == 2 }

0 commit comments

Comments
 (0)