Skip to content

Commit

Permalink
Launcher app dir improvements (#943)
Browse files Browse the repository at this point in the history
* Launcher app dir imprs

* Launcher with app dir fixes
  • Loading branch information
krystian-panek-vmltech authored Jan 2, 2023
1 parent fb9745b commit c64b12d
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 37 deletions.
9 changes: 9 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<img src="docs/logo.png" alt="Gradle AEM Plugin"/>
</p>


---

TL;DR

To add an automated AEM environment setup to the AEM Maven project [click here](https://github.com/wttech/gradle-aem-plugin/blob/main/docs/launcher.md).

---

## Table of contents

* [About](#about)
Expand Down
40 changes: 32 additions & 8 deletions docs/launcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Simply, using e.g bash script - download the GAP launcher run it with regular GA

| GAP Launcher | AEM Project Archetype |
|--------------|-----------------------|
| 16.0.1 | 39 |
| 16.0.4 | 39 |

See also [plugin compatibility](../README.MD#compatibility).

Expand All @@ -53,10 +53,34 @@ Below there are some sample usages of standalone launcher.

### Enhancing Maven build

To add Gradle/GAP support to existing Maven build generated from Adobe AEM Archetype, run command below:
To add Gradle/GAP support to existing Maven build generated from Adobe AEM Archetype...

Choose one of the available options:

A) Gradle files added to the project directly (recommended):

```shell
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/16.0.4/gap.jar && java -jar gap.jar && rm gap.jar
```
Consequences:

* newly scaffolded files will be VCS-tracked by the Maven project,
* there will be a single code repository with Maven files supplemented by only a few extra Gradle environment files,
* on fresh setups cloning only a single code repository is needed to set up an automated AEM environment.

B) Gradle files at the root, Maven files nested into the `maven` directory

Consequences:

* newly scaffolded files will be NOT VCS-tracked by the Maven project,
* there will be 2 code repositories:
* existing one with Maven files (moved to sub dir `maven`),
* dedicated one for Gradle environment files (root dir/parent of `maven`),
* the advantage is separation of concerns (dedicate repository for environment files and the second one for application code),
* on a fresh setup, cloning both code repositories is required to set up an automated AEM environment.

```shell
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/15.5.3/gap.jar && java -jar gap.jar && rm gap.jar
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/16.0.4/gap.jar && java -jar gap.jar --app-dir=maven && rm gap.jar
```

Demo (to play again refresh the page):
Expand Down Expand Up @@ -149,7 +173,7 @@ It is using `sh gradlew props` task to provide AEM instance files details, howev
To set up and turn on AEM instance(s) by single command, consider running:

```bash
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/15.5.3/gap.jar \
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/16.0.4/gap.jar \
&& java -jar gap.jar --save-props \
-PfileTransfer.user=foo -PfileTransfer.password=pass \
-PlocalInstance.quickstart.jarUrl=http://company-share.com/aem/cq-quickstart-6.5.0.jar \
Expand All @@ -170,7 +194,7 @@ sh gradlew down
For deploying to AEM instance CRX package from any source consider using command:

```shell
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/15.5.3/gap.jar && java -jar gap.jar && rm gap.jar
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/16.0.4/gap.jar && java -jar gap.jar && rm gap.jar
sh gradlew instanceDeploy -Pinstance.author -Pinstance.deploy.packageUrl=https://github.com/neva-dev/felix-search-webconsole-plugin/releases/download/search-webconsole-plugin-1.3.0/search-webconsole-plugin-1.3.0.jar
```

Expand All @@ -188,7 +212,7 @@ Also instead of URL, dependency notation could be used to resolve package from M
To interactively monitor logs of any AEM instances using task [`instanceTail`](instance-plugin.md#task-instancetail), consider running command:

```bash
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/15.5.3/gap.jar \
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/16.0.4/gap.jar \
&& java -jar gap.jar --save-props \
-Pinstance.dev-author.httpUrl=http://foo:[email protected]:4502 \
-Pinstance.dev-publish.httpUrl=http://foo:[email protected]:4503 \
Expand All @@ -203,7 +227,7 @@ Assuming instance running at URL *http://localhost:4502* or *http://localhost:45
Consider appending parameter e.g `-Pinstance.list=http://admin:admin@localhost:4502` to customize the instance to work with.

```bash
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/15.5.3/gap.jar && java -jar gap.jar && rm gap.jar
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/16.0.4/gap.jar && java -jar gap.jar && rm gap.jar
sh gradlew packageSync -Pfilter.roots=[/content/example,/content/dam/example]
```

Expand All @@ -212,7 +236,7 @@ sh gradlew packageSync -Pfilter.roots=[/content/example,/content/dam/example]
To copy JCR content between any AEM instances using task [`instanceRcp`](instance-plugin.md#task-instancercp), consider running commands:

```bash
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/15.5.3/gap.jar && java -jar gap.jar && rm gap.jar
curl -OJL https://github.com/Cognifide/gradle-aem-plugin/releases/download/16.0.4/gap.jar && java -jar gap.jar && rm gap.jar
sh gradlew instanceRcp \
-Pinstance.rcp.source=http://foo:[email protected]:4502 \
-Pinstance.rcp.target=http://foo:[email protected]:4503 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class EnvCloudScaffolder(private val launcher: Launcher) {
println("Saving root Gradle build script file '$this'")

val mavenRootDir = when {
launcher.appDirPath.isBlank() -> ""
else -> """rootDir("${launcher.appDirPath}")"""
launcher.appDirNested -> """rootDir("${launcher.appDirPath}")"""
else -> ""
}

writeText(
Expand Down Expand Up @@ -271,7 +271,7 @@ class EnvCloudScaffolder(private val launcher: Launcher) {
load("dispatcherImage") { aem.localInstanceManager.dispatcherImage }
resolve { listOf("cache", "logs").forEach { ensureDir(aem.localInstanceManager.dispatcherDir.resolve(it)) } }
reload { cleanDir("/mnt/var/www/html") }
dev { watchRootDir("${launcher.appDirPath}/dispatcher/src") }
dev { watchRootDir("${launcher.appDirPath("dispatcher/src")}") }
}
}
}
Expand Down Expand Up @@ -339,7 +339,7 @@ class EnvCloudScaffolder(private val launcher: Launcher) {
- COMMERCE_ENDPOINT=http://localhost/graphql
volumes:
# Use project-specific dispatcher config
- {{ rootPath }}/${launcher.appDirPath}/dispatcher/src:/mnt/dev/src:ro
- {{ rootPath }}/${launcher.appDirPath("dispatcher/src")}:/mnt/dev/src:ro
- {{ rootPath }}/env/.gradle/aem/localInstance/sdk/dispatcher/lib:/usr/lib/dispatcher-sdk:ro
- {{ rootPath }}/env/.gradle/aem/localInstance/sdk/dispatcher/lib/import_sdk_config.sh:/docker_entrypoint.d/zzz-import-sdk-config.sh:ro
# Enable invalidation by any client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class EnvInstanceOnlyScaffolder(private val launcher: Launcher) {
println("Saving root Gradle build script file '$this'")

val mavenRootDir = when {
launcher.appDirPath.isBlank() -> ""
else -> """rootDir("${launcher.appDirPath}")"""
launcher.appDirNested -> """rootDir("${launcher.appDirPath}")"""
else -> ""
}

writeText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class EnvOnPremScaffolder(private val launcher: Launcher) {
println("Saving root Gradle build script file '$this'")

val mavenRootDir = when {
launcher.appDirPath.isBlank() -> ""
else -> """rootDir("${launcher.appDirPath}")"""
launcher.appDirNested -> """rootDir("${launcher.appDirPath}")"""
else -> ""
}

writeText(
Expand Down Expand Up @@ -319,9 +319,10 @@ class EnvOnPremScaffolder(private val launcher: Launcher) {
}
dev {
watchRootDir(
"${launcher.appDirPath}/dispatcher/src/conf.d",
"${launcher.appDirPath}/dispatcher/src/conf.dispatcher.d",
"env/src/environment/httpd")
"${launcher.appDirPath("dispatcher/src/conf.d")}",
"${launcher.appDirPath("dispatcher/src/conf.dispatcher.d")}",
"env/src/environment/httpd",
)
}
}
}
Expand Down Expand Up @@ -378,8 +379,8 @@ class EnvOnPremScaffolder(private val launcher: Launcher) {
ports:
- 80:80
volumes:
- "{{ rootPath }}/${launcher.appDirPath}/dispatcher/src/conf.d:/etc/httpd/conf.d"
- "{{ rootPath }}/${launcher.appDirPath}/dispatcher/src/conf.dispatcher.d:/etc/httpd/conf.dispatcher.d"
- "{{ rootPath }}/${launcher.appDirPath("dispatcher/src/conf.d:/etc/httpd/conf.d")}/"
- "{{ rootPath }}/${launcher.appDirPath("dispatcher/src/conf.dispatcher.d:/etc/httpd/conf.dispatcher.d")}"
- "{{ sourcePath }}/httpd:/etc/httpd.extra"
- "{{ workPath }}/httpd/modules/mod_dispatcher.so:/etc/httpd/modules/mod_dispatcher.so"
- "{{ workPath }}/httpd/logs:/etc/httpd/logs"
Expand Down Expand Up @@ -441,7 +442,7 @@ class EnvOnPremScaffolder(private val launcher: Launcher) {
writeText("".trimIndent())
}

launcher.workFileBackupOnce("${launcher.appDirPath}/dispatcher/src/conf.dispatcher.d/cache/ams_author_invalidate_allowed.any") {
launcher.workFileBackupOnce(launcher.appDirPath("dispatcher/src/conf.dispatcher.d/cache/ams_author_invalidate_allowed.any")) {
println("Creating author flush config file '$this'")
writeText(
"""
Expand All @@ -454,7 +455,7 @@ class EnvOnPremScaffolder(private val launcher: Launcher) {
)
}

launcher.workFileBackupOnce("${launcher.appDirPath}/dispatcher/src/conf.dispatcher.d/cache/ams_publish_invalidate_allowed.any") {
launcher.workFileBackupOnce(launcher.appDirPath("dispatcher/src/conf.dispatcher.d/cache/ams_publish_invalidate_allowed.any")) {
println("Creating publish flush config file '$this'")
writeText(
"""
Expand All @@ -467,7 +468,7 @@ class EnvOnPremScaffolder(private val launcher: Launcher) {
)
}

launcher.workFileBackupOnce("${launcher.appDirPath}/dispatcher/src/conf.d/rewrites/xforwarded_forcessl_rewrite.rules") {
launcher.workFileBackupOnce(launcher.appDirPath("dispatcher/src/conf.d/rewrites/xforwarded_forcessl_rewrite.rules")) {
println("Creating X-Forwarded-For SSL rewrite config file '$this'")
writeText(
"""
Expand All @@ -482,7 +483,7 @@ class EnvOnPremScaffolder(private val launcher: Launcher) {
/**
* Replacing md5 checksums in dispatcher's pom.xml for the files that are being replaced
*/
launcher.workFileBackupAndReplaceStrings("${launcher.appDirPath}/dispatcher/pom.xml",
launcher.workFileBackupAndReplaceStrings(launcher.appDirPath("dispatcher/pom.xml"),
// Replacing md5 checksum for xforwarded_forcessl_rewrite.rules file
"cd1373a055f245de6e9ed78f74f974a6" to "3f6158d0fd659071fa29c50c9a509804",
// Replacing md5 checksum for ams_author_invalidate_allowed.any and ams_publish_invalidate_allowed.any files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import kotlin.system.exitProcess

class Launcher(val args: Array<String>) {

val gradleArgs get() = args.filterNot { ARGS.containsArg(it) || WRAPPER_ARGS.containsArg(it) }

val wrapperArgs get() = args.filter { WRAPPER_ARGS.containsArg(it) }

val printStackTrace get() = args.contains(ARG_PRINT_STACKTRACE)

val colorOutput get() = !args.contains(ARG_NO_COLOR_OUTPUT)

val workDirPath get() = args.firstOrNull { it.startsWith("$ARG_WORK_DIR=") }?.substringAfter("=")

val appDirPath get() = args.firstOrNull { it.startsWith("$ARG_APP_DIR=") }?.substringAfter("=") ?: "maven"

val gradleArgs get() = args.filterNot { ARGS.containsArg(it) || WRAPPER_ARGS.containsArg(it) }
val appDirPath get() = args.firstOrNull { it.startsWith("$ARG_APP_DIR=") }?.substringAfter("=") ?: ""

val wrapperArgs get() = args.filter { WRAPPER_ARGS.containsArg(it) }
fun appDirPath(subPath: String) = if (appDirPath.isNotBlank()) "$appDirPath/$subPath" else subPath

val buildConfig get() = Properties().apply {
load(Launcher::class.java.getResourceAsStream("/build.properties"))
Expand All @@ -32,14 +34,18 @@ class Launcher(val args: Array<String>) {

val workDir get() = (if (workDirPath != null) currentDir.resolve(workDirPath!!) else currentDir).canonicalFile

val appDir get() = appDirPath.let { workDir.resolve(it) }
val appDir get() = appDirPath.takeIf { it.isNotBlank() }?.let { workDir.resolve(it) } ?: workDir

val appDirNested get() = appDir != workDir

val eol get() = System.lineSeparator()

val buildScaffolder by lazy { BuildScaffolder(this) }

val miscScaffolder by lazy { MiscScaffolder(this) }

val selfJar get() = File(javaClass.protectionDomain.codeSource.location.toURI())

fun launch() {
nestWorkDirAsAppDir()
scaffold()
Expand All @@ -49,6 +55,9 @@ class Launcher(val args: Array<String>) {
}

private fun nestWorkDirAsAppDir() {
if (!appDirNested) {
return
}
if (!appDir.canonicalPath.contains(workDir.canonicalPath)) {
println("Work dir must nest an app dir!")
exitProcess(1)
Expand All @@ -60,7 +69,7 @@ class Launcher(val args: Array<String>) {
} else {
println("Nesting a work dir '$workDir' inside an app dir '$appDir'")
appDir.mkdirs()
workDir.listFiles()?.forEach { it.renameTo(appDir.resolve(it.name)) }
workDir.listFiles()?.filter { it != selfJar }?.forEach { it.renameTo(appDir.resolve(it.name)) }
}
}

Expand Down Expand Up @@ -164,7 +173,7 @@ class Launcher(val args: Array<String>) {

const val ARG_SAVE_PREFIX = "-P"

val ARGS = setOf(ARG_SAVE_PROPS, ARG_PRINT_STACKTRACE, ARG_NO_COLOR_OUTPUT, ARG_WORK_DIR)
val ARGS = setOf(ARG_SAVE_PROPS, ARG_PRINT_STACKTRACE, ARG_NO_COLOR_OUTPUT, ARG_APP_DIR, ARG_WORK_DIR)

val WRAPPER_ARGS = setOf("--gradle-version", "--distribution-type", "--gradle-distribution-url", "--gradle-distribution-sha256-sum")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ class MiscScaffolder(private val launcher: Launcher) {
}

private fun appendGitIgnore() = launcher.workFile(".gitignore") {
val content = """
val content = if (launcher.appDirNested) {
"""
### Gradle/GAP ###
.gradle/
build/
/gradle.properties
/gap.jar
/${launcher.appDirPath}
""".trimIndent()
""".trimIndent()
} else {
"""
### Gradle/GAP ###
.gradle/
build/
/gradle.properties
/gap.jar
""".trimIndent()
}

if (!exists()) {
println("Saving Git ignore file '$this'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ class AwaitUpAction(aem: AemExtension) : DefaultAction(aem) {
}

companion object {
fun quickOptions(): AwaitUpAction.() -> Unit = {
/**
* Simplified options to more quickly check condition of AEM instances
* assuming that no package deployment was performed before running checks.
*/
fun noPackageDeployOptions(): AwaitUpAction.() -> Unit = {
runner { doneTimes.set(1) }
unchanged { enabled.set(false) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open class InstanceProvision : Instance() {
}
}

private var awaitUpOptions = AwaitUpAction.quickOptions()
private var awaitUpOptions = AwaitUpAction.noPackageDeployOptions()

fun awaitUp(options: AwaitUpAction.() -> Unit) {
this.awaitUpOptions = options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.gradle.api.tasks.TaskAction

open class InstanceUp : LocalInstance() {

private var awaitOptions = AwaitUpAction.quickOptions()
private var awaitOptions = AwaitUpAction.noPackageDeployOptions()

/**
* Controls instance awaiting.
Expand Down

0 comments on commit c64b12d

Please sign in to comment.