diff --git a/README.md b/README.md index aeb4b8b..63d13b3 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ You can include/exclude files using global parameters in config file. 2. Add `{{incl ...}}` helper with the specified parameter in file path and/or file name. -``` +```yaml globalParams: ... addTests: false @@ -162,6 +162,55 @@ And use the partials in templates: {{> hello name="Developer"}} ``` +## Helpers +In config yaml file: +```yaml +globalParams: + items: + - name: "name1" + type: "type1" + ... + - name: "name2" + type: "type2" + ... +``` +`filterByAllOf` filters `items` by all pair key-value (AND condition), for example: +```handlebars +{{~#each (filterByAllOf items type="type1" name="name1")}} +{{! there will only be items when type="type1" and name="name1"}} +{{~/each}} +``` +`filterByOneOf` filters `items` by one of pair key-value (OR condition), for example: +```handlebars +{{~#each (filterByAllOf items type="type1" name="name2")}} +{{! there will only be items when type="type1" or name="name2"}} +{{~/each}} +``` +`containsAllOf` checks for all pair key-value (AND condition) in `items`, for example: +```handlebars +{{~#if (containsAllOf items type="type1" name="name1")}} +... +{{~/if}} +``` +`containsOneOf` checks for one of pair key-value (OR condition) in `items`, for example: +```handlebars +{{~#if (containsOneOf items type="type1" name="name2")}} +... +{{~/if}} +``` +`containsKey` checks for exist key in `items`, for example: +```handlebars +{{~#if (containsKey items "type")}} +... +{{~/if}} +``` +`containsValue` checks for exist value in `items`, for example: +```handlebars +{{~#if (containsValue items "type1")}} +... +{{~/if}} +``` + ## License Copyright 2021 IceRock MAG Inc diff --git a/build.gradle.kts b/build.gradle.kts index e64be22..40b6a50 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { subprojects { group = "dev.icerock.tools" - version = "0.3.0" + version = "0.4.0" apply(plugin = "org.jetbrains.kotlin.jvm") } diff --git a/install-shaper.sh b/install-shaper.sh index 558f164..506792f 100755 --- a/install-shaper.sh +++ b/install-shaper.sh @@ -1,10 +1,10 @@ mkdir -p ~/.shaper && \ cd ~/.shaper && \ -curl -L -s "https://github.com/icerockdev/shaper/releases/download/release%2F0.3.0/shaper-cli-0.3.0.zip" > cli.zip && \ +curl -L -s "https://github.com/icerockdev/shaper/releases/download/release%2F0.4.0/shaper-cli-0.4.0.zip" > cli.zip && \ unzip -q cli.zip && \ rm cli.zip && \ rm -rf shaper-cli || true && \ -mv shaper-cli-0.3.0 shaper-cli && \ +mv shaper-cli-0.4.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 -o ' diff --git a/shaper-cli/src/main/kotlin/dev/icerock/tools/shaper/cli/ShaperCli.kt b/shaper-cli/src/main/kotlin/dev/icerock/tools/shaper/cli/ShaperCli.kt index 91007f5..9fb0f7b 100644 --- a/shaper-cli/src/main/kotlin/dev/icerock/tools/shaper/cli/ShaperCli.kt +++ b/shaper-cli/src/main/kotlin/dev/icerock/tools/shaper/cli/ShaperCli.kt @@ -26,16 +26,22 @@ fun main(args: Array) { description = "output files directory", ).default(".") + val skipOverrider: Boolean by parser.option( + type = ArgType.Boolean, + shortName = "s", + description = "skip overrider" + ).default(false) + parser.parse(args) val shaperConfig = ShaperConfig.read() val templatesRepository = TemplatesRepository(shaperConfig) val config = templatesRepository.getTemplateConfig(input) - val configOverrider = ConfigOverrider() - val overridenConfig = config.copy( - globalParams = configOverrider.override(config.globalParams) - ) + val overridenConfig = when (skipOverrider) { + true -> config + else -> config.copy(globalParams = ConfigOverrider().override(config.globalParams)) + } val shaper = Shaper(templateConfig = overridenConfig) val consoleResult = shaper.execute(output) diff --git a/shaper-core/src/main/kotlin/dev/icerock/tools/shaper/core/HandlebarsFactory.kt b/shaper-core/src/main/kotlin/dev/icerock/tools/shaper/core/HandlebarsFactory.kt index 639d728..89f2c9f 100644 --- a/shaper-core/src/main/kotlin/dev/icerock/tools/shaper/core/HandlebarsFactory.kt +++ b/shaper-core/src/main/kotlin/dev/icerock/tools/shaper/core/HandlebarsFactory.kt @@ -48,10 +48,74 @@ object HandlebarsFactory { context && options.params[0] as Boolean }) - handlebars.registerHelper("raw", Helper> { context, options -> + handlebars.registerHelper("raw", Helper> { _, options -> options.fn() }) + handlebars.registerHelper("filterByAllOf", Helper>> { context, options -> + if (options.hash.isEmpty()) { + return@Helper arrayListOf>() + } + + val keyValueMap = options.hash.toMap() + context.filter { map: Map -> + keyValueMap.filter { entry: Map.Entry -> + map.containsKey(entry.key) && map[entry.key] == entry.value + }.size == keyValueMap.size + } + }) + + handlebars.registerHelper("filterByOneOf", Helper>> { context, options -> + if (options.hash.isEmpty()) { + return@Helper arrayListOf>() + } + + val keyValueMap = options.hash.toMap() + context.filter { map: Map -> + keyValueMap.filter { entry: Map.Entry -> + map.containsKey(entry.key) && map[entry.key] == entry.value + }.isNotEmpty() + } + }) + + handlebars.registerHelper("containsAllOf", Helper>> { context, options -> + if (options.hash.isEmpty()) { + return@Helper false + } + + val keyValueMap = options.hash.toMap() + context.any { map: Map -> + keyValueMap.filter { entry: Map.Entry -> + map.containsKey(entry.key) && map[entry.key] == entry.value + }.size == keyValueMap.size + } + }) + + handlebars.registerHelper("containsOneOf", Helper>> { context, options -> + if (options.hash.isEmpty()) { + return@Helper false + } + + val keyValueMap = options.hash.toMap() + context.any { map: Map -> + keyValueMap.filter { entry: Map.Entry -> + map.containsKey(entry.key) && map[entry.key] == entry.value + }.isNotEmpty() + } + }) + + handlebars.registerHelper("containsKey", Helper>> { context, options -> + context.any { map: Map -> + map.keys.contains(options.params[0]) + } + }) + + handlebars.registerHelper("containsValue", Helper>> { context, options -> + context.any { map: Map -> + map.values.contains(options.params[0]) + } + }) + return handlebars } }