Skip to content

Commit

Permalink
Merge pull request #25 from icerockdev/develop
Browse files Browse the repository at this point in the history
Release 0.3.0
  • Loading branch information
Alex009 authored Apr 29, 2021
2 parents 4c6673d + db5d5f6 commit 73f5166
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 31 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {

subprojects {
group = "dev.icerock.tools"
version = "0.2.0"
version = "0.3.0"

apply(plugin = "org.jetbrains.kotlin.jvm")
}
4 changes: 2 additions & 2 deletions install-shaper.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mkdir -p ~/.shaper && \
cd ~/.shaper && \
curl -L -s "https://github.com/icerockdev/shaper/releases/download/release%2F0.2.0/shaper-cli-0.2.0.zip" > cli.zip && \
curl -L -s "https://github.com/icerockdev/shaper/releases/download/release%2F0.3.0/shaper-cli-0.3.0.zip" > cli.zip && \
unzip -q cli.zip && \
rm cli.zip && \
rm -rf shaper-cli || true && \
mv shaper-cli-0.2.0 shaper-cli && \
mv shaper-cli-0.3.0 shaper-cli && \
echo "repositories:" > config.yaml && \
echo 'To complete setup add into your environments: export PATH=~/.shaper/shaper-cli/bin:$PATH'
echo 'After it you can call shaper by command: shaper-cli -i <input yaml> -o <output dir>'
12 changes: 12 additions & 0 deletions samples/kmm-module/sub/build.gradle.kts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
plugin(Deps.Plugins.androidLibrary)
plugin(Deps.Plugins.kotlinMultiplatform)
plugin(Deps.Plugins.mobileMultiplatform)
}
# Suka
dependencies {
{{#each androidMainDeps}} androidMainImplementation(Deps.Libs.Android.{{this}})
{{/each}}
{{#each commonMainDeps}} commonMainImplementation(Deps.Libs.MultiPlatform.{{this}}.common)
{{/each}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ConfigOverrider {
println("Default: $value (Press enter to skip change)")
val input = readLine() ?: return value
val newValue = if (input.isBlank()) value else mapper(input)
println("$path = $newValue")
println("$path = $newValue\n")
return newValue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import com.github.jknack.handlebars.io.AbstractTemplateSource
import java.io.File
import java.nio.charset.Charset

class FileTemplateSource(private val file: File) : AbstractTemplateSource() {
class FileTemplateSource(private val file: File, private val filename: String? = null) : AbstractTemplateSource() {
override fun content(charset: Charset?): String {
return file.readText(charset ?: Charsets.UTF_8)
}

override fun filename(): String = file.name
override fun filename(): String = filename ?: file.name

override fun lastModified(): Long = 0L
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ object HandlebarsFactory {
handlebars.registerHelper("eq", Helper<String> { context, options ->
context == options.params[0]
})
handlebars.registerHelper("incl", Helper<Boolean> { include, _ ->
if (include) "" else Shaper.NOT_INCLUDE
})

handlebars.registerHelper("or", Helper<Boolean> { context, options ->
context || options.params[0] as Boolean
})

handlebars.registerHelper("and", Helper<Boolean> { context, options ->
context && options.params[0] as Boolean
})

handlebars.registerHelper("raw", Helper<Map<String, String>> { context, options ->
options.fn()
})

return handlebars
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.tools.shaper.core

object PathLocator {

private val dirMapList = mutableMapOf<String, String>()

fun set(path: String, base: String) = dirMapList.put(path, base)

fun getBaseDir(path: String) = dirMapList[path]

fun getAbsolutePath(path: String) = getBaseDir(path) + "/" + path

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class Shaper(private val templateConfig: TemplateConfig) {
templateConfig.files.forEach { fileConfig ->
val allParams = templateConfig.globalParams + fileConfig.templateParams

val fileNameTemplate = handlebars.compileInline(fileConfig.pathTemplate)
val fileNameTemplate = handlebars.compileInline(fileConfig.pathTemplate.replace("\\", "/"))
val filePath = fileNameTemplate.apply(allParams)
if (filePath.contains(NOT_INCLUDE)) return@forEach

val file = File(outputPath, filePath)
with(file.parentFile) {
Expand Down Expand Up @@ -69,4 +70,8 @@ class Shaper(private val templateConfig: TemplateConfig) {
}
return resultWriter.toString()
}

companion object {
const val NOT_INCLUDE = "{not_include}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dev.icerock.tools.shaper.core

import com.github.jknack.handlebars.internal.lang3.Validate
import com.github.jknack.handlebars.io.TemplateSource
import com.github.jknack.handlebars.io.URLTemplateLoader
import com.github.jknack.handlebars.io.URLTemplateSource
Expand All @@ -20,10 +19,16 @@ class TemplateClassPathLoader() : URLTemplateLoader() {
}

override fun sourceAt(location: String?): TemplateSource? {
Validate.notEmpty(location, "The uri is required.")
val templateFile = File(location)
if (location == null) throw Exception("The uri is required.")

val templateFile = if (PathLocator.getBaseDir(location) == null) {
File(location)
} else {
File(PathLocator.getAbsolutePath(location))
}

return if (templateFile.exists()) {
FileTemplateSource(templateFile)
FileTemplateSource(templateFile, location)
} else {
URLTemplateSource(location, getResource(location) ?: throw FileNotFoundException(location))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ object YamlConfigReader {
private fun buildConfig(map: Map<String, Any>, directory: File): TemplateConfig {
val globalParams = map["globalParams"] as? Map<String, Any>
val files = map["files"]
val includes = map["includes"]
val includes = map["includes"] as? List<String>

val filesConfigs: List<TemplateConfig.FileConfig> = collectFileConfig(files, directory)
val outputsConfigs: List<TemplateConfig.OutputConfig> = collectOutputConfig(
map["outputs"] as? List<Map<String, Any>>,
directory
)
val includesConfigs = collectIncludeConfig(includes as List<String>, directory)
val includesConfigs = collectIncludeConfig(includes, directory)

return TemplateConfig(
globalParams = globalParams.orEmpty(),
Expand Down Expand Up @@ -63,24 +63,23 @@ object YamlConfigReader {
val filesList = files as? List<Map<String, Any>>
val filesDirectory = files as? String
return filesList?.map { fileMap ->
val templateFile = File(directory, fileMap["contentTemplateName"] as String)
val contentTemplateName = fileMap["contentTemplateName"] as String
PathLocator.set(contentTemplateName, directory.absolutePath)
TemplateConfig.FileConfig(
pathTemplate = fileMap["pathTemplate"] as String,
contentTemplateName = templateFile.path,
contentTemplateName = contentTemplateName,
templateParams = (fileMap["templateParams"] as? Map<String, Any>).orEmpty()
)
}
?: if (filesDirectory != null) {
val filesDir = File(directory, filesDirectory)
val rootPrefix = filesDir.path + "/"

filesDir.walkTopDown().filterNot { it.isDirectory }.map {
TemplateConfig.FileConfig(
pathTemplate = it.path.removeSuffix(".hbs").removePrefix(rootPrefix),
contentTemplateName = it.path,
templateParams = emptyMap()
)
}.toList()
} else emptyList()
} ?: if (filesDirectory != null) {
PathLocator.set(filesDirectory, directory.absolutePath)
val filesDir = File(directory, filesDirectory)
filesDir.walkTopDown().filterNot { it.isDirectory }.map {
TemplateConfig.FileConfig(
pathTemplate = it.relativeTo(filesDir).path.removeSuffix(".hbs"),
contentTemplateName = it.path,
templateParams = emptyMap()
)
}.toList()
} else emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

class ShaperTest {

@Test
fun `generation of gradle module`() {
val buildGradleFile = TemplateConfig.FileConfig(
Expand Down Expand Up @@ -42,6 +43,24 @@ class ShaperTest {
)
}

@Test
fun `with yaml test`() {
val result = YamlConfigReader.read(File("src/test/resources/configForShaper.yaml"))

val shaper = Shaper(templateConfig = result)
shaper.execute("build/test")

assertFileEquals(
expectedResourceName = "sub/build.gradle.kts",
actualFilePath = "build/test/sub/build.gradle.kts"
)

assertFileEquals(
expectedResourceName = "build.gradle.kts",
actualFilePath = "build/test/build.gradle.kts"
)
}

private fun assertFileEquals(expectedResourceName: String, actualFilePath: String) {
val expectedUrl: URL = this::class.java.classLoader.getResource(expectedResourceName)
val actualFile = File(actualFilePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class YamlReaderTest {
assertEquals("build.gradle.kts", files[0].pathTemplate)
assertThat(
files[0].contentTemplateName,
containsString("shaper/shaper-core/src/test/resources/kmm-module/build.gradle.kts.hbs")
containsString("kmm-module/build.gradle.kts.hbs")
)
assertEquals(0, files[0].templateParams.count())
assertEquals("src/commonMain/kotlin/{{dts packageName}}/di/{{moduleName}}Factory.kt", files[1].pathTemplate)
assertThat(
files[1].contentTemplateName,
containsString("shaper/shaper-core/src/test/resources/kmm-module/Factory.kt.hbs")
containsString("kmm-module/Factory.kt.hbs")
)
assertEquals(0, files[1].templateParams.count())
assertThat(
Expand All @@ -42,7 +42,7 @@ class YamlReaderTest {
assertEquals("=== Tips for feature setup ===", outputs[0].outputTitle)
assertThat(
outputs[0].contentTemplateName,
containsString("shaper/shaper-core/src/test/resources/kmm-module/console.output.hbs")
containsString("kmm-module/console.output.hbs")
)
assertEquals(0, outputs[0].templateParams.count())
}
Expand Down
16 changes: 16 additions & 0 deletions shaper-core/src/test/resources/configForShaper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
globalParams:
packageName: dev.icerock.shaper.sample.kmm.auth
moduleName: Auth
androidMainDeps:
- lifecycle
- recyclerView
dependencies:
- dep1
- dep2
files:
- pathTemplate: 'build.gradle.kts'
contentTemplateName: build.gradle.kts.hbs
- pathTemplate: 'sub/build.gradle.kts'
contentTemplateName: sub/build.gradle.kts.hbs
includes:
- includes
9 changes: 9 additions & 0 deletions shaper-core/src/test/resources/sub/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sub

plugins {
id("org.jetbrains.kotlin.jvm") version "1.4.20"
}
# Sub-file {{test}}
dependencies {
implements("dep1")
}
16 changes: 16 additions & 0 deletions shaper-core/src/test/resources/sub/build.gradle.kts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sub

plugins {
id("org.jetbrains.kotlin.jvm") version "1.4.20"
}
# Sub-file {{{{raw}}}}{{test}}{{{{/raw}}}}
dependencies {
{{~#if (or (eq "test1" "test") (eq "test2" "test2"))}}
{{~#if (and (eq "test" "test") (eq "test2" "test2"))}}
implements("dep1")
{{/if~}}
{{/if~}}
{{~#if (or (eq "test1" "test") (eq "test" "test2"))}}
implements("dep2")
{{~/if~}}
}

0 comments on commit 73f5166

Please sign in to comment.