Skip to content

Commit

Permalink
Merge pull request #30 from icerockdev/develop
Browse files Browse the repository at this point in the history
Release 0.4.0
  • Loading branch information
Alex009 authored May 11, 2021
2 parents 096c9da + 85f9512 commit f2b3d88
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 9 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
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.3.0"
version = "0.4.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.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 <input yaml> -o <output dir>'
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ fun main(args: Array<String>) {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,74 @@ object HandlebarsFactory {
context && options.params[0] as Boolean
})

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

handlebars.registerHelper("filterByAllOf", Helper<ArrayList<Map<Any, Any>>> { context, options ->
if (options.hash.isEmpty()) {
return@Helper arrayListOf<Map<Any, Any>>()
}

val keyValueMap = options.hash.toMap()
context.filter { map: Map<Any, Any> ->
keyValueMap.filter { entry: Map.Entry<String, Any> ->
map.containsKey(entry.key) && map[entry.key] == entry.value
}.size == keyValueMap.size
}
})

handlebars.registerHelper("filterByOneOf", Helper<ArrayList<Map<Any, Any>>> { context, options ->
if (options.hash.isEmpty()) {
return@Helper arrayListOf<Map<Any, Any>>()
}

val keyValueMap = options.hash.toMap()
context.filter { map: Map<Any, Any> ->
keyValueMap.filter { entry: Map.Entry<String, Any> ->
map.containsKey(entry.key) && map[entry.key] == entry.value
}.isNotEmpty()
}
})

handlebars.registerHelper("containsAllOf", Helper<ArrayList<Map<Any, Any>>> { context, options ->
if (options.hash.isEmpty()) {
return@Helper false
}

val keyValueMap = options.hash.toMap()
context.any { map: Map<Any, Any> ->
keyValueMap.filter { entry: Map.Entry<String, Any> ->
map.containsKey(entry.key) && map[entry.key] == entry.value
}.size == keyValueMap.size
}
})

handlebars.registerHelper("containsOneOf", Helper<ArrayList<Map<Any, Any>>> { context, options ->
if (options.hash.isEmpty()) {
return@Helper false
}

val keyValueMap = options.hash.toMap()
context.any { map: Map<Any, Any> ->
keyValueMap.filter { entry: Map.Entry<String, Any> ->
map.containsKey(entry.key) && map[entry.key] == entry.value
}.isNotEmpty()
}
})

handlebars.registerHelper("containsKey", Helper<ArrayList<Map<Any, Any>>> { context, options ->
context.any { map: Map<Any, Any> ->
map.keys.contains(options.params[0])
}
})

handlebars.registerHelper("containsValue", Helper<ArrayList<Map<Any, Any>>> { context, options ->
context.any { map: Map<Any, Any> ->
map.values.contains(options.params[0])
}
})

return handlebars
}
}

0 comments on commit f2b3d88

Please sign in to comment.